Skip to content
LowLevelDesign Mastery

Relational Databases Deep Dive

The foundation of structured data storage

A relational database organizes data into tables (relations) with rows (tuples) and columns (attributes). Tables are connected through relationships defined by keys.

Relational database basics: a Users table linked to an Orders table through the user_id foreign key relationship

ACID is an acronym for four essential properties that guarantee reliable database transactions:

ACID properties chain: atomicity ensures consistency, which maintains isolation, which guarantees durability

Atomicity means a transaction is all-or-nothing. Either all operations succeed, or all fail (rollback).

Atomicity in a bank transfer: both debit and credit commit together, or any failure rolls back both accounts unchanged

Example: Transferring money between accounts. If deducting from Account A succeeds but adding to Account B fails, both operations are rolled back. You can’t have money disappear!


Consistency ensures the database always remains in a valid state. All constraints, rules, and relationships are maintained.

Consistency across a transaction: constraints hold before, temporary internal inconsistency during, valid state after commit

Example: If a table has a constraint “age must be > 0”, the database will reject any transaction that tries to insert a negative age, keeping the database consistent.


Isolation ensures concurrent transactions don’t interfere with each other. Each transaction sees a consistent snapshot of the data.

Isolation between two concurrent transactions writing x: each sees a consistent snapshot, with no dirty reads or lost updates

Example: Two users updating the same account balance simultaneously. Isolation ensures each transaction sees a consistent view and updates don’t interfere.


Durability guarantees that once a transaction is committed, it persists even if the system crashes.

Durability: committed data written to disk and the transaction log survives a system crash and is restored on recovery

Example: After you commit a bank transfer, even if the server crashes immediately after, your transaction is safely stored and will be restored when the system recovers.


Understanding how major companies use relational databases helps illustrate ACID properties in practice:

ACID in Financial Systems: Banking Transactions

Section titled “ACID in Financial Systems: Banking Transactions”

The Challenge: Banks process millions of transactions daily. A single transaction failure could mean lost money or incorrect balances.

The Solution: Banks use ACID transactions extensively:

  • Atomicity: Transfer $100 from Account A to Account B. Either both succeed (debit A, credit B) or both fail. No partial transfers.
  • Consistency: Account balances always sum correctly. No negative balances allowed (constraints).
  • Isolation: Two transfers happening simultaneously don’t interfere. Each sees consistent state.
  • Durability: Once committed, transaction persists even if database crashes.

Example: User transfers $100:

  1. Begin transaction
  2. Debit Account A: $1000 → $900
  3. Credit Account B: $500 → $600
  4. Commit transaction

If database crashes between steps 3 and 4, transaction rolls back. Account A stays at $1000, Account B stays at $500. No money lost.

Impact: Zero data loss incidents. All transactions are atomic and durable. Critical for financial systems.

The Challenge: E-commerce platforms store product data. Without normalization, product information is duplicated across orders, causing inconsistencies.

The Solution: Amazon uses normalized database design:

  • Products table: Product ID, name, price, description (single source of truth)
  • Orders table: Order ID, user ID, product ID (references product)
  • Order Items table: Order ID, product ID, quantity (normalized)

Why Normalize? If product price changes:

  • Without normalization: Must update price in millions of order records (inconsistent, slow)
  • With normalization: Update price once in Products table, all orders reference it (consistent, fast)

Example: Product “iPhone 15” price changes from $999 to $899:

  • Normalized: Update 1 row in Products table
  • Denormalized: Update millions of rows in Orders table

Impact: Price updates instant and consistent. Reduced storage by 70% (no duplication). Faster queries.

The Challenge: Social media platforms need to find users quickly. Searching 2 billion users without indexes would be extremely slow.

The Solution: Facebook uses extensive indexing:

  • Primary index: User ID (fast user lookups)
  • Unique index: Email (fast login lookups)
  • Composite index: (name, location) for search
  • Full-text index: User posts for search

Example: User searches for “John Smith in New York”:

  • Without index: Scan 2 billion users (hours)
  • With composite index: Direct lookup (milliseconds)

Impact: User search: 50ms (with index) vs 10+ seconds (without index). 200x faster. Handles billions of users efficiently.

The Challenge: GitHub stores millions of repositories, users, and code. Needs strong consistency, complex queries, and reliability.

The Solution: GitHub uses PostgreSQL:

  • ACID transactions: Ensures repository operations are atomic
  • Complex queries: JOINs across repositories, users, issues, pull requests
  • Reliability: Zero data loss, handles millions of operations daily

Why PostgreSQL?

  • Strong ACID guarantees (critical for code repositories)
  • Complex queries (find repositories by language, stars, forks)
  • Mature and reliable (handles GitHub’s scale)

