CSE Roadmap

Common tier · about 4 hours · 12 parts

Part 9 of 12 · 2 min read

Mistakes that corrupt shared state

Common mistakes, and what the example does not do.

Mistakes that corrupt shared state

  • Replacing the whole state on every write.
  • Retrying without idempotency.
  • Optimistic retries with no back-off or limit.
  • Merging decisions as if they were counters.
  • Broadcasting the full state into every prompt.
  • Assuming messages arrive in order.
  • Losing the trace: no ids or versions in logs, so a lost update cannot be found.
  • A shared state that every agent may write, including agents that read untrusted content.

What the example does not do

About this example What the example does not do
  • No real reasoning. An agent's update is deterministic; real agents may produce inconsistent updates even without concurrency.
  • A single store. No replication, partitions or clocks; those are database topics.
  • No back-off. Retries are immediate, which exaggerates contention.
  • A fixed, simple channel. Real channels add authentication, backpressure (slowing a sender that is faster than its receiver) and security.
  • Optional real-system experiment: run three real agents that append to a shared file or a Redis stream with and without versions, and count lost entries.

Check yourself

Two agents read the same list, each adds an item, and each writes the list back. One item is missing. What happened, and what are two fixes?

Show answer

A lost update: the second write replaced the first. Write only if the version is unchanged and retry, or send the operation "append" instead of the new list.

For the curious Retries cost tokens, not milliseconds

Optimistic concurrency lost no items but needed 83 retries, and every retry re-runs the agent reasoning, so model calls rose from 30 to 113. Merging by operation lost nothing with no retries, but only works when the order does not matter. A contested claim, where several agents want the same task, cannot be merged and needs a visible conflict or a person.

What you will use daily at work

Practice What it looks like
One writer per field Other agents send messages to the owner.
An idempotency key on every message At-least-once delivery plus an idempotent receiver gives the effect of exactly once.
Show agents a scoped view Twelve agents each shown everything used 260,400 input tokens; a scoped view used 83,280.