Ch.77: System Design: Build an eBay-Style Auction Service
Outline
- 0:00 One camera, one sale
- 0:38 The complete architecture
- 1:03 Requirements and exercise rules
- 2:19 Listings and stale search
- 3:29 Proxy bidding and the public price
- 5:18 Durable bids and safe retries
- 6:55 Reliable live updates
- 8:56 Which last-moment bids count?
- 10:16 A separate soft-close policy
- 11:26 Capacity and writer recovery
- 13:12 One winning order, awaiting payment
- 14:26 Architecture recap
Transcript
0:00 Let's design an online auction service, like eBay: a seller lists an item, buyers bid until it ends, and the winner gets an order. Two accepted winners would break that sale. We'll follow one camera. The camera starts at 20 dollars. Ava enters 100, yet the listing still says 20. I'd want to check that before deciding what our bid API is supposed to do. Ava is setting a private spending limit. The service bids for her up to that ceiling. That's proxy bidding; eBay's public bidding guides are linked in the description.
0:31 Then her hundred is permission to compete. Does entering that maximum take 100 dollars from her account? So that limit causes no charge. Follow it into our architecture map: the middle decides bids, the browsing side shows public state, and the last branch turns a closed auction into an order. So a phone can display the latest price without being allowed to decide who wins. I want to keep those paths separate when the auction gets busy. We'll return to that map with a winner. First, what does Ava need from this backend to buy the camera?
1:03 First, our functional requirements follow that purchase: publish and find the listing, submit or raise a private limit, follow the current price, and close with an order for the winner. Those are the actions we provide. We have the buyer's journey. What has to stay true when Ava refreshes the page just as Ben places a bid? For the non-functional requirements, Ben's accepted bid must survive a server failure, and concurrent bids need one order. Ava's private limit cannot leak onto Ben's screen. Overloaded requests need bounded waiting.
1:34 I'd accept Ava briefly seeing an old price. I wouldn't accept Ava and Ben each getting a winning result. They should be arguing over the camera, not both holding a winner confirmation. Agreed. Our exercise sells one item in dollars, uses 1-dollar increments, and has no hidden minimum sale price. Limits can rise. Bid retractions and seller cancellation after bidding need separate policies. Let's keep those outside this version, along with payment processing and shipping. The auction should hand them a final result.
2:04 A winning bid alone doesn't say that money arrived. For scale, imagine a million people watching one popular auction, with 10 price changes a second. That's our invented workload. We'll use it to size the viewing path after the bid rules are clear. With those requirements set, publishing starts with the camera's image in object storage, served through a delivery network. The listing record holds its seller, item, currency, starting price, closing policy and deadline. The picture can come from a fast cache.
2:33 The decision about whether this auction is open has to come from the current auction record. Right. The publishing transaction also saves work to update search and browsing. That work can run afterward, so a slow index doesn't keep the listing transaction open. For example, I find the camera just after bidding ended, but search still advertises it as available. Does the service let me place a bid from that page? It receives the request, then checks the authoritative record. If bidding has ended, it refuses the new bid.
3:04 Finding a listing is useful discovery; it doesn't reserve a right to bid later. I'd still show an ended state as soon as the display catches up. Otherwise the system is correct while the buyer keeps clicking a button that can't work. Yes, the rejection should refresh that page. Now the camera is published and Ava has found it. Let's work out how her private limit produces the public price. Now take Ava's single row on screen. Her limit is 100. With no competing bidder, the public price stays at our 20-dollar starting price.
3:38 The unused part of her limit stays private. Then Ben enters 60. Walk me through the pricing. How can Ava stay ahead without paying the full hundred she authorized? We compare their limits. Ava can beat Ben's 60 by our 1-dollar increment, so the public price becomes 61. We store that result; we don't need two automated buyers taking turns on the network. Oh, my mental model was a little bidding robot repeatedly clicking for her. The service can calculate the result directly from the standing limits.
4:08 It can. Cam next matches Ava's limit. Our tie rule favors whoever established that maximum first, so Ava keeps the lead. The public price rises to 100. That reaches Ava's limit without adding another dollar. The formula on screen has a ceiling: the price can never exceed the leading bidder's own maximum. Right. With multiple bidders, take the runner-up's limit plus a dollar, subject to the starting price and that ceiling. These simplified increments and tie rules belong to our exercise. That caveat matters: real eBay increments vary by price band.
4:42 Now Ben raises his limit from 60 to 120. That gives him the lead at 101. And Ben has one current row. His old submission remains audit history; it doesn't get a seat as another bidder. We compare people, each with their current limit. Ava raises to 110. Ben can still beat her, so the visible price becomes 111. His ceiling is hidden from other bidders. We've separated 3 values: Ben's private ceiling, the displayed price of 111, and his current lead. He becomes the final winner only when bidding closes.
5:18 Now suppose Ben and Ava reach different API servers together. Both servers could read the same old price. We need one ordered decision for this auction before either returns an accepted result. I'd start with a database transaction locking the parent auction row. Every change to its bidder limits, including a close, has to go through that same lock. Yes. Under that lock, look up the authenticated bidder's request key. For a new request, check the auction, time and bid rules, then update the maximum, price and auction version together.
5:50 And save the request's outcome and its publication work in that transaction. We tell the buyer Accepted only after the commit meets our durability requirement, including the configured replica confirmation. That keeps the next API server from choosing a competing result in parallel. The two possible orders on screen both finish with Ben ahead for the same final amount, although the intermediate leader can differ. Ava's original bid committed, but her phone missed the reply. Now Ben leads. When she retries the original request key, what are we telling her?
6:21 The same key and payload return her original accepted receipt, labeled with its old version. She then reads current auction state. The receipt proves her earlier bid counted, not that she currently leads. I'd make that label explicit in the API response. Otherwise a successful retry could paint an old 20-dollar price over the newer 111 on her screen. Exactly. Reusing her key with a changed amount is a conflict. Another buyer can use the same key text without retrieving Ava's receipt. A retry recovers the original decision.
6:54 Next, the dispatcher reads that saved publication task, sends the committed update, then records delivery. The table holding those tasks is our outbox. It keeps this work out of the buyer's request. Suppose the server saves Ben's bid, then stops before sending anything. We have an accepted decision, but no new price on the phones. Where does recovery find its work? In the saved task, beside Ben's committed state. In 2008, eBay technical fellow Dan Pritchett described committing a sale record alongside its pending changes to user totals.
7:27 The sale could complete while that update waited. That's our frozen price screen: the decision survived, and recovery can find its unfinished work. We're borrowing the historical decision, without claiming that it describes eBay's current bidding stack. Our public message carries the auction, version, price, leader alias, deadline and state. The private maximum stays out of that message, the browsing cache and public logs. How does a buyer reconnect after missing updates? Replaying every old price sounds unnecessary if the auction has already moved on.
7:58 Give each viewing node a public snapshot and its list of watchers. It handles joins and state changes one at a time. A join captures that snapshot and registers the watcher together, then sends the starting snapshot first. Suppose it captures Open at version 5, then bidding closes at six while the phone is catching up. Where does six wait? Look at the slot beside this connection. Six waits there while five is being sent, replacing any older pending update. It goes next on the same stream. The client keeps the highest version; a disconnect repeats the join.
8:33 So the close is either in the initial snapshot or queued for this connection. I'd use server-sent events for that one-way feed, with private bids arriving through separate authenticated requests. Yes, and the join logic is ours; SSE doesn't supply it automatically. A gap triggers a fresh join. We can skip intermediate display prices while preserving the durable bid history. Now return to our fixed deadline: the scheduled end doesn't move. Cam tries 150. The phone sends before cutoff, but his request waits for the auction lock until just afterward.
9:08 My first thought was to use the time it reached our API. If it's already queued inside our service, why shouldn't that count as an on-time bid? Because we're choosing eligibility at the serialized check. We sample the authoritative database clock after acquiring the lock and require it to be before the deadline. Earlier arrival doesn't reserve that decision point. Okay, that's a real tradeoff. An overloaded queue can reject someone who clicked in time. The client needs an accepted receipt, not confidence in its own countdown.
9:39 There's a subtle database trap too. Postgres's now function reports transaction-start time. A transaction that waited for the lock needs an actual current-time sample afterward, or it could use an obsolete time. Then the late close worker can't leave a loophole. For example, while the record hasn't been marked Closed, Cam's new bid fails the deadline check. What happens to a valid check whose commit finishes after cutoff? It still counts under our stated rule. Closing waits for that same lock, then records the result. Cam was checked too late in our main trace.
10:13 Ben's earlier lead therefore survives closing. But that fixed deadline isn't the only product choice. eBay's current help describes testing extended bidding in some categories. A bid in the final minute resets the remaining time to 60 seconds. So consider a separate soft-close example. A valid bid at 10 seconds before eight moves the end from eight o'clock to 50 seconds past eight. The same bid commit records the revised cutoff and increments its deadline version. Winner selection will use the updated deadline, while our fixed-close camera example keeps its original result.
10:47 I can see an old scheduled close message still arriving at eight. Does the worker trust the time written on that message? It locks the auction row and compares the message's expected deadline version with the current one. Version one is stale against version 2. It cannot close this auction early. And I'd keep a database scan for due, unfinished auctions. If a replacement timer disappears, the scan can rediscover the work instead of leaving the auction open indefinitely. Agreed. The timer wakes a worker; the current record authorizes closing.
11:18 That's useful for either policy. We still have to stop the record from becoming the bottleneck for all those watching phones. Now separate that one auction's decisions from the crowd watching them. Different auctions can live on different database shards. Keep each auction's row, current bidder limits, receipts and outbox together. Adding shards spreads independent auctions without splitting one auction's decision. The popular camera still queues decisions behind one writer. I'd measure its complete lock-holding time, including replica confirmation.
11:45 More API servers just add callers to that queue. One divided by that time gives an ideal rate ceiling, before other overhead. We'd load-test for a safe limit, cap waiting, and return Busy. A buyer who retries can still miss the deadline. On the viewing side, our million viewers, 10 updates a second and 500 bytes per update produce 5 gigabytes a second before overhead. That's just one auction. So that bill sits with our viewers. Fanout means distributing an update across those watching connections.
12:17 Its workers and public-state cache can expand while the bid transaction stays short. Keeping just the latest update each second cuts the example to half a gigabyte a second. Still substantial. Viewers may miss a brief intermediate price, so that viewing contract has to be explicit. Suppose Ava is waiting for her bid's reply when the primary fails. We chose to confirm the saved bid on a replica before telling her Accepted. Does that make any replica safe to promote? Only one with the committed state.
12:48 We must also stop the former writer from accepting more bids. If we can't verify both, do we take Ava's next bid while working that out? No. Pause new acceptance until authority and clock health are trustworthy. Her missing reply doesn't prove the bid failed; after recovery, the same key retrieves any recorded result. We sacrifice bidding availability while preserving the auction's one authoritative history. With Ben's winning price settled, we can create his order. The closing transaction saves that final result and an order-creation task.
13:19 The order service handles the next step independently. Take an order attempt that succeeds while the dispatcher misses its acknowledgement. What prevents a repeated close event from creating two purchases? Give the order a unique auction identity. Reprocessing auction 42 finds the existing order and its original winner and price. It returns that result instead of creating another purchase. Right, and the order begins Awaiting payment. Payment success, failure and recovery are separate states. A failed payment doesn't quietly reopen bidding or substitute another winner.
13:52 Exactly. An auction with no accepted bids closes Unsold and creates no order. For the camera, operators should be able to follow the closed result into Ben's single pending order. That recovery route needs observability. I'd measure where it waits: bid decisions, publication tasks, due closes or order creation. A healthy public price cache wouldn't tell me that an order was still missing. Those separate delay measurements point us to the right worker. Then we can recover the unfinished step from saved work, while preserving the bid history and the final auction result.
14:26 Finally, back on the original architecture map, follow the camera from its listing into the auction authority. Ava, Ben and Cam's private limits produce the public price without becoming public limits themselves. The viewing branch spreads our public snapshot to the crowd. The deadline worker returns through the same authority, and the order branch receives Ben's settled result. Ben permitted 120, but the recorded winning price is 111. That creates an order awaiting payment; the auction service hasn't moved his money.
14:56 That's the distinction I want on the buyer's screen: a limit they chose, a current price they see, and a completed auction they can act on. And the backend keeps enough durable state to support each of those promises. Thanks for listening to Learning Podcasts.