A periodical about version control, git, and the future of agentic version control.

Optimistic vs Pessimistic Concurrency Control

Pessimistic locking prevents conflicts upfront; optimistic control validates them before commit.

· 9 min read
Cover illustration for “Optimistic vs Pessimistic Concurrency Control”
Distributed Systems Primitives · October 31, 2026 · 9 min read · 1,951 words

Optimistic and pessimistic concurrency control make different assumptions about conflict. Pessimistic control prevents conflicting work through upfront locking, while optimistic control allows work to proceed and validates it before commit. The right choice depends on the isolation the application requires, how often operations collide, and what recovery costs. Those same criteria explain both the value of merge queues and the need for different coordination mechanisms in multi-agent systems.

The core problem both strategies are solving

Concurrent access can lead to results that no transaction meant to produce. A lost update occurs when two transactions read the same value and compute changes, but the second write erases the first. A dirty read exposes data from an uncommitted transaction that may later roll back. A non-repeatable read gives a transaction different values for the same row because another write committed between reads. A phantom read changes the set of rows returned by a repeated range query because another transaction inserted or deleted matching records.

These anomalies share a cause, which is uncontrolled access to mutable state. Concurrency control handles the isolation part of ACID. At serializable isolation, concurrent transactions must produce an outcome equivalent to some serial ordering of those transactions. The operations may overlap in time, but their combined result must respect that constraint. Both strategies meet this goal, but their rules differ on when work that may conflict can go ahead.

How pessimistic concurrency control works

Pessimistic concurrency control assumes that conflicts are likely enough to be worth preventing before work starts. A transaction acquires locks before operating on shared data. A shared lock lets more than one transaction read the same resource, but blocks writes as long as any shared lock is held. An exclusive lock permits only one transaction to hold the resource, making other reads and writes wait until release.

Two-phase locking, or 2PL, organizes acquisition and release into a protocol that guarantees serializability. During the growing phase, a transaction acquires locks and releases none. The first release begins the shrinking phase. From that point, the transaction is limited to releasing locks, a restriction that governs its sequence of access.

The resource covered by each lock determines its granularity. Row-level locks let transactions operate independently on different rows, while table-level locks cover the whole table. Row-level locking needs more tracking, while table-level locking uses fewer locks that cover more data and also hold up unrelated transactions. A transaction does not first complete speculative work and then ask whether another transaction has invalidated it.

How optimistic concurrency control works

Optimistic concurrency control reverses that sequence by assuming conflicts are rare. Its phases are read, compute, validate and write. A transaction performs its work without holding locks during the read or compute phases, then checks for interference before committing. Other transactions need not wait for it to release resources during those phases.

A row has a version number or equivalent token, which the transaction records when reading it. Before commit, the system checks that the stored version still matches the recorded one. A match permits the write and increments the version. A mismatch causes an abort and retry because the transaction worked from a state that has since changed.

Application-level optimistic locking usually uses version columns or ETags in place of the database engine’s native lock system. Validation must cover every relevant data item the transaction touched. Otherwise, checking one unchanged value cannot establish that all of the transaction’s assumptions remain valid. The version token is therefore part of a protocol that defines what requires validation.

Multi-version concurrency control, or MVCC, keeps more than one version of the data so work can go ahead without read-write blocking. Readers see a consistent snapshot while writers make changes. Readers and writers need not block each other. This gives read-heavy systems another way to separate concurrent operations, rather than forcing every reader to compete for access to one current version.

What workload characteristics actually determine which strategy fits

The decision begins with the required isolation level. A team should establish which anomalies the application cannot tolerate before comparing the cost of mechanisms that satisfy that requirement. Financial systems and inventory management make the consequences of lost updates especially clear. That need can favor the upfront exclusion of pessimistic locking, particularly when writes contend for the same records. Correctness requirements constrain the choice, but do not replace workload analysis.

Within that constraint, repeated access to the same records is the primary signal. Frequent optimistic aborts show that work done in parallel is being thrown away before the transactions can complete. Pessimistic locking can avoid that waste by making writers that compete for the same data wait before they start. Short transactions make this a better fit because they hold locks for only a short time. Taking and managing locks still has a cost, but avoiding expensive retries can make it worthwhile.

Waiting has its own failure modes, however. Exclusive locks serialize access even where actual conflicts would have been rare, while contention increases response time. Deadlock occurs when one transaction holds resource A and waits for B while another holds B and waits for A. Neither can proceed unless the database detects and breaks the cycle. Lock waits and deadlocks show costs that the abort rate alone would miss.

Read-heavy paths with few conflicting writes are a good fit for optimistic control or MVCC because validation rarely turns down work that is already done. Long compute phases and distributed operations also make holding locks unattractive, as other transactions can accumulate behind them. Cheap, fast, stateless retries make occasional optimistic failures easier to absorb. Expensive retries point the other way, especially when they involve substantial computation or downstream side effects. When contention stays high, repeated aborts can also starve a large transaction as it competes with many small ones.

Where merge queues and CI/CD pipelines sit on this spectrum

