Live Bidding on Creator Posts: Hot-Auction Backend Design
System DesignModel answerLast updated
Design a backend where creators run timed auctions on posts, bids are ordered correctly under extreme contention, and millions of watchers see the leading bid within a fraction of a second.
Live bidding on creator posts
You are building a feature for a very large photo and video sharing platform. A creator can turn a post into a timed auction. Viewers place bids, and everyone looking at the post should see the current top bid with minimal delay.
Most auctions are quiet: a handful of bids and a few hundred viewers. Occasionally a celebrity runs one, with millions of viewers and a flood of bid attempts in the closing seconds.
What to cover
- Requirements: functional and non-functional, plus the scale numbers you assume.
- APIs: creating an auction, placing a bid, reading state, and subscribing to live updates.
- Data model: what is stored, where, and what the source of truth is.
- High-level architecture: the main components and how a bid flows through them.
- Bid ordering and concurrency: how you guarantee that each accepted bid beat the state right before it, and that there is exactly one winner.
- Real-time top-bid distribution: how updates reach millions of watchers cheaply.
- Scaling hot auctions: what changes for the celebrity case.
- Failure handling: node crashes, retries, duplicate submissions, and clock issues.
Specific question to address
Compare optimistic locking with pessimistic locking and with routing all bids for an auction through a single writer. Explain why, for highly contended auctions, queuing or serializing bids is usually preferable to optimistic retries.
Hints
Hint 1
Start by writing down the invariant: the top bid only goes up, and every accepted bid must exceed the state immediately preceding it. What gives you a total order per auction?
Hint 2
Estimate what happens with optimistic version checks when thousands of bidders read the same version at once. How many succeed per round, and what do the losers do next?
Hint 3
Fan-out is likely your largest cost, not the bid writes. Think about coalescing updates, sending sequence numbers, and a tree of broadcast nodes instead of one publisher talking to every watcher.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Model answer
1. Requirements
Functional
- A creator attaches an auction to a post with start price, minimum increment, end time, and an optional reserve.
- A bid is accepted only if
amount >= current_high + min_incrementand it is processed before the close. - The bidder gets an immediate result: accepted, outbid, closed, or invalid, with a reason.
- Watchers see the top bid and bidder in near real time.
- At close, one winner is determined and payment is triggered.
Non-functional
- Correctness first: no lost bids, no double-applied bids, one winner, deterministic order.
- Latency: bid acknowledgment p99 under ~200 ms; propagation to watchers p99 under ~500 ms.
- Availability: consistency over availability for the bid path (a bid is never acknowledged and then lost); availability over freshness for the view path (a watcher may be briefly stale).
Assumed scale: about 100K concurrent auctions; a typical one sees under 1 bid/s. A celebrity auction may have around 10M watchers and 50K+ bid attempts/s near the end. Naively pushing every change to every watcher (10M x 10 updates/s) is 100M messages/s, so updates must be coalesced.
2. APIs
POST /auctions {post_id, start_price, min_increment, end_time, reserve}
POST /auctions/{id}/bids {amount, idempotency_key}
-> {status: ACCEPTED|OUTBID|CLOSED|INVALID, seq, current_high}
GET /auctions/{id} -> {current_high, high_bidder, seq, end_time, status}
WS subscribe(auction_id) -> stream of {seq, current_high, high_bidder, end_time}
The idempotency key plus user id makes client retries safe. seq is a per-auction version so clients can drop stale or reordered updates.
3. Data model
- Auction row (sharded relational store keyed by
auction_id): config, status,current_high,high_bidder,last_seq. It is a materialized view. - Bid log (append-only, partitioned by
auction_id, ordered byseq): bid id, user, amount, server timestamp, result, idempotency key. This is the source of truth and the replay source. - Idempotency store:
(auction, user, key) -> result, with a TTL of a few hours.
4. Architecture
Client -> gateway -> stateless Bid Service (auth, rate limits, cheap pre-filter against a cached top bid) -> sequencer that owns the auction (routing by consistent hash of auction_id) -> replicated bid log -> persisters (update the auction row and archive) -> pub/sub broadcast tier -> edge WebSocket gateways -> watchers.
The sequencer's ownership is held through a lease in a coordination service so that only one node acts as writer for an auction at a time.
5. Ordering and concurrency
Optimistic locking (read high and version, write with WHERE version = v):
- Fine when conflicts are rare.
- With N bidders reading the same version, one wins and N-1 retry, so work per round is O(N) and total work grows roughly quadratically as retries pile up on one hot row.
- Retries add load exactly when the database has the least headroom, which causes retry storms and exploding tail latency.
- It is unfair: arrival order no longer decides the outcome, and an early bidder can keep losing to lucky retries. That is hard to defend in a money feature.
- Many retries are pointless, since after one success the retried bid is often below the new minimum anyway.
Pessimistic locking (SELECT ... FOR UPDATE):
- Contenders queue rather than fail, so there are no retries and work is linear. Order follows lock acquisition.
- Good for ordinary auctions on a sharded database.
- The limit is that each bid holds a row lock across a database round trip, capping throughput at a few thousand per second per row, and lock waits can pile up.
Single-writer serialization (recommended for hot auctions):
- All bids for an auction go to one sequencer that holds
current_high,seq, andend_timein memory and applies bids in arrival order. - It is pessimistic concurrency taken to its limit: no conflicts to detect, so no retries.
- The sequencer assigns
seq, which is the official order. - A single in-memory loop can process 100K+ bids/s; batching log appends (group commit every 1-5 ms) keeps durability cheap.
- A bid is acknowledged only after its batch is durably replicated, so acknowledged bids are never lost.
Rule of thumb: optimistic wins when conflicts are rare; queuing or serializing wins when conflicts are the norm. A celebrity auction is the norm case.
Closing: the sequencer is the only clock that matters. A bid counts if its sequencer timestamp is before end_time; client clocks are ignored. Optionally add anti-sniping, where a bid in the last 30 seconds extends the end time and the new value is broadcast like any state change.
Also pre-filter in the Bid Service: bids below the cached top bid can be rejected without reaching the sequencer, which sheds a large share of load in a spike. The sequencer remains authoritative.
6. Real-time updates
- After each committed batch the sequencer publishes one
{seq, high, bidder, end_time}message. - Hierarchical fan-out: sequencer -> regional broadcast nodes -> edge gateways with at least one subscriber -> local WebSocket connections. One message becomes thousands of gateway messages, not millions of origin sends.
- Coalescing: publish only the latest state at a fixed tick (for example 100-200 ms) per auction. Intermediate values are skipped; only the latest matters. This turns 100M msgs/s into roughly 10M/s of cheap edge writes, spread over many gateways.
- Sequence numbers let clients ignore stale messages; on reconnect they fetch the current state and resume.
- Viewers who are not on the auction screen can fall back to polling or periodic refresh.
7. Scaling hot auctions
- Normal auctions share sequencers; a celebrity auction gets a dedicated, pre-warmed sequencer and its own broadcast topic.
- Pre-register big auctions so capacity (gateways, broadcast nodes) is provisioned before start.
- Apply per-user rate limits and admission control at the gateway; shed excess bids with a clear "try again" response rather than letting queues grow unbounded.
- Cache the read view at the edge with very short TTLs.
8. Failure handling
- Sequencer crash: the lease expires, a standby takes over, replays the log to rebuild
current_highandseq, and resumes. Fencing tokens on the lease prevent a zombie old writer from appending. - Duplicate or retried bids: the idempotency key returns the original result.
- Log replica failure: appends use a majority or all-replica ack, so acknowledged bids survive.
- Broadcast loss: clients detect gaps in
seqand resync via GET. - Payment at close: trigger through an idempotent job keyed by auction id so a restart cannot charge twice.
Common mistakes
- Acknowledging a bid before it is durable.
- Trusting client timestamps for the close.
- Pushing every change to every watcher without coalescing.
- Using optimistic retries on the hottest row and calling it scalable.
- Forgetting fencing, so two sequencers briefly both accept bids.