Scrivio Interview library

ACID, isolation and MVCC

31 questions in 11 subtopics.

ACID properties

ConceptFoundations6 min read
Explain each letter of ACID for a relational database and give, for each one, a concrete example of what goes wrong in a money transfer between two accounts if that property is missing.
ScenarioIntermediate6 min read
An order service writes the order row to PostgreSQL in a @Transactional method and then publishes an OrderCreated event to Kafka inside the same method. In production some events refer to orders that do not exist, and some orders never get an event. Explain why ACID does not help here and how you would fix it.
LeadershipPrincipal6 min read
Your company runs core ledgers on a relational database, and several product teams now propose moving to stores that offer weaker guarantees for scale and cost. How do you decide, at company level, which data needs full ACID transactions and what guarantees you accept elsewhere?

Write-ahead logging

InternalsFoundations7 min read
What is write-ahead logging, and how does it let a database such as PostgreSQL promise durability without writing every changed data page to disk at commit time?
DebuggingIntermediate6 min read
A PostgreSQL 17 primary runs out of disk. The pg_wal directory has grown to 400 GB while max_wal_size is 4 GB. Which causes do you check, with which queries, and how do you recover safely?
DesignAdvanced7 min read
Different teams set PostgreSQL synchronous_commit, MySQL innodb_flush_log_at_trx_commit and replica acknowledgement differently, and after a failover one team lost a few seconds of committed payments. Design a durability standard for databases across the company.

Dirty, non-repeatable and phantom reads

ConceptFoundations6 min read
Define dirty reads, non-repeatable reads and phantom reads with a short example of each, and say which standard isolation level is the first to prevent each one.
CodingFoundations6 min read
Two PostgreSQL sessions run at READ COMMITTED on table account(id int primary key, balance int) with id 1 balance 100. Session A:
BEGIN;
SELECT balance
FROM account
WHERE id = 1;
Session B:
UPDATE account
SET balance = 50
WHERE id = 1;
(autocommit). Session A:
SELECT balance
FROM account
WHERE id = 1;
COMMIT. What does each SELECT in A return, and what changes if A starts with BEGIN ISOLATION LEVEL REPEATABLE READ?
DebuggingIntermediate7 min read
A stock reservation endpoint does: read quantity with a repository findById, check quantity >= requested, then save the entity with quantity - requested, all in one @Transactional method on PostgreSQL READ COMMITTED. Under load the stock goes negative in reports. Explain the anomaly and give three fixes with their trade-offs.

Write skew

ScenarioIntermediate6 min read
A hospital rule says at least one doctor must stay on call per shift. Two doctors who are both on call press 'go off call' at the same moment. Each transaction runs SELECT count(*) FROM on_call WHERE shift_id = 7 AND on_call = true, sees 2, and updates its own row to false. Both commit at PostgreSQL REPEATABLE READ. Explain why this is possible and how to prevent it.
DesignAdvanced7 min read
Your platform has dozens of services on PostgreSQL READ COMMITTED, and incident reviews show several write skew bugs: double-booked rooms, overlapping discount campaigns, and two primary addresses per customer. Define an approach that teams can follow to find and prevent this class of bug.

MVCC

InternalsFoundations7 min read
How does PostgreSQL implement MVCC at the row level? Explain xmin, xmax, snapshots, and why an UPDATE leaves a dead tuple behind.
DebuggingIntermediate6 min read
A PostgreSQL table with heavy updates grows from 5 GB to 80 GB in a week, queries slow down, and autovacuum runs constantly but removes few tuples. pg_stat_activity shows a session in state 'idle in transaction' that started six days ago. Explain the link and what you change.
DesignAdvanced6 min read
You own the database platform for 300 PostgreSQL clusters. Bloat, wraparound warnings and vacuum-related incidents keep recurring. Design the operating model for MVCC housekeeping across the fleet.

Snapshot isolation

