Przejdź na język polski zmień na polski
Contact us +48 660 043 353 biuro@wiseweb.pl
or use contact form on our website
We are wiseweb - a great IT company.

We are a Software House, specializing in PHP, Java and React.js.

How to implement Event Sourcing in Java

If you are interested in Software architecture, you probably should take a look at Event Sourcing.
In this post we will show you, how Event Sourcing implementation can help you with.

The basic idea of Event Sourcing is the fact, that every domain (DDD - Domain Driven Design) event which has happened in the past, is stored in the database.
Let's take a look at the quick example and compare two different entities.

For example: We are preparing some Order System, where a logged customer can order some products.

How should the database look like?
Product (productId, productTitle, productPrice)
Customer (customerId, firstName, lastName)
Order (orderId, customerId, List products)

Will this solution work? YES
Is this solution scalable? NO - why?

a) You can't split the database into two small databases (one for Orders, one for Customers), because of the relations in the database. - it's possible to do with REST API, but it's hard and you break the relations,

b) There is a problem with database integration when we want to change the Order entity.
We don't want to have relations between Product and Order anymore, because the prices change during the time. What then?

The table Order should be looking as follows:

Order (orderId, customerId, orderDetails)

It would be great... but what about the old orders? They are not compatible.
Event Sourcing
Event Sourcing is the solution.
It gives you a high level of security and you a guarantee that you will never lose data. It can also allow you to change the database schema and type in the future.
Do not hesitate to contact us if you have any questions:

+48 660 043 353
How does Event Sourcing work?
What if you save all the user Order actions to a database. Let's create a MySQL database with one table called: EventStore. That table will store all the events in your application. The table schema is shown below:



It's a very simple implementation of the Event Store. It stores:

Event ID,
AggregateId (Id of the Order, usually UUID generated ID),
Aggregate Type (OrderCreated, OrderPlaced, OrderCanceled, OrderChangedStatus),
PayLoad (JSON data connected to type of event), version of change.
Date - when the event has happended;

Below the example of Events for single Order:



After these 3 lines, we can see that a customer firstName: Mateusz has Ordered new Items. Later the Order was paid and after that, the Order Changed Status to DELIVERED.

Okay, so we have a list of events stored in the database. The EventStore can also have any event types, such as: CustomerRegistered, CustomerLoggedIn, CustomerChangedProfileData, ProductAddedByAdministrator, PaymentSuccessEmailSent, etc.

Why is this safe? Because you can make horizontal replication of your database. One database can be a MASTER, the rest can be SLAVES which will only synchronize. The EventStore is a table where you can only INSERT data. DON'T select, DON'T update. Just insert. If you want to remove Order, just add new Event OrderRemoved. If you want to update OrderDetails, just create new event OrderDetailsChanged, etc.
Do not hesitate to contact us if you have any questions:

+48 660 043 353
I need a list of Orders with status DELIVERED
Let's say, we need a list of orders from this month in the Backend (for administrator). As I mentioned before, we can't select from the EventStore database.

Projection/ View
Have you ever heard of views in (my)SQL? Yes. that's how projections work. We need to create a view for an Administrator. You can act table/entity as.. view or projection. If you want to see all the orders from this month, just call the table LastMonthOrders with fields:

OrderId, OrderDate, OrderDetails, UserId

I am using "H2 in-memory database" for that kind of tables/projections.
Putting it all together - the Order entity view / projection with data from EventStore
All we need to do now is take the Events we are interested in and build a temporary in-memory database (LastMonthOrders).
It's very simple to do it in Java Spring:

Then you can just fetch all the data from the temporary database like it's been a normal table.
Now, depending on the Event, we create a new Entity, update or remove the existing row from the H2 database.
Do not hesitate to contact us if you have any questions:

+48 660 043 353
Why Event Sourcing?
1. No data are lost - all the application history is stored in the EventStore table,
2. When you need to change business logic, you simply work with temporary in-memory entities/tables, so... you are building a new table (with new columns) when you restart the application.
3. It's an introduction to CQRS (Command Query Responsibility Segregation) ideology and a great way to building a scalable Microservices.
4. EventStore is a big database and stores all the information about your customers, from which you can create new Projections (tables) an get great business tips for a future.
5. You don't have to use one technology - every single Event is stored in an EventStore and you can fetch it via Rest API, so... Build a microservices crafted exactly for business needs. Every single microservice can be done in a different language: Python, Java, C#, PHP, whatever.

If you are interested more about Event Sourcing, feel free to contact us via e-mail: biuro@wiseweb.pl.

Next articles will explain the CQRS technology and how the Events should be published.
Dedykowane oprogramowanie - Wiseweb - Mateusz
Mateusz Nowak, Software Architect & CEO

PHP and Java programmer with over 8 years of business experience.

During his career he was working for Polish, German, United Kindgdom and Nederlands customers. He created an e-commerce application for Trade Fair in Germans as well as take part of a team (as Solution architect) in a government project for a British customer.

Main topics of interests are a software architecture, third party integrations and custom API building.

What we offer
Thank you for your visit.
If you have any question, do not hestitate to contact us