Interview kitsBlog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2026 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Databases
Databaseseasy

What is a transaction and why do we use it?

Tags
#transaction#acid#consistency
Back to categoryPractice quiz

Answer

A transaction groups multiple operations into one unit of work: either all succeed or all are rolled back. It protects data consistency, especially with concurrent users and failures.

Advanced answer

Deep dive

A transaction is a boundary around a unit of work. It gives you ACID properties (to varying degrees depending on isolation level):

  • **Atomicity**: all-or-nothing.
  • **Consistency**: constraints/invariants are preserved (assuming correct logic).
  • **Isolation**: concurrent transactions don’t see each other in unsafe ways.
  • **Durability**: once committed, data survives crashes.

Example (why it matters)

Money transfer must not “lose” money: 1) subtract from account A 2) add to account B If step 2 fails, you must roll back step 1.

Practical guidance

  • Keep transactions short (long transactions hold locks or keep old MVCC versions around).
  • Touch rows in a consistent order to reduce deadlocks.
  • Be prepared to retry on transient failures (e.g., serialization conflicts).

Common pitfalls

  • Doing network calls inside a transaction (slow + increases lock time).
  • Assuming transactions automatically fix race conditions without proper isolation/locking.
  • Swallowing errors and committing partial work accidentally.

Related questions

Databases
Autocommit vs explicit transactions: when does it matter?
#database#transactions#autocommit
Databases
Isolation levels: what’s the difference between Read Committed, Repeatable Read, and Serializable?
#database#transactions#isolation
Databases
How can you make a write idempotent at the database level?
#idempotency
#unique-constraint
#upsert
Databases
What are ACID properties?
#transaction#acid#database-theory
MongoDB
Read preference in replica sets: what does `primary` vs `secondary` mean?
#mongo#replica-set#read-preference
MongoDB
Read concern vs write concern: what do they control?
#mongo#consistency#read-concern