BackTpriceFeb 2026 — Present

Backend and Infrastructure

Tprice compares grocery prices in Croatia and Serbia on iOS and Android. Every day its data pipeline downloads the price lists that supermarket chains publish, parses them, spreads prices to stores and recalculates a price quality label. The backend, both countries' databases, monitoring and CI share one server, a single k3s node with 4 vCPU and 15 GiB of memory.

I design and build Tprice end to end, from the first commit in February 2026. I wrote the Expo app, the Go backend and the k3s setup with the AI coding agents Claude Code and Cursor. Of 1,099 commits, I made 1,076, and 949 carry an AI co-author trailer. A second developer made 23 commits, among them the first Go port of the catalog API and the first OTA server.

The design side of the app is in the Price comparison case.

Most of the load comes from data, about 13 million store prices a day for Croatia and 22 to 25 million for Serbia. The catalog API peaks at 4.85 requests a second, so the work is memory optimisation, CPU and disk use, and query optimisation.

A horizontal bar chart of memory in use on the Tprice server in one reading on 1 October 2026, in MiB. ClickHouse 1,739, k3s and the operating system 1,605, monitoring 1,421, Postgres 1,315, the Go services 198, ingress 118, cert-manager 115, kube-system 81 and Redis 4, 6,596 MiB in total.
In this reading the Go services took 198 MiB, monitoring seven times more

Results

42%

of the server's memory in use with both countries live, 6,596 of 15,610 MiB on 1 October

61%

less peak memory for the price archive importer, 3,241 MiB down to 1,267

11 s

for the heaviest price quality query, which took 143 to 406 s before

89%

of the node's memory requested with Serbia live, against 119% with Croatian sizing

0

memory errors in the Serbian ClickHouse after the metric log fix, down from 2,014 in three days

6.25 GiB

for 1.6 billion price rows in ClickHouse

18 GiB

of disk freed on the server, 57 GiB used down to 39.5

93%

less time for text search in Postgres, 115 ms down to 7.9

2,587

Jest tests in the mobile app, up from 71 in seven weeks

The performance figures are production measurements from June to October 2026, taken from Prometheus, ClickHouse system tables, EXPLAIN ANALYZE and the server itself.

Stack and why I chose it

Layer Choice Why Alternative I passed on
Mobile app Expo and React Native in JavaScript One codebase for iOS and Android, with over-the-air updates Separate Swift and Kotlin apps
Data in the app TanStack Query Caches and de-duplicates reads from a read-heavy catalog Hand-written request state
Backend Go with the standard net/http The first Python backend hit 500% CPU on the home screen and p99 over 2 s Tuning FastAPI
SQL access pgx and sqlc Plain SQL I can check with EXPLAIN, with no ORM layer An ORM
Catalog and users PostgreSQL, one database per country Joins, trigram search and isolation between countries One shared database
Prices ClickHouse, one instance per country Columnar storage for a daily time series of tens of millions of rows Partitioned Postgres tables
Cache Redis Counters the catalog reads often Counting on every request
Background jobs A Go worker per country with its own scheduler 33 periodic jobs, and price quality starts right after each import A message broker
App updates Own OTA server EAS Update meant vendor lock-in and charges once an app passes 1,000 monthly users EAS Update

Moving prices out of Postgres

Prices started in Postgres as one row per product, store and day. On a local copy from 22 April, 38.5 million price rows took 15 GiB, and 11 GiB of that was indexes. About 4.7 GiB of those indexes were dead or nearly dead, and cold aggregate queries ran past the 15 s write timeout.

A donut chart of a 15 GiB Postgres price table on a local copy from 22 April 2026. Dead or nearly dead indexes 4.7 GiB, live indexes 6.3 GiB and table data 3.8 GiB.
Dead indexes alone took more space than the data

Between 23 and 25 April I moved the price rows to ClickHouse, with materialized views for reads and rows that expire after 183 days. Postgres kept the catalog and the users. On the local copy Postgres shrank from 27 to 13 GiB, and a day's import took 4 to 6 minutes, down from about 10.

