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

StrengthPlain EnglishExample
Structured relationships"This order belongs to this user"JOIN users ON orders.user_id = users.id
Complex queriesFilter, sort, aggregate across tables"Top 10 customers by spend this month"
Transactions (ACID)Multi-step updates that must succeed or fail togetherTransfer money between accounts
ConstraintsEnforce rules at storage levelemail 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:

FurnitureNoSQL familyWhat it holdsReal products
Filing folders with flexible papersDocumentJSON-like documentsMongoDB, Firestore
Labeled cubbyholesKey-valuekey β†’ valueRedis, DynamoDB, Memcached
Wide spreadsheets with sparse columnsWide-columnRows + flexible column familiesCassandra, HBase
Sticky-note relationship mapsGraphNodes + edgesNeo4j, 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)

QuestionLean SQLLean 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

BenefitCost
Faster reads / lookupsSlower writes (index must update too)
Faster sorting/filtering on indexed columnsExtra 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.

StrategyExampleProsCons
Auto-increment integer1, 2, 3, …Simple, compact, sortableHard in distributed systems; reveals volume
UUID550e8400-e29b-41d4-a716-446655440000Generate anywhere, no coordinationLonger, random β†’ index fragmentation
Snowflake / ULIDTime-ordered 64-bit IDsSortable by time, distributed-friendlyRequires 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

LetterNameEveryday analogy
AAtomicPaying for groceries: either the whole checkout completes, or nothing is charged β€” no "paid but didn't get items"
CConsistentRules always hold: balance never goes negative if you forbid it
IIsolatedTwo tellers serving two customers don't accidentally mix up whose money they're moving
DDurableOnce 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.

PatternNormalize when…Denormalize when…
User profileSingle source of truth mattersRarely β€” profiles change
News feed / timelineNever for read path at scalePrecompute fanout rows
Product catalogInventory counts in one tableDisplay snapshots in search index
LeaderboardRaw events normalizedMaterialized 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):

  1. Verify inventory
  2. Decrement inventory
  3. Create order + order_items
  4. Charge payment (often external API)
  5. 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 typeTypical storeWhy
User accounts & billingPostgreSQLACID, relationships
Session tokensRedisFast TTL key-value
Product searchElasticsearchFull-text, facets
Media filesS3 / object storageCheap blobs, CDN-friendly
Clickstream eventsKafka β†’ column storeWrite-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)

MistakeWhy it's wrongBetter approach
"We'll use MongoDB because SQL doesn't scale"Oversimplified; Postgres scales farPick based on access pattern + ops skills
No indexes on hot query columnsFull table scans kill latencyIndex columns in WHERE, JOIN, ORDER BY
Storing images inside the databaseBlobs bloat backups and slow queriesObject storage (S3) + URL in DB
Denormalizing everything on day oneUpdate nightmaresNormalize first; denormalize measured hot paths
Using the cache as source of truthData loss on eviction/crashDB commits first; cache is derived
UUIDs everywhere without reasonLarger indexes, random I/OAuto-increment fine for single-DB MVP
Ignoring connection limitsApp opens 10k DB connectionsPool 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

  1. What is the difference between a cache and a database in terms of durability?
  2. When would you prefer SQL over a document database?
  3. Explain indexes using the book index analogy. What do they cost?
  4. What does "atomic" mean in a money transfer transaction?
  5. Why might a social media feed be denormalized?
  6. Name two NoSQL families and one use case for each.
  7. What is polyglot persistence, and when should a beginner avoid it?
  8. Why is a composite index on (conversation_id, created_at) useful for chat apps?

Answers

Click to reveal answers
  1. Cache is a fast, optional layer β€” data can disappear (eviction, restart). Database is durable: committed data survives crashes and is the authoritative record.

  2. Prefer SQL when you need complex relationships, joins, and multi-row ACID transactions β€” e.g., payments, inventory, order processing.

  3. 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.

  4. Atomic means all steps (debit + credit) complete together, or none do β€” no partial transfer leaving accounts inconsistent.

  5. 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.

  6. Examples: Key-value β€” sessions in Redis; Document β€” flexible product JSON in MongoDB; Wide-column β€” event ingestion; Graph β€” social friend networks.

  7. 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.

  8. 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.