THN Interview Prep

Transactions & isolation

Core details

Transaction = atomic unit of work against the database. ACID maps to operations discipline:

PropertyPractical meaning
Atomicityall statements commit or none—no half-applied business steps
Consistencyconstraints + your app rules stay true—DB + domain invariants
Isolationconcurrent transactions do not produce forbidden interleavings—level chosen
Durabilityafter commit ack, data survives process crash—WAL / replication matter

ACID vs BASE (when interviewers contrast them)

ACIDBASE
StabilityStrong per-transaction guarantees in one engineBasically available, soft state, eventual consistency
Typical homeRDS OLTP cores you treat as system of recordCaches, globally replicated stores, async projections
Story“Debit + credit must commit together in the ledger DB.”“Social like-count can lag; repair or recompute later.”

BASE is not “sloppy”—it’s explicit acceptance of temporary inconsistency with defined convergence.

Anomalies (name them crisply):

AnomalyWhat went wrong
Dirty readread uncommitted data that may roll back
Non-repeatable readsame row reread in one txn returns different values
Phantom readrepeated range query sees new matching rows
Lost updatetwo writers overwrite each other’s changes

Isolation levels (know the ladder + which anomaly each allows)

LevelDirty readNon-repeatablePhantomNotes
Read uncommitted✓ possiblerare in production; debugging danger
Read committeddefault on many engines
Repeatable readvaries by enginesnapshot semantics often
Serializablestrongest; retries, deadlocks

Engine specifics differ (e.g. Postgres repeatable read vs MySQL history); in interviews: name anomalies you prevent per level, then say you’d verify vendor docs.

MVCC (multi-version concurrency control)

Idea: readers see a snapshot at txn start (or statement); writers create new row versions; old versions are garbage-collected later.

BenefitCost / watch
Readers often don’t block writersVacuum / purge lagbloat, long snapshots
Fewer reader–writer locksxmin/xmax-style visibility rules in Postgres; undo logs in others
Good fit for read-heavy OLTPLong txns hold snapshot and prevent reuse of old versions

Interview line: “MVCC trades storage + cleanup for read isolation without locking the whole table—long transactions still hurt.”

Optimistic vs pessimistic (unchanged but quick)

Optimistic: version column—UPDATE … WHERE id = ? AND version = ?. Pessimistic: SELECT … FOR UPDATE for short critical sections.

Distributed transactions: 2PC vs saga

Two-phase commit (2PC)

Phases: Prepare (participants vote) → Commit/Abort (coordinator decides).

ProCon
Atomic “all or nothing” across participantsBlocking if coordinator dies after prepare
Familiar in textbooksLatency + availability hit; not the default microservice glue

Staff: “2PC is fragile across many Node.js services over unreliable HTTP—I’d avoid it as the default integration pattern.”

Saga pattern (microservices default story)

Idea: sequence of local transactions each with a compensating action if later steps fail.

StyleMechanismTrade
Choreographyservices react to domain eventsfewer moving parts; harder to trace
Orchestrationcentral orchestrator drives stepsclearer flow; orchestrator is SPoF unless hardened

Pair with idempotency, outbox, and at-least-once messaging (idempotency).

Loading diagram…

Understanding

Long transactions hold locks or snapshots and hurt throughput; calling HTTP, queues, or heavy CPU inside a DB transaction is a staff-level trap. Pattern: short DB txn → commit → post-commit side effects (cache bust, outbox, async).

“Use SERIALIZABLE for everything” is usually wrong: prove which endpoints need strongest guarantees (inventory, balances) vs read-heavy paths on snapshot isolation.

Senior understanding

ScenarioNarrative
Double-submit checkoutidempotency key + unique constraint + txn boundary
Phantom inventoryrange locks or careful application-level reservation
Read replica “bugs”stale read—not isolation—fix routing or versioning
Deadlock stormlock ordering, smaller txns, timeout + retry with backoff
“Eventual is fine”define SLA for convergence + user-visible staleness

Diagram (local transaction boundary)

Loading diagram…

See also

Last updated on

Spotted something unclear or wrong on this page?

On this page