CQRS and database per service: splitting reads, writes and owners
Vinnie on the top rung of the scaling ladder: CQRS separates the write model from read models built for each screen, often in their own store, kept up to date by events and eventually consistent; event sourcing, briefly; and a database per service in microservices, with what it costs: no joins or transactions across services. Then the whole ladder, recapped.
Diagrams in this video · 2Free in archhy
The diagrams, full size
Copy any of them into archhy, free, and change it for your own system.
After this lesson you can
- Explain CQRS and why the read side is eventually consistent
- Build a read model from events, safely
- Say what event sourcing adds, and what it costs
- Weigh a database per service against what you give up
Transcript
CQRS and database per service: splitting reads, writes and owners
Vinnie here. The top rung of the ladder. CQRS, command query responsibility segregation. And a database per service. Both are about splitting. CQRS splits reads from writes. A database per service splits your data by who owns it. Both are powerful. Both cost a lot. My nose says, only climb this high if you need to.
The problem
Most apps use one model for everything. The same tables take the writes, and answer the reads. Writes want rules, validation and transactions. Reads want data shaped for a screen, fast, with no joins. As an app grows, those two pull apart. And reads often far outnumber writes.
CQRS, step by step
Here's CQRS. The simplest form keeps one database. This one gives reads their own store. One. Commands, like place order, go to the write model. It checks the rules, and saves the change in a transaction. Two. Each change is published as an event. Order placed. Save the event in the same transaction, in an outbox table, so a crash can't lose it. Three. A handler listens, and updates a read model, shaped for one screen. You can have several, even in different databases. Four. Queries only read. They never change data. A read model shaped for the screen avoids joins. Five. The catch. The read side is updated after the write, so it's eventually consistent. A user can save something, and not see it straight away. Sound familiar? Replica lag again. Same fix: send that user's reads to the write side for a moment.
A read model, updated by an event
Here's one read model, in PostgreSQL. When an order placed event arrives, insert a row for that customer. One order, and what they spent. Customer ID is the primary key, so on conflict can find the row. If the row's already there, on conflict, add to it instead. The account page now reads one row. No joins. One warning. Events are usually delivered at least once, so one can arrive twice, and this snippet would count it twice. Record each event's ID in the same transaction, and skip repeats.
Event sourcing, briefly
Event sourcing is a separate pattern, often paired with CQRS. You store every event, in an append-only log, and get the current state by replaying them. Lose a read model? Replay the events, and build it again. But it changes how you store, query and evolve your data. Microsoft's own guidance says most systems don't need it.
A database per service
Now the other split. A database per service. One. In microservices, each service owns its data. No other service reads its tables. Two. If payments needs an order, it asks the orders service, through its API, or it listens for its events. Three. The cost. No joins across services, and no single transaction across them. Keeping them consistent becomes your job. Two patterns to look up: sagas, and the transactional outbox.
How the databases help
How do databases help? Many can publish their own changes. MongoDB has change streams, on a replica set or sharded cluster. PostgreSQL has logical decoding, once you turn it on. MySQL writes every change to its binary log. Tools like Debezium turn those changes into events, so a read model can follow along.
In the cloud
The managed databases publish changes too. On AWS, DynamoDB Streams records every item change, in order for each item, and keeps it for twenty-four hours. A Lambda function can build a read model from it. On Azure, Cosmos DB has a change feed, on by default. To see deletes too, you need its all versions and deletes mode, which needs continuous backup. On Google Cloud, Spanner has change streams, kept from one to thirty days. And Datastream captures changes from MySQL, PostgreSQL and others.
When not to climb this high
When not to? When your app is mostly create, read, update and delete. Microsoft's guidance says the same. Simple domains don't need CQRS. And a database per service pays off when services really need to be loosely coupled. Otherwise, you run more databases, and a lot of plumbing to keep them in step.
The whole ladder
Let's recap this rung, and the whole ladder. CQRS splits writes from reads, and the reads catch up eventually. A database per service splits data by owner, and gives up joins and transactions across services. And the ladder. Indexing, query optimization, connection pooling, caching, read replicas, materialized views, sharding, and CQRS. Climb only as high as you need. Every rung costs more to run, and more to get wrong.
The full article, and what's next
That's the ladder, bottom rung to top. Next, go back through the series, to whichever rung your app needs. Thanks for sniffing along. If something's slow, you know where I am. The Alpaca Mango channel. Off you go.






