A multi-agent document processing pipeline has a coordinator agent and 8 worker agents. Workers process document sections in parallel and must write their results to a shared state store. Two workers may complete processing of adjacent sections simultaneously and write to state at nearly the same time. What shared state pattern prevents data corruption?
Select an answer to reveal the explanation.
Short Explanation
Optimistic locking lets each worker read the current state version, apply its update, and write only if the version hasn't changed. If two workers collide, the loser retries, so you keep parallelism without corrupting shared state.
Full Explanation
The optimistic locking pattern is correct because it handles concurrent writes without blocking: workers operate independently, version checks at write time detect conflicts, and retries handle the rare case of concurrent updates to the same state. This pattern scales well and is the standard approach in distributed systems for low-contention scenarios. The global dictionary in memory shared across all agent processes is wrong because an in-memory shared dictionary without locking leads to race conditions — concurrent writes can corrupt the dictionary or lose updates when two processes overwrite each other's changes. Requiring the coordinator to collect all worker outputs before writing any state, serializing all writes through a single process, is wrong because it creates a bottleneck that eliminates the parallelism benefit of 8 workers — throughput is limited to the coordinator's write speed. The file-based state pattern where each worker writes to a separate file and the coordinator merges files at the end is wrong because it is a valid pattern but requires a final merge step that cannot be used for cross-worker reads during processing — workers cannot see each other's intermediate results needed for dependency resolution.