Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 36/ 160
02. Advanced SQL & Relational Databases

Concurrency Control, Isolation Levels & Deadlock Resolution

Pick isolation levels consciously and diagnose deadlocks when writers contend on the same rows.

AdvancedIsolation · Locks · Deadlocks3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

What concurrent changes cannot coexist. Next, read Committed, Repeatable Read, or Serializable. Finally, identify cycle, rollback, and bounded retry.

Think of it like…

Two forklifts each block an aisle the other needs; deadlock detection clears the jam by aborting one route, while a fixed one-way lock order prevents it.

Where you’ve seen it

Transaction A locks order then inventory; B must lock inventory then order-no, reverse that to order then inventory for both. On PostgreSQL SQLSTATE 40P01, rollback and retry the whole transaction with jitter.

Words you’ll hear

Isolation
The transaction visibility and anomaly contract, trading consistency strength against abort rate and blocking.
Locks
Synchronization on rows or objects held until commit or rollback, subject to an engine-specific lock matrix.
Deadlocks
A wait-for cycle that the database resolves by aborting a victim transaction.
Operational boundary
At REPEATABLE READ or SERIALIZABLE a transaction may be aborted and must retry, while READ COMMITTED can still observe changing rows across separate statements and a deadlock retry can rerun external side effects. Use the weakest isolation that preserves the invariant, lock rows in a global order, shorten transactions, add retry policies only for classified serialization/deadlock errors, and test multi-worker races with deterministic keys.

Visual diagram

Loading interactive diagram

Theory & deep dive

Isolation: the working mechanism

Transactions choose how concurrent writes and snapshots interact; row/version locks protect invariants, while a deadlock occurs when transactions cyclically wait on each other's owned locks.

What concurrent changes cannot coexist. Read Committed, Repeatable Read, or Serializable, then same sequence, narrow statement, short time; this exposes the state governed by Deadlocks.

Concurrency Control, Isolation Levels & Deadlock Resolution in one concrete design

Transaction A locks order then inventory; B must lock inventory then order-no, reverse that to order then inventory for both. On PostgreSQL SQLSTATE 40P01, rollback and retry the whole transaction with jitter.

Key takeaway: Transactions choose how concurrent writes and snapshots interact

Concurrency Control, Isolation Levels & Deadlock Resolution: failure mode and production response

At REPEATABLE READ or SERIALIZABLE a transaction may be aborted and must retry, while READ COMMITTED can still observe changing rows across separate statements and a deadlock retry can rerun external side effects.

Use the weakest isolation that preserves the invariant, lock rows in a global order, shorten transactions, add retry policies only for classified serialization/deadlock errors, and test multi-worker races with deterministic keys. Track Retryable SQLSTATE as "PostgreSQL 40001/40P01 classes for serialization or deadlock", and make Transaction time visible to the dataset owner.

Key takeaway: Use the weakest isolation that preserves the invariant, lock rows in a global order, shorten transactions, add retry policies only for classified serialization/deadlock errors, and test multi-worker races with deterministic keys.

How it works, step by step

Step 1 of 4
Isolation

1. State invariant

What concurrent changes cannot coexist. Transactions choose how concurrent writes and snapshots interact; row/version locks protect invariants, while a deadlock occurs when transactions cyclically wait on each other's owned locks.

what concurrent changes cannot coexist
Key numbers at this step
Retryable SQLSTATE
PostgreSQL 40001/40P01 classes for serialization or deadlock
Locks
Synchronization on rows or objects held until commit or rollback, subject to an engine-specific lock matrix.

When to use it & trade-offs

What it is

Transactions choose how concurrent writes and snapshots interact; row/version locks protect invariants, while a deadlock occurs when transactions cyclically wait on each other's owned locks. Transaction A locks order then inventory; B must lock inventory then order-no, reverse that to order then inventory for both. On PostgreSQL SQLSTATE 40P01, rollback and retry the whole transaction with jitter.

Why it matters: At REPEATABLE READ or SERIALIZABLE a transaction may be aborted and must retry, while READ COMMITTED can still observe changing rows across separate statements and a deadlock retry can rerun external side effects. Use the weakest isolation that preserves the invariant, lock rows in a global order, shorten transactions, add retry policies only for classified serialization/deadlock errors, and test multi-worker races with deterministic keys.

When to reach for it

  • Pick isolation levels consciously and diagnose deadlocks when writers contend on the same rows.
  • Choose Isolation because it provides this capability: The transaction visibility and anomaly contract, trading consistency strength against abort rate and blocking.
  • Keep evidence for Retryable SQLSTATE at "PostgreSQL 40001/40P01 classes for serialization or deadlock" so the last production stage-Recover - identify cycle, rollback, and bounded retry-remains testable.

