sagelearnai

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

FamilyExamplesStrengthsWeaknessesTypical use
RelationalPostgreSQL, MySQLTransactions, joins, constraints, mature toolingWrite scaling needs shardingUsers, orders, payments, most business data
Key-valueRedis, DynamoDBVery fast lookups by key, simple scalingQueries only by key (plus limited secondary indexes)Sessions, carts, counters, caches
Wide-columnCassandra, HBase, BigtableMassive write throughput, partitioned by key, time-ordered rowsNo joins; query patterns fixed up frontMessages, events, time series
DocumentMongoDBFlexible, nested recordsCross-document transactions are costlierCatalogues, content, user-generated data
GraphNeo4jMulti-hop relationship queriesHarder to scale horizontallySocial graphs, fraud rings, recommendations
SearchElasticsearch, OpenSearchFull-text, faceting, relevanceNot a source of truth; eventual consistencyProduct search, logs
Need transactions, joins, constraints? RelationalPostgres, MySQL Huge write volume, key access? Wide-column / KVCassandra, DynamoDB Search text or relationships? Search / graphElasticsearch, Neo4j Document storeflexible, nested records yes no yes no yes no
Client / userData store
Choosing a database. Start relational unless a requirement pushes you elsewhere, and name that requirement out loud.
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)
WritesUpdate pages in place; random I/OAppend to memory then flush sorted files; sequential I/O
ReadsPredictable, fastMay check several files (helped by bloom filters)
Best forRead-heavy, transactional workloadsWrite-heavy workloads
CostPage splits, fragmentationBackground compaction uses I/O

SQL vs NoSQL: the interview answer

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

Last reviewed 2026-11-05.