Quick revision
The short answer
Per-campaign budgets updated 200,000 times a second fit a single-writer model: if exactly one actor, or one partition owner, applies every update for a campaign, there is no lock contention at all. Actors deliver that but bring sharding across nodes, rebalancing and state recovery after failures; a Kafka topic keyed by campaign with one consumer per partition gives a similar single-writer model with durable, replayable input. A shared database with row locks is simpler to reason about but is unlikely to sustain that update rate on hot campaigns. We evaluate the options on team experience, tooling and crash recovery, and prototype with realistic, skewed load before choosing.
What to remember
In this answer, we evaluated a real-time bidding budget system at 200,000 events per second. The shared database with row locks is simple and exact but cannot sustain a hot campaign's 20,000 updates per second on one row. The single-writer principle, one owner per campaign applying updates in memory, removes contention, and can be built with sharded actors, which need cluster management, rebalancing and state recovery, or with Kafka partitions keyed by campaign, which add durable, replayable input. We decide on team experience, tooling and crash recovery within an agreed overspend limit, and prototype with realistic, skewed load first.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Per-campaign budgets updated 200,000 times a second fit a single-writer model: if exactly one actor, or one partition owner, applies every update for a campaign, there is no lock contention at all. Actors deliver that but bring sharding across nodes, rebalancing and state recovery after failures; a Kafka topic keyed by campaign with one consumer per partition gives a similar single-writer model with durable, replayable input. A shared database with row locks is simpler to reason about but is unlikely to sustain that update rate on hot campaigns. We evaluate the options on team experience, tooling and crash recovery, and prototype with realistic, skewed load before choosing.
Why this question is asked
Real-time bidding is a good stress test for concurrency designs. Every ad impression triggers an auction that must be answered in tens of milliseconds, and each bid spends from a campaign budget that must not be overspent by much. Two architects proposing actors and locks are really arguing about where the contention goes. Interviewers ask this at the fifteen-year level to see whether we can reason from the shape of the workload to a design, put numbers on it and plan to verify the choice. In this answer, we will recap the workload and why contention is the core problem, evaluate the lock-based database design, explain the single-writer principle, compare actors with Kafka partitions, cover recovery and overspend, and end with how to decide.
1A Quick Recap of the Workload
Each bid event says that a campaign won or placed a bid at a price, and the system must subtract that amount from the campaign's remaining budget, and stop bidding for a campaign when its budget runs out. At 200,000 events per second, the important question is how they are distributed. Traffic in advertising is heavily skewed: a few large campaigns receive a large share. If the top campaign gets 10 percent of events, that one campaign receives 20,000 budget updates per second.
So the core problem is not total throughput, which can be spread over many machines, but contention: many concurrent updates to the same small piece of state, one campaign's budget. Any design is judged by how it handles the hottest campaign.
2Option 1: Lock-Based Services With a Shared Database
The lock-based proposal has many stateless service instances that each apply a bid with a statement such as UPDATE budgets SET remaining = remaining - ? WHERE campaign_id = ? AND remaining >= ?. The database serialises updates to the same row with a row lock. This is simple to reason about: the budget is always consistent, overspend is impossible, every instance is identical, and recovery after a crash is the database's problem, solved long ago.
The trouble is the arithmetic. Updates to one row are strictly sequential, and each holds the lock for at least the network round trip and the commit, typically from a few hundred microseconds to a millisecond. That allows roughly 1,000 to 3,000 updates per second for one row, an order of magnitude short of the hot campaign's 20,000. Batching helps, but then we are no longer applying each event under the lock. So the database design is the right choice only if the rates per campaign are low or the budget is checked approximately, for example by updating the database every second from an aggregate.
3The Single-Writer Principle
The alternative starts from a different principle: if exactly one thread owns a piece of state and applies every update to it, that thread needs no locks and no compare-and-set loops, and an in-memory update takes nanoseconds. A single thread can easily apply millions of simple budget subtractions per second, so even our 20,000 per second hot campaign uses a tiny fraction of one core. The work then becomes routing: every event for campaign 42 must reach the one owner of campaign 42, and the system must handle what happens when that owner fails. Actors and Kafka partitions are two ways to build exactly that.
- Bid events200,000 per second from the auction servers
- Route by campaign idactor shard or Kafka partition by key
- One owner per campaignapplies updates in memory, no locks
- Budget stateremaining amount, spent so far, last applied offset
- Snapshot and changelogperiodic snapshots for fast recovery
- Budget decisions back to biddersstop or throttle a campaign
6 layers, from the top down. Step through them one at a time.
Routing replaces locking: every event for a campaign goes to its single owner.
4Actors Versus Kafka Partitions
With actors, each campaign is an entity actor holding its budget, and a framework such as Apache Pekko's cluster sharding places the actors across nodes by campaign id and routes messages to them. The model fits perfectly: one actor, one campaign, messages processed one at a time. The costs are operational and conceptual: the team must run a cluster with membership and failure detection, understand rebalancing when nodes join or leave, during which a campaign's actor is briefly unavailable, and recover actor state after a crash, typically with event-sourced persistence or snapshots in a separate store. If the bid events arrive over a network without a durable log, events in flight to a failed node can be lost.
With Kafka, the bid events are produced to a topic keyed by campaign id, so all events of a campaign go to the same partition, and one consumer in the group owns each partition. That consumer keeps the budgets of its campaigns in memory and applies events in order, which is again a single writer, now with a durable, replayable input. After a crash, a new owner takes over the partition, restores the budgets from a snapshot or a compacted changelog topic, and replays the events after the snapshot's offset. Kafka Streams packages this pattern with local state stores and changelog topics. The costs are a few milliseconds of latency through the log, partition count as the unit of parallelism, and the fact that a hot campaign cannot be split across partitions without extra design.
| Concern | DB with row locks | Actors (sharded) | Kafka partition owner |
|---|---|---|---|
| Hot campaign at 20k updates/s | Too slow on one row | Easy in memory | Easy in memory |
| Consistency of budget | Exact, transactional | Exact per actor | Exact per partition |
| Crash recovery | Built into the database | Persistence or snapshots needed | Snapshot plus replay from offset |
| Input durability | Each update committed | Depends on transport | Durable, replayable log |
| Operations | Familiar | Cluster, sharding, rebalancing | Kafka cluster, partitions |
| Latency per event | Commit per update | Microseconds in memory | Milliseconds through the log |
5Recovery and Acceptable Overspend
Both single-writer designs keep the truth in memory, so the most important design question is what happens in the seconds after an owner dies. With Kafka, events are not lost, but until the new owner has restored and replayed, nobody is applying updates for those campaigns, and the auction servers either keep bidding on stale budgets or pause the campaigns. With actors, the same gap exists during rebalancing. So the business must define acceptable overspend, for example 0.5 percent of a daily budget, and the system must enforce it: bidders can hold a small local allowance per campaign, leased from the owner, and stop when it is used up, which bounds overspend during any failure to the outstanding allowances. This leasing idea also helps the hottest campaigns, because the owner hands out chunks of budget instead of handling every bid individually.
6How to Decide
The decision weighs requirements and people, not just models. Team experience counts heavily: a team that already runs Kafka well and knows Kafka Streams can deliver the partition-owner design with familiar tooling, while an actor cluster would be a new platform to learn under production pressure, and the Akka licence change means a new adoption would likely use Pekko. Tooling for debugging and monitoring, such as consumer lag dashboards versus actor tracing, matters for the incidents that will come. Recovery behaviour, as above, must meet the overspend limit.
Finally, we prototype before committing, with realistic load. That means replaying recorded or synthetic traffic at 200,000 events per second with the real skew, including a campaign at 20,000 per second, and testing the failure cases: kill an owner, add a node, slow a disk. We measure latency of budget decisions, overspend during failover and recovery time. A two-week prototype of the top two options is cheap compared with discovering a year later that the chosen design cannot absorb the hottest campaign.
7Follow-up questions
Could we keep the database but avoid the hot row?
Yes, by splitting each campaign's budget into several sub-budget rows, for example 16 rows per campaign, and having each update pick one at random, then summing them to check the total. That spreads contention but makes the check approximate and complicates exhaustion. At 200,000 events per second it still requires a very large database, so it is usually a stopgap.
What if one campaign is too hot even for a single owner?
A single in-memory owner can handle far more than 20,000 updates per second, so CPU is rarely the limit; the limits are the partition's throughput and the network. If a campaign outgrows them, the leasing approach lets the owner hand out budget chunks to bidders, so the owner processes chunk requests instead of individual bids.
8Conclusion
In this answer, we evaluated a real-time bidding budget system at 200,000 events per second. The shared database with row locks is simple and exact but cannot sustain a hot campaign's 20,000 updates per second on one row. The single-writer principle, one owner per campaign applying updates in memory, removes contention, and can be built with sharded actors, which need cluster management, rebalancing and state recovery, or with Kafka partitions keyed by campaign, which add durable, replayable input. We decide on team experience, tooling and crash recovery within an agreed overspend limit, and prototype with realistic, skewed load first.
Open with
I would start from the shape of the load. 200,000 events a second is spread over many machines easily, but advertising traffic is skewed, so the hottest campaign might get 20,000 updates a second on one budget.
Then, if they want more
That contention decides the design. The lock-based database option is simple and exact, every update is a conditional UPDATE under a row lock, but updates to one row are sequential and each needs a commit, so one row tops out at a few thousand per second, short of the hot campaign.
The better model is a single writer: one owner per campaign applies every update in memory with no locks, which is nanoseconds per update. Actors give that naturally, one entity actor per campaign with cluster sharding, but the team has to run a cluster, deal with rebalancing and recover actor state after a crash.
A Kafka topic keyed by campaign id with one consumer per partition gives the same single writer with durable, replayable input: after a crash a new owner restores a snapshot and replays from the offset. Either way I would agree an acceptable overspend with the business and bound it, for example by leasing small budget allowances to bidders.
Then I weigh team experience and tooling, and since Akka's licence changed I would look at Pekko. Before choosing, I would prototype the top two at full rate with realistic skew and kill owners to measure overspend and recovery time.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A real-time bidding system keeps per-campaign budgets in memory and receives 200,000 bid...": https://scrivio-interview-library.netlify.app/java/concurrency-patterns/69d1fac6eb/