When to avoid or be careful

  • At REPEATABLE READ or SERIALIZABLE a transaction may be aborted and must retry, while READ COMMITTED can still observe changing rows across separate statements and a deadlock retry can rerun external side effects.
  • Do not conflate Locks with Isolation. The second concept contributes different behavior: Synchronization on rows or objects held until commit or rollback, subject to an engine-specific lock matrix.
  • Release only after this guardrail is in place: Use the weakest isolation that preserves the invariant, lock rows in a global order, shorten transactions, add retry policies only for classified serialization/deadlock errors, and test multi-worker races with deterministic keys.

Isolation / Locks / Deadlocks - roles, limits, and operating cost

Isolation
Best for: The transaction visibility and anomaly contract, trading consistency strength against abort rate and blocking.

Strengths: The transaction visibility and anomaly contract, trading consistency strength against abort rate and blocking. Transactions choose how concurrent writes and snapshots interact.

Watch out: At REPEATABLE READ or SERIALIZABLE a transaction may be aborted and must retry, while READ COMMITTED can still observe changing rows across separate statements and a deadlock retry can rerun external side effects.

Locks
Best for: Synchronization on rows or objects held until commit or rollback, subject to an engine-specific lock matrix.

Strengths: Synchronization on rows or objects held until commit or rollback, subject to an engine-specific lock matrix. In this workflow it supports: Order locks - same sequence, narrow statement, short time.

Watch out: The operating cost is real: keep lock and snapshot duration measured in milliseconds or bounded seconds.

Deadlocks
Best for: A wait-for cycle that the database resolves by aborting a victim transaction.

Strengths: A wait-for cycle that the database resolves by aborting a victim transaction. Its output can be checked with Lock order.

Watch out: Operating guardrail: Use the weakest isolation that preserves the invariant, lock rows in a global order, shorten transactions, add retry policies only for classified serialization/deadlock errors, and test multi-worker races with deterministic keys.

Interview signals & numbers to know

When this topic comes up

  • “Where is the failure boundary for Isolation, and how would Retryable SQLSTATE reveal it?”
  • “Why is Locks distinct from this behavior: a wait-for cycle that the database resolves by aborting a victim transaction?”
  • Production clue: At REPEATABLE READ or SERIALIZABLE a transaction may be aborted and must retry, while READ COMMITTED can still observe changing rows across separate statements and a deadlock retry can rerun external side effects.
  • Design probe: Use the weakest isolation that preserves the invariant, lock rows in a global order, shorten transactions, add retry policies only for classified serialization/deadlock errors, and test multi-worker races with deterministic keys.

Benchmarks & rules of thumb

Retryable SQLSTATE -
PostgreSQL 40001/40P01 classes for serialization or deadlock
Transaction time -
keep lock and snapshot duration measured in milliseconds or bounded seconds
Lock order -
every multi-row path acquires the same key sequence

Common mistakes & follow-ups

Common interview mistakes

  • At REPEATABLE READ or SERIALIZABLE a transaction may be aborted and must retry, while READ COMMITTED can still observe changing rows across separate statements and a deadlock retry can rerun external side effects.
  • Reporting success from a job exit code without checking Retryable SQLSTATE and Transaction time.
  • Treating this example as complete without replay, rollback, or access boundaries: Transaction A locks order then inventory; B must lock inventory then order-no, reverse that to order then inventory for both. On PostgreSQL SQLSTATE 40P01, rollback and retry the whole transaction with jitter.

Likely interviewer follow-ups

  • How would you prove this contract instead of only checking that the command ran: The transaction visibility and anomaly contract, trading consistency strength against abort rate and blocking.
  • What does the recovery plan do when this occurs? At REPEATABLE READ or SERIALIZABLE a transaction may be aborted and must retry, while READ COMMITTED can still observe changing rows across separate statements and a deadlock retry can rerun external side effects.
  • When is Isolation insufficient for Transaction time, and what does adding Locks cost in Retryable SQLSTATE?
Interview tip: Lead with the data path and failure boundary: Transactions choose how concurrent writes and snapshots interact; row/version locks protect invariants, while a deadlock occurs when transactions cyclically wait on each other's owned locks. Then make the answer operational: Use the weakest isolation that preserves the invariant, lock rows in a global order, shorten transactions, add retry policies only for classified serialization/deadlock errors, and test multi-worker races with deterministic keys.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Concurrency Control, Isolation Levels & Deadlock Resolution with data engineer interview Q&A and hands-on practice.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
Table Partitioning (Range, List, Hash) & Sharding Concepts
Next topic
NoSQL Databases Overview (Document: MongoDB, Key-Value: Redis, Wide-Column: Cassandra)