A merge queue applies optimistic concurrency control to a shared codebase. Development proceeds speculatively in parallel, and integration validates whether changes work together. The more restrictive alternative is an “up-to-date branch” requirement that forces open pull requests to rebase and rerun CI before merging. Each successful merge can force all the requests still waiting to go through another full CI run. As concurrency grows, more completed verification becomes stale.

GitHub Merge Queue, generally available since July 2023, tests each pull request against the latest target branch and every change ahead of it in the queue, then merges the requests in order. Testing the changes together checks whether changes that each pass on their own could break main when combined. The queue’s “Build concurrency” setting controls the maximum number of merge_group webhooks dispatched, between 1 and 100. That setting directly limits concurrent CI builds.

GitLab Merge Trains also validate changes together this way. Each merge request gets an internal merged-result commit that joins the latest base branch with all the requests ahead of it. Pipelines run in parallel on those cumulative states. An agentic merge queue extends this arrangement by assigning conflict detection, CI failure triage and queue management to an AI agent. Humans then intervene on decisions requiring judgment rather than routinely deciding when to queue or requeue a request.

Why multi-agent workflows expose the limits of both classical strategies

Multiple coding agents working against the same git tree, configuration or database schema face the familiar problem of concurrent mutable state. Their execution times change the economics of coordination. An agent can hold a resource throughout a long, unpredictable inference step, blocking every other agent that needs it. The Cursor browser-building experiment exposed this failure mode with locking. Agents held locks so long that a large team degraded to the throughput of only a handful.

Optimistic control avoids those waits but has a cost of its own. Invalidating an agent’s work at commit time can discard minutes of generative reasoning. A retry must then repeat expensive work.

The deeper limitation is the treatment of conflicting work. Classical blocking and abort-and-retry models offer a choice between waiting and discarding the transaction’s work entirely. An LLM-powered agent has another capability, which is assessing whether a conflicting write actually invalidates its plan. It may be able to repair only the operations that depended on the changed state. That possibility requires a coordination protocol that can use the agent’s reasoning without treating every detected conflict as a reason to restart.

Agent-native concurrency control as a distinct approach

CoAgent, a framework by Hongtao Lyu et al. submitted to ATC 2026, makes agent participation part of conflict resolution. Its tools declare which resources they read and write, and the runtime tracks each toolcall’s declared footprint. The tools are also designed to be undoable. When commit-time validation detects a conflict, the runtime informs the agent. The agent judges whether the conflicting write invalidates its plan and repairs the operations that depended on the changed state.

The runtime identifies interference, while the agent determines the repair. CoAgent reports results on ten contended workloads within a narrow margin of serial correctness, with meaningful speedup and near-serial token cost. Classical 2PL and optimistic control surrender nearly all concurrency gains on those workloads. The reported correctness margin remains important because performance gains alone do not establish that a mechanism satisfies an application’s isolation requirements.

Related 2026 proposals address other parts of this coordination problem. Atomix records tool-call effects and settles them through progress-aware transactions. S-Bus reconstructs read sets automatically, reducing the burden of declaring footprints. Cordon stages and validates tool effects using shadow state, an effect outbox and recovery metadata before making consequences durable or externally visible. These proposals address distinct requirements rather than offering interchangeable implementations of one protocol.

Together, these mechanisms make the agent’s reasoning an active component of concurrency control. Runtime visibility becomes part of that design. Agent transcripts can show which tools agents use, which operations conflict and which tools could be made undoable. Treating transcripts only as debugging artifacts misses their value as operational signals. Selective repair depends on understanding the work being coordinated.

Choosing between optimistic and pessimistic control in practice

For a practical selection, the first step is to name the anomalies the application must prevent, including dirty reads, lost updates or phantoms. The next step is to identify where concurrent operations actually collide. Pessimistic control is a good fit for banking and financial ledgers when writes contend for the same account balances, while optimistic control or MVCC fits social media feeds and read-heavy analytics when write contention is low. Naturally partitioned work also reduces the case for locking. The application’s label is less decisive than its access pattern.

Where conflict probability is uncertain, retry cost provides the next filter. Cheap, stateless retries make optimistic control a good fit even at moderate contention, while long computation and downstream side effects make a stronger case for prevention or a hybrid that cuts down on work thrown away. Mixed workloads need not share one mechanism. Writes with high contention can use pessimistic locking, while reads and paths where conflicts are rare can use optimistic control or MVCC. That division follows the workload rather than a preference for architectural uniformity.

For merge queues and CI pipelines with moderate pull request volume, runner capacity and concurrency limits may constrain throughput before the optimistic integration model does. Multi-agent coding introduces a different constraint because long inference makes both waiting and restarting expensive. Advisory repair is an emerging option for that setting, subject to the correctness the workflow requires. In a software factory, the actionable approach is to observe conflicts, lock waits, retry costs and tool behavior at each shared resource, then choose the mechanism that meets its isolation needs. This makes concurrency design a repeatable engineering decision rather than a convention inherited across the platform.

Sources

  1. Combined pessimistic and optimistic concurrency control
  2. CoAgent: Concurrency Control for Multi-Agent Systems

More in Distributed Systems Primitives