Key-Value, Document, Wide-Column, and Graph stores compared against Relational RDBMS.
What you will learn
Say what NoSQL stores trade away compared with relationaldatabases (BASE versus ACID).
Tell the key-value, document, wide-column and graph models apart, and what each suits.
Design a schema from the queries rather than from the entities.
The limits of relational modeling at extreme scale
Relationaldatabases are strong at structured data with a strict schema, transactions, and complex joins, and they have clear, well-understood patterns for scaling. NoSQL stores make a different trade:
Data is denormalized, and joins are generally done in application code.
Most NoSQL stores lack true ACID transactions and favor eventual consistency. This is often summed up as BASE: basically available (the system guarantees availability), soft state (state may change over time, even without input) and eventual consistency. Compared with CAP, BASE chooses availability over consistency.
The primer's reasons to pick NoSQL: semi-structured data, a dynamic or flexible schema, non-relational data, no need for complex joins, many terabytes (or petabytes) of data, and very data-intensive workloads with very high IOPS. Data that suits it well: rapid ingest of clickstream and log data, leaderboards and scores, temporary data such as a shopping cart, frequently accessed ("hot") tables, and metadata or lookup tables.
The four major NoSQL data models
Model
Abstraction
Examples
Good for
Watch out for
Key-value
A hash table: usually O(1) reads and writes, often backed by memory or SSD; some keep keys sorted so key ranges are cheap
Redis, Riak, DynamoDB (key-value and document)
Simple data models and rapidly changing data, such as an in-memory cache layer
Only a limited set of operations, so complexity moves into the application
Document
A key-value store whose values are documents (JSON, XML, binary), queryable by their internal structure
MongoDB, CouchDB
Occasionally changing data with a flexible shape; documents in a collection can have completely different fields
The line with key-value stores blurs: many key-value stores also index value metadata
Wide column
Nested maps: ColumnFamily<RowKey, Columns<ColKey, Value, Timestamp>>; every value carries a timestamp for versioning and conflict resolution
Bigtable (the first), HBase, Cassandra
Very large data sets with high availability and scalability; keys kept in lexicographic (dictionary) order make key-range reads efficient
Queries that do not supply the partition key (the row key)
Graph
Records are vertices and relationships are edges, also called arcs
Neo4j, FlockDB
Data with complex relationships, many foreign keys or many-to-many relationships, such as a social network
Relatively new and less widely used: fewer tools, and many are only reachable through RESTAPIs
Query-driven data modeling vs. relational normalization
In relational design you normalize data around entities (users, orders, products) and write the queries afterward, using joins.
In Cassandra you do the opposite: "you don't start with the data model; you start with the query model." You cannot join, so you design tables around the queries and denormalize freely. If an application needs orders by user and orders by status, you create two tables, each holding the data one query needs:
DynamoDB also starts from the access patterns, but AWS advises keeping as few tables as possible, often a single table, using sort keys and secondary indexes to serve the different queries. Either way, you must know the questions before you design the schema.
Follow a request from the users: the load balancer passes it to a service, which sends each kind of data to the store whose data model suits it. Simple, fast-changing values go to a key-value store, flexible documents to a document store, very large data read by key range to a wide-column store, and data that needs transactions and joins to a relational database. Each store is designed around the queries made of it, not around one shared schema.
Users: 1 × Users
Load balancer: 2 × Load balancer
Services: 4 × App server
Key-value store: 1 × NoSQL store
Document store: 1 × NoSQL store
Wide-column store: 1 × NoSQL store
Relational database: 1 × Relational database
Users sends requests to Load balancer
Load balancer sends requests to Services
Services sends requests to Key-value store
Services sends requests to Document store
Services sends requests to Wide-column store
Services sends requests to Relational database
Key takeaways
NoSQL stores denormalize, join in application code and mostly favor availability with eventual consistency (BASE).
Key-value suits simple, fast-changing data; document suits flexible shapes; wide column suits very large data read by key range; graph suits many-to-many relationships.
In Cassandra you start from the queries and may keep one table per query; DynamoDB also starts from the access patterns but keeps as few tables as possible.