ConceptIntermediate6 min read
What exactly does snapshot isolation guarantee and what does it not? Relate it to PostgreSQL REPEATABLE READ, Oracle SERIALIZABLE and MySQL InnoDB REPEATABLE READ.
DebuggingIntermediate6 min read
After moving a batch job's transactions to PostgreSQL REPEATABLE READ to get consistent reports, the nightly job that also updates a status column fails intermittently with 'ERROR: could not serialize access due to concurrent update'. The team proposes going back to READ COMMITTED. What do you recommend?
LeadershipPrincipal6 min read
Most distributed SQL and cloud databases advertise snapshot isolation or serializable as their default, while traditional databases default to READ COMMITTED. As a senior technical leader choosing a database direction for the next decade, how do you weigh isolation guarantees against latency and cost?

Serializable isolation

InternalsIntermediate6 min read
How does Serializable Snapshot Isolation (SSI) in PostgreSQL detect anomalies? Explain SIRead locks, rw-antidependencies and why some transactions abort even though no real anomaly happened.
CodingIntermediate6 min read
Write a Spring Boot service method that books a seat under PostgreSQL SERIALIZABLE isolation and retries on serialization failures up to three times. Explain where the retry must sit and which exceptions to catch.
DesignAdvanced5 min read
An architecture group proposes making SERIALIZABLE the default isolation for all new PostgreSQL-backed services so that developers stop reasoning about anomalies. Evaluate the proposal and describe how you would roll it out if you agree.
LeadershipPrincipal6 min read
Twenty years ago most enterprise applications relied on database locks and READ COMMITTED; today many teams use ORMs, microservices and distributed databases. How should a company's engineering standards on transaction isolation change, and how do you make sure the standard is followed?

Two-phase locking

ConceptFoundations6 min read
What is two-phase locking (2PL), how does strict 2PL differ from basic 2PL, and why does it guarantee serializable schedules? Do not confuse it with two-phase commit.
DebuggingIntermediate6 min read
A PostgreSQL service logs 'ERROR: deadlock detected' (SQLSTATE 40P01) several times an hour. The detail shows two transactions each updating rows in the accounts table for a transfer between the same two accounts in opposite directions. Explain the cause and the fixes.
LeadershipPrincipal6 min read
Lock-based concurrency control dominated databases for decades and was then largely replaced by MVCC with optional locking. Explain why the ecosystem moved, and what this history means for how your organisation should design contention-heavy systems today.

Savepoints

CodingFoundations6 min read
In PostgreSQL an insert in the middle of a transaction fails with a unique violation, and every following statement fails with 'current transaction is aborted, commands ignored until end of transaction block'. Show with SQL and with plain JDBC how a savepoint lets you continue the transaction.
InternalsIntermediate6 min read
How does Spring's Propagation.NESTED use savepoints, how does it differ from REQUIRES_NEW, and what pitfalls exist when using it with JPA and Hibernate?

Autocommit

DebuggingFoundations5 min read
A plain JDBC method updates the orders table and then inserts into order_audit. The insert fails, but the order update is still in the database. The code never calls setAutoCommit. Explain what happened and fix the code.
InternalsIntermediate6 min read
Your Spring Boot JPA service sets spring.datasource.hikari.auto-commit=false and spring.jpa.properties.hibernate.connection.provider_disables_autocommit=true. What do these settings change, what is the benefit, and what can break?

Isolation defaults by database

ConceptFoundations5 min read
What is the default transaction isolation level in PostgreSQL, MySQL InnoDB, Oracle and SQL Server, and what does Spring's Isolation.DEFAULT mean?
ScenarioAdvanced6 min read
Your company is migrating 25 services from MySQL 8.4 to PostgreSQL 17. Code reviews show that teams never set isolation explicitly. Which behaviour changes do you expect because of different isolation defaults and locking, and how do you manage the risk?
LeadershipPrincipal5 min read
Engineers across your organisation assume the database default isolation level is safe because it is the default. As head of engineering, how would you set policy for isolation levels across many teams and several database products?