Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Understanding ACID Properties

What are ACID Properties?

ACID is an acronym that stands for Atomicity, Consistency, Isolation, and Durability. These properties are crucial for ensuring reliable processing of database transactions. They guarantee that the database remains in a valid state even in the event of errors, power failures, or other unforeseen circumstances.

Atomicity

Atomicity ensures that a transaction is treated as a single unit, which either completely succeeds or completely fails. If any part of the transaction fails, the entire transaction is rolled back to maintain the database's original state.

Example:

Consider a transaction that transfers money from Account A to Account B:

BEGIN TRANSACTION;
UPDATE Account SET balance = balance - 100 WHERE account_id = 'A';
UPDATE Account SET balance = balance + 100 WHERE account_id = 'B';
COMMIT;

If either update fails, the transaction is aborted, and no changes are made.

Consistency

Consistency ensures that a transaction brings the database from one valid state to another, maintaining all predefined rules, including constraints and triggers. If a transaction violates any of the database's integrity rules, it will be rolled back.

Example:

If a bank requires that account balances cannot go below zero, a transaction that tries to withdraw more money than is available will be rolled back:

BEGIN TRANSACTION;
UPDATE Account SET balance = balance - 500 WHERE account_id = 'A';
IF balance < 0 THEN ROLLBACK;
COMMIT;

Isolation

Isolation ensures that transactions are executed independently of one another. This means that the intermediate state of a transaction is not visible to other transactions until it is completed. This property prevents data inconsistency caused by concurrent transactions.

Example:

When two transactions are occurring at the same time:

Transaction 1: BEGIN TRANSACTION; UPDATE Account SET balance = balance - 100 WHERE account_id = 'A';
Transaction 2: BEGIN TRANSACTION; UPDATE Account SET balance = balance - 50 WHERE account_id = 'A';

Transaction 2 will not see the changes made by Transaction 1 until Transaction 1 is committed.

Durability

Durability guarantees that once a transaction has been committed, it will remain so, even in the event of a system crash or failure. The changes made by a committed transaction are stored permanently in the database.

Example:

After a successful transaction to transfer money:

BEGIN TRANSACTION;
UPDATE Account SET balance = balance - 100 WHERE account_id = 'A';
UPDATE Account SET balance = balance + 100 WHERE account_id = 'B';
COMMIT;

If the system crashes after the commit, the changes will still be present when the system is restored.

Conclusion

Understanding ACID properties is essential for designing robust databases. These properties ensure that database transactions are processed reliably, maintaining data integrity and consistency, even in complex scenarios involving multiple transactions.