DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Event-Driven Microservices: How Kafka and RabbitMQ Power Scalable Systems
  • APIs for Logistics Orchestration: Designing for Compliance, Exceptions, and Edge Cases
  • Event Sourcing 101: When to Use and How to Avoid Pitfalls
  • Using Event-Driven Ansible to Monitor Your Web Application

Trending

  • Modern Test Automation With AI (LLM) and Playwright MCP
  • Apple and Anthropic Partner on AI-Powered Vibe-Coding Tool – Public Release TBD
  • Secrets Sprawl and AI: Why Your Non-Human Identities Need Attention Before You Deploy That LLM
  • Distributed Consensus: Paxos vs. Raft and Modern Implementations
  1. DZone
  2. Data Engineering
  3. Data
  4. Ordering in Event-Sourced Systems

Ordering in Event-Sourced Systems

Ordering is extremely important in Event-Sourced Systems. Let's find out how what guarantees Event Store can provide in terms of ordering!

By 
Milan Savic user avatar
Milan Savic
·
Aug. 10, 23 · Analysis
Likes (1)
Comment
Save
Tweet
Share
1.8K Views

Join the DZone community and get the full member experience.

Join For Free

In a utopian world, Event Store will write events to the disk in the same order they have happened in real life. However, computers are not so good at observing things by themselves (yet); we (still) must tell them what to do. Hence, we must provide a way to deterministically order events inside the Event Store.

A naive approach would be to attach a timestamp to each and every event and use it for ordering. The reason for its naiveness lies in the difficulty of synchronizing clocks across different machines in a distributed system. As a consequence, we mustn't rely on timestamps for ordering.

Since we also cannot impose ordering of events sent by different Event Store clients among them, the only thing left is to accept the ordering of received events by the Event Store. In other words, the order in which Event Store receives events is going to be the order of events written to the disk. This means that two events could have happened in one order in reality, but they are written in a different order on the disk. And this is acceptable as long as these two events are not related (not in conflict). 

Each and every Event Store must provide a mechanism to detect conflicts between appends* (append conflict) and a way to resolve the append conflict. One of the solutions to this challenge is the Dynamic Consistency Boundary. 

To give an example, events for two different bank accounts are not related and could be written in one order that differs from the order that happened in reality. However, if they are related to the same bank account, they must be written in the exact same order (see Diagram 1). In the diagram, two clients want to withdraw money from the green bank account. In order to maintain consistency, the Event Store must detect this situation as a conflicting one.

Conflicting Appends

Diagram 1

Once event transactions** are received, the Event Store will accept the receiving order as THE order, detect the conflicting transactions, resolve append conflicts, and write them to disc. Voila! The Event Store determined the order, and from this point on, whenever we read events from the Event Store, they are always going to be read in the same order.

When there is only one replica of the Event Store in the system, the order of events on that instance is the total order of events in general. 

Clustered Event Store

Of course, surviving in the wild with a single replica of the Event Store is maybe too brave. Therefore, Event Store must be clustered. Each node in the Event Store cluster must have a copy of all events - events are replicated across the cluster. In cases when some nodes in the cluster are down, others can serve events. What does this mean for ordering? Are we able to preserve the total order of events on all nodes in a cluster?  

The short answer is yes! Luckily for us, there are protocols that resolve this kind of issue. They are called Consensus Protocols; one typical representative is RAFT. Usually, protocols that guarantee the total order have a single node that acts as the leader of the cluster, and it replicates data across the cluster. This means that all appends must go through the leader. Essentially, we are back to the previous scenario, where we had only one replica of the Event Store, and the total order was guaranteed. Now, this single replica (the leader) replicates events to other nodes, and we have the total order on all nodes (see Diagram 2). You may ask, what happens if the leader fails? And that is a completely valid question; luckily (again), this situation is covered by the very same protocols; in most cases, there is an election that results in a new leader.

Event Store Cluster

Diagram 2

Another valid question would be, isn't the leader a bottleneck? The answer is yes; the leader is the bottleneck. However, unlike previous situations, we are not so lucky; we just have to accept this fact. 

Although the leader is the bottleneck for appends, it is not the bottleneck for reads required for (re-)building View Models. I'll leave it up to You to answer why that is. (Hint question: do we need to reach the consensus while reading events for View Models?)

Recap

The total order of events in event-sourced systems provides ease with designing and reasoning about systems. In a single-replica Event Store deployment, the total order is not difficult to achieve. The easiest way to provide the total order of events in a clustered Event Store is to use single-leader Consensus Protocols. However, this approach comes with a price; the leader is the bottleneck for append operations.  

* Append is an Event Store operation that accepts events and stores them durably on disk in an append-only fashion.

** An Event Transaction is a single append operation with one or more events that are stored in an ACID manner.

Diagram Event store Requirements engineering clustered Event systems

Published at DZone with permission of Milan Savic. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Event-Driven Microservices: How Kafka and RabbitMQ Power Scalable Systems
  • APIs for Logistics Orchestration: Designing for Compliance, Exceptions, and Edge Cases
  • Event Sourcing 101: When to Use and How to Avoid Pitfalls
  • Using Event-Driven Ansible to Monitor Your Web Application

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!