Scrivio Interview library
DesignPrincipal5 min read · updated 12 November 2026

Your company is evaluating a move from a single-primary relational database to a distributed SQL database for global scale. How would existing locking and concurrency patterns be affected, and how would you assess the risk?

Distributed SQL databases usually run at serializable or snapshot isolation by default and resolve conflicts by aborting transactions, so code built for a single PostgreSQL primary must handle retryable transaction errors everywhere. Pessimistic locks and hot rows become much slower, because every lock and commit needs consensus between nodes, possibly across regions. I would assess the risk by inventorying locking patterns, testing real workloads with contention rather than throughput benchmarks, and asking first whether partitioning data by region or tenant would meet the scale need with far less change.

Background to this question

A move to a distributed SQL database such as CockroachDB, YugabyteDB or Google Spanner is often presented as a drop-in replacement: same SQL, more scale. The SQL is similar, but the concurrency behaviour is not, and applications that relied on the locking habits of a single primary can become slow or start failing under load. A principal engineer is expected to find these risks before the migration commits the company. In this answer, we will see what changes under the hood, how each familiar pattern is affected, and how to assess and reduce the risk.

1What Changes Under the Hood

In a single-primary database, every lock and commit happens in one process's memory, in microseconds. A distributed SQL database splits data into ranges or tablets, each copied to three or more nodes, and uses a consensus protocol such as Raft to agree on every write. A commit must reach a majority of replicas, so it costs at least one network round trip between nodes, a few milliseconds in one region and 50 to 150 milliseconds between distant regions. A transaction that touches rows in several ranges also needs a distributed commit across them.

Isolation also differs. CockroachDB defaults to SERIALIZABLE, YugabyteDB offers SERIALIZABLE and snapshot isolation, and Spanner provides strict serializability; check the current defaults of the product being evaluated. These levels prevent anomalies that READ COMMITTED allows, but they do it by detecting conflicts and aborting one of the transactions, which then must be retried.

2How Familiar Patterns Are Affected

Each locking pattern our services use behaves differently, so the assessment starts by listing them and asking how each will fare.

Single primary versus distributed SQL
PatternOn one PostgreSQL primaryOn distributed SQL
Read-modify-write at READ COMMITTEDSilent lost updates possibleSerialization aborts, needs retry
SELECT ... FOR UPDATEMicroseconds, in memoryLock is a replicated write, milliseconds
Hot counter rowLimited by lock hold timeFar lower limit, consensus per update
Sequences and serial idsCheap, orderedHot spot on one range; prefer UUIDs
SKIP LOCKED queuesWell supportedSupport varies; check the product
Long transactionsHold locks and bloatAbort more often, retries grow
Every row needs a decision during the assessment.

3Retries Become Part of Every Write Path

The biggest code change is retry handling. Under serializable isolation, two transactions that conflict cannot both commit; the database aborts one with a retryable error, SQLSTATE 40001 serialization_failure in the PostgreSQL protocol that these databases speak. In a single-primary system at READ COMMITTED, the same conflict either blocked briefly or silently lost an update, so most code has no retry logic at all.

So every transactional use case must be retried as a whole, in a new transaction, with backoff, and must be safe to retry: no emails or payments inside the transaction. The consistent way is one shared helper or interceptor that every service uses, rather than each team writing its own loop.

Java
public <T> T inRetryingTransaction(Supplier<T> work) {
for (int attempt = 1; ; attempt++) {
try {
return tx.execute(s -> work.get());
} catch (TransientDataAccessException | ConcurrencyFailureException e) {
if (attempt == 5) throw e;
sleepWithJitter(attempt); // 20 ms, 40 ms, 80 ms ... plus random part
}
}
}
// which Spring exception a 40001 maps to depends on the driver and version, so check yours

4Hot Rows and Pessimistic Locks Get Slower

