Scrivio Interview library
DesignPrincipal6 min read · updated 12 November 2026

Pessimistic locking, optimistic locking with versions, and lock-free designs (append-only events, conditional writes, CRDTs) all exist. Thinking about the next decade of your company's systems, how would you guide the choice of concurrency control strategy for new domains?

I would guide the choice from two questions per domain: how often do writes really conflict, and how far apart are the parties that must agree. Rare conflicts favour optimistic versions, hot rows favour short pessimistic locks or, better, a design that removes the hot row, and anything that crosses services or regions needs conditional writes, idempotent steps and sagas rather than locks. The decision belongs in each design document and is checked in production with conflict and retry metrics.

Why this question is asked

Concurrency control is a decision every new domain makes, often by accident, through whatever the first developer typed. Over a decade the accidents add up: hot rows that limit throughput, retries nobody planned for, and locks that cannot cross the services a domain was later split into. Interviewers ask senior engineers this question to hear a framework, not a favourite. In this answer, we will recap the three families of strategies, build a simple decision process from contention and scope, look at designs that avoid hot rows, consider how distributed SQL databases change the costs, and finish with how to make the choice explicit and measurable.

1A Quick Recap of the Three Families

Pessimistic locking assumes conflicts are likely and prevents them: a transaction locks rows with SELECT ... FOR UPDATE before changing them, and others wait. It gives simple code and no retries, but waiting is the cost, and the lock only exists inside one database and one transaction.

Optimistic locking assumes conflicts are rare and detects them: each row has a version number, and an update succeeds only if the version is unchanged (UPDATE ... WHERE id = ? AND version = ?). Nobody waits, but the loser must retry or report a conflict. JPA supports it with @Version.

Lock-free designs avoid shared mutable rows altogether. Append-only events record what happened and compute state from them. Conditional writes, such as compare-and-set on a key in DynamoDB or a conditional UPDATE, let the store decide atomically. CRDTs, conflict-free replicated data types, are data structures such as counters and sets whose concurrent updates can always be merged, which suits multi-region data.

Concurrency strategies at a glance
StrategyBest whenMain cost
Pessimistic lockhot rows, short local transactionswaiting, deadlocks, no cross-service scope
Optimistic versionrare conflicts, long think timeretries or user-visible conflicts
Conditional writesingle-key invariants, any scaleinvariant must fit one key
Append-only eventshigh write rates, audit needsreads need aggregation
CRDTmulti-region, always-available writesonly mergeable data types

2Choosing From Contention and Scope

The first question is contention: for a given row, how often do two writers arrive within the same few milliseconds? For a customer profile it is almost never, so optimistic versions cost nothing and avoid locks. For a flash-sale stock counter it is constantly, and optimistic retries would waste most of the work, so either short pessimistic locks or a redesign is needed.

The second question is scope: who has to agree? If the invariant lives in one database and one short transaction, pessimistic or optimistic locking both work. If it spans services, a lock cannot help, because no single database transaction covers the steps; we need sagas, which are sequences of local transactions with compensating actions, plus idempotent steps so retries are safe. If it spans regions, waiting for a remote lock adds hundreds of milliseconds, so conditional writes on a home region per key, or CRDTs where the data allows it, fit better.

Step throughPicking a concurrency strategy for a new domain
  1. Does the invariant span services or regions?
    Yesconditional writes, idempotency, sagasNostay in one database
  2. Do writers to the same row often collide?
    Yesremove the hot row, else short pessimistic locksNooptimistic versions
  3. Write the choice downdesign document: strategy, retry policy, expected conflict rate
  4. Measure in productionconflicts, retries, lock waits per endpoint

4 steps in order. Play them, or step through them one at a time.

3Designing Hot Rows Away

Often the best concurrency control is a data model where there is nothing to fight over. A single counter row updated by every order becomes a set of 16 counter shards, each request incrementing a random one, and reads sum the 16 rows; contention drops sixteenfold. A balance row updated by every transaction can become an append-only ledger of entries, with the balance computed or periodically snapshotted; inserts of new rows do not conflict with each other. Work can be partitioned by key, so all writes for one account go to one consumer of a Kafka partition, which processes them in order without any lock.

