Problems / Databases, hard

Lost updates on a hot row

Two requests read a counter, both add one, both write it back: one increment disappears. Popular rows are updated constantly.

Free to open. Runs in your browser, no sign-up.

Your goals

No lost updates, success ≥ 99%.

The traffic

1,200 requests a second for 30 seconds: 50% reads, 50% writes.

The system

What each component does in this design, and what calls it. The simulator mimics each one from these settings.

ComponentIts job hereSettings
Load balancerLoad balancerTraffic enters here.Calls API.Layer 4 balancers route by IP and port; layer 7 balancers read HTTP and can route by path or header.–
APIApp serviceLoad balancer forwards to it.Calls Postgres.Little's law: throughput ≈ concurrency ÷ latency. 200 slots at 50 ms serve about 4,000 req/s.3 replicas × 200 slots · timeout 1.0 s
PostgresSQL databaseAPI calls it.Postgres runs one process per connection, which is why poolers like PgBouncer sit in front of busy databases.8 vCPU · 60 connections

Hints

Try it first. Open one hint at a time if you get stuck.

Hint 1

Read-then-write races under concurrency.

Hint 2

Let the database do the check-and-set.

One way to solve it

Use atomic conditional updates (UPDATE … WHERE version = ?).

More databases problems