Impact: Handles 100+ million repositories. Zero data loss incidents. Complex queries execute in milliseconds.

The Challenge: Facebook started with MySQL for user data, posts, and relationships. Needed to scale to billions of users.

The Solution: Facebook used MySQL extensively:

  • User data: Profiles, friends, posts
  • ACID transactions: Ensures data consistency
  • Replication: Read replicas for scaling reads

Why MySQL?

  • Mature and reliable
  • Good performance for structured data
  • Strong ACID guarantees

Impact: Handled billions of users. Strong consistency for user data. Foundation for Facebook’s growth.



Normalization is the process of organizing data to reduce redundancy and eliminate anomalies. It involves splitting tables and creating relationships.

Normalization: a redundant unnormalized Orders table is split into separate Customers and Orders tables linked by customer_id

Normalization follows normal forms (NF), each eliminating specific types of redundancy:

Normal forms progression from 1NF to 2NF to 3NF to BCNF, eliminating partial and then transitive dependencies

Before Normalization (Problems):

order_idcustomer_namecustomer_emailproduct_namepricequantity
1Alice[email protected]Laptop10001
2Alice[email protected]Mouse202
3Bob[email protected]Keyboard501

Problems:

  • Customer info repeated (redundancy)
  • If Alice changes email, need to update multiple rows
  • If we delete order #2, we lose Alice’s info

After Normalization (3NF):

Customers Table:

customer_idnameemail
1Alice[email protected]
2Bob[email protected]

Orders Table:

order_idcustomer_idproduct_namepricequantity
11Laptop10001
21Mouse202
32Keyboard501

Benefits:

  • Customer info stored once
  • Update email in one place
  • Delete order without losing customer info

An index is a data structure that speeds up data retrieval. It’s like a book’s index—instead of scanning every page, you look up the index to find the exact location.

Email lookup without an index uses an O(n) full table scan at 1000ms versus an O(log n) index lookup at 5ms

Analogy: Finding a word in a dictionary:

  • Without index: Read every page (full table scan)
  • With index: Use alphabetical index to jump directly (index lookup)

Four index types compared: primary key index, unique index, composite multi-column index and full-text search index
AspectBenefitCost
Read SpeedMuch faster queriesSlower writes (must update index)
Storage-Extra storage space
Maintenance-Index must be maintained

How relational database concepts affect your class design:

💡 Tip: Click dropdown to switch between languages
Entity Class Mapping to Table
from dataclasses import dataclass
from typing import Optional
from datetime import datetime
@dataclass
class User:
"""Maps to 'users' table"""
id: Optional[int] = None # Primary key
name: str
email: str # Has unique index
created_at: datetime = datetime.now()
@dataclass
class Order:
"""Maps to 'orders' table"""
id: Optional[int] = None # Primary key
user_id: int # Foreign key -> users.id
total: float
created_at: datetime = datetime.now()
# Relationship (not in DB, but in code)
user: Optional[User] = None
💡 Tip: Click dropdown to switch between languages
Repository Pattern
from abc import ABC, abstractmethod
from typing import List, Optional
class UserRepository(ABC):
@abstractmethod
def find_by_id(self, user_id: int) -> Optional[User]:
"""Find user by primary key"""
pass
@abstractmethod
def find_by_email(self, email: str) -> Optional[User]:
"""Find user by email (uses index)"""
pass
@abstractmethod
def save(self, user: User) -> User:
"""Save user (handles insert/update)"""
pass
class SQLUserRepository(UserRepository):
def __init__(self, db_connection):
self.db = db_connection
def find_by_email(self, email: str) -> Optional[User]:
# Uses email index for fast lookup
query = "SELECT * FROM users WHERE email = %s"
# Execute query...
💡 Tip: Click dropdown to switch between languages
ACID Transaction
class AccountService:
def __init__(self, db_connection):
self.db = db_connection
def transfer(self, from_account_id: int, to_account_id: int, amount: float):
# Atomicity: All or nothing
with self.db.transaction(): # BEGIN TRANSACTION
try:
# Deduct from source
self.db.execute(
"UPDATE accounts SET balance = balance - %s WHERE id = %s",
(amount, from_account_id)
)
# Add to destination
self.db.execute(
"UPDATE accounts SET balance = balance + %s WHERE id = %s",
(amount, to_account_id)
)
# Consistency: Check constraints
if amount < 0:
raise ValueError("Amount must be positive")
# Commit (Durability: persisted)
self.db.commit() # COMMIT
except Exception:
# Rollback on any error (Atomicity)
self.db.rollback() # ROLLBACK
raise


Now that you understand relational databases, let’s explore how isolation levels control what concurrent transactions can see:

Next up: Database Isolation Levels — Learn how read committed, repeatable read, and serializable isolation levels prevent race conditions.