STACK
Ch 09 · Databases & Storage
Chapter 09 · Where state lives

Databases & Storage — ten families, one matrix

Picking a DB is the most consequential architectural decision you make. This is the family map, the consistency vocabulary, and the schema/migration discipline that keeps the lights on.

9.1 · Ten families at a glance

FamilyExamplesPick when
OLTP relationalPostgres, MySQL, Aurora, Neon, PlanetScaleDefault for transactional apps.
KV / cacheRedis, Valkey, Dragonfly, KeyDBSessions, hot lookups, rate limits, queues.
DocumentMongoDB, CouchDB, FirestoreFlexible schemas; one-doc reads at scale.
Wide-columnCassandra, ScyllaDB, BigTable, DynamoDBMassive write throughput; predictable row reads.
OLAP / columnarClickHouse, DuckDB, Snowflake, BigQuery, DatabricksFast aggregations on billions of rows.
SearchOpenSearch, Elasticsearch, Typesense, MeilisearchFull-text + faceted search.
Vectorpgvector, Qdrant, Weaviate, Pinecone, MilvusANN search on embeddings.
Time-seriesTimescaleDB, InfluxDB, VictoriaMetrics, QuestDBMetrics, telemetry, sensor data.
GraphNeo4j, Memgraph, Apache AGE, Amazon NeptuneTraversal-heavy queries.
Object / blobS3, R2, GCS, Azure Blob, MinIOFiles, media, backups, lakehouse layer.

9.2 · OLTP relational — Postgres is the default

  • Postgres: extensions for JSON, full-text, geospatial (PostGIS), time-series (Timescale), vector (pgvector), graph (AGE), HLL. One DB, many superpowers.
  • MySQL / MariaDB: simpler ops; PlanetScale / Vitess for horizontal sharding.
  • Managed: Aurora (AWS), Cloud SQL (GCP), Neon (serverless branching), Supabase.
  • SQLite: surprisingly fine for small apps; Litestream + Fly = edge SQLite.

9.3 · KV · document · wide-column

FamilyRead/write trade
KV (Redis/Memcached)Sub-ms RW; in-memory; tiny payloads.
Document (Mongo)Embed-or-reference design; rich queries; transactions per shard.
Wide-column (Cassandra/Scylla)Heavy write throughput; design schema around query patterns (no joins).

9.4 · OLAP / analytics

  • ClickHouse — columnar, parallel, ridiculous aggregation speed.
  • DuckDB — embedded; "SQLite for analytics"; query Parquet directly.
  • Snowflake / BigQuery / Databricks SQL — managed warehouses.
  • Iceberg / Delta / Hudi — open table formats on S3/Parquet (lakehouse).
  • OpenSearch / Elasticsearch — full-featured; Lucene-based; powerful but heavy.
  • Typesense / Meilisearch — typo-tolerant, fast, dev-friendly.
  • Postgres FTS + tsvector — surprisingly OK for < 10M docs.
  • Hybrid search: combine keyword (BM25) + vector with RRF (see AI Codex Ch 9).

9.6 · Vector — the ANN tier

IndexTrade-off
Flat (brute-force)Exact, slow at scale.
IVFCluster + probe nearest; fast, slight recall loss.
HNSWGraph-based; sub-ms at huge scale. The default.
PQ / SQCompressed vectors; trades memory for accuracy.

9.7 · Time-series

TimescaleDB, InfluxDB, VictoriaMetrics, QuestDB. Continuous aggregates, downsampling, retention policies. For metrics specifically — Prometheus, Mimir/Cortex/Thanos for long-term.

9.8 · Graph

Neo4j (Cypher), Memgraph, Apache AGE, Neptune (Gremlin + openCypher), TigerGraph. Use when traversal is the core query pattern (recommendations, fraud rings, knowledge graphs).

9.9 · Object & files

S3 / R2 / GCS / Azure Blob / MinIO. Plan for: lifecycle rules, presigned URLs, KMS encryption, replication, multi-part uploads. R2 has zero egress fees — material at scale.

9.10 · NewSQL & distributed SQL

SystemNotes
Google Spanner / AlloyDBGlobal strong consistency via TrueTime.
CockroachDBSpanner-style OSS; HLC clocks.
YugabyteDBPostgres-compatible distributed.
TiDBMySQL-compatible; row + columnar (TiFlash).
FoundationDBOrdered KV; rock-solid; the substrate under Snowflake, Apple's iCloud.

9.11 · ACID, MVCC, isolation levels

LevelPreventsStill allows
Read Uncommitted—Dirty reads possible
Read CommittedDirty readsNon-repeatable reads, phantoms
Repeatable Read+ Non-repeatablePhantoms (classic); Postgres MVCC blocks these
SnapshotDirty, non-repeatable, phantoms via MVCCWrite skew (use SERIALIZABLE)
SerializableAll anomalies—

MVCC: each transaction sees a snapshot; Postgres tracks tuple versions, vacuum reclaims dead ones. Long-running transactions cause bloat — kill them.

9.12 · Schema & migrations

  1. Migrations are code. Version them, review them, test them.
  2. Every migration is online + reversible.
  3. Add nullable column → backfill → flip NOT NULL.
  4. Renames: add new → write to both → backfill → switch reads → drop old.
  5. Postgres: CREATE INDEX CONCURRENTLY; watch lock_timeout, statement_timeout.
  6. Tools: Atlas, Flyway, Liquibase, Prisma Migrate, Alembic, Goose, sqlx, golang-migrate.

9.13 · Multi-tenant patterns

PatternIsolation
Shared schema, tenant_id columnLogical; enforce with row-level security (Postgres RLS).
Schema per tenantStronger; migration cost scales.
DB per tenantStrongest; operationally heavy.
Hybrid (shards of tenants)The pragmatic large-scale choice.

9.14 · Self-check

Q1 · 5B-row analytics aggregations need…
Postgres
ClickHouse
Redis
MongoDB