System Design · Practice problem · Hard
Hold the Last Ticket
Hold each ticket for a limited time while the buyer pays, and release it when the hold expires. Let one place decide every claim in a single step that checks and takes together, so no ticket sells twice.
Hard means correctness and scale at the same time. You solve it on a canvas that runs your design. The canvas needs JavaScript; everything on this page works without it.
What is the situation?
A band is playing its last ever show, and you are selling the tickets. There are 1,000 of them, all general admission, so any ticket is as good as any other, and they all go on sale at the same moment. In the opening seconds 8,000 people a second press Buy. POST /tickets/claim asks for one ticket, and the answer is yes, one is held for you, or sold out. Sold out counts as an answer. A buyer who wins then sends POST /orders to pay, and the order has to end up in the Orders database, which is already on your canvas. Some buyers win and walk away without paying. In these tests a buyer who pays does so 5 seconds after winning, so a hold has to last longer than that. After that, a ticket nobody paid for must be able to go back on sale. Machines fail: during a sale any one machine can go down for a few seconds and come back, and the sale has to carry on. Three facts about your hardware. A Counter Store keeps a count in memory. A Counter Store or a Database can put a time limit on a hold, and either one holds a ticket until it is paid for if you do not. And a Counter Store that crashes comes back with the count it had when it went down.
What must your design do?
- Never sell more than the 1,000 tickets that exist, even while a machine is down, and take no more orders than there are tickets.
- A ticket held for someone who walked away must be able to go back on sale, so no hold may go without a time limit.
- At least 99.9% of claims and orders get an answer, and at least 99% while a machine is down.
- Buyers hear about a claim within 500 ms at the 99th percentile. When 1,000 or more people a second are pressing Buy that may stretch to 3 seconds, and to 5 seconds while the store holding the count is down.
- Buyers hear about an order within 1 second at the 99th percentile, even while the Orders database is down.
- Every order ends up in the Orders database.
- The system costs $475 a month or less.
What does Run test?
Run works without an account. It checks your design against 2 tests you can read before you start:
- A steady sale. 100 people a second pressed Buy for 8 seconds, and everyone who won paid. That is about 800 claims, so the tickets did not run out.
- A slow sell out. 20 people a second pressed Buy for 65 seconds, and everyone who won paid. That is more claims than tickets, so the tickets ran out about 50 seconds in.
What does Submit add?
Submit needs an account. It runs 6 more tests you cannot read in advance, and stops at the first one your design fails, showing what happened and why. Passing every test, or giving up, unlocks the written solution.
Which parts can you use?
Already on your canvas: Orders.
- Waiting Room: effectively unlimited, $25 a month each
- Load Balancer: about 50,003 a second, $18 a month each
- App Server: about 2,000 a second, $45 a month each
- Counter Store: about 100,003 a second, $30 a month each
- Database: about 5,000 a second, $120 a month each
- Queue: effectively unlimited, $25 a month each
- Worker: about 1,000 a second, $45 a month each
Every connection takes 1 ms, and the budget is $475 a month.
What does this problem leave out?
- Named seats. Every ticket here is as good as any other, so a seat map and choosing seats together are left out.
- How soon a ticket goes back on sale. We grade whether a hold has a time limit at all and whether orders outnumber tickets, not how many walked-away tickets are sold again before a run ends.
- How far behind a copy runs. A copy here arrives within one connection's delay and is never lost on the way, so a failover never forgets a sale in flight.
- Fencing after a failover. A store that comes back catches up on what it missed before it takes claims, but nothing here decides which store may take claims for good, so nothing stops two of them both deciding after a split.
- How long a crash takes to notice. Your servers learn that a store is down on the first failed call.
- A store lost for good, or one that restarts empty. Every crash here comes back with the count it had.
- A row lock on a claim. A claim at a Database takes no row lock here, though a real one would, so a Database looks faster at this than it is.
- A Waiting Room reopening. Here it closes for good on the first sold out answer. No test frees a ticket during a run, so this never changes a result.
- Fairness in the line, bots, and a limit per buyer.
- The payment provider. Paying here is one write to Orders, and whether the card was charged is another problem in this bank.
- A page showing how many tickets are left. Every test here is claims and orders.
Where does this problem fit?
Its category is Inventory & contention: Many users want very few items. It practises replication, consistency, concurrency control and reliability & ops.
For the whole system, read the interview breakdown Design a Flash Sale System.
By GOAT Engineer. Explore System Design · Privacy Policy · Contact.