Common storage types

Common storage types An architecture diagram generated by Archify. One read · user 7, and her orders · Architecture component One read user 7, and her orders Postgres · rows on disk, one schema · SQL: rows in two tables, joined on the way out · 1.2 ms Postgres rows on disk, one schema 1.2 ms users · primary key id, one row each · SQL: rows in two tables, joined on the way out users primary key id, one row each orders · index on user_id · SQL: rows in two tables, joined on the way out orders index on user_id One row set · no orders, no rows, because JOIN · SQL: rows in two tables, joined on the way out One row set no orders, no rows, because JOIN MongoDB · one document per person · Document: one read, and a shape you own · 0.6 ms MongoDB one document per person 0.6 ms users collection · orders nested in the document · Document: one read, and a shape you own users collection orders nested in the document the same collection · no orders field, no null column · Document: one read, and a shape you own the same collection no orders field, no null column Redis or Valkey · keys in RAM, no planner · Cache: a copy that expires · 0.1 ms Redis or Valkey keys in RAM, no planner 0.1 ms user:7 · a copy of the answer, TTL 60s · Cache: a copy that expires user:7 a copy of the answer, TTL 60s Expired · its own deadline, not memory pressure · Cache: a copy that expires Expired its own deadline, not memory pressure walks the key id 7 picks these rows one seek or this shape hit 60 seconds later SQL: rows in two tables, joined on the way out Document: one read, and a shape you own Cache: a copy that expires Legend The read What comes back Where the record sits

SQL

  • • Rows on disk, one schema, enforced. Joins are first-class, not cheap
  • • Pick a user with no orders and this returns 0 rows. To keep her, that is LEFT JOIN
  • • The join runs again on every read, and always answers from the current rows

Document

  • • One read on the key, no join. In DynamoDB the same request is GetItem
  • • Two shapes in one collection, no migration and no null column
  • • Nothing stops a third document spelling it order

Cache

  • • Reach for it when the same answer is being computed over and over
  • • Expired is not evicted. Eviction is memory pressure and allkeys-lru picking a victim you did not choose
  • • Gone on restart is a deployment choice. Redis and Valkey both persist, RDB or AOF