A bar chart of ClickHouse storage on 1 October 2026. The Croatian database holds 1.03 billion rows in 4.41 GiB against 89.4 GiB uncompressed, the Serbian one 600 million rows in 1.84 GiB against 56.6 GiB uncompressed.
The Croatian data is 20 times smaller than uncompressed, the Serbian data 31 times

Two countries on one server

Kubernetes places pods by their memory requests, so right-sizing starts from real peaks. In August ClickHouse requested 1 GiB while its peak working set was 3,727 MiB, which made it the first candidate for eviction. After a week of Prometheus data I raised the request to 4 GiB. Limits add up to 220% of the node and act as a safety net.

Before Serbia launched I did capacity planning again. With Croatia's settings, node requests would have reached 119% and the Serbian pods would not have been scheduled. I sized them from Serbia's own data, a daily archive of 2.9 MB against 81.8 MB, and requests sit at 89% today.

A horizontal bar chart of memory requests as a share of the node. 53% with ClickHouse at 1 GiB, 73% after raising it to 4 GiB, 119% for Serbia with Croatian settings, which does not fit, and 89% today with Serbia sized from its own data.
Serbia went live without a second server

Streaming the daily price archive

The archive importer held a whole day in memory, 10.4 million Croatian price rows, and peaked at 4,941 MiB with a 512 MiB request. On an offline copy, price data made up 86% of the live heap.

First I set a soft memory limit with GOMEMLIMIT, ran garbage collection more often with GOGC at 50, reused buffers and parsed three chains at a time. Then I rewrote the parser to stream price files row by row. On the offline copy the live heap after unpacking fell from 1,606 MiB to at most 25 MiB.

In production the peak working set fell from 3,241 to 1,267 MiB, and with streaming alone the run peaked at 1,289 MiB. Lowering the memory limit from 4 to 2 GiB on 27 August cost no GC time, which went from 143.8 to 115.2 ms per second. A smaller import of three chains barely got faster, 22.24 s against 24 s, because its bottleneck is writing to the databases.

A grouped bar chart of the archive importer's peak memory per full import in production. On 25 August live heap was 2,202 MiB and working set 3,241 MiB, on 26 August with streaming 804 and 1,289 MiB, on 27 August with a 2 GiB memory limit 778 and 1,267 MiB.
Streaming alone brought the peak working set to 1,289 MiB

Serbia has no single daily archive, so a collector builds one in the same format. Some chains publish files that only grow, 2.5 GB for seven of them. A date filter before each download skips 227 of 242 files, and the collector runs as a CronJob twice a day.

94%

less data downloaded for one Serbian chain, 993 MB down to 56.8 MB

75%

shorter collector run, 18 min 57 s down to 4 min 47 s

40%

lower collector peak memory, 347 MiB down to 210

Price quality in ClickHouse

Price quality compares today's best price with the city's 30-day median. ClickHouse recalculates it for about 6.5 million product and city pairs per country after each import, and it is the heaviest job on the server.

In August the pass failed 23 times in 4 of 12 regions. Peak memory followed the number of product and city groups in a chunk, with a sharp edge between 380,000 and 430,000. I split each failing region into the fewest chunks that stay near or under that edge.

On 14 August the whole pass took 600 s with a 0.56 GiB peak, against 804 s and 2.06 GiB with one region failed.

A grouped bar chart of price quality peak memory per chunk in the four Croatian regions that failed. Before the change 2.06, 1.67, 1.62 and 1.79 GiB, after it 0.34, 0.32, 0.32 and 0.32 GiB.
The four failing regions dropped to about a third of a GiB per chunk

More chunks made the pass faster, because a large chunk wrote intermediate data to disk for about 65 s and an extra chunk costs about 11 s.

A column chart of the daily peak working set of the Croatian ClickHouse in production from 7 to 21 August 2026. Up to the change on 14 August peaks were 4,090 to 4,728 MiB, with 7,039 MiB on 13 August from a manual session. From 15 August they were 2,459 to 3,727 MiB.
From 15 August no daily peak reached 3.7 GiB

