CityScout
A flash-sale ordering backend that sustains 5,000 TPS on one 8-vCPU VM, with Redis Lua stock reservation, Kafka async order writes, and an explicit recovery for every failure path.
Java 17 · Spring Boot · Redis · Kafka · MySQL · Docker
CityScout started from the hm-dianping Spring Boot course project. The part I rebuilt is the seckill path, where the hard problem isn’t speed but never selling a voucher twice, and never losing one when a step fails halfway.
On a single 8-vCPU VM the path sustains 5,000 TPS, and moving order writes off the request path to Kafka cut latency by 40%.
A request runs one Lua script that checks stock and the per-user limit, reserves, and writes a pending record,
then returns the order id before Kafka confirms. The consumer writes MySQL, and afterCommit clears pending.
- Pending is the source of truth for in-flight orders. A failed Kafka send is only logged. A reconciler scans entries older than 90 s and republishes only if there’s no DB order, no failed row, and no DLT fence.
- A fence closes the DLT race. After three failed attempts, a fence key is written before the message goes to the dead-letter topic, so the reconciler can’t revive an order the DLT consumer is still handling.
- The database is the final arbiter. Stock decrements use
stock > 0, and a generated column makes(user, voucher)unique only for active orders, so a cancelled order doesn’t block buying again. - Releases are idempotent. Cancel and timeout write an outbox row in the same transaction; the release script sets a marker so retries can’t return stock twice.
A concurrency script fires 200 users at 100 units of stock and checks that every count reconciles: 100 orders, no user with two, Redis and MySQL in agreement. Next I’d add metrics for consumer lag and DLT volume.