NoSQL Databases

Compare document, key-value, wide-column and graph databases with relational ones, and learn how to choose between SQL and NoSQL for a given workload.

Intermediate · about 4 hours · difficulty 2/5

Learn first: Indexing and Query Performance

"SQL or NoSQL?" appears in almost every system design interview. The right answer is always "it depends on the access pattern", followed by a concrete justification.

The main families

  • Key-value (Redis, DynamoDB): get and put by key, extremely fast. Great for caches, sessions and counters.
  • Document (MongoDB): JSON-like documents with flexible schemas; good when data is read as a whole unit, like a product with nested attributes.
  • Wide-column (Cassandra, HBase): rows partitioned by key and optimised for massive write throughput and time-series style access.
  • Graph (Neo4j): nodes and edges for highly connected data like social graphs and recommendations.

How they differ from relational databases

  • Schemas are flexible or per-record instead of fixed.
  • Most are built to scale horizontally by partitioning data across nodes.
  • Joins are limited or absent, so data is often denormalised and modelled around queries rather than entities.
  • Many favour availability with eventual consistency (you will study CAP in system design), though many now offer tunable or strong consistency.

Choosing

  • Strong consistency, complex queries and relationships, transactions (payments, inventory): relational.
  • Huge write volume, simple access by key, flexible or evolving data: NoSQL.
  • Many real systems use both: PostgreSQL as the source of truth, Redis as a cache, Elasticsearch for search.

Pitfalls

Choosing MongoDB because "it is web scale" without an access-pattern reason, and assuming NoSQL means no schema design (bad partition keys create hot spots).

Good enough to move on: for a given feature you can name a suitable database type, explain the trade-off, and sketch how the data would be modelled.

Key concepts

  • Key-value stores
  • Document databases
  • Wide-column stores
  • Graph databases
  • Schema flexibility and denormalization
  • SQL vs NoSQL decision making
  • Partition keys and hot spots

Mini project

Model a URL shortener and a blog with comments twice, once in PostgreSQL and once in MongoDB or Redis, implement the three most common queries in each, and write a one-page comparison of the trade-offs you noticed.

Check yourself

For a chat application's message history, would you pick a relational database or a wide-column store, and what partition key would you use?

Free resources