Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Types of Databases: SQL and NoSQL

Overview

There is no single “best database” — there are families of databases optimized for different data shapes, workloads, and consistency needs. This page is the map: it catalogs the SQL and NoSQL families, real products in each, what they’re good at, and how to choose. Each family links to a deeper page in this book.

Two orthogonal dimensions classify most databases:

  1. Data model — relational (tables), key-value, document, wide-column, graph, vector…
  2. Workload — OLTP (transactions), OLAP (analytics), search, real-time…
graph TD
    DB["Databases"] --> SQL["SQL / Relational"]
    DB --> NOSQL["NoSQL"]
    DB --> NEW["NewSQL"]
    DB --> SPEC["Purpose-built"]
    SQL --> R1["Traditional RDBMS<br/>(PostgreSQL, MySQL, SQL Server)"]
    SQL --> R2["OLAP / Columnar<br/>(ClickHouse, Snowflake, BigQuery)"]
    SQL --> R3["Embedded<br/>(SQLite, DuckDB)"]
    NOSQL --> K1["Key-Value<br/>(Redis, DynamoDB)"]
    NOSQL --> K2["Document<br/>(MongoDB, Couchbase)"]
    NOSQL --> K3["Wide-Column<br/>(Cassandra, ScyllaDB)"]
    NOSQL --> K4["Graph<br/>(Neo4j, Neptune)"]
    NEW --> N1["Distributed SQL<br/>(CockroachDB, TiDB, Spanner)"]
    SPEC --> S1["Time-Series<br/>(InfluxDB, TimescaleDB)"]
    SPEC --> S2["Vector<br/>(pgvector, Qdrant, Milvus)"]
    SPEC --> S3["Search<br/>(Elasticsearch, OpenSearch)"]
    SPEC --> S4["In-Memory<br/>(Redis, Memcached)"]

The SQL / Relational Family

Core idea: data in tables (rows + columns) with a fixed schema, joined via foreign keys, queried with SQL, transactions with ACID guarantees.

Sub-typeProductsBest forLimitations
Traditional RDBMSPostgreSQL, MySQL, SQL Server, Oracle, MariaDBOLTP apps, financial data, anything needing ACID + complex queriesVertical scaling; joins get expensive at massive scale
OLAP / ColumnarClickHouse, Snowflake, BigQuery, Redshift, DuckDBAnalytics, BI, aggregations over billions of rowsNot for high-frequency point writes
Embedded SQLSQLite, DuckDBLocal/single-process storage, tools, mobile, analytics-on-filesSingle-writer, not for high concurrency
Distributed SQL (NewSQL)CockroachDB, TiDB, YugabyteDB, Google SpannerSQL + ACID that scales horizontally across nodesOperational complexity; consistency costs latency

The NoSQL Family

Core idea: non-relational models optimized for scale, flexible schemas, and specific access patterns — trading away (or relaxing) relational joins and strict ACID. See NoSQL Overview for the full treatment.

TypeModelProductsBest forWatch out
Key-ValueOpaque value by keyRedis, Memcached, DynamoDB, etcd, RocksDBCaches, sessions, leader election, lookups by IDNo queries by value, no joins
DocumentJSON/BSON documentsMongoDB, Couchbase, Firestore, CouchDBFlexible-schema apps, catalogs, content, event logsJoins/aggregations awkward; multi-doc transactions limited
Wide-ColumnRows keyed by partition + sorted columnsCassandra, ScyllaDB, HBase, BigtableWrite-heavy time-series-ish data, event logs at scaleNo real joins; query by partition key
GraphNodes + edgesNeo4j, Amazon Neptune, ArangoDB, JanusGraphSocial networks, fraud detection, recommendation, dependency graphsTraversal-heavy; not for generic OLTP

Each has a deep page: Key-Value, Document, Column-Family, Graph.

Purpose-Built Databases

TypeProductsBest forNotes
Time-SeriesInfluxDB, TimescaleDB, Prometheus, QuestDB, kdb+Metrics, IoT sensor data, monitoringAppend-heavy, downsampling/retention, time-bucket queries
Vectorpgvector, Qdrant, Milvus, Pinecone, WeaviateRAG, semantic search, embeddings, similaritySee Vector Databases
SearchElasticsearch, OpenSearch, SolrFull-text search, log analyticsInverted index, relevance ranking, aggregations
In-MemoryRedis, MemcachedCaches, real-time counters, pub/sub, rate limitingRAM-bound; persistence is secondary
Multi-modelArangoDB, Couchbase, Cosmos DB, OrientDBMultiple access patterns from one systemAdds complexity; often weaker at each model
GeospatialPostGIS (Postgres), MongoDB GeoLocation queries, mapsOften better as an extension of an RDBMS
LedgerHyperledger Fabric, QLDBAudit trails, tamper-evident recordsNiche

How to Choose

By workload

You needReach for
Transactions, ACID, complex joinsPostgreSQL / MySQL
Scale + SQL + ACID across nodesCockroachDB / TiDB / Spanner
Analytics / BI / aggregationsClickHouse / Snowflake / BigQuery
Cache / sessions / countersRedis
Flexible-schema app dataMongoDB / Couchbase
Write-heavy event/activity logs at scaleCassandra / ScyllaDB
Relationships / graph traversalNeo4j
Semantic search / RAGpgvector / Qdrant / Milvus
Full-text searchElasticsearch / OpenSearch
Metrics / monitoringPrometheus / TimescaleDB

