A Ten-Step Architecture for Flash Sales: How to Stop Your Database From Dying When 100,000 Users Hit at Once
Do you know how to design a flash sale system? This question involves a wide range of knowledge points, and it's no exaggeration to say that this single question can determine your technical level. Many businesses have to deal with high-concurrency scenarios, and flash sales are a typical example. Today, let's talk about this topic in depth.
Let's assume we're selling concert tickets. Based on past experience, we estimate 100,000 people will try to grab them. What does that mean? So many people hitting the server at the same time creates enormous pressure. If we let these requests go directly to the database, the database simply can't handle it and will definitely crash.
Let's analyze the possible problems and solutions one by one. Feel free to add anything I miss.
First, what can go wrong
The most obvious issue is high concurrency. 100,000 people accessing at once. A single-machine Redis can actually handle a lot—it's not hard to sustain tens of thousands of QPS in daily operations. But for hot products, traffic can double or triple, and Redis can't keep up. High concurrency also brings classic problems like cache avalanche, cache breakdown, and cache penetration—if any of these occur, requests will eventually hammer the database.
Second, overselling. A merchant prepares 100 phones for a promotion, and after the event, inventory becomes -300 because 300 orders were placed. If they ship, they lose money; if they don't ship, users complain and the platform closes the store. The root cause is concurrent decrement: when only one item is left in stock, four servers query at the same time, all see "in stock," and all decrement. Stock goes negative.
Third, machines beat humans. Humans can't outpace scripts—that's common sense. Think about grabbing train tickets or concert tickets: the moment the page opens, it's sold out. It's probably not your hand speed. The ticket-snatching systems used by scalpers have architecture designs more refined than many legitimate companies' business systems.
Fourth, exposed links. Technically savvy people press F12 to open developer tools, and the flash sale URL in the webpage code is plainly visible. If the link leaks early, someone can send requests before the sale opens. You gray out the button on the frontend? Useless. Graying out only prevents clicks; it doesn't stop people who have the link and directly call the API.
Fifth, database collateral damage. Tens of thousands or even hundreds of thousands of requests per second hitting the database directly—it's only a matter of time before it collapses. The trouble is that a website usually runs more than just the flash sale. When the flash sale drags down the database, the homepage, orders, and payments all go 404, and the entire site is down.
Next, how to save it
First move: isolate the flash sale. In a microservices architecture, give the flash sale its own service and its own database, just like an order service has its own order database. Add indexes on key fields when creating tables, and after writing SQL, use EXPLAIN to check the execution plan to confirm there's no full table scan. The goal is simple: if the flash sale fails, other business keeps running.
Second move: add salt to the link. The server uses an algorithm like MD5 to encrypt a randomly generated string and appends it to the flash sale link. Nobody knows the address before the sale starts. When a request arrives, the backend verifies the link again; if it doesn't match, it's rejected. Dynamic links plus backend validation make F12 useless.
Third move: Redis cluster. If a single machine can't handle it, go cluster: master-slave replication, read-write separation, multiple Redis instances working together. Sentinel monitors each node's status and immediately detects and handles failures. Persistence is enabled to ensure data isn't lost. With all three, high availability is achieved.
Fourth move: Nginx at the door. Nginx is a high-performance web server with strong concurrency handling, but a single node still has limits. So use load balancing to spread requests—for example, Nginx Plus supports multiple load-balancing algorithms to evenly distribute traffic across multiple nodes, and then down to a pool of Tomcat servers. Keepalived handles node high availability: if one machine dies, traffic automatically switches to another. The gateway layer also filters obvious malicious requests so abnormal traffic doesn't hit the backend.
Fifth move: static resources to CDN. Flash sale pages have a fixed pattern. With front-end/back-end separation, static resources on the frontend server—images, styles, script files—can be pushed to a CDN. When the sale starts, users load the page directly from the CDN, so the backend only handles the truly critical order requests, and response times improve.
Sixth move: subtle button tricks. The frontend periodically requests server time; the button stays grayed out until the time arrives. After a user clicks, the button grays out for a few seconds to prevent repeated clicks. Don't underestimate this—it directly cuts down the "density" of requests.
Seventh move: rate limiting. The frontend limits click frequency: after one or two clicks, the user must wait a few seconds before clicking again. The backend is more direct: when inventory is sold out, it returns "sale ended" and keeps subsequent invalid requests out of the business flow. For a more professional approach, rate-limiting components like Sentinel or Hystrix are readily available.
Eighth move: preload inventory into Redis. A flash sale is essentially a race for inventory. If every request goes to the database to check and decrement, the database will collapse. Before the sale, a scheduled task loads inventory into Redis. During the sale, all checks and decrements happen in Redis, and after the sale, changes are asynchronously synced back to the database. The database never has to face the traffic.
But here's a new problem: when one item remains, four servers query Redis at the same time, all see "in stock." Doesn't that cause overselling again?
Ninth move: Lua scripts. Redis has supported a Lua environment since version 2.6. A key feature of Lua scripts is atomicity: no other commands can interleave during execution. Write "check inventory" and "decrement inventory" into the same script and hand it to Redis for execution—there's no gap between checking and decrementing. Once inventory hits zero, all subsequent requests return failure. Overselling is firmly suppressed.
Tenth move: triple insurance. If pressure is too high, rate limit first, blocking some requests at the door. If rate limiting isn't enough, degrade: cut non-core features to protect the main flow. If the system is truly overwhelmed, circuit-break to avoid dragging down other systems. Also isolate services so no one's failure spills over to neighbors.
Final move: message queue for peak shaving and valley filling. If 100,000 requests directly hit the database, the database will die. First stuff all requests into a message queue (MQ), then process them bit by bit at your own pace. The flood comes in as a torrent but leaves as a trickle—the database survives.
This whole setup is a funnel
Looking back at the entire chain: CDN and staticization block page requests at the outermost layer, Nginx and the gateway spread traffic and filter malicious requests, button graying and rate limiting cut invalid clicks, Redis absorbs the inventory checks, and MQ slows the final write rate to the database.
Each layer blocks a portion of traffic for the next layer. Of the 100,000 people, only a few thousand real orders reach the database.
Next time you fail to grab tickets and the page spins and shows "sold out"—don't blame your hand speed. You're probably caught in one layer of this funnel, politely stopped by this design. The designers want exactly this effect: let most requests fail quietly, and let a few succeed.