System design concepts · Data and storage

SQL vs NoSQL

Short answer

SQL databases store data in tables with a declared schema, let you join across tables, and give multi-row ACID transactions; PostgreSQL and MySQL are the usual choices. NoSQL is an umbrella term for stores that give up part of that model to gain something else: key-value stores such as Redis and DynamoDB, document stores such as MongoDB, wide-column stores such as Cassandra, and graph databases. Many NoSQL systems were built to partition across many machines from the start and to serve simple key-based access at very high volume. Choose SQL when the data is relational and correctness across rows matters. Choose a NoSQL store when the access pattern is known and key based, the scale is beyond a single primary, or the data fits a document, wide row or graph more naturally than tables.

Read the video transcript

SQL or NoSQL? The useful question isn't which one scales. It's how your data is shaped, and how you'll read it. A relational database keeps data in tables with a declared schema. Orders point to users and items through foreign keys, and joins stitch them back together, even for queries you haven't planned. When an order, its items and the stock level must change together, one ACID transaction commits all of them, or none. A document store takes another route. The order and its items live together in one document, filed under a partition key, like the user ID. The key is hashed to pick a partition, so a read by key goes straight to one machine. Need more capacity? Add partitions. The price is flexibility. A query that ignores the key has to ask every partition, and joins move into your application code. So start from the access pattern. Orders, payments and inventory want constraints and transactions. Feeds, sessions and carts at huge volume want a key. And many systems use both: a relational source of truth, with change events feeding the stores built for each high-volume read.

SQL vs NoSQL: comparison table

AspectSQLNoSQL
Data modelTables, rows, typed columns, foreign keysKey-value, document, wide-column or graph
SchemaDeclared up front and changed with migrationsFlexible in the store; the application still assumes a shape
QueriesSQL with joins, aggregates and ad hoc filtersMostly by key or by a pre-planned access pattern; joins limited or done in the app
TransactionsMulti-row and multi-table ACID by defaultVaries: single-item atomicity is common; MongoDB and DynamoDB add multi-item transactions with limits
Scaling writesVertical first, then sharding (Vitess, Citus) or a distributed SQL databaseMost are designed to partition horizontally
ConsistencyStrong on the primary; replicas may lagRanges from tunable per query (Cassandra) to strong per key
Good fitPayments, orders, inventory, accounts, anything with constraintsSessions, carts, feeds, time series, event logs, catalogs with varied attributes

When to use each

Choose SQL when

  • Several entities change together and must stay consistent, like an order, its items and stock levels.
  • You need ad hoc queries, reporting or joins you cannot predict today.
  • Constraints such as uniqueness and foreign keys protect important invariants.
  • The data fits on one primary with replicas, which covers more systems than people expect.

Choose NoSQL when

  • Reads and writes are by key at a volume a single primary cannot absorb.
  • Records vary in shape, or nest naturally as documents.
  • You need multi-region writes and can accept eventual consistency or conflict resolution.
  • The data is a graph and the main queries traverse relationships.
The verdict

Choose SQL when

Several entities change together and must stay consistent, like an order, its items and stock levels.

Otherwise

Choose NoSQL when

Reads and writes are by key at a volume a single primary cannot absorb.

The real question is the access pattern

Start from the queries, not the label. Write down the main reads and writes with their rough rates, then ask which store serves them with the fewest moving parts. A feed read by user ID at high volume suits a wide-column or key-value store. A checkout that touches an order, a payment and inventory suits a relational database.

A read by partition key touches one partition, a query without the key asks every partition.

The boundary has also blurred. PostgreSQL stores and indexes JSON documents. MongoDB and DynamoDB support transactions. Distributed SQL databases such as Spanner and CockroachDB scale writes horizontally while keeping SQL and strong consistency. Pick for the workload in front of you.

Using both is normal

Large systems usually keep the source of truth for money and orders in a relational database, and put high-volume derived data, such as timelines, counters, sessions and search indexes, in stores built for that access pattern. The cost is keeping them in sync, typically through change data capture or events, and being clear about which store wins when they disagree.

Orders commit in PostgreSQL, and change events feed a timeline store, a cache and a search index.
Interview angle

How it comes up in a system design interview

Every design has a storage step, and "I will use NoSQL because it scales" is the answer interviewers hear most and like least. Name the store, tie it to the access pattern and the consistency you need, and say what you give up.

Where you will meet it

  • Where do you store seats and bookings so two people can never buy the same seat?
  • Which database holds tweets, and which holds the precomputed home timelines?
  • A key-value store is the product. What does its storage engine look like underneath?
  • Payments need an audit trail and exact balances. What store, and what isolation level?
How a strong answer sounds
“Bookings go in PostgreSQL because a booking touches seats, orders and payments together and we need a transaction with a uniqueness constraint on the seat. The event catalog is read heavy and rarely changes, so it is cached aggressively, and the search index is a separate store fed by change events.”

Common mistakes

  • Choosing NoSQL for scale before checking whether one relational primary with replicas would handle the load.
  • Calling NoSQL schemaless. The schema moves into application code, and changing it still needs a migration plan.
  • Forgetting that denormalized NoSQL models need every write path to update every copy.

Frequently asked questions

Is NoSQL faster than SQL?

Not inherently. A key lookup is fast in both. NoSQL stores can be faster for their target access pattern at very large scale because the data is laid out for it and spread across machines, and they are often slower or unable to answer queries they were not designed for.

Can SQL databases scale horizontally?

Yes. Read replicas scale reads, and sharding layers such as Vitess and Citus or distributed SQL databases such as Spanner and CockroachDB scale writes. It takes more planning than in stores built around partitioning.

Is MongoDB SQL or NoSQL?

MongoDB is a NoSQL document database. It stores JSON-like documents, supports secondary indexes and aggregation pipelines, and offers multi-document transactions.

Should a system use both SQL and NoSQL?

Often, yes. A common pattern is a relational source of truth for transactional data plus specialized stores for caching, search, feeds or analytics, kept in sync through events.

Learn and practise

Lessons

Problems where this choice matters

Knowing the trade-off is half of it

Practise saying it under follow-up questions: a timed system design interview with an AI interviewer that probes your choices and grades against a rubric.

Start a mock interview