In September the Serbian pass grew to 37 to 39 minutes, and together with the Croatian pass it pushed the server to 99% CPU. The job burned 9.66 CPU hours a day and wrote 136 to 139 GiB of spill files a day, with no time waiting on disk. The CPU went to compressing those files.

A grouped bar chart of CPU hours spent on price quality per day before the fix. Croatia 2.24, 3.48 and 4.63 hours and Serbia 1.31, 3.08 and 5.03 hours on 6, 7 and 8 September.
CPU time grew from 3.6 to 9.7 hours a day in two days

The spill threshold was compared with the memory of the whole query, so about ten aggregations spilled on every block. I set the threshold per country, removed a dead branch that ran three of six full scans and staggered the two countries.

Before

  • 23 failed runs in 5 days of August
  • 37 to 39 minutes for a Serbian pass in September
  • 143 to 406 s for the heaviest query
  • 8 to 28 GiB written to disk per pass

After

  • No failed region in the pass on 14 August
  • 6 to 10 minutes for a Serbian pass on 9 to 11 September
  • 11.0 s for the heaviest query
  • Nothing written to disk

The CPU fix costs 31 to 44% more peak memory per query, up to 2.89 GiB in one Croatian region. Since 24 September an alert has flagged slower Serbian passes, with a 7-day p95 of 1,150 s, and that regression is still open.

ClickHouse and its own metric log

On 30 August the empty Serbian ClickHouse threw about 180 memory errors a minute. The cause was its system metric log, a table with 1,102 columns. A merge held about 1 MiB of buffer for each of about 2,200 column streams, about 4.3 GiB however few rows it merged.

Doubling the pod limit to 8 GiB stopped the errors and cut CPU from 0.86 to about 0.06 of a core. The errors came back on 9 September, when the log's daily cleanup merge landed in the same slot as the price quality pass. ClickHouse counted 15.3 GiB of memory while the pod used about 1.7 GiB, and it refused 2,014 queries in three days.

The fix that held was a vertical merge with 64 KiB compression blocks for the log, and a 14-day TTL for its sibling query metric log. Log merges in production now take 40 MiB or less.

A bar chart of ClickHouse memory before and after the metric log fix on 11 September. The memory counter in Serbia fell from 15.3 GiB to 255 MiB, log merges in production from a 6.80 GiB peak to 40 MiB or less, one regular merge on a copy from 4.00 GiB to 8 MiB and one cleanup merge on a copy from 4.03 GiB to 652 MiB.
ClickHouse refuses queries by its own counter, which fell from 15.3 GiB to 255 MiB

Cleaning up data nobody reads

Two daily aggregates in ClickHouse had no expiry. They grew by about 3.5 GiB a year, and 21% of one of them was orphaned rows.

A donut chart of one daily aggregate in ClickHouse on 16 July 2026, 1.25 GiB in total. Orphaned rows 265 MiB, 21%, and rows still in use 1,015 MiB, 79%.
A fifth of one aggregate was orphaned rows

I set a 190-day data retention limit, detached three old months with 833 MiB and 234.9 million rows, and added a daily job that merges closed months into fewer parts. I also removed a forced merge that rewrote about 625 million rows on every run.

Bytes read for 500 products fell from 3.63 to 2.70 GiB after the retention change, and the time from 1,014 to 838 ms. Merging closed months later cut a 2.58 GiB read to 1.62 GiB, while the time stayed at 756 to 902 ms because decompression keeps the CPU busy.

Logs and metrics got limits too. ClickHouse system logs keep 7 to 30 days, async metrics are written every 15 s, and the query log keeps queries that take 50 ms or longer. Journald fell from 2.4 GiB to 128 MiB, and Prometheus keeps 15 days or 12 GiB.

Faster answers for the mobile app

