Skip to content
mentoor

Topic 2 of 5 · 4 min read

When document and key-value stores fit

Goal: Decide whether an entity belongs in a document store, a key-value cache or relational tables, and state what you give up.

Use a document store when you read and write a whole entity as one unit, and a key-value store when you look things up by one key and can lose them. Both are fast because they refuse to do something a relational database does. Name that refusal before you accept the speed.

Check yourself · 1 of 3

A product page and its 5 fixed variants are always read together and rarely change. Which design fits a document store best?

Choose one answer

Document stores

A document database such as MongoDB stores nested JSON-like records. The main design choice is embed or reference. Embed the order lines inside the order document when you always load them together and they belong to one order. Reference by id when the child is shared, grows without bound, or is read alone. Documents have a size cap per record, so an ever-growing array, such as every comment on a viral post, must be referenced.

You give up cheap joins across collections and the habit of leaning on foreign keys. MongoDB does support multi-document transactions, but designs that need them often should have been relational. If your queries start as "orders joined to customers joined to products, grouped by region", you are rebuilding a relational model by hand.

Key-value stores

Redis stores values under keys in memory, with types such as strings, hashes, lists and sorted sets. It suits sessions, rate-limit counters, leaderboards, queues and caches. Keys can expire automatically (a TTL), which is the point of a cache and a trap for anything you cannot afford to lose.

You give up queries. You cannot ask "all sessions for user 7" unless you built a second key for it. Persistence is configurable but is not the same as a primary database with full durability, so treat Redis data as rebuildable unless you have configured and tested it otherwise.

Before and after

Weak: "Store users, orders and payments in Redis because it is fast." Fixed: orders and payments stay in the relational database, where transactions and constraints protect money. Redis holds the session by token (TTL 30 minutes) and a cached product page that can be rebuilt on a miss.

Choosing

Entity Fits Gives up
Product catalogue with varying attributes, read whole Document Cross-collection joins
Session by token, expires Key-value Querying by anything but the key
Payments ledger Relational Nothing needed here

What goes wrong

  • Documents used as a relational database. Many references, no joins, the application merges data in code.
  • Unbounded embedded arrays. The document hits its size limit in production.
  • A cache treated as storage. An eviction or restart silently deletes data nobody can rebuild.
  • Choosing "schemaless" to skip modelling. The schema moves into application code, where it is harder to enforce.

Examples

Embed or reference

A blog post has 3 authors' names and 40,000 comments. Authors are few and read with the post, so embed them. Comments grow without bound and are paged, so store them in their own collection with a postId and an index. A single document holding all 40,000 comments would hit the document size limit.

Cache that became storage

A team keeps the shopping cart only in Redis with a 1-hour TTL. A customer returns after lunch and the cart is gone. If the cart matters, store it in the database and use Redis only to speed up reads of it.

Key points

  • Embed data you read and write together; reference data that is shared, unbounded or read alone.
  • A document store trades cheap joins and strong constraints for loading a whole entity in one read.
  • A key-value store trades queries for speed: you can only ask for a key, so plan any second lookup as another key.
  • Treat Redis data as rebuildable unless you configured and tested durability for it.
  • Money, inventory and anything needing multi-row constraints usually belongs in relational tables.

Common mistakes

  • Putting data that needs joins and constraints in a store that has neither.
  • Letting an array grow inside one document without a limit.
  • Forgetting that cache keys expire or get evicted.
  • Calling a store schemaless and skipping the design of its documents.

Check yourself · 2 of 3

What does a key-value store such as Redis give up compared with a relational database?

Choose one answer

Check yourself · 3 of 3

Where should a payments ledger with balances that must never drift live?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic