08. Databases
Where this fits: You know how apps talk to servers and how load balancers spread traffic. Now we ask: where does the data actually live, and how do we pick the right storage?
Learning goals
By the end of this lesson, you should be able to:
- Explain what a database is and why it is the durable source of truth in most systems
- Compare SQL (relational) vs NoSQL families and pick one for a simple scenario
- Describe indexes using the book-index analogy and explain their trade-offs
- Summarize ACID transactions in plain English with a money-transfer example
- Contrast normalization vs denormalization and know when each makes sense
- Sketch example schemas for common apps (URL shortener, chat, e-commerce)
- Introduce polyglot persistence β using multiple databases in one product
- Avoid beginner mistakes like "NoSQL because it sounds modern"
The big picture: why databases matter
Imagine you run a small online bookstore. Users sign up, browse books, place orders, and pay. Where does all that information go?
- RAM disappears when the server restarts.
- Log files are awkward to query ("show me all orders from user 42 in March").
- Spreadsheets break at scale and lack concurrent access controls.
A database is software built to store, organize, query, and protect data reliably β often for years.
Everyday analogy β the filing cabinet vs sticky notes:
- Cache = sticky notes on your desk (fast, but can fall off or get outdated).
- Queue = an inbox tray for tasks ("mail this package later").
- Database = the locked filing cabinet in the back office. If it's not in the cabinet, it didn't officially happen.
In system design interviews and real products, the database is usually the source of truth. Everything else (cache, search index, analytics warehouse) is derived from or synchronized with it.
SQL (relational databases)
Examples: PostgreSQL, MySQL, SQLite, Amazon RDS (Postgres/MySQL).
SQL databases store data in tables β rows and columns, like a spreadsheet with strict rules.
users
ββββββ¬βββββββββββββββββββ¬ββββββββββββββββββββββ
β id β email β created_at β
ββββββΌβββββββββββββββββββΌββββββββββββββββββββββ€
β 1 β alice@example.comβ 2026-01-15 10:00:00 β
β 2 β bob@example.com β 2026-01-16 14:30:00 β
ββββββ΄βββββββββββββββββββ΄ββββββββββββββββββββββ
orders
ββββββ¬ββββββββββ¬βββββββββ¬ββββββββββββ
β id β user_id β total β status β
ββββββΌββββββββββΌβββββββββΌββββββββββββ€
β 10 β 1 β 29.99 β shipped β
β 11 β 2 β 14.50 β pending β
ββββββ΄ββββββββββ΄βββββββββ΄ββββββββββββ
What SQL is good at
| Strength | Plain English | Example |
|---|---|---|
| Structured relationships | "This order belongs to this user" | JOIN users ON orders.user_id = users.id |
| Complex queries | Filter, sort, aggregate across tables | "Top 10 customers by spend this month" |
| Transactions (ACID) | Multi-step updates that must succeed or fail together | Transfer money between accounts |
| Constraints | Enforce rules at storage level | email must be unique; total cannot be negative |
Everyday analogy β a library catalog system:
Books, authors, and borrowers are separate tables linked by IDs. The librarian can ask precise questions: "Which books is Alice currently holding?" SQL excels when your data has clear structure and relationships.
When to reach for SQL (beginner heuristic)
- User accounts, profiles, permissions
- Orders, payments, inventory
- Anything where correctness matters more than extreme write throughput
- You need joins across entities regularly
NoSQL β an umbrella, not one thing
"NoSQL" does not mean "no SQL ever" or "always better." It means not only relational tables β optimized for different shapes and access patterns.
Think of NoSQL like different types of storage furniture:
| Furniture | NoSQL family | What it holds | Real products |
|---|---|---|---|
| Filing folders with flexible papers | Document | JSON-like documents | MongoDB, Firestore |
| Labeled cubbyholes | Key-value | key β value | Redis, DynamoDB, Memcached |
| Wide spreadsheets with sparse columns | Wide-column | Rows + flexible column families | Cassandra, HBase |
| Sticky-note relationship maps | Graph | Nodes + edges | Neo4j, Neptune |
Document stores
{
"_id": "user_42",
"name": "Alice",
"email": "alice@example.com",
"preferences": {
"theme": "dark",
"notifications": true
},
"addresses": [
{ "type": "home", "city": "Boston" }
]
}
Good for: product catalogs, user profiles, content with varying fields, mobile apps with flexible schemas.
Watch out: Complex multi-document transactions are harder than in SQL (though improving).
Key-value stores
session:abc123 β { "user_id": 42, "expires": "..." }
url:aZ9kQ2 β "https://example.com/long-page"
cart:user_42 β { "items": [...] }
Good for: sessions, caching, simple lookups, high-throughput counters, feature flags.
Watch out: No rich queries β you must know the key.
Wide-column stores
Designed for massive scale and high write throughput. Data is partitioned by key; columns can vary per row.
Good for: time-series events, IoT, analytics ingestion, messaging at huge volume.
Watch out: Query patterns must be designed upfront β "query-driven design."
Graph databases
(Alice) --FRIENDS_WITH--> (Bob)
(Alice) --PURCHASED--> (Book: "Design Patterns")
Good for: social graphs, fraud detection, recommendation "friends of friends," org hierarchies.
Watch out: Overkill for simple CRUD apps.
SQL vs NoSQL β decision table (simplified)
| Question | Lean SQL | Lean NoSQL |
|---|---|---|
| Do I need multi-row transactions (payments)? | β | β οΈ Often harder |
| Is my schema stable and relational? | β | Maybe document |
| Do I only ever fetch by one key? | Maybe still SQL | β Key-value / document |
| Do I need billions of writes/day on simple events? | β οΈ Expensive | β Wide-column / Kafka + store |
| Is my access pattern "friends of friends"? | β οΈ Painful joins | β Graph |
The real rule (not dogma)
Choose the database that fits your access patterns and consistency needs β not the logo on a blog post.
If you always fetch user_id β profile, a document or key-value store is natural.
If you need to debit one account and credit another atomically, SQL transactions shine.
Indexes β the book index analogy
Without an index, finding a row can mean scanning every row in the table β like reading an entire textbook cover to cover to find one definition.
An index is a separate sorted structure pointing to rows β like the index at the back of a book.
Table: users (1 million rows)
Query: SELECT * FROM users WHERE email = 'alice@example.com'
Without index on email:
β Scan all 1,000,000 rows (slow)
With index on email:
β Jump to 'alice@example.com' in index (fast)
β Fetch that one row
Index trade-offs
| Benefit | Cost |
|---|---|
| Faster reads / lookups | Slower writes (index must update too) |
| Faster sorting/filtering on indexed columns | Extra disk space |
Enables unique constraints (UNIQUE INDEX on email) | Too many indexes confuse query planner |
Everyday analogy: A book index helps you find topics quickly, but someone had to create and maintain that index when the book was printed. Every new edition (write) takes more work if the index is huge.
Common indexes beginners should know
-- Primary key: unique identity for each row (automatic index)
PRIMARY KEY (id)
-- Unique index: no duplicates allowed
CREATE UNIQUE INDEX idx_users_email ON users(email);
-- Composite index: multiple columns together
CREATE INDEX idx_messages_conv_time ON messages(conversation_id, created_at);
Composite index tip: Order matters. An index on (conversation_id, created_at) helps:
WHERE conversation_id = 5 ORDER BY created_at
It may not help much for:
WHERE created_at > '2026-01-01' -- conversation_id not in filter
Primary keys and unique IDs
Every row needs a unique identity.
| Strategy | Example | Pros | Cons |
|---|---|---|---|
| Auto-increment integer | 1, 2, 3, β¦ | Simple, compact, sortable | Hard in distributed systems; reveals volume |
| UUID | 550e8400-e29b-41d4-a716-446655440000 | Generate anywhere, no coordination | Longer, random β index fragmentation |
| Snowflake / ULID | Time-ordered 64-bit IDs | Sortable by time, distributed-friendly | Requires ID service or library |
For a single Postgres database MVP, auto-increment or UUID both work. At scale (many services writing IDs), prefer time-ordered distributed IDs β see case study 05 in this series.
Transactions and ACID β explained like a bank teller
A transaction groups multiple changes into one unit: all succeed, or all fail.
Scenario: Transfer $10 from Account A to Account B.
BEGIN;
UPDATE accounts SET balance = balance - 10 WHERE id = 'A';
UPDATE accounts SET balance = balance + 10 WHERE id = 'B';
COMMIT;
If the server crashes after debiting A but before crediting B, you have a disaster. Transactions prevent that.
ACID in plain English
| Letter | Name | Everyday analogy |
|---|---|---|
| A | Atomic | Paying for groceries: either the whole checkout completes, or nothing is charged β no "paid but didn't get items" |
| C | Consistent | Rules always hold: balance never goes negative if you forbid it |
| I | Isolated | Two tellers serving two customers don't accidentally mix up whose money they're moving |
| D | Durable | Once the receipt prints (commit), the record survives even if the power goes out |
When you need transactions: payments, inventory reservation, seat booking, any multi-step update where partial completion is unacceptable.
Go deeper: 18. Transactions & Isolation β isolation levels, lost updates, pessimistic vs optimistic locking, deadlocks, and sagas.
When you might relax: like counts, view counters, analytics β eventual accuracy is OK (more in lesson 11).
Normalization vs denormalization
Normalization β avoid duplication
Split data into related tables so each fact lives in one place.
Normalized:
users(id, name)
orders(id, user_id, total)
order_items(id, order_id, product_id, qty)
To show an order page β JOIN tables
Pros: Less storage waste, updates in one place, fewer inconsistencies.
Cons: Reads can require multiple joins (slower at huge scale).
Everyday analogy: Normalized = everyone's phone number stored in one address book, referenced by name ID β not copied on 50 sticky notes.
Denormalization β duplicate for speed
Store redundant copies so reads are simple and fast.
Denormalized feed table:
feed_items(user_id, post_id, author_name, post_text, created_at)
-- author_name duplicated from users table
Pros: One query returns the feed β no joins at read time.
Cons: When Alice renames herself, you must update many feed rows (or accept stale names briefly).
Everyday analogy: Denormalized = printing pre-made handouts for every student instead of making them look up the textbook each time. Faster to hand out, harder to update when the textbook changes.
| Pattern | Normalize when⦠| Denormalize when⦠|
|---|---|---|
| User profile | Single source of truth matters | Rarely β profiles change |
| News feed / timeline | Never for read path at scale | Precompute fanout rows |
| Product catalog | Inventory counts in one table | Display snapshots in search index |
| Leaderboard | Raw events normalized | Materialized top-N table |
Most production systems do both: normalized core in SQL + denormalized read models for hot paths.
Example schemas (starter patterns)
URL shortener
urls(
short_code VARCHAR(8) PRIMARY KEY,
long_url TEXT NOT NULL,
user_id BIGINT NULL,
created_at TIMESTAMP NOT NULL,
expires_at TIMESTAMP NULL,
click_count BIGINT DEFAULT 0
)
-- Index for user's list of links
CREATE INDEX idx_urls_user ON urls(user_id, created_at DESC);
Access patterns:
- Redirect:
SELECT long_url FROM urls WHERE short_code = ?β single-row lookup - Analytics: increment
click_count(consider async aggregation at scale)
Chat / messaging
conversations(
id BIGINT PRIMARY KEY,
created_at TIMESTAMP
)
conversation_members(
conversation_id BIGINT,
user_id BIGINT,
PRIMARY KEY (conversation_id, user_id)
)
messages(
message_id BIGINT PRIMARY KEY,
conversation_id BIGINT NOT NULL,
sender_id BIGINT NOT NULL,
body TEXT,
created_at TIMESTAMP NOT NULL
)
CREATE INDEX idx_messages_conv_time
ON messages(conversation_id, created_at DESC);
Why the composite index? Loading a chat thread = "all messages in this conversation, newest last" β one indexed range scan.
E-commerce (simplified)
products(id, name, price_cents, inventory_count)
carts(id, user_id)
cart_items(cart_id, product_id, quantity)
orders(id, user_id, status, total_cents, created_at)
order_items(order_id, product_id, quantity, price_cents)
Checkout transaction (conceptual):
- Verify inventory
- Decrement inventory
- Create order + order_items
- Charge payment (often external API)
- Commit or rollback
Polyglot persistence β one size does not fit all
Large products rarely use one database for everything. Polyglot persistence = right tool for each job.
| Data type | Typical store | Why |
|---|---|---|
| User accounts & billing | PostgreSQL | ACID, relationships |
| Session tokens | Redis | Fast TTL key-value |
| Product search | Elasticsearch | Full-text, facets |
| Media files | S3 / object storage | Cheap blobs, CDN-friendly |
| Clickstream events | Kafka β column store | Write-heavy analytics |
Beginner advice: Start with one primary SQL database for your MVP. Add specialized stores when you hit a concrete pain point (search is slow β add Elasticsearch; sessions hammer DB β add Redis). Don't pre-optimize with five databases on day one.
How databases fit in a typical HLD
Client β Load Balancer β App Servers β Redis (cache) β PostgreSQL (source of truth)
β Queue β Workers β DB / email / etc.
When you draw this in an interview, label:
- What is stored where
- Read path vs write path
- Whether you need strong consistency or can tolerate lag
Common mistakes (and how to avoid them)
| Mistake | Why it's wrong | Better approach |
|---|---|---|
| "We'll use MongoDB because SQL doesn't scale" | Oversimplified; Postgres scales far | Pick based on access pattern + ops skills |
| No indexes on hot query columns | Full table scans kill latency | Index columns in WHERE, JOIN, ORDER BY |
| Storing images inside the database | Blobs bloat backups and slow queries | Object storage (S3) + URL in DB |
| Denormalizing everything on day one | Update nightmares | Normalize first; denormalize measured hot paths |
| Using the cache as source of truth | Data loss on eviction/crash | DB commits first; cache is derived |
| UUIDs everywhere without reason | Larger indexes, random I/O | Auto-increment fine for single-DB MVP |
| Ignoring connection limits | App opens 10k DB connections | Pool connections (PgBouncer, ORM pool) |
Interview phrases that sound solid
- "Postgres is our source of truth for users and orders; Redis caches hot profile reads with a 5-minute TTL."
- "Checkout uses a transaction to decrement inventory and create the order atomically."
- "The feed is denormalized into a per-user timeline table for O(1) reads; we fan out on write."
- "We index
(conversation_id, created_at)because every thread load hits that pattern."
Check your understanding
Questions
- What is the difference between a cache and a database in terms of durability?
- When would you prefer SQL over a document database?
- Explain indexes using the book index analogy. What do they cost?
- What does "atomic" mean in a money transfer transaction?
- Why might a social media feed be denormalized?
- Name two NoSQL families and one use case for each.
- What is polyglot persistence, and when should a beginner avoid it?
- Why is a composite index on
(conversation_id, created_at)useful for chat apps?
Answers
Click to reveal answers
-
Cache is a fast, optional layer β data can disappear (eviction, restart). Database is durable: committed data survives crashes and is the authoritative record.
-
Prefer SQL when you need complex relationships, joins, and multi-row ACID transactions β e.g., payments, inventory, order processing.
-
An index is like a book index: jump directly to rows matching a column instead of reading the whole table. Cost: slower writes, extra disk, and maintenance overhead.
-
Atomic means all steps (debit + credit) complete together, or none do β no partial transfer leaving accounts inconsistent.
-
Feeds are read far more often than profile names change. Denormalizing stores pre-built timeline rows so reads are one simple query, not expensive joins at scale.
-
Examples: Key-value β sessions in Redis; Document β flexible product JSON in MongoDB; Wide-column β event ingestion; Graph β social friend networks.
-
Polyglot persistence = using multiple specialized databases in one system. Beginners should avoid it until one database clearly fails for a specific job β otherwise operational complexity explodes.
-
Chat UIs load messages by conversation, sorted by time. That composite index matches the exact filter + sort pattern, avoiding full scans.
Quick reference card
SQL β tables, joins, ACID, structured relationships
NoSQL β document / key-value / wide-column / graph β match access pattern
Index β faster reads, slower writes (book index analogy)
ACID β all-or-nothing, rules hold, isolated, survives crash
Normalize β less duplication, more joins
Denormalize β duplicate for fast reads (feeds, leaderboards)
Polyglot β many stores at scale; one SQL DB for MVP
Next: 09. Replication & Sharding β what happens when one database machine is no longer enough.