What I wish I had known when starting with Event Sourcing – part 2, consistency
Popular assumptions about the consistency guarantees of event sourcing, set against the actual facts
The first part of the series covered some of event sourcing’s big advantages: time traveling and being able to answer practically any analytical question about the service, given the events stream. This time, let’s start with a few widespread assumptions about the consistency guarantees of event sourcing and then compare them with the facts.
What’s the story with consistency
Eventual consistency often gets mentioned in the same breath as CQRS, and event sourcing in particular. People tend to assume that event sourced systems are by definition eventually consistent: a write side appends events to storage, while a completely separate read side consumes those events and builds individual read models per use case, using whichever storage fits the requirements and the situation. This is certainly not a bad architecture – it brings benefits such as a database type tailored to particular reads, isolation, scaling reads separately from writes, and so on. But, as we know, there is no choice that is best everywhere; it all comes down to tradeoffs – which less important qualities you’re prepared to sacrifice to get more important ones. Still, is eventual consistency the only way? What if, for whatever reason, you don’t want to or can’t deal with it?
In reality, eventual consistency, and a separate read database in particular, is by no means mandatory for event sourced systems (or for the CQRS approach as a whole). Not only can you skip separate databases, you can also make your read side strictly consistent when needed. And once more, there isn’t one best way to do it; instead, there are various options depending on what you’re designing for.
On the topic of eventual consistency: you can run into it even with a single database handling both reads and writes! If writes go to a primary node (or leader, coordinator, whatever you call it) while reads come from one of the asynchronously updated replicas – there you go, your reads are now eventually consistent, despite having just one logical database.
Let’s go through a range of possible solutions to see how strict consistency can be achieved when required, what it costs, and how we can gradually move toward eventual consistency. We’ll focus solely on consistency on the read side and how to handle it, assuming that writes (command handling) are always consistent – for instance, a single ACID-compliant node that alone handles write operations. Hopefully we’ll get to scaling writes past that limitation in a later part.
Reading on demand from the event stream
From the way we handle commands, we already know how to obtain the latest state: first replay and apply every event from the relevant stream, then process the command using that state. If you need replies to always reflect the freshest state, you can do precisely the same on the query side: answer queries with state built on demand at query time from the event streams in your primary storage – the very database you write events to. This way, you always work with the freshest state, reconstructed from the latest events stream.
Naturally, this approach has its pitfalls. In practice, it only works when queries cover a single aggregate or just a handful of them, and when their event streams aren’t too long (what these mean exactly depends on your specific requirements). Otherwise, it can put heavy load on both the database and the application, since they have to fetch many events, replay them, and compute all the states before responding. Consequently, such queries may perform poorly, and running many of them simultaneously carries a real risk of degrading write performance.
A fairly classic example is a BankAccount entity that keeps track of the account balance. If you need balance queries to always be current for some reason, this is one way to achieve it – just keep all the tradeoffs in mind.
Moreover, the requirement that queries (particularly those from the user interface) reflect the most current state is actually rather weak – after all, by the time a response gets to the client, the data in it may already be outdated, because it might have changed in the meantime.
Transactional read models
There may be a better name for this, but hear me out until I’ve explained it. A downside of the previous approach is that every query request requires replaying all the events. Sure, there are snapshots, but even then you still have to replay the events that came after the latest snapshot. Depending on the query, the number of entities involved, the size of their streams, and so on, this can noticeably hurt performance and latency. Ideally, we’d keep that data precomputed while still updating it the moment the entity’s state changes (when new events are added). Sounds like an ordinary read model, doesn’t it? It is, but with a small twist. The “classic” read model approach is considered eventually consistent because a separate process maintains it; here, instead, we update the read model within the same transaction in which we save our events, and we serve queries from this consistent read model.
The clear benefit is that read models are always strictly current, with no need to replay events for queries. Going back to the BankAccount example, you could have, say, an account_balances table that holds the current state of accounts and always reflects it accurately. You also get more freedom in designing read models – the data can aggregate many entities without having to load all their events each time, and so on.
Downsides? Naturally there are some; here are a few. To begin with, the solution only works with event storages that offer strong transactional semantics. Furthermore, expanding the transaction from a simple event insert to include extra updates can cost some performance, particularly in write-heavy scenarios. If you choose this path, it’s best to restrict it to critical read model updates only. The more operations you pack into a single transaction, the longer the commit takes and the more likely something goes wrong. In addition, whether a command is handled successfully and its events stored now depends entirely on your read model logic being correct and not panicking. A bug in your projection update logic can roll back the whole transaction, which in effect makes command processing and event storage fail. You also lose the ability to scale the write and read sides independently. Finally, note that not every event sourcing framework is flexible enough to support transactional read models and save projection updates along with persisted events – Akka/Pekko’s persistence extension, for example, doesn’t allow it.
Serving reads from DB replicas
At this point we step into eventual consistency territory, where nothing guarantees that reads are always in sync with the write side. As mentioned earlier, there are at least a few variants to pick from.
In read-heavy systems, you may want to scale reads aggressively without scaling writes, yet the “classic” asynchronous read model may be off the table because the consistency delays are unacceptable – for instance, your read models are refreshed by periodically polling the eventstore for new events, which inherently adds too much lag. If some delay is fine but you want to keep it as small as possible, there’s a variant of the previous approach: you still update projections transactionally while handling commands, but you serve reads from dedicated database replica nodes. Replication in today’s database engines is typically fast enough for this. Although eventually consistent, this approach tries to shrink the inconsistency window as much as possible.
There’s also a more dynamic variant. For a while after a write (usually a few seconds, to be on the safe side), you keep serving strictly consistent queries from the primary node, and once the replicas have caught up after those seconds, you switch to them. This is more complex on the application side, since you must track replication state and decide when to switch, but in practice it can narrow the inconsistency gap (barring major failures), combining strict consistency with read scalability in certain scenarios.
Checking the version on read
Another interesting way to soften some of the downsides of eventual consistency is to use version checks at query time. This requires clients to know about the mechanism and cooperate with our backend – here’s how it goes. After each successful command, we send the client the entity’s new version number. The version number is typically a monotonically increasing sequence number, incremented by 1 whenever a new event is saved. It also helps detect conflicts on the write side, but here we just hand it back to the client, saying something like: I’ve processed your command and your BankAccount entity is now at version 42. From then on, whenever the client queries the entity’s state, it sends along the most recent version number it knows (in this case, 42). If the read side hasn’t yet caught up with the event carrying version 42 (say it has only seen up to 41), we detect this and respond with, for example, a Retry-After header until it catches up and the versions match, at which point a proper response is returned.
This works well when reads cover one or just a few entities whose version numbers are easy to track. We could try using some sort of global sequence number across all events in all streams, but then a temporary inconsistency in one entity would affect queries on another, possibly unrelated, one. Occasionally that’s exactly what you want – for instance, with aggregate reports where you track consistency across all streams – but then, besides a single entity version, you’d also need to return the matching “global” sequence number. Whether this is useful and worth the effort depends on your particular use case.
Embracing eventual consistency
It’s well known that eventual consistency is your ally once you need to scale past a certain point. Given enough load, performance demands, and data volume, you can hit the limits of a single strictly consistent database (or of fully synchronous replication). Admittedly, these are problems everyone would love to have, though most of us never actually do. I’m assuming they’re genuine needs arising from the project’s growth and success, not us frantically overengineering for no good reason.
Eventual consistency brings many advantages, starting with scaling reads and writes independently by fully isolating the two sides in terms of performance, availability, and technology, and ending with the possibility of building any number of read models, each dedicated to a specific task with its own storage.
At times it looks as though strict consistency is an absolute must, but asking stakeholders or product owners the right questions while discovering the business process can show that this isn’t really so and that eventual consistency would work just as well. Perhaps embracing it could even open up interesting business opportunities? Conversely, instead of pursuing strict consistency at all costs, it may be cheaper and sufficient from a business standpoint to handle inconsistencies by resolving them manually at the business level. In an online shop, for instance, you might consider cancelling customer orders when an item is out of stock and sending a gift card instead. Depending on how frequently this happens, it may be the right business decision.
Sometimes, though, looking at the same problem from a different angle reveals ways to use eventual consistency internally while remaining consistent toward the outside world.
To show this, let me take another classic example: guaranteeing that certain data is unique across the domain. For our BankAccount entities, we want owners to have a secondary account number linked to each account (for example, the owner may only withdraw and transfer to this account), and for some reason this secondary account number must be unique among our users – no two users may be assigned the same secondary account number.
Let’s set aside our inner voice asking whether the domain is modeled correctly or whether something smells here (hint: it probably does) – it’s only an example, so bear with me for a moment.
In a CRUD world, we’d likely create a secondary_accounts table linked to accounts by id, put a unique constraint on the secondary_account_number field, and be done. Every insert or update to the secondary_accounts table would be checked for uniqueness at the DB level, and the strict consistency aspect of ACID guarantees would cover us completely.
With event sourcing in the picture, things look rather different. Our consistency boundaries now surround an individual account, and since command handling saves nothing but the resulting events, the event streams alone don’t let us enforce global uniqueness at the command handling level.
Instead, we can split the process of a user requesting a new secondary account number into two steps. First, we accept the command (assuming it is syntactically valid and all other conditions hold) and emit an event such as NewSecondaryAccountNumberAssigned, putting the account into a “change pending” state. Then, on the read side, there’s an ordinary database table with a unique constraint like the one described earlier – secondary_accounts – into which the read side, listening for NewSecondaryAccountNumberAssigned events, tries to insert the account numbers.
Handling the initial command and processing its event on the read side
Now uniqueness is easy to handle: if inserting the secondary number succeeds, it means nobody has taken it yet, so we send a command such as ConfirmSecondaryAccountNumberChange (or, in a more notification-like style, SecondaryAccountNumberAvailable) back to the entity, which handles it by emitting SecondaryAccountNumberChanged. From that moment the secondary account number counts as valid, unique, and ready for use.

