postgresappnode Aid customer41 Ben42 Ana43 Cainode Border_id item qty41 pen 342 mug 242 pen 143 tea 242 tea 1{"customer":"Ana", "items":[ {"item":"mug","qty":2}, {"item":"pen","qty":1} {"item":"tea","qty":1} ] "gift":true},,414243{ }
Back row: PostgreSQL, the shop app and the two nodes of a document storeFront: order 42 as two tables, and as one JSON documentEnvelopes: queries and answers · pucks: document keys on their nodeAt the back: PostgreSQL, the shop app and a document store of two nodes. In front: order 42 as two PostgreSQL tables (left) and as one JSON document (right). The tables are read back with a JOIN; the document is read whole, by its key, from the one node its hash picks. The document grows and gains a field no other order has, but a search inside the documents must open every one on every node, unless an index is added. The figure adds up the key’s character codes so you can check the placement by hand. Real stores use a strong hash: Cassandra’s default partitioner uses Murmur3, and DynamoDB feeds the partition key to an internal hash function.

Two ways to store an order

  1. NoSQL, often read as “not only SQL”, is a loose name for databases not built on relational tables. The four main families are key-value, document, wide-column and graph stores. We will store one shop order two ways.

Documents at work

The trade-off

What happens

;; step 1/10 · nosql
NoSQL: databases not built on relational tables

key-value   a value stored under a key; get and put by key
            e.g. Redis; DynamoDB (also document)
document    a JSON-like document under a key; the store can look inside
            e.g. MongoDB, Couchbase
wide-column rows of many columns, grouped by a partition key
            e.g. Apache Cassandra, HBase
graph       nodes joined by relationships, for following links
            e.g. Neo4j

the example: order 42 by Ana
  in PostgreSQL tables   (left)
  in a document store    (right, 2 nodes)

The figure adds up the key’s character codes so you can check the placement by hand. Real stores use a strong hash: Cassandra’s default partitioner uses Murmur3, and DynamoDB feeds the partition key to an internal hash function. Document stores are weak at joins, not always without them: MongoDB has a $lookup stage. Joining across many nodes is still slow, because the data must travel between machines.