Patterns that rely on locking one row many times per second suffer most. On a single primary, a row lock costs microseconds, so a hot counter can take hundreds of updates per second. In a distributed database each update is a replicated write needing consensus, and if the row's leader is in another region, every update crosses an ocean. A counter that handled 500 updates per second may manage 20. Pessimistic locks with FOR UPDATE work, but each acquisition is itself a replicated write, and waiting transactions are often aborted rather than queued.

So hot rows need redesign before the move: atomic updates, bucketed counters, or a single-writer queue per key, and data placement that puts each row's leader near the region that writes it most.

5Assessing the Risk Properly

Vendor benchmarks measure throughput on evenly spread data with few conflicts, which is the case where distributed SQL looks best. Our risk is in contention, so I would run our own workloads: replay a day of production traffic, include the hot paths such as stock counters and account balances, run with the planned multi-region placement, and measure p99 latency, abort rate and retries per transaction. A pilot with one non-critical service in production gives the operational picture: upgrades, backups, failure behaviour and the team's ability to debug it.

6Consider Partitioning First

Finally, I would ask what problem we are solving. If the need is global scale because customers are in three regions, partitioning data by region or by tenant, with one ordinary PostgreSQL primary per partition, may meet it with almost no change to concurrency behaviour. Most transactions involve one customer or tenant, so they stay on one primary with familiar locking. Cross-partition operations are rarer and can be handled with events or sagas. Distributed SQL is the right choice when transactions genuinely span the globe, or when the operational simplicity of one logical database outweighs the costs above; the assessment should make that comparison explicit.

7Follow-up questions

Is SERIALIZABLE by default a good thing?

For correctness, yes: anomalies such as lost updates and write skew cannot happen. The cost is that conflicts surface as aborts that the application must retry, and long or contended transactions abort more often. Correct code needs the retry discipline described above.

What about ORM features like sequences and batch inserts?

Sequential ids concentrate inserts on one range, creating a hot spot, so UUIDs or the database's own distributed id functions are usually preferred, with Hibernate's batching adjusted accordingly. Check the product's guidance for Hibernate and the PostgreSQL driver.

How would you present the result?

As a comparison of options against the actual need: distributed SQL, partitioned PostgreSQL, and staying put with replicas and caching, each with estimated migration work, latency and abort rates from our own tests, and the operational cost. The decision then rests on evidence instead of a benchmark.

8Conclusion

In this answer, we saw that distributed SQL changes concurrency behaviour even though the SQL looks the same: conflicts become aborts under serializable or snapshot isolation, so every write path needs consistent retries, and locks and hot rows cost consensus round trips, possibly across regions. The risk is assessed by inventorying locking patterns, testing our own contended workloads and running a pilot, and by comparing against partitioning by region or tenant, which may meet the need with much less change.

What to say in the interviewAbout 80 seconds aloud

Open with

I would start by being clear that the SQL compatibility hides a different concurrency model. Distributed SQL databases replicate every write with consensus, so a lock or commit costs network round trips, and across regions that can be a hundred milliseconds.

Then, if they want more

Most of them default to serializable or snapshot isolation and handle conflicts by aborting a transaction with a retryable error, 40001. Our services were written for one PostgreSQL primary at READ COMMITTED, where conflicts mostly just block, so they have little retry logic.

That means every write path needs a consistent, shared retry of the whole transaction with backoff, and side effects must be outside it. Hot rows and SELECT FOR UPDATE patterns get much slower, so counters and balances need redesign and leaders placed near their writers. Sequential ids can become hot spots.

To assess the risk, I would inventory our locking patterns, then test our own workloads with realistic contention and the planned region layout, measuring p99 latency and abort rates, not vendor throughput numbers, and run a pilot service.

And I would compare with partitioning by region or tenant on ordinary PostgreSQL, which may give the scale we need with far less change, and present all options with evidence.

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 "Your company is evaluating a move from a single-primary relational database to a...": https://scrivio-interview-library.netlify.app/databases/locking-and-concurrency-control/3074f3582b/