Choosing and Modelling a Database
"Which database?" is asked in every design interview. A senior answer starts from the access patterns, defaults to relational unless a requirement pushes elsewhere, and knows how indexes and storage engines shape performance.
Learning objectives
- Compare relational, key-value, wide-column, document, graph and search stores.
- Choose a database from access patterns and consistency needs, not fashion.
- Design indexes and explain B-tree vs LSM-tree trade-offs.
- Model data for the queries a design actually runs.
The families
| Family | Examples | Strengths | Weaknesses | Typical use |
|---|---|---|---|---|
| Relational | PostgreSQL, MySQL | Transactions, joins, constraints, mature tooling | Write scaling needs sharding | Users, orders, payments, most business data |
| Key-value | Redis, DynamoDB | Very fast lookups by key, simple scaling | Queries only by key (plus limited secondary indexes) | Sessions, carts, counters, caches |
| Wide-column | Cassandra, HBase, Bigtable | Massive write throughput, partitioned by key, time-ordered rows | No joins; query patterns fixed up front | Messages, events, time series |
| Document | MongoDB | Flexible, nested records | Cross-document transactions are costlier | Catalogues, content, user-generated data |
| Graph | Neo4j | Multi-hop relationship queries | Harder to scale horizontally | Social graphs, fraud rings, recommendations |
| Search | Elasticsearch, OpenSearch | Full-text, faceting, relevance | Not a source of truth; eventual consistency | Product search, logs |
Text description
If you need transactions, joins and constraints, use a relational database such as Postgres or MySQL. Otherwise, for very high write volume with simple key-based access, use a wide-column or key-value store such as Cassandra or DynamoDB. If the core need is text search or traversing relationships, add a search engine or graph database. Otherwise a document store fits flexible, nested records. Relational is the default unless a requirement says otherwise.
Model from the queries
List the queries first, then design tables and keys around them.
Example: messages in a chat app. Query: "latest 50 messages in conversation X, then older ones by cursor". In a wide-column store, partition by conversation_id and cluster by message_id (time-ordered) descending: one partition read returns the page in order. In Postgres, an index on (conversation_id, created_at DESC) serves the same query until write volume forces sharding.
Indexes
- An index makes reads by that column fast and every write slower (the index must be updated).
- Composite indexes follow the leftmost-prefix rule:
(user_id, created_at)serves "by user" and "by user, ordered by time", not "by time" alone. - Covering indexes include all columns a query needs, avoiding a table lookup.
- Don't index everything; index what the hot queries filter and sort on.
Storage engines: B-tree vs LSM-tree
| B-tree (Postgres, MySQL InnoDB) | LSM-tree (Cassandra, RocksDB) | |
|---|---|---|
| Writes | Update pages in place; random I/O | Append to memory then flush sorted files; sequential I/O |
| Reads | Predictable, fast | May check several files (helped by bloom filters) |
| Best for | Read-heavy, transactional workloads | Write-heavy workloads |
| Cost | Page splits, fragmentation | Background compaction uses I/O |
SQL vs NoSQL: the interview answer
"Payments and orders need transactions and constraints, so Postgres. Chat messages are a huge, append-only, partition-by-conversation workload with no joins, so Cassandra or DynamoDB fits. Search goes to Elasticsearch, fed from the source of truth." Name the access pattern that decides it each time.
Self-check
How do you choose a database in an interview?
- List access patterns and consistency needs; relational by default; switch for massive key-based writes, search, graph traversal or flexible documents; name the deciding requirement.
Explain B-tree vs LSM-tree storage.
- B-tree: in-place page updates, fast predictable reads, read-heavy/transactional.
- LSM: memtable + sorted files, fast sequential writes, compaction, bloom filters for reads.