CSE Roadmap

Common tier · about 4 hours · 12 parts

Part 8 of 12 · 2 min read

Answering shared-state questions in an interview

Two agents update the same shared state at the same time. What can go wrong and how do you prevent it?

"The classic failure is a lost update: both read the same value, each computes a new one, and the second write silently overwrites the first. In the example's simulation with six agents, last-writer-wins kept 6 of 30 items and corrupted a running total. I prevent it by turning writes into operations the store can merge (append, add), by optimistic concurrency with a version check, or by giving each field one owner. Optimistic concurrency is correct, but each conflict costs a model call. Model calls went from 30 to 113 in the example's test." Follow-up: "Why is that worse for agents than for a database?" The window between read and write is an LLM call, so conflicts are likelier, and a retry repeats an expensive call. Follow-up: "When can't you merge?" Decisions, such as two agents claiming the same task: you need compare-and-set, a priority rule, or a person.

How do you make agent-to-agent messaging reliable?

"I treat the channel as unreliable: messages can be lost, duplicated and reordered. I use at-least-once delivery with bounded retries, give every message an id and make the receiver idempotent, and add sequence numbers if order matters. At-most-once lost 20 of 100 messages in the example's test. Plain at-least-once repeated 35 effects. Keys plus sequence numbers gave zero missing, zero repeated and zero out of order, with the same 153 messages sent." Follow-up: "Is exactly-once delivery possible?" Not as a channel guarantee; what you get is exactly-once effect from at-least-once delivery plus idempotent processing.

Shared blackboard or message passing?

"It depends on who owns the data. A blackboard is simple and good for append-only contributions that commute, like findings. It needs versions or merge rules for anything else, and every agent can see everything, which costs tokens and raises exposure. Message passing gives clear ownership, isolation and replay, at the price of protocol design: ids, retries, ordering. I use scoped views either way: in the example's test, showing all twelve agents the whole state cost 260,400 input tokens against 83,280 for a scoped view." Follow-up: "What about security?" A shared blackboard lets one compromised agent write to everyone's context, which is a prompt-injection path; scoped views and validated message schemas limit it.

What is idempotency and why do agents need it?

"An operation is idempotent if doing it twice has the same effect as doing it once. Agents retry: a tool call times out, a message is redelivered, a run is resumed from a checkpoint. So any side effect, such as sending an email or creating a record, must carry an idempotency key that the receiver checks." Follow-up: "Where does the key come from?" The caller generates it from the logical operation (task id plus step), not from the attempt, so a retry reuses it.