DevNotes
Support
System Design Basics · Chapter 06

Choosing a Database — SQL vs NoSQL

Relational databases and ACID, the four NoSQL families — key-value, document, wide-column and graph — search engines, time-series and object storage, how to choose by access pattern, polyglot persistence, and common misconceptions.

◷ 6 min read§ 10 sections? 5 interview questions{ } 0 code examples

#Introduction

"SQL or NoSQL?" isn't a matter of fashion — it depends on the data's shape, the access patterns, the consistency needs and the scale. Most real systems use several kinds of storage, each for what it does best.

#Relational (SQL) Databases

MySQL, PostgreSQL, Oracle, SQL Server — tables with a fixed schema, rows and columns, relationships through foreign keys, and SQL for queries. (See the SQL & MySQL topic.)

StrengthsLimitations
ACID transactions across many rows and tablesScaling writes beyond one primary needs sharding
Powerful queries — joins, aggregations, ad-hoc reportingSchema changes on huge tables need care
Strong consistency and constraints (FKs, unique, check)Not ideal for unstructured or highly variable data
Mature tooling, well understoodObject–relational mismatch (ORMs help)

Use for: orders, payments, accounts, inventory, bookings — anything relational and transactional. Modern relational databases scale further than many people assume (read replicas, partitioning, very large single instances).

#NoSQL Families

Key-Value Stores

Redis, Amazon DynamoDB (also document-like), Memcached, etcd.

Text
GET  session:9f1c2a7e  →  {"userId": 42, "cart": [...]}
SET  rate:user:42  17  EX 60
StrengthsLimitations
Extremely fast lookups by keyNo queries by value (without secondary indexes)
Simple, easy to scale horizontallyNo joins or relationships

Use for: caching, sessions, rate limiting, leaderboards, feature flags, shopping carts.

Document Databases

MongoDB, Couchbase, Firestore, Amazon DocumentDB — JSON-like documents, flexible schema.

JSON
{
  "_id": "ORD-1001",
  "customer": { "id": "C-42", "name": "Ravi" },
  "items": [ { "sku": "SKU-1", "qty": 2, "price": 499 } ],
  "status": "PAID"
}
StrengthsLimitations
Flexible schema; nested data in one readJoins are limited or expensive — design documents around queries
Maps naturally to objectsDenormalised data can become inconsistent
Horizontal scaling via shardingMulti-document transactions exist but are costlier

Use for: product catalogues, content management, user profiles, event data with varying fields.

Wide-Column Stores

Apache Cassandra, ScyllaDB, HBase, Google Bigtable — rows identified by a partition key, with many columns, spread across many nodes.

StrengthsLimitations
Massive write throughput and linear horizontal scalingQueries must follow the partition key — model tables per query
Multi-datacentre replication, high availabilityNo joins; limited ad-hoc queries
Tunable consistencyEventual consistency by default

Use for: time-series and event logs, messaging history, IoT data, activity feeds — huge write volumes queried by key.

Graph Databases

Neo4j, Amazon Neptune — nodes and relationships as first-class data.

Text
(Ravi)-[:FOLLOWS]->(Asha)-[:FOLLOWS]->(Kiran)
MATCH (me)-[:FOLLOWS]->()-[:FOLLOWS]->(suggestion) RETURN suggestion

Use for: social graphs, recommendations, fraud rings, network and dependency analysis — queries that traverse many relationships, which are slow as SQL joins.

#Other Specialised Stores

StoreExamplesUse for
Search enginesElasticsearch, OpenSearch, SolrFull-text search, filters and facets, log analytics
Time-seriesInfluxDB, TimescaleDB, PrometheusMetrics, sensor readings, time-window queries
Object storageAmazon S3, Azure Blob, GCS, MinIOImages, video, files, backups, data lakes
Analytical (OLAP)Snowflake, BigQuery, ClickHouse, RedshiftReporting over billions of rows
Event logApache KafkaDurable, replayable event streams (not a query database)

#SQL vs NoSQL

SQLNoSQL
Data modelTables and relationsKey-value, document, wide-column, graph
SchemaFixed, enforcedFlexible / schema-on-read
TransactionsMulti-row ACIDOften single-record; multi-record varies
QueriesRich SQL, joins, ad hocOptimised for known access patterns
ScalingVertical + replicas; sharding is harderUsually designed for horizontal scaling
ConsistencyStrong by defaultOften eventual or tunable
Best forRelational, transactional dataHuge scale, flexible data, specific access patterns

#How to Choose

Need multi-entity transactions and complex queries or reporting?
relational — PostgreSQL or MySQL
keep asking
Simple lookups by key at very high speed?
key-value — Redis or DynamoDB
keep asking
Flexible, nested documents read as a whole?
document — MongoDB
keep asking
Enormous write volume queried by partition key?
wide-column — Cassandra
keep asking
Deep relationship traversal?
graph — Neo4j

Questions to ask:

  1. What are the main queries? (Design storage around them.)
  2. What's the read/write ratio and the expected data size?
  3. How consistent must the data be? Can a read be slightly stale?
  4. Do you need transactions across entities?
  5. How will the schema evolve?
  6. What does the team know how to operate?

#Polyglot Persistence

Large systems use the right store per job — often one per microservice:

  • E-commerce platform
    • Order and payment servicesPostgreSQL — transactions
    • Catalogue serviceMongoDB — flexible product attributes
    • Search serviceElasticsearch — full-text and facets
    • Cart and sessionsRedis — fast key-value
    • MediaS3 + CDN
    • Activity historyCassandra — high write volume
    • Events between servicesKafka

The cost: more systems to run, secure, back up and keep in sync (usually through events or CDC).

#Common Misconceptions

  • "NoSQL is always faster." — Only for the access patterns it's designed for; ad-hoc queries can be much slower.
  • "SQL doesn't scale." — Relational databases handle very large workloads with replicas, partitioning and sharding (Vitess, Citus).
  • "NoSQL means no schema." — The schema still exists; it's just enforced by your code instead of the database.
  • "Pick one database for everything." — Different data has different needs.

#Interview Questions

Q1When would you choose SQL over NoSQL?

When data is relational, needs multi-entity ACID transactions, strong consistency, constraints, or flexible querying and reporting — orders, payments, accounts.

Q2When would you choose NoSQL?

For massive scale with simple, known access patterns, flexible or nested data, very high write throughput, or specialised needs like key-value caching or graph traversal.

Q3What are the main types of NoSQL databases?

Key-value (Redis, DynamoDB), document (MongoDB), wide-column (Cassandra, HBase) and graph (Neo4j).

Q4What is polyglot persistence?

Using different storage technologies for different parts of a system, each chosen for its data and access patterns.

Q5Why do you model Cassandra tables around queries?

Because data is distributed and retrieved by partition key without joins, so each query pattern needs a table whose key matches it.

#Key Takeaways

  • Relational databases: ACID, joins, constraints — the default for transactional data.
  • Key-value for speed, document for flexible objects, wide-column for huge write volumes, graph for relationships.
  • Search engines, time-series stores, object storage and Kafka fill specialised roles.
  • Choose by access patterns, consistency, scale and team expertise.
  • Polyglot persistence is normal — and adds operational cost.