These designs cost something: reads get more complex, and some invariants, like never going below zero, need extra care. But they scale where locks cannot, and they are much easier to adopt at the start of a domain than ten years later.

4How Distributed SQL Changes the Costs

Distributed SQL databases such as Spanner-style systems, CockroachDB and YugabyteDB are becoming common for new domains. They often run at SERIALIZABLE isolation by default (CockroachDB does, YugabyteDB lets us choose; check the current defaults), and under contention they abort transactions and expect the client to retry. A cross-region write also waits for consensus between replicas, so a lock held across regions is expensive.

This changes the guidance in two ways. First, every service on such a database must wrap transactions in a retry loop for serialization failures (SQLState 40001), with backoff and only around idempotent work, which is a standard library rather than per-team code. Second, the gap between optimistic and pessimistic narrows, because the database is effectively optimistic underneath; the design work shifts to keeping transactions small and avoiding hot keys, which matter even more when every conflict costs a network round trip.

5Making the Choice Explicit and Measured

A framework only helps if teams use it. Every design document for a new domain should state the concurrency strategy for each write path, the expected conflict rate, the retry policy and what the user sees on conflict. Reviewers ask the two questions above. The platform provides the building blocks: a retry component for serialization failures and deadlocks, an optimistic-locking helper that maps conflicts to HTTP 409, and patterns for idempotency keys and outbox events.

Then production keeps the decision honest. We measure optimistic conflicts and retries per endpoint, lock wait time and deadlocks per table, and serialization failures per transaction type. If a domain designed for rare conflicts shows retries on 5 percent of requests, the data says the assumption was wrong, and we revisit the design before it becomes an outage.

6Follow-up questions

Is optimistic locking always cheaper when conflicts are rare?

Usually, but the cost of a conflict matters too. If the losing transaction did expensive work, such as calling an external service or recomputing a large result, even a 1 percent conflict rate can be costly, and a short pessimistic lock on the key row may be cheaper overall. Measure both the rate and the price of a retry.

Where do sagas fit compared with locks?

A saga replaces a cross-service transaction with local transactions plus compensations, such as reserving stock, charging a card and, if shipping fails, refunding and releasing the stock. Each local step may still use a lock or a version check inside its own database, but nothing is held across services, so the saga trades isolation for availability and needs idempotent, retryable steps.

When are CRDTs worth the complexity?

When writes must be accepted in several regions at once, even during network partitions, and the data is naturally mergeable, such as counters, presence, carts or collaborative documents. For invariants like unique usernames or non-negative balances, CRDTs do not help, and a single home region per key with conditional writes is simpler.

7Conclusion

In this answer, we saw that pessimistic locks, optimistic versions and lock-free designs each fit a different mix of contention and scope. Rare conflicts favour versions, hot rows favour short locks or, better, sharded counters, ledgers and partitioned work, and anything crossing services or regions needs conditional writes, idempotency and sagas. Distributed SQL databases push every team toward retry-aware transactions, and the choice should be written into design documents and checked against conflict and retry metrics in production.

What to say in the interviewAbout 80 seconds aloud

Open with

I wouldn't pick one strategy for the company; I'd give teams a short framework and make them write the answer down. The two questions are contention, meaning how often two writers really hit the same row at the same moment, and scope, meaning how far apart the parties that must agree are.

Then, if they want more

If conflicts are rare, optimistic locking with a version column is cheap and avoids waiting. If a row is hot, I'd first try to design the hot row away, with sharded counters, an append-only ledger, or partitioning work by key so one consumer owns each key, and only then use short pessimistic locks.

Pessimistic locks only work inside one database and one short transaction, so anything crossing services needs sagas with idempotent steps, and anything crossing regions needs conditional writes or, where data is mergeable, CRDTs.

Distributed SQL databases are often serializable by default and abort under contention, so the platform should provide one retry component for serialization failures rather than each team writing loops. Finally I'd make the choice part of every design document and track conflict rates, retries and lock waits per endpoint, so we notice when an assumption turns out wrong.

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 documentation below. 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 "Pessimistic locking, optimistic locking with versions, and lock-free designs...": https://scrivio-interview-library.netlify.app/databases/database-locking/6612d3c327/