Scrivio Interview library
LeadershipPrincipal6 min read · updated 12 November 2026

Your services moved between PostgreSQL and MySQL over the years, and lock-related incidents keep recurring because engineers assume one engine's isolation behaviour. What would you standardise across the company about isolation levels and locking?

I would standardise three things: a short engine-specific guide that states each engine's default isolation and locking behaviour, a set of concurrency patterns that are correct on both engines (unique constraints, optimistic versioning, locking a parent row, and retries for deadlocks and serialization failures), and a chosen isolation level per engine with documented exceptions. Shared retry and idempotency libraries plus concurrency tests in CI make the patterns easy to apply, and a cross-team incident review keeps the guide current so the knowledge does not live in a few experts' heads.

Why this question is asked

When a company runs both PostgreSQL and MySQL, the same Java code can behave differently on each, and the difference shows up only under concurrency, usually in production. Engineers carry their mental model from one engine to the other: someone who learned on PostgreSQL does not expect gap locks, and someone who learned on MySQL expects REPEATABLE READ to block where PostgreSQL raises an error instead. Interviewers ask this leadership question to see whether we can turn recurring incidents into a durable standard. In this answer, we will look at where the engines really differ, what the guide should say, which patterns work on both, how to choose an isolation level per engine, and how libraries, tests and incident reviews keep it alive.

1Where the Two Engines Really Differ

Both engines use MVCC, keeping old row versions so readers do not block writers, but their defaults and failure modes differ. MySQL InnoDB defaults to REPEATABLE READ and protects ranges at that level with gap and next-key locks, so a locking read on an empty range can block inserts and cause deadlocks. PostgreSQL defaults to READ COMMITTED, and its REPEATABLE READ is snapshot isolation without gap locks: instead of waiting, a conflicting update fails with a serialization error. PostgreSQL's SERIALIZABLE adds predicate tracking and aborts transactions with SQLSTATE 40001, while MySQL's SERIALIZABLE turns plain reads into locking reads.

What engineers must not assume
TopicMySQL InnoDBPostgreSQL
Default isolationREPEATABLE READREAD COMMITTED
Range protection at RRgap and next-key locks, inserts waitsnapshot only, no gap locks
Concurrent update at RRwaits, then works on the latest rowfails: could not serialize access
SERIALIZABLEplain reads become locking readsSSI, aborts with 40001
Typical retryable errors1213 deadlock, 1205 lock wait timeout40001 serialization, 40P01 deadlock

Notice the last row. The same logical failure arrives as different error codes, so retry logic written for one engine silently does nothing on the other. That single fact explains a surprising share of repeat incidents.

2What the Engine Guide Should Say

The guide must be short enough that people read it: two or three pages per engine, written around situations instead of theory. For MySQL it explains next-key and gap locks with the booking deadlock as the example, why a locking read without a good index locks far more than expected, and how to read performance_schema.data_locks. For PostgreSQL it explains that REPEATABLE READ and SERIALIZABLE fail fast with 40001 and must be retried, that SELECT ... FOR UPDATE locks only existing rows, and how to look at pg_locks. Each section ends with the incident that taught it, which makes the guide memorable and shows why it exists.

3Patterns That Are Correct on Both Engines

The most valuable part of the standard is a small set of patterns that give the same, correct result on either engine, so engineers do not need to reason about isolation details for everyday work. Uniqueness is enforced by unique constraints, never by checking then inserting. Lost updates are prevented with optimistic versioning: a version column, which JPA supports with @Version, so a stale update affects zero rows and fails loudly. Rules across several rows, like a limit per day, lock one parent row that always exists with SELECT ... FOR UPDATE. And every transaction that can hit a deadlock or a serialization failure runs inside a retry.

Java
@Entity
class Account {
@Id Long id;
@Version long version; // UPDATE ... SET version = 8 WHERE id = ? AND version = 7
long balanceCents;
}
// Stale write on either engine:
// ObjectOptimisticLockingFailureException: Row was updated or deleted by another transaction

Notice that none of these patterns depends on gap locks or on snapshot checks, which is exactly why they behave the same on both engines.

4Choosing an Isolation Level per Engine

We pick one default per engine, record it in the guide, and treat anything else as a documented exception. For PostgreSQL, READ COMMITTED is the usual default, with SERIALIZABLE for specific transactions that protect invariants across rows, always with retries. For MySQL, many companies choose READ COMMITTED as well, because it removes most gap locking and the deadlocks it causes, and it brings the two engines' behaviour closer. With binary logging on, MySQL requires row-based (or mixed) logging for READ COMMITTED with InnoDB, and row is the default format anyway.

