NoSQL databases
The four families of database that are not built from tables, shown with one small shop: what each stores, how they spread over many machines, and what they give up.
In 30 seconds
NoSQL is a loose name for databases that do not store data as tables of fixed columns. There are four main families: key-value, document, wide-column and graph.
Most let each record have its own fields, and most are built to spread data across many machines rather than one large one.
The price is usually weaker guarantees: fewer ways to query, and copies of the data that can briefly disagree. They fit best when you know in advance how the data will be read.
NoSQL databases, step by step
The families overlap in practice: many products mix models, and several NoSQL stores now offer SQL-like queries and full transactions. Modulo 3 stands in for a real hash function.
- Step 1: Key-value: one key, one value. The simplest family is a giant dictionary. The application hands over a key and gets back whatever was stored under it. The store does not interpret the value, so it cannot search inside it, but a lookup by key stays fast however much data there is.
- Step 2: Document: flexible fields. A document store keeps each record as a self-describing document, usually JSON. Two documents in one collection may have different fields, so a new kind of product needs no change to a table definition. Unlike a key-value store, the database can read the fields and query them.
- Step 3: Wide-column: sparse rows. A wide-column store looks like a table, but each row holds only the columns it actually has, grouped into families such as profile and orders. A row can grow to thousands of columns, which suits data that keeps arriving: orders, events, sensor readings.
- Step 4: Graph: follow the links. A graph database stores things as nodes and the relationships between them as edges. Questions about connections become walks along edges: start at Mug, step to everyone who bought it, step again to what else they bought. Each extra hop is cheap, where a relational database would need another join.
- Step 5: Scale out across machines. Most NoSQL stores are built to spread data over many ordinary machines instead of one ever-larger one. A rule on the key decides which node holds each record; here, the cart number modulo 3. Real systems hash the key and use schemes such as consistent hashing, so that adding a node moves only a share of the data.
- Step 6: Relaxed consistency. Every record is copied to several nodes so that losing one loses nothing. Many NoSQL stores accept a write before every copy has it, which keeps them fast and available but lets a read from a lagging copy return the old value for a moment. This is eventual consistency; many stores let you ask for stricter guarantees per request.
- Step 7: When NoSQL fits. NoSQL fits when the data has the shape of one of the four families, the application reads it in a few ways known in advance, and the volume calls for many machines. When the questions are unpredictable, span many kinds of record, or need several records changed all-or-nothing, a relational database is usually the simpler choice.
Try it yourself
Learn this first
Nothing. This concept starts from scratch.
Learn next
- SQL vs NoSQLThe same order stored both ways, and what each choice costs and buys. Beginner
- Database replicationCopies of a database kept in step, and what happens when one falls behind. Intermediate
- Consistency models (strong vs eventual)What a read may return after a write when the data lives on three machines. Intermediate
Spotted a mistake on this page?