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.

In SQL tables
TableHolds
products3 rows
orders4 rows
order_items6 rows
As documents
DocumentHolds
o1u1: pen ×2, ink ×1
o2u2: pen ×1
o3u1: pad ×4, pen ×1
o4u3: ink ×2
SQL rows touched
not shown yet
Whole documents touched
not shown yet
  1. Two ways to store. The same 4 orders can sit in 3 SQL tables, or in 4 documents that each hold a whole order.
  2. Tables keep facts once. In SQL tables each fact is stored once: the name pen sits in one products row.
  3. Documents copy them in. A document holds the whole order, with a copy of each product name: pen appears in 3 of them.
  4. Read one order. Showing order o1 is one document read, or one SQL query joining 3 tables.
  5. Rename a product. Renaming pen changes 1 SQL row, but 3 documents if every copy must change too.
  6. Let the queries choose. So list the main reads and writes first, and pick the model that makes those cheap.
  7. 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

  1. SQL tables keep each fact once, and a query joins rows from several tables.
  2. A document keeps a whole record together, so reading it back is one simple read.
  3. Copies inside documents make reads easy but changes costly, since every copy must change.
  4. 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 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

Pick a query

Relational (SQL)

Each fact is stored once. Tables are linked by ids and combined with JOINs.

users
id
name
posts
id
user_id →
body
comments
id
post_id →
user_id →
text

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.

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

  2. 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. and MySQL are examples. Pick this when the data has many links or money is involved.

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

  4. 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 and search fields inside the document. MongoDB is an example.

  5. Wide-column store

    Rows are grouped by a 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.

  6. Shape the keys around the main query

    Pick a case to follow below

    With this case: The read the keys were shaped for asked 1 of 3 servers and got rows already in order.

    Chat app, reads messages, NOWChat appreads messageschat 7 → server 3NOWServer 1, chats 1 to 3Server 1chats 1 to 3Server 2, chats 4 to 6Server 2chats 4 to 6Server 3, chats 7 to 9Server 3chats 7 to 9
    • 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 2

    Step 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 asked
    0 of 3
    the key points to one
    Partition key
    chat_id
    sort key: sent time

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

Check yourself

Why can a key-value store spread its data over many machines easily?

Judgement

Trade-offs and how it fails

What you give up

Consistency
A relational database gives transactions: several changes succeed or fail together. Many NoSQL stores default to , 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 , 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

Your data has many relationships, and you often join it in new ways. Which store fits best?

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?

A shop must take an order, lower the stock count and record the payment, all together or not at all. Which store fits best?

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

A wide-column table of chat messages uses chat_id as the partition key and the send time as the sort key. The app now needs all messages sent by user 9. What happens?

Apply it

Which of these fits a key-value store best?

Judge a design

A team picks Cassandra because it is fast, before listing any queries. Later the app needs to filter the same data in 10 different ways. What went wrong?

Next: indexes, which let a database find a row without reading the whole table.

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.