← Writing

Own-store architecture, part 3: Measure bottlenecks and scale with traffic

On this page

When traffic grows, match the slow business operation with the resource it waits on. If catalog and order reads queue up, reduce read load first. The primary PostgreSQL database still settles contention for the last TOTE-NVY. Order 9001 remains one unit of SKU 101 for 18,000 KRW.

Choose a change based on the bottleneck

For the same measurement window, track sample count, user-facing error rate, and successful-request p95 by business operation. Compare them with DB lock waits, connection pool wait and saturation, and the age of the oldest queued job. The table shows diagnostic examples.

Operation and signal Option Remaining constraint
Catalog p95 and read I/O rise Inspect selected columns, indexes, and cursors. Apply a CDN and caching to images and public responses. Price and availability can lag. Checkout rechecks the source.
Checkout errors rise with SKU locks and pool wait Shorten transactions and review the pool budget and concurrent admission limit. One write owner serializes competition for the last unit of a SKU.
Reconciliation and notification queues age Separate outbox workers and tune concurrency and retry limits. Provider limits and duplicate handling still apply. A bigger queue can accumulate more delay.

Tune the catalog query first

First catalog request → Listed SKUs by time and ID → Save last timestamp and ID → Rows below the cursor

Use the two seeded SKUs from part 1 in PostgreSQL 17. The public list selects listed rows in published_at DESC, id DESC order. The first request has no cursor. Subsequent requests use the two values from the last row of the preceding page. The example page size is 1.

CREATE INDEX sku_list_cursor ON sku (published_at DESC, id DESC)
WHERE listed;

SELECT id, code, price_minor, published_at
FROM sku WHERE listed
ORDER BY published_at DESC, id DESC LIMIT 1;

PREPARE list_after(timestamptz, bigint) AS
SELECT id, code, price_minor, published_at
FROM sku
WHERE listed AND (published_at, id) < ($1, $2)
ORDER BY published_at DESC, id DESC LIMIT 1;

EXECUTE list_after('2026-11-01 00:00:00+00', 102);
EXPLAIN (COSTS OFF) EXECUTE list_after('2026-11-01 00:00:00+00', 102);

The first page contains SKU 102, TOTE-NAT. The next contains SKU 101, TOTE-NVY, with the same timestamp. The final cursor yields no rows. The ID breaks timestamp ties so a page boundary can include both SKUs across two requests. Bind the cursor to the same sort and filters. Use an explicit snapshot contract if readers need a fixed view across publish-time or catalog changes.

The index DDL, first, next and empty pages, and EXPLAIN were checked on local 17.12. A two-row fixture cannot establish performance. Start with the estimated plan. Since EXPLAIN (ANALYZE, BUFFERS) executes the query, compare read-only queries with a timeout on representative data in isolation. Account for index build impact and added write cost.

Move reads first

Serve images and public lists from the CDN and check prices and stock at the primary during checkout. If read pressure persists after tuning, use a replica for reads with an agreed staleness tolerance. Read from the primary immediately after checkout so replication lag does not hide a new order. Add search or a read model when search needs or read load justify it. Independent deployments or resource isolation can justify extracting a read module first.

Migration stage Completion criterion
Expand → backfill Add the new read structure and change capture while keeping the old path. Checkpoint ordered primary-key batches and use version-guarded upserts to rerun safely. Preserve versions and deletion markers so an old scan cannot overwrite a newer change or delete. Checkpoint only after applying the batch. Inject a crash between those steps and replay the batch to verify an identical result.
Compare → small cutover Catch up to a common change watermark and reconcile keys, amounts, quantities and deletions. Start with a small traffic share and compare errors, p95, lock and pool waits, queue age and replica lag with the baseline for that operation.
Stop or expand → contract Stop immediately for data divergence, duplicates or overselling. If latency or errors breach the agreed budget, route reads back to the primary and check its capacity. Expand after holding the thresholds and rehearsing recovery. Retire old fields and routes after callers and dependencies are gone.

Keep a single writer for orders and stock throughout a read cutover. If writes must move, route order retries, webhooks, and workers to the current owner. Fence the old owner’s writes and drain in-flight work. Once the new owner commits or starts an external operation, a routing-only rollback could lose that change or apply it twice. Restrict intake and reconcile new commits and external effects. Transfer write ownership back only with proven reverse migration and compatibility. Otherwise, recover at the new source of truth.

Official documentation

Previous — Own-store architecture, part 2: Stock reservations and payments in first production

Discussion

View on GitHub ↗