Stage 2 · Building blocks · Easy · 18 min
Choosing a database and modelling data
Start from access patterns: what you read and write, how often, and what must stay consistent. Then choose relational, key-value, wide-column or document storage, and shape the data model around it.
What each interview level is expected to know
Learn first: Functional and non-functional requirements, Vertical and horizontal scaling
After this lesson you can:
- Start a database choice from the reads and writes the app makes.
- Compare relational, key-value, document and wide-column stores.
- Shape the keys around the main query, and say when you need a real transaction.
Start here
See it in 60 seconds
Tables or documents
SQL tables store each fact once and join rows when you read; documents store whole records together, so the queries you need decide.
Scene 1 of 6 · Two ways to store
The same 4 orders can sit in 3 SQL tables, or in 4 documents that each hold a whole order.
| Table | Holds |
|---|---|
| products | 3 rows |
| orders | 4 rows |
| order_items | 6 rows |
| Document | Holds |
|---|---|
| o1 | u1: pen ×2, ink ×1 |
| o2 | u2: pen ×1 |
| o3 | u1: pad ×4, pen ×1 |
| o4 | u3: ink ×2 |
- SQL rows touched
- not shown yet
- Whole documents touched
- not shown yet
- Two ways to store. The same 4 orders can sit in 3 SQL tables, or in 4 documents that each hold a whole order.
- Tables keep facts once. In SQL tables each fact is stored once: the name pen sits in one products row.
- Documents copy them in. A document holds the whole order, with a copy of each product name: pen appears in 3 of them.
- Read one order. Showing order o1 is one document read, or one SQL query joining 3 tables.
- Rename a product. Renaming pen changes 1 SQL row, but 3 documents if every copy must change too.
- Let the queries choose. So list the main reads and writes first, and pick the model that makes those cheap.
- Now you try. Pick a query, and see what each model has to touch. For example: SQL: one query over the 6 small order_items rows. Documents: read all 4 whole orders to find the pens. SQL suits questions across many records; a document store can do it too, but reads whole orders to get there.
Scene 1 of 6: Two ways to store. The same 4 orders can sit in 3 SQL tables, or in 4 documents that each hold a whole order.
Key points
- SQL tables keep each fact once, and a query joins rows from several tables.
- A document keeps a whole record together, so reading it back is one simple read.
- Copies inside documents make reads easy but changes costly, since every copy must change.
- Documents are one kind of NoSQL; list your main reads and writes, then pick the model that makes them cheap.
In plain words
Say you are building a shop. An order links to a customer, to products and to a payment, and all of them must agree. A relational (SQL) database fits this. It keeps data in tables, links rows by id, and can change several rows together or not at all. Now say you also store 50 million chat messages a day, and each read uses one key. A NoSQL store fits that. NoSQL is a loose name for stores that are not relational: key-value, document and wide-column. Most give up joins, and in return they spread data across many servers more easily.
Before choosing, write down the main reads. If each one fetches a single item by its key, a key-value store fits. If reads join orders, customers and payments, keep that data in SQL. Many apps use both: SQL for orders and money, and a NoSQL store for the chat history.
The problem it solves
No database is good at everything. A relational database keeps linked data correct, but its writes are hard to spread beyond one machine. Most NoSQL stores spread writes across many machines, but most drop joins, and many limit what one (A group of database changes that succeed or fail together: either all of them are saved, or none are.) can cover. Pick the wrong kind and you rebuild the missing part in your own code.
Why it matters
The database is the hardest part of a system to change later. Moving live data to a different kind of database can take a team months, while the app must keep running. So choose from how the data will be read and written, not from what is popular.
A worked example
A chat app writes 50 million messages a day. Spreading them evenly across 20 servers means each one handles about 50,000,000 ÷ 20 = 2,500,000 messages a day. A payments table instead keeps its order and customer rows together, so the database can enforce that every order points to a real, existing customer.
Think of it like…
Think of a spreadsheet with fixed column headings, where a rule rejects any row that names a missing customer. A document store is more like a folder of forms, where each form can have its own boxes.
Where this analogy stops working
Who checks the shape of the data is the useful part. A folder that takes any form is the misleading part. Your code still expects a shape, and the database only stops checking it. The line is also blurred: some NoSQL stores have transactions, and some SQL databases spread over many machines.
Try it
See it working
Relational (SQL)
Each fact is stored once. Tables are linked by ids and combined with JOINs.
Cost: 1 query with a JOIN across 3 tables
Document (NoSQL)
Data that is read together is stored together, even if that means copying it.
post:42 {
author: { id: 7, name: "Ana" },
body: "Hello",
comments: [
{ user: { id: 9, name: "Bo" }, text: "Nice" },
{ user: { id: 7, name: "Ana" }, text: "Thanks" }
]
}Cost: 1 read of one document
Both are fine. The document model is a single read because the comments are stored inside the post.
Also try: Sharding · Replication
Mechanics
How it works
Pick a database from the questions your app asks most, not from the data alone. The steps go from listing those questions to the main kinds of store, and end by shaping the keys of one table, with a picture.
List the access patterns
Write down the main queries and writes with their rough rates: "fetch a user's latest 20 posts", "append a message". The model and the database follow from these.
Relational (SQL)
Data lives in tables with fixed columns, and rows in different tables link by id. One query can join tables, and a transaction changes several rows together or not at all. (A widely used open-source SQL database.) and MySQL are examples. Pick this when the data has many links or money is involved.
Key-value store
You save a value under a key and read it back by that key, such as cart:42. Reads and writes are fast, and the data is easy to spread across many servers. A plain key-value store cannot search by anything except the key. DynamoDB and (A widely used data store that keeps its data in memory, so it answers much faster than a database on disk. It is often used as a cache.) are examples.
Document store
Each record is one JSON-like document, such as a whole product with its options inside it. Documents in one collection can have different fields. You can (An extra structure, kept sorted or grouped, that lets a database find matching items without reading all of them.) and search fields inside the document. MongoDB is an example.
Wide-column store
Rows are grouped by a (One slice of the data. In Kafka, one ordered log inside a topic.) key, and each group lives on one set of servers. Writes are appended, so the store can take a very high write rate. You must design each table around one query. Cassandra and ScyllaDB are examples.
Shape the keys around the main query
Pick a case to follow belowWith this case: The read the keys were shaped for asked 1 of 3 servers and got rows already in order.
- Chat app — reads messages — healthy — NOW — chat 7 → server 3
- Server 1 — chats 1 to 3 — healthy
- Server 2 — chats 4 to 6 — healthy
- Server 3 — chats 7 to 9 — healthy
- Rows
Step 1 of 2Step 1 of 2 · Chat app
Done: The query names chat 7
- What happens
- chat_id is the partition key, and chats 7 to 9 live on server 3.
- Why
- The partition key decides which server holds a row, so the app knows where to ask.
- What changes
- Only server 3 is asked.
- Next
- Server 3 returns the latest 50, already in order
Servers asked0 of 3the key points to onePartition keychat_idsort key: sent timeStep 1 of 2: Chat app. The query names chat 7. chat_id is the partition key, and chats 7 to 9 live on server 3.
What these pictures simplify. This picture uses 3 servers and stated chat ranges. A real table spreads millions of partitions over many servers, usually by a hash of the key rather than by ranges.
Say the main read is "the latest 50 messages in chat 7". In a wide-column table, make chat_id the partition key and the send time the sort key. That read then touches one partition and gets rows already in order. A query the keys do not serve, such as all messages by one user, needs a second table.
In the picture, chat messages are spread over 3 servers by chat_id. Pick the first case: the read names chat 7, so the app asks one server, and the rows come back in order.
Pick the second case: a read by user 42. The partition key says nothing about users, so the app asks every server and sorts what comes back. With hundreds of servers that is too slow, so you add a second table keyed by user, and write each message to both.
Use it when
- SQL: orders, payments, bookings and stock counts, where several rows must change together or not at all.
- NoSQL: chat messages, sensor readings, click logs and feeds, where writes are very frequent and each read uses one key.
Skip it when
- A wide-column store such as Cassandra for a bank ledger. A transfer changes two accounts together, and that kind of transaction is not what it is built for.
- A relational database for a write rate that one machine cannot take, unless you are ready to (One slice of a database. Each shard holds part of the data on its own machine.) it.
Check yourself
Judgement
Trade-offs and how it fails
What you give up
- Consistency
- A relational database gives (Four promises a database transaction can make. All or nothing, rules kept, kept apart from other transactions (as far as its isolation level says), and kept after a crash.) transactions: several changes succeed or fail together. Many NoSQL stores default to (Copies of the data may differ for a short time after a write, but they all reach the same value once writes stop.), so a read may briefly return an old value.
- Scalability
- Most NoSQL stores are built to spread data across many servers from the start. A relational database usually grows with read replicas first, then (Splitting one database's data into parts and keeping each part on a different machine. Each part is called a shard.), which takes more work.
Failure modes
- Joins across shards: once a relational database is split over many servers, a join must fetch rows from several of them. It is slow and can time out.
- Half-done changes: a store with no multi-row transaction saves the order, then the stock update fails. Nothing undoes the first write.
In the real world
- Uber's engineering blog describes Schemaless, a datastore it built on top of sharded MySQL to hold trip data.
- AWS reported that DynamoDB peaked above 100 million requests a second during Prime Day 2022, with single-digit millisecond responses. Source: AWS blog: Prime Day 2022 (checked 14 October 2026)
Trade-offs to be ready to discuss: Sql vs nosql
Check yourself
Interview
What interviewers look for
- Do not say "SQL is old, NoSQL is modern". Choose from the shape of the data, the queries, and what must stay consistent.
- If several rows must change together, or you need joins across tables, choose a relational database.
Say it out loud: What do the four letters of ACID stand for?
Expected depth by interview level
Levels describe the depth interviewers probe, not years of experience. Choose a level on the learning path to highlight it.
- JuniorCore
- Pick SQL or NoSQL for a simple app and sketch the tables.
- MidCore
- Model data from access patterns; indexes; when NoSQL fits.
- SeniorCore
- Consistency, transactions and the scaling limits of each choice.
- StaffExpected
- Polyglot persistence and data ownership between teams.
Practice
Apply it
Three harder questions
The first two use the idea on a new case. The last asks you to judge a design.
Apply it
Apply it
Judge a design
Go deeper
Sources and further reading
First-hand sources to check this page against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- BookDesigning Data-Intensive ApplicationsMartin Kleppmann (O’Reilly)Chapter 2 compares the relational, document and graph models with the same data.
- Research paperDynamo: Amazon’s Highly Available Key-value Store (2007)DeCandia et al., SOSPWhy Amazon built a key-value store that stays writable during failures, and what it gave up.
- Official docsNoSQL design for DynamoDBAWS documentationDesigning a table from the queries you need, the opposite order to a SQL schema.