Polyglot Persistence
Several data stores · Right store per job
Written by Darjus Kazakevic · reviewed
Several kinds of store in one system, each picked for the job it does best.
Polyglot persistence means one system uses several kinds of data store, each for the work it suits. Orders live in a relational database, product search runs on a search index, sessions sit in a key-value cache, and reports run on a column store. Each store is fast at its own job and poor at the others.
The cost is everything between the stores: data copied from one to another, gaps while a copy lags, and more systems to back up, patch and watch. For many products, one Postgres database with its JSON, full-text and vector features covers every need for years. Reach for a second store when you can name the query the first one cannot serve.
In essence — Add a second kind of store only for a workload the first cannot serve, keep one source of truth for each piece of data, and feed every other copy from it.
When to reach for it
Reach for it when
- A workload the main database serves badly: ranked text search, huge analytic scans, sub-millisecond hot reads
- Separate services or teams that each own their data and choose its store
- Scale or cost limits one kind of store cannot meet, with measurements to show it
Reconsider when
- A small team: every kind of store is one more thing to run and learn
- The main database can still do the job with an index, an extension or a replica
- Data that must change atomically across stores, in one transaction
Key concepts
- System of record vs derived store
- For each piece of data, one store holds the truth. Every other store holds a derived copy: built from the source, a little behind it, and safe to drop and rebuild.
- Sync paths
- How a change reaches the derived stores. There are three common ways, from weakest to strongest.
- Ownership
- Each store has one owning service or team that writes to it, runs it and decides its schema. Others read through the owner's API or a published feed, never by writing to the store directly.
- Operational surface
- Each kind of store brings its own backups, upgrades, monitoring, failover, security patches and on-call know-how. The bill grows with the number of kinds you run, more than with the number of instances.
- One Postgres first
- A single relational database with extensions covers documents (jsonb), full-text search (tsvector), vectors (pgvector) and time series (partitioned tables). It is rarely the best at each, often good enough, and keeps one transaction around all of them.
Design rules
- Name the query before adding a store
- Write down the query or load the current store cannot serve, with numbers: latency, volume, cost. Try an index, an extension or a replica first. Add the store only if those fail.
- One source of truth per entity
- Each entity is written to exactly one store. Every other copy is fed from that store, so when copies disagree, there is one answer to which is right.
- Never dual-write from request code
- A crash between two writes leaves the stores disagreeing, and nothing notices. Commit the change with an outbox event, or capture it from the log, and let a worker update the copies.
- Give every store an owner and a restore drill
- A store with no owner goes unpatched and unwatched. Before adding one, name who runs it, how it is backed up, and how a derived copy is rebuilt from scratch.
Common pitfalls
- Dual writes from request code, so the search index slowly drifts from the database and no one can say by how much.
- No clear source of truth, so the cache, the index and the database each show a different current price.
- Adding a store for one feature, then keeping it for years after the feature is gone, unowned and unpatched.
- Backing up the main database but not the store that turned out to hold the only copy of some data.