<< back to Guides

๐Ÿ“š Guide: Database Transactions, Concurrency, Locking, and Normalization

This guide covers foundational concepts that ensure data integrity, consistency, and performance in transactional systems โ€” including ACID properties, concurrency control, locking, and data modeling trade-offs between normalization and denormalization.


โœ… 1. Database Transactions & ACID

A transaction is a unit of work that must be atomic, consistent, isolated, and durable.

๐Ÿงช ACID Properties

Property Description
Atomicity All steps succeed or none do
Consistency Must leave DB in valid state (constraints respected)
Isolation Transactions don't interfere with each other
Durability Once committed, it remains โ€” even after crash

Example: Atomic transfer between accounts

BEGIN;

UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;

COMMIT;

๐Ÿ”€ 2. Concurrency & Isolation Levels

Concurrency allows multiple transactions to run in parallel. To prevent race conditions and anomalies, databases use isolation levels:

Isolation Level Prevents... Notes
Read Uncommitted โŒ Nothing Fast, unsafe
Read Committed โœ… Dirty Reads Default in many DBs
Repeatable Read โœ… Non-repeatable Reads Good for reads
Serializable โœ… Phantom Reads, full isolation Strict, slowest

Common concurrency issues:

Issue Description
Dirty read Read uncommitted data
Non-repeatable read Same query returns different results mid-tx
Phantom read New rows appear that match query mid-tx
Lost update Overwrite data written by another tx

๐Ÿ”’ 3. Locking Strategies

Locks control access to rows, tables, or ranges.

๐Ÿ”ต Pessimistic Locking

-- Example: SELECT ... FOR UPDATE (pessimistic)
BEGIN;
SELECT * FROM accounts WHERE id = 1 FOR UPDATE;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
COMMIT;

๐ŸŸข Optimistic Locking

-- Example: optimistic version check
UPDATE products
SET stock = stock - 1, version = version + 1
WHERE id = 123 AND version = 4;

If 0 rows affected โ†’ retry, conflict detected.


๐Ÿ” 4. Normalization vs Denormalization

These are two competing strategies for modeling data in relational databases.

๐Ÿงช Normalization

Example:

Table: Orders Table: Customers
id, customer_id, ... id, name, email, ...

Pros

โœ… Smaller storage
โœ… Fewer anomalies
โœ… Easier to update single source of truth

Cons

โŒ More joins = slower queries
โŒ Complex schema evolution
โŒ Tougher for analytics


๐Ÿ“ฆ Denormalization

Example:

Table: Orders
id, customer_name, customer_email, ...

Pros

โœ… Fewer joins
โœ… Faster reads
โœ… Simple queries for dashboards

Cons

โŒ Data duplication
โŒ Risk of inconsistencies
โŒ Update anomalies (requires syncing)


๐Ÿ†š Comparison Table

Feature Normalized Denormalized
Write Performance Better Worse (updates touch more)
Read Performance Worse (joins) Better (fewer joins)
Storage Usage Efficient Redundant
Data Consistency Strong Weaker
Analytics Use Harder Easier

๐Ÿง  Bonus: When to Choose What?

Use Case Recommendation
OLTP (banking, inventory) โœ… Normalize
OLAP (dashboards, BI) โœ… Denormalize
Mixed workloads ๐Ÿค Hybrid (views, materialized tables)

๐Ÿ“š Further Reading


<< back to Guides