In a small city the category feed showed 12 empty pages and needed 20 requests and 14 s for 25 cards. A city prefilter brought it to 30 ms per page.

The app calls 46 endpoints of a REST API in three Go services. Versions live in the path, lists use keyset pagination, and bodies over 1 KB go out with brotli or gzip. Search timings come from EXPLAIN ANALYZE in production, and a trigram index matched to the real search expression did most of that work.

Request Before After Change
Text search in Postgres 115 ms 7.9 ms −93%
Two-letter search 165 ms 35.6 ms −78%
Barcode that is not in the catalog 117 ms 0.34 ms −99.7%
Search inside one store 163 ms 4 to 14 ms −91 to −98%
Favourites with 200 items up to 400 queries 2 queries −99.5%
Photo-first sort in a category 124 ms 0.6 ms −99.5%
Product image, first byte 3.3 s 58 ms cached, 116 ms on a miss −96 to −98%

CI/CD on the same server

GitLab CI scans dependencies, builds one 84 MiB image with all Go services on a minimal distroless base and rolls it out to k3s with rollout checks. Deploys run once or twice a day. In May production moved from Google Cloud Run and Cloud SQL to this self-hosted server.

The runner ran with no limits on the four cores. On 21 August the server had 57 GiB of disk in use, and CI images with build cache were its largest group.

A donut chart of disk use on the server on 21 August 2026, 57 GiB in total. CI images and build cache 15.9 GiB, Postgres 13.6 GiB, k3s images 11.3 GiB, ClickHouse and Prometheus 10.1 GiB, logs 3.4 GiB and other 2.7 GiB.
CI images and build cache took more space than Postgres

Before

  • 4 parallel jobs with no limits
  • Load average up to 25.18 with builds running
  • 57 GiB of disk used
  • 7.9 GB of build cache and 106 old Docker images

After

  • 2 jobs capped at 2 CPUs and 2 GB
  • Load average up to 7.16 during builds
  • 39.5 GiB used, with 18 GiB freed
  • Build cache capped at 2 GB with a weekly prune
  • Unused images removed after 7 days

Integration tests and Jest

Suite Size Run time
Go unit and integration tests 2,566 test functions in 347 files 24.7 s for the main module without databases
Jest tests in the mobile app 2,587 tests in 156 suites 22.9 s
Alert rules with promtool 423 tests, at least one for 130 of 179 rules 186 s

Integration tests run against real Postgres and ClickHouse with no mocks. Turning on eslint with zero warnings found three defects at once, among them a crash on the product screen.

A line chart of Jest tests in the mobile app over time. 71 tests on 15 August, 1,868 on 20 September, 2,457 on 24 September and 2,587 on 1 October 2026.
From 71 to 2,587 tests in under seven weeks

Observability, alerts and incidents

Prometheus, Grafana, Alertmanager and Loki run on the same server. The Go services export 196 metrics of their own, and a watcher in the worker checks every 10 minutes for stale catalogs and silent price feeds. Alert rules grew from about 20 in late June to 179, and they post to a Telegram channel backed by runbooks. Rule fixes are checked with promtool tests and mutations.

A line chart of alert rules and promtool tests from 13 August to 1 October 2026. Alert rules grew from 63 to 179 and promtool tests from 127 on 24 August to 423.
The tests grew faster than the rules

Three production incidents from September show how the alerts worked.

Date What happened Broken since Fixed
4 Sep A chain's store list fell from 47 stores to 1 2 Sep 4 h 51 min after the alert
7 Sep The Serbian collector dropped 4 chains 4 Sep the next day
11 Sep The metric log merge broke price quality about 2 days earlier 5.5 h in Serbia, 7 h in Croatia

Alerts also missed things. ClickHouse has no metrics exporter yet, so one memory error loop went unseen for 26.5 hours, and I found phantom stores with 311,050 rows by hand. In the app, GA4 through Firebase tracks 40 events, 10 user properties and 25 screen names after an opt-in consent screen.

NextDevPlatformA developer platform with an AI assistant on every screen