All system design challenges
#55 Deadlock/Optimistic Locking

Online Auction (eBay-like)

Run timed auctions where thousands may bid on one item in the last seconds, the highest valid bid always wins, and the clock is trusted.

Asked at eBay

What is being asked

Design an auction site. The hard part is the bidding path: many concurrent bids on a single item, each of which must be compared with the current highest, especially just before closing.

Requirements

Functional

  • List an item with a start price and end time; place bids; show the current price live.
  • Close the auction and determine the winner; notify outbid users.

Non-functional

  • A bid is accepted only if it beats the current highest at that instant.
  • No accepted bid is lost; the close is exact.
  • Bursts of bids at the end.

Design

BiddersBid APIBidding service(serialised per auction)Auctions + bids DBCurrent-price cacheLive update channelAuction closer(scheduler)
Boxes are services; rounded shapes are data stores and queues.
  1. A bid request carries the auction id, amount and an idempotency key.
  2. The service runs a transaction: accept the bid only if the auction is open and the amount exceeds the current highest by the minimum increment; then store the bid and the new highest.
  3. The new price is published to watchers over a live channel.
  4. A scheduler closes the auction at its end time by marking it closed in the database, after which no bid can pass the check.

Key decisions and trade-offs

  • Optimistic locking. Read the auction with its version, and update it only where the version is unchanged. A bidder who loses the race gets "you were outbid" and may retry. This avoids holding locks while many bidders wait.
  • Pessimistic locking (SELECT ... FOR UPDATE) is simpler to reason about but queues every bidder on one row, and risks deadlock if a transaction touches several rows in different orders.
  • Serialise per auction. Routing all bids for one auction through a single queue or worker removes the contention altogether and gives a clear order.
  • Server time only. The deadline is judged by the server's clock. Extending the end by a short time on a late bid removes the incentive to bid in the final second.

Follow-up questions

  • How would you implement automatic bidding up to a secret maximum?
  • What happens when the bid arrives one millisecond after closing?
  • How do you update ten thousand watchers of one auction?
  • How would you detect a seller bidding on their own item?
Log in to tick off the challenges you have worked through. Log in