03. Data & Storage / Lesson 1 of 11

SQL vs NoSQL

Five kinds of database, and how to pick the right one.

  • 10 min
  • Beginner
  • 10 steps
  • ConsistencyStrong (good)
  • JoinsBuilt in (good)
  • Scaling outHarder (warning)

Two tables

users(id, name, email)

orders(id, user_id, total)

✓ Ana appears once

user_id → idAppusersorders
  • Request
  • Write
Step 1/10

Step 1 of 10: SQL: data in tables with relations

Read every step as text
  1. Step 1: SQL: data in tables with relations

    A relational (SQL) database keeps data in tables. A table has columns that every row must follow. A users table holds each person once, and an orders table points back to a user with a user_id column.

    Because each fact is written in only one place, a change (a new email address) is made once and is right everywhere. Splitting data like this is called normalization.

    SQL means Structured Query Language, the way you ask a relational database questions. Examples: PostgreSQL, MySQL, SQL Server.

  2. Step 2: A join puts the pieces back together

    To show an order with the customer's name, we ask for a join: match each order's user_id with a user's id. The database does the matching, and we write no loops.

    Joins let you ask questions you did not plan for, such as "total spent per country last month", without changing how the data is stored. This is a main reason SQL is the default choice.

  3. Step 3: ACID: transactions you can trust

    A transaction groups changes so they succeed or fail together. SQL databases follow ACID: Atomic (all or nothing), Consistent (rules such as foreign keys always hold), Isolated (users at the same time do not see half-done work), Durable (once committed, it survives a crash).

    Moving 100 from one account to another needs this: the debit and the credit must both happen, or neither.

    For money, stock and bookings, ACID is worth more than almost any speed gain.

  4. Step 4: The price of a fixed shape: schema changes

    Every row in a table has the same columns, the schema. This catches mistakes early, because a text value cannot go into a number column. But when the product changes, the shape must change, using a migration.

    On a table with hundreds of millions of rows, a migration needs planning, because some changes lock the table or rewrite it. Document and key-value stores let rows differ, which makes early changes easier but moves the checking into your code.

    A flexible schema is not free: the rules still exist, just in your application instead of the database.

  5. Step 5: Key-value: the simplest and fastest lookup

    A key-value store works like a giant dictionary: give it a key, get back a value, and it does not look inside the value. Redis and DynamoDB are examples.

    Reads and writes by key are very fast and easy to spread over many machines, because each key can live anywhere. The price: you can only ask by key. There are no joins and no "find all values where...".

    Great for sessions, caches, shopping carts, counters and feature flags.

  6. Step 6: Document: keep related data together

    A document database stores JSON-like documents. One order document can hold the customer's name and the full list of items inside it. A single read returns the whole thing, with no join.

    Documents in one collection need not match, so the shape can evolve. The price is duplication: Ana's name is copied into every order she made, and a change must be made in each copy. MongoDB and Firestore are examples.

    A good fit for catalogs, profiles and content, where you read one whole object at a time.

  7. Step 7: Wide-column: huge amounts of writes, spread out

    A wide-column store (Cassandra, Bigtable, HBase) groups rows by a partition key, and the partitions are spread over many servers. Each row can have many columns, and columns may differ between rows.

    It is built for very high write rates and large data, such as sensor readings or messages. You design the table for the exact queries you will run, so there are no joins, and you cannot ask new kinds of questions cheaply.

    Model your queries first, then your tables. Changing the questions later often means a new table.

  8. Step 8: Graph: when the links are the data

    A graph database stores things (nodes) and the links between them. "Who are the friends of Ana's friends?" follows links from one node to the next, instead of running a join for each step.

    That stays fast even with many hops, where SQL needs a join per hop and slows down quickly. Neo4j is an example. Use it for social networks, fraud rings and recommendations, but not as your general store.

    Pick a graph database when the questions are about paths and connections, not about rows.

  9. Step 9: Choose by the questions you must answer

    Start from your data and your questions. If you need joins, flexible queries or transactions, use SQL. If you only look things up by key, use a key-value store. If you read whole objects, try a document store.

    If you must write a flood of time-ordered data across many servers, look at wide-column. If you walk relationships, look at a graph. Large systems often use several of these side by side, each for its own job.

    When unsure, start with PostgreSQL. It does most jobs well, even JSON, and you can add a specialist store later when you can show a real need.

  10. Step 10: SQL and NoSQL in one picture

    SQL stores facts once in tables, joins them on demand and gives ACID transactions, at the cost of a fixed schema and harder scaling out. NoSQL is four families, each giving up some of that to do one job better.

    Key-value is the simplest and fastest, document keeps an object whole, wide-column absorbs huge writes, and graph follows links. Choose by your questions, and start with SQL when unsure.