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

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

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Constructing Real-Time Analytics: Fundamental Components and Architectural Framework — Part 2
  • Resilient Kafka Consumers With Reactor Kafka
  • Applying Kappa Architecture to Make Data Available Where It Matters
  • Building an Enterprise CDC Solution

Trending

  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • A Guide to Developing Large Language Models Part 1: Pretraining
  • Rethinking Recruitment: A Journey Through Hiring Practices
  • Fixing Common Oracle Database Problems
  1. DZone
  2. Data Engineering
  3. Big Data
  4. Apache Kafka Is Not for Event Sourcing

Apache Kafka Is Not for Event Sourcing

Apache Kafka is great for delivering messages, but if you're using it for event sourcing—think again. Learn about why Kafka isn't suitable for event sourcing.

By 
Jesper Hammarbäck user avatar
Jesper Hammarbäck
·
Aug. 28, 20 · Opinion
Likes (8)
Comment
Save
Tweet
Share
20.4K Views

Join the DZone community and get the full member experience.

Join For Free

Apache Kafka is a cool product, but if you are thinking about using it for event sourcing you should think again.

Kafka is a great tool for delivering messages between producers and consumers and the optional topic durability allows you to store your messages permanently. Forever if you’d like.

So, if your messages are events you can use Kafka as an event store or an event log, but it really isn’t a suitable tool for event sourcing.

Here’s why.

Loading Current State

When a service receives a command requesting a state change of an event sourced entity, we first need to recreate the current state of that object. We do that by loading all previous events for that particular entity ID from our event store and then re-apply them on our object, fast-forwarding it into its current state.

Loading events for a particular entity like this is not easy in Kafka. Topics are usually centered around and partitioned by entity types like "Orders", "Payments" or "Notifications" so we would have to go through all events for all "Orders" and filter them by their ID to load up a single "Order" entity. Although this is POSSIBLE, it’s not very practical.

One alternative would be to have one topic per entity but then we would probably end up in a situation where we have thousands of topics and on top of that, the subscribing services downstreams need a way to automatically discover the newly created topics for each new entity. Again - not very practical.

Consistent Writes

When our entity’s state has been recreated, it’s time to execute the business logic requested by the incoming command. If the business logic fails we return an error to the client but if it succeeds a new event is emitted. In that case we must be able to save the new event to our event store with a guarantee that no other event has been stored for this particular entity ID in the meantime, or we would risk breaking the consistency of our domain objects.

OCC to the Rescue

One way to guarantee write consistency is by utilizing the event store’s optimistic concurrency control. A proper event store provides a way for the user to say "save this event only if the version of the entity is still x". Kafka does not support this and the suggested workaround from the experts in the field seems to be to put a "database" in front of it all to provide a consistency point. While this suggestion might be a viable solution in some cases, choosing a tool more tailored for the specific problem is probably wiser in the long run.

Single-Writer

Another way to get consistency is to assure serialized writes, i.e using the single-writer principle, meaning we make sure all writes concerning a particular entity ID occur on a single thread. It is possible we could make this scenario work by making our producer a consumer of its own events and effectively block until the event is committed and available in the other end of Kafka. This kind of design would have severe impact on performance, especially considering the limited loading capabilities described above.

Does It Fit at All?

So, is there a place for Kafka in an event-sourced architecture? Maybe. Probably. It might be a good complement to your event store as a way of transporting events to downstream query services or read models.

However, we should always be careful when adding large and complex pieces of infrastructure to our systems - everything comes with a cost so make sure it’s a good enough fit for your problem at hand!

kafka Event Database

Published at DZone with permission of Jesper Hammarbäck. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Constructing Real-Time Analytics: Fundamental Components and Architectural Framework — Part 2
  • Resilient Kafka Consumers With Reactor Kafka
  • Applying Kappa Architecture to Make Data Available Where It Matters
  • Building an Enterprise CDC Solution

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!