System Design Playbook
07 · Data & databases10 min read

Types of databases: SQL vs NoSQL, Redis, graph and more

Relational, document, key-value, wide-column, graph, time-series and search engines. How each one stores data, what it's good and bad at, real examples, and how to pick (or combine) them in a system design.

In one sentence

Each database type is optimized for a different shape of data and access pattern. Pick by the questions you need to answer fast, not by fashion.

Same data, different shapes

The easiest way to understand database types is to store the same thing in each of them. Here it's a user called Ana, her orders and the people she follows.

The same data, stored seven ways

users

id | name
1 | Ana
2 | Bob

orders

id | user_id | total
91 | 1 | 30.00
92 | 1 | 12.50
SELECT u.name, o.total FROM users u JOIN orders o ON o.user_id = u.id WHERE u.id = 1;

ExamplesPostgreSQL, MySQL, SQL Server, Aurora

Strong at

  • + Joins across tables
  • + ACID transactions
  • + Strict schema and constraints

Weak at

  • − Sharding is hard
  • − Schema changes on huge tables

Reach for it whenYour data has clear relations and correctness matters: payments, orders, users.

Same user (Ana), her orders and her follows. Pick a database type to see how the data is shaped and which questions it answers well.

SQL vs NoSQL

"NoSQL" isn't one thing: it's every database that isn't relational. Still, the comparison is useful as a starting point.

SQL (relational)NoSQL (most types)
Data modelTables with rows and fixed columnsDocuments, key-value pairs, wide rows, graphs…
SchemaDefined up front, enforced by the DBFlexible, often enforced by the app
RelationsJoins and foreign keysEmbed related data or join in the app
TransactionsACID across many tablesUsually per item or document (some offer more)
ScalingVertical + read replicas; sharding is hardDesigned to scale horizontally
QueriesAny question with SQLFast for the access patterns you designed for

Redis: the Swiss army knife

Redis is an in-memory key-value store with rich data types (strings, hashes, lists, sets, sorted sets, streams). Because it lives in RAM it answers in well under a millisecond.

  • Caching query results and sessions
  • Rate limiting with counters and TTLs
  • Leaderboards with sorted sets
  • Pub/sub and lightweight queues
  • Distributed locks

It's rarely the source of truth: memory is limited and expensive, so the main data usually lives in another database and Redis holds the hot part.

Graph databases

In a graph database relationships are first-class: "Ana follows Bob, Bob liked this video". Questions like "videos liked by people I follow" or "how is this account connected to known fraudsters?" are a few hops instead of chains of expensive joins.

Great for social graphs, recommendations, fraud detection and knowledge graphs. Not great as a general store for everything else.

Real systems use several (polyglot persistence)

Part of a YouTube-like appGood fit
Users, channels, subscriptions, billingRelational (PostgreSQL / MySQL)
Video files and thumbnailsObject storage (S3) + CDN, not a database
Comments and watch history at huge scaleWide-column (Cassandra / Bigtable)
Sessions, hot counters, rate limitsKey-value (Redis)
Video searchSearch engine (Elasticsearch / OpenSearch)
Recommendations "people like you watched"Graph or precomputed tables
Metrics and monitoringTime-series (Prometheus / TimescaleDB)

Each extra database is one more system to run, back up and keep in sync, so add them only when the main database is clearly the wrong tool.

Key takeaways

  • 01Relational databases give joins, constraints and ACID transactions; they're the default choice.
  • 02NoSQL types trade flexible queries for scale or speed on specific access patterns.
  • 03Redis is an in-memory key-value store: perfect for caches, sessions, counters and rankings.
  • 04Graph databases shine when the relationships themselves are what you query.
  • 05Big systems combine several databases, each for the job it does best.

Check your understanding

1 of 3

How should you choose a database type?

Related concepts