The insert succeeded, no duplicate. Send an OK command/message
If, on the other hand, the insert fails due to a unique constraint violation – meaning the account number is already in use – we send a RejectSecondaryAccountNumberChange command (or, again as a notification, SecondaryAccountNumberDuplicated), which leads to an event such as SecondaryAccountNumberChangeRejected.Here we cancel the pending change in the entity and most likely let the user know about it.

The insert failed, the unique constraint was violated. Send a NOT OK command/message
This projection can also serve as an initial uniqueness check, but because of eventual consistency the whole process described above still has to remain, since such a check can only reduce (not fully rule out) the likelihood of duplicates – for example, by improving the UX and stopping users from submitting account numbers that are already taken.
Yes, it’s quite a dance and a fair amount of code, since this entire process has to be implemented, but it’s available if you need it. Should you apply it blindly and everywhere, just like event sourcing or practically any other tool? Definitely not. It’s all about tradeoffs – you lose something in one place to win something in another. Know the options available to you and make well-informed decisions grounded in the real drivers you’re facing.
One last point: if all the events we emitted above just to handle a simple uniqueness case make you uncomfortable, remember that they don’t all need to be public. Event sourcing is a local approach to managing state, and how you communicate with the outside world is up to you. Perhaps you want other systems to learn about every single attempt to change the secondary account number, or perhaps you only want to announce the change once you’re certain it has actually been applied. Public and private events probably deserve their own chapter of the series, which I hope to write soon.
Recap
As shown above, the claim that event sourcing (and CQRS) is entirely and exclusively about eventual consistency doesn’t hold. It certainly makes eventual consistency possible and takes full advantage of it in the right situations, but it doesn’t require it. We’ve seen that event sourced systems can be built with strict consistency when that’s needed. Once again, the tools are there, and it’s up to you to understand the strengths and weaknesses of each and pick wisely.
In the next part, we’ll explore the storage options available for event sourced systems – which one to pick and when. Stay tuned!