Skip to the page

SQL interview question · hard

What are transactions, and what are the ACID properties? Why are they important?

The short answerA transaction in a database is a sequence of operations performed as a single logical unit of work.

The full answer

A transaction in a database is a sequence of operations performed as a single logical unit of work. Transactions ensure data consistency and integrity, particularly in multi-user environments where multiple operations need to execute safely.

ACID Properties
ACID properties define the key characteristics of a reliable database transaction:

PropertyDescriptionWhy It Matters?
AtomicityEnsures that a transaction is all-or-nothing—either all operations succeed or none are applied.Prevents partial updates that could leave data in an inconsistent state.
ConsistencyEnsures that a transaction moves the database from one valid state to another, maintaining all integrity constraints.Prevents corrupt or invalid data from being saved.
IsolationEnsures that concurrent transactions do not interfere with each other. Transactions execute as if they were alone.Prevents race conditions, dirty reads, and inconsistent updates.
DurabilityEnsures that once a transaction is committed, changes are permanently stored, even in case of system failures.Prevents data loss due to crashes or power failures.

Example of a Transaction

BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 500 WHERE account_id = 1;
UPDATE accounts SET balance = balance + 500 WHERE account_id = 2;
COMMIT;

This ensures that either both updates succeed together or neither happens at all.

Nuances to Consider

  • Isolation Levels Affect Performance

    • Higher isolation (e.g., Serializable) prevents anomalies but reduces concurrency.
    • Lower isolation (e.g., Read Committed) improves performance but may allow minor inconsistencies.
  • Durability Depends on Storage Mechanisms

    • ACID compliance depends on proper WAL (Write-Ahead Logging) and disk persistence.
    • Some databases offer eventual consistency instead of strict durability.
  • Distributed Transactions May Weaken ACID Guarantees

    • In distributed databases, achieving strict ACID properties can be costly.
    • Some systems use BASE (Basically Available, Soft state, Eventually consistent) instead.

Why ACID Properties Are Important

  • Prevents data corruption in case of failures.
  • Ensures reliable multi-user operations without interference.
  • Guarantees consistency across database states.

Tecyfy Takeaway
ACID properties form the backbone of data integrity and reliability in transactional databases. Without them, databases could face data loss, inconsistency, or corruption, making them unreliable for financial, medical, or critical systems.

Last updated 1 February 2025