跪拜 Guibai
← All articles
Backend · Java · CloudBase

A Ten-Step Architecture for Flash Sales: How to Stop Your Database From Dying When 100,000 Users Hit at Once

By 挖掘狂人 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Every developer who has run a flash sale event knows the pain of a database meltdown, overselling tickets, or a site-wide outage. This design pattern shows a concrete, layered answer that any team can adapt, and it makes clear why a single Redis instance or a simple button disable is never enough.

Summary

Flash sales are the stress test every e-commerce backend dreads. A typical scenario: 100,000 users try to buy concert tickets at the same moment. If requests hit the database directly, it collapses. The failure modes are familiar: high concurrency triggers cache avalanche, breakdown, and penetration; concurrent stock decrements cause overselling (100 phones sell 300 orders); bots beat humans to the API; exposed URLs let anyone fire requests early; and one crashed flash sale database takes down the whole site.

The fix is a multi-layered funnel. The flash sale gets its own microservice and database so failures stay isolated. Links are hashed with random strings and verified server-side to block F12 snooping. Redis runs in a cluster with master-slave, Sentinel, and persistence. Nginx or a gateway load-balances and filters malicious traffic. Static resources go to CDN. Buttons are disabled until server time, with click throttling. Rate limiting and degradation with Sentinel or Hystrix handle extreme pressure. Inventory is preloaded into Redis, and atomic stock checks and decrements are done in Lua scripts to prevent overselling. Finally, a message queue absorbs the request burst and writes to the database at a controlled pace.

The result is a funnel: CDN absorbs page loads, gateways spread and filter, frontend tricks reduce clicks, Redis handles the inventory race, MQ regulates writes. Only a few thousand real orders ever reach the database. Most requests fail gracefully, and the system stays standing.

Takeaways
— Isolate the flash sale into its own microservice and database so a crash doesn't take down the rest of the site.
— Generate dynamic MD5-encrypted sale URLs and validate them server-side to stop early requests from leaked links.
— Run Redis in a cluster with master-slave replication, read-write separation, Sentinel for monitoring, and persistence enabled.
— Use Nginx or a load balancer with algorithms like Nginx Plus to spread traffic across multiple Tomcat nodes, with Keepalived for failover.
— Push all static assets (images, CSS, JS) to a CDN so the frontend server only handles order requests.
— Disable the buy button until the server-side sale time and gray it out for a few seconds after each click to reduce request density.
— Apply rate limiting on the frontend (click frequency) and backend (return 'sold out' for invalid requests) using tools like Sentinel or Hystrix.
— Preload inventory into Redis before the sale and perform all checks and decrements there, syncing back to the database asynchronously after the sale.
— Use Lua scripts in Redis (2.6+) to atomically combine inventory check and decrement, eliminating the overselling race condition.
— Layer protective measures: rate limiting, degradation, circuit breaking, and service isolation to keep failures contained.
— Route all order requests through a message queue (MQ) to smooth the peak, writing to the database at a controlled rate.
— The complete architecture works as a funnel: CDN, gateway, button logic, Redis, and MQ each block or smooth part of the traffic, so only a fraction of the 100,000 requests ever touch the database.
Conclusions

The author's core argument is that a flash sale is less about any single technology and more about defense in depth: each layer assumes the next will be overwhelmed and tries to keep it from being hit.

Overselling is framed as a concurrency bug, not a data integrity one, which is why the solution is atomic Lua scripts rather than database locks.

The article treats frontend button disabling as a joke, correctly noting that any determined user can bypass the UI and call the API directly—a reminder that security controls belong on the server.

The funnel metaphor highlights an important design philosophy: it's acceptable for most requests to fail, as long as they fail cheaply and don't drag down the core transaction path.

Many of the techniques described (CDN, Nginx, Redis, MQ) are standard building blocks, but the real value here is the clear sequence and reasoning for how they combine into a resilient sale pipeline.

Concepts & terms
Cache avalanche / breakdown / penetration
Three distinct cache failure modes that all result in requests hitting the database: avalanche (a large set of keys expire simultaneously), breakdown (a hot key expires at a peak moment), and penetration (queries for non-existent data bypass the cache).
Overselling
When simultaneous requests all read the same remaining stock and decrement it, the final sold count can exceed the available inventory, leading to negative stock and unfillable orders.
Lua script atomicity in Redis
Redis executes Lua scripts as a single atomic operation, meaning no other commands can interleave during execution. This allows check-and-decrement to be performed without a race condition.
MQ peak shaving and valley filling
A message queue buffers a huge burst of incoming requests and releases them to the database at a controlled, steady rate, preventing the database from being overwhelmed by spikes.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