LearnFrenzy
Sign in Take a free test

Database Management interview · Set 3 of 9

Transactions, ACID and Isolation Levels

  • 5 follow-up questions
  • 4 key points
  • Code examples
Test yourself

Correctness under concurrency

A single user never sees these problems; a thousand users always do. Explain the anomaly, the isolation level that prevents it, and the cost of choosing that level.

  • ACIDWhat each property protects.
  • IsolationAnomalies, levels and MVCC.
  • LockingOptimistic vs pessimistic and deadlocks.

Transactions are where database theory meets real money. Interviewers use the bank-transfer example to check whether you understand what can go wrong when many users work at once, and how isolation levels trade safety for speed.

1. Explain ACID with a bank transfer.

Moving ₹500 from A to B is two updates. Atomicity: both happen or neither does. Consistency: rules such as “balance ≥ 0” still hold afterwards. Isolation: another transaction never sees money that has left A but not reached B. Durability: once committed, the transfer survives a crash (thanks to the write-ahead log).

START TRANSACTION;
UPDATE accounts SET balance = balance - 500 WHERE id = 'A' AND balance >= 500;
UPDATE accounts SET balance = balance + 500 WHERE id = 'B';
COMMIT;   -- or ROLLBACK if the first update affected 0 rows

2. What are dirty reads, non-repeatable reads and phantom reads?

  • Dirty read: reading another transaction's uncommitted change, which may later be rolled back.
  • Non-repeatable read: reading the same row twice and getting different values because someone committed an update in between.
  • Phantom read: running the same range query twice and getting new rows because someone inserted them.

3. What are the four SQL isolation levels?

Read Uncommitted allows dirty reads. Read Committed prevents them (PostgreSQL and Oracle default). Repeatable Read also prevents non-repeatable reads (MySQL InnoDB default; InnoDB also blocks most phantoms with gap locks). Serializable behaves as if transactions ran one after another. Higher levels are safer but allow less concurrency.

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
START TRANSACTION;
-- ...
COMMIT;

4. What is MVCC?

Multi-Version Concurrency Control keeps several versions of a row so readers see a consistent snapshot without blocking writers, and writers do not block readers. PostgreSQL and MySQL InnoDB both use it. The cost is cleanup of old versions (VACUUM in PostgreSQL, purge in InnoDB).


5. What is the difference between optimistic and pessimistic locking?

Pessimistic: lock the row when you read it (SELECT … FOR UPDATE) so nobody else can change it — good when conflicts are frequent. Optimistic: read freely, and on update check a version number; if someone else changed it, retry — good when conflicts are rare, such as editing a profile.

-- Optimistic: succeeds only if nobody updated the row since we read version 7
UPDATE products
SET stock = stock - 1, version = version + 1
WHERE id = 42 AND version = 7;

6. How do database deadlocks happen and how are they handled?

Transaction 1 locks row A and waits for B; transaction 2 locks B and waits for A. The database detects the cycle and rolls one back with a deadlock error. Prevent them by touching rows in a consistent order, keeping transactions short, and retrying the victim in application code.


7. What is a write-ahead log (WAL)?

Before changing data pages, the database writes the change to an append-only log on disk. After a crash it replays committed changes and undoes uncommitted ones. That is how durability is achieved without flushing every data page on every commit. Replication also reads this log.


8. How do you prevent two users from booking the last seat at the same time?

Make the check and the update one atomic statement, or lock the row. A unique constraint on (show_id, seat_no) is the final safety net — the second insert fails and the app tells the user the seat is gone.

UPDATE shows SET seats_left = seats_left - 1
WHERE id = 9 AND seats_left > 0;   -- 0 rows affected → sold out

Remember

Key points

  • Atomic check-and-update

    Put the condition in the UPDATE's WHERE clause.

  • Know the defaults

    Read Committed (PostgreSQL), Repeatable Read (MySQL).

  • MVCC = snapshots

    Readers and writers do not block each other.

  • Retry deadlock victims

    Deadlocks are normal; handle them in code.

Interview drill

Follow-up questions

What the interviewer asks after your first answer. Turn on practice mode, answer each one aloud, then reveal.

  1. 1 What is a savepoint? Basic

    A marker inside a transaction you can roll back to without abandoning the whole transaction.

  2. 2 Why not use SERIALIZABLE for everything? Intermediate

    It reduces concurrency and causes more serialisation failures that must be retried, which hurts throughput under load.

  3. 3 What is a two-phase commit? Advanced

    A protocol to commit across several databases: a coordinator asks everyone to prepare, then tells all to commit. It is slow and blocks on failures, so microservices often prefer sagas with compensating actions.

  4. 4 What is the lost update problem? Intermediate

    Two transactions read the same value, both modify it and write back; the second write overwrites the first. Fix with atomic updates, row locks or optimistic versioning.

  5. 5 Does autocommit mean every statement is a transaction? Basic

    Yes — each statement commits on its own. Turn it off (or use START TRANSACTION) when several statements must succeed or fail together.

Preparing with friends?

Share this page with your study group or placement batch.

Search LearnFrenzy

Esc

Quick links