System Design · 03. Data & Storage · 10 min · Beginner
SQL vs NoSQL
Five kinds of database, and how to pick the right one.
In short
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.
Key ideas
- SQL means Structured Query Language, the way you ask a relational database questions. Examples: PostgreSQL, MySQL, SQL Server. (step 1)
- 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. (step 9)
Good to know
- For money, stock and bookings, ACID is worth more than almost any speed gain. (step 3)
- Great for sessions, caches, shopping carts, counters and feature flags. (step 5)
- A good fit for catalogs, profiles and content, where you read one whole object at a time. (step 6)
- Pick a graph database when the questions are about paths and connections, not about rows. (step 8)
Watch out
- A flexible schema is not free: the rules still exist, just in your application instead of the database. (step 4)
- Model your queries first, then your tables. Changing the questions later often means a new table. (step 7)
Terms
- Relational database
- A database that stores data in tables of rows and columns, linked by keys.
- Normalization
- Splitting data into tables so each fact is stored only once.
- Join
- A query that combines rows from two tables by matching a column.
- ACID
- Atomic, Consistent, Isolated, Durable: the promises of a database transaction.
- Schema
- The fixed set of tables and columns that every row must follow.
- Schema migration
- A planned change to a table's structure, such as adding a column.
- Key-value store
- A database that stores a value under a key and can only look it up by that key.
- Document database
- A database that stores whole JSON-like documents, so one read returns a full object.
- Wide-column store
- A database that spreads partitions of wide rows over many servers for very high write rates.
- Graph database
- A database that stores nodes and links, and answers path questions quickly.