The three-question test

  1. Does the data fit tables with fixed relationships? → SQL.
  2. Do you need horizontal scale and flexible schema? → NoSQL (document/wide-column).
  3. Is there one dominant access pattern? → the purpose-built store that nails it.

Common production stack (the “default” combo)

graph LR
    APP["Application"] --> P["PostgreSQL<br/>(source of truth, ACID)"]
    APP --> R["Redis<br/>(cache, sessions, rate limits)"]
    APP --> E["Elasticsearch<br/>(search)"]
    P --> W["Data warehouse<br/>(analytics via CDC)"]

Most real systems are polyglot — not one database, but the right one per job, with data moving between them (CDC/ETL).

Big Picture Comparison

DatabaseModelSchemaScalingConsistencyQueryBest for
PostgreSQLRelationalFixedVertical (+replicas)ACID strongSQL + joinsGeneral OLTP
MySQLRelationalFixedVertical (+replicas)ACID strongSQL + joinsWeb apps
SQLiteRelationalFixedSingle processACIDSQLEmbedded/tools
ClickHouseColumnar OLAPFixedHorizontalSnapshotSQL analyticsAggregations
SnowflakeColumnar OLAPFixedHorizontal (elastic)SnapshotSQLCloud warehouse
RedisKey-valueNoneClusterEventual/simpleGET/SET + typesCache, counters
DynamoDBKey-value/docFlexibleHorizontalEventual (or strong)Key + secondary indexServerless scale
MongoDBDocumentFlexibleHorizontalEventual (or strong)JSON queriesFlexible apps
CassandraWide-columnFlexibleHorizontalEventual/tunableCQL by keyWrite-heavy scale
ScyllaDBWide-columnFlexibleHorizontalEventual/tunableCQLHigh-perf Cassandra
Neo4jGraphFlexibleVertical (clusters)ACIDCypher traversalGraph analytics
CockroachDBRelational (NewSQL)FixedHorizontalStrong (Raft)SQLScale + ACID
TiDBRelational (NewSQL)FixedHorizontalStrong (Raft)SQLScale + ACID
InfluxDBTime-seriesFlexibleHorizontalEventualFluxMetrics
ElasticsearchSearchFlexibleHorizontalNear-real-timeLucene queriesFull-text
pgvectorVector (Postgres ext)FixedVertical (+replicas)ACIDSQL + ANNRAG at modest scale
Qdrant / MilvusVectorFlexibleHorizontalEventualANN + filtersRAG at scale

Interview Questions

Q: How do you choose between SQL and NoSQL for a new system?

Ask about data shape, relationships, consistency, and scale. Fixed relational data with joins and ACID → SQL. Flexible schemas, horizontal scale, or a specific access pattern → NoSQL. If you need both SQL semantics and scale → NewSQL (CockroachDB/TiDB/Spanner). Most answers should end with “and we’d probably use a combination” — polyglot persistence is the norm.

Q: What are the four main types of NoSQL databases?

Key-value (Redis, DynamoDB — lookup by key), document (MongoDB, Couchbase — JSON documents, flexible schema), wide-column (Cassandra, ScyllaDB, HBase — rows by partition key, write-scalable), and graph (Neo4j, Neptune — nodes and edges for relationships). They trade relational joins/ACID for scale, flexibility, and specific query patterns.

Q: When would you use a columnar (OLAP) database instead of a traditional RDBMS?

When the workload is analytics: aggregations and scans over millions/billions of rows using few columns. Columnar storage reads only needed columns with 5–10×+ compression, enabling sub-second scans that a row-oriented OLTP database would choke on. Use a columnar store (ClickHouse/Snowflake/BigQuery) fed by CDC/ETL from the OLTP source — see OLTP vs OLAP.

Q: What is NewSQL?

NewSQL databases (CockroachDB, TiDB, Spanner) provide the SQL interface and ACID transactions of a relational database with horizontal scaling — typically via consensus replication (Raft/Paxos) and distributed transactions. They target the gap where traditional RDBMS can’t scale and NoSQL can’t give you SQL/ACID. Trade-off: distributed-transaction cost and operational complexity.

Q: Why would you use a graph database?

When relationships are the primary query dimension — social graphs, fraud rings, recommendation, dependency/impact analysis. Traversing multi-hop relationships is natural in a graph DB (Neo4j Cypher) but requires expensive recursive joins in SQL. For generic OLTP with occasional relationships, an RDBMS with indexes is usually simpler.

Q: What is polyglot persistence?

Using multiple database technologies in one system, each chosen for its workload: Postgres for source-of-truth OLTP, Redis for caching, Elasticsearch for search, a warehouse for analytics, a vector DB for RAG. Data moves between them via CDC/ETL. The trade-off: more moving parts, eventual-consistency boundaries, and operational cost — justified when each store pays for itself.

References

  • DB-Engines ranking (popularity by category) — https://db-engines.com/en/ranking
  • Martin Fowler: NosqlDefinition / PolyglotPersistence — https://martinfowler.com/bliki/PolyglotPersistence.html
  • AWS: Choosing an AWS database service — https://aws.amazon.com/products/databases/
  • Microsoft: Choose a data store (Azure architecture) — https://learn.microsoft.com/en-us/azure/architecture/guide/technology-choices/data-store-overview