There is a catch with changing MySQL's default: code written for REPEATABLE READ may silently depend on gap locks to prevent duplicates. So the switch is done service by service, after checking that uniqueness rules are backed by constraints, and it is easy to set per connection pool, for example with HikariCP's transactionIsolation property or spring.datasource.hikari.transaction-isolation.

5Making the Standard Easy to Follow

People follow what is easy. We provide a shared library with a retry helper that knows the retryable errors of both engines by SQLSTATE and vendor code, so teams do not each write their own incomplete list, and an idempotency helper based on a unique request key, so retried requests and retried transactions cannot apply twice. We add concurrency tests to CI: Testcontainers starts the real engine the service uses, and a test fires the same operation from 20 threads and checks the invariant afterwards, such as exactly one booking per slot.

Java
// Shared retry policy (sketch)
boolean retryable(SQLException e) {
return "40001".equals(e.getSQLState()) // MySQL 1213 deadlock and PostgreSQL serialization failure
|| "40P01".equals(e.getSQLState()) // PostgreSQL deadlock
|| e.getErrorCode() == 1205; // MySQL lock wait timeout (retry only if the transaction was rolled back)
}

A lock wait timeout in MySQL rolls back only the statement by default, not the transaction, so the helper's documentation explains when retrying 1205 is safe; this is the kind of detail that belongs in the library once, not in every team's code.

6Keeping the Knowledge Alive

Finally, the standard must learn. Lock and concurrency incidents from every team go into a shared review once a month, where we ask one question: which line of the guide, which library or which test would have prevented this? Then we change that thing. Ownership sits with a small group of engineers from several teams rather than one database expert, so the knowledge spreads, and new engineers get the guide and one hands-on exercise, reproducing the booking deadlock themselves, during onboarding.

Step throughThe improvement loop for concurrency incidents
  1. Incident in any teamlock wait, deadlock, duplicate
  2. Monthly cross-team reviewwhat would have prevented it?
  3. Is a pattern or library missing?
    YesAdd it, with a CI testNoUpdate the engine guide
  4. Share in onboardingreproduce one incident hands-on
  5. Track repeat incidentsgoal: the same cause never twice

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

7Follow-up questions

Why not standardise on SERIALIZABLE everywhere to be safe?

Because the two engines implement it very differently and both cost throughput: MySQL turns reads into locking reads, which increases waits and deadlocks, and PostgreSQL aborts more transactions under contention. It is a good tool for specific invariants, with retries, but a poor default.

How do we enforce the standard without slowing teams down?

Mostly through defaults: the service template configures the isolation level, includes the retry and idempotency libraries, and contains an example concurrency test. A review checklist then only needs to look for the exceptions.

What metrics show the standard is working?

The count of lock-related incidents per quarter, how many repeat a known cause, deadlock and serialization error rates per service, and how many services run concurrency tests in CI.

8Conclusion

In this answer, we saw that lock incidents recur because engineers carry one engine's model to the other: MySQL defaults to REPEATABLE READ with gap locks, PostgreSQL to READ COMMITTED with fail-fast serialization errors. The standard combines a short engine guide, patterns correct on both engines (unique constraints, optimistic versions, parent row locks, retries), a default isolation per engine with documented exceptions, shared retry and idempotency libraries with CI concurrency tests, and a cross-team incident review that keeps it all current.

What to say in the interviewAbout 90 seconds aloud

Open with

The root problem is that engineers assume one engine's behaviour on the other. MySQL defaults to REPEATABLE READ and uses gap and next-key locks, so empty-range locking reads block inserts and cause deadlocks.

Then, if they want more

PostgreSQL defaults to READ COMMITTED, and at its higher levels it does not wait but fails with serialization errors that must be retried. So first I would write a short, situation-based guide per engine, with the real incidents as examples.

Second, and most important, I would standardise patterns that are correct on both: unique constraints for uniqueness instead of check then insert, optimistic versioning with a version column, locking a parent row for rules across rows, and retries for deadlocks and serialization failures.

Third, I would pick a default isolation per engine, READ COMMITTED on PostgreSQL and very often READ COMMITTED on MySQL too, which needs row-based binlog, and document exceptions, switching services carefully because some code may rely on gap locks.

Then I would make it easy: a shared retry library that knows both engines' error codes, an idempotency helper, and concurrency tests in CI with Testcontainers against the real engine.

Finally, a monthly cross-team incident review asks what would have prevented each incident and updates the guide or the library, so the knowledge does not depend on a couple of experts.

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.
Official documentation
MySQL reference manual
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 services moved between PostgreSQL and MySQL over the years, and lock-related...": https://scrivio-interview-library.netlify.app/databases/mysql/1ded2f5304/