Event Sourcing & CQRS
By Mohit Kumar Mittal
About me
● Seasoned Enterprise/Solution/Technical Architect.
● Responsible for providing solutions , proposals and technical leadership.
● Consulted various organizations on Microservices and applied patterns like
CQRS, Event Sourcing etc.
● Founder of www.javaarchitectforum.com
Agenda
Event Sourcing and CQRS are modern and very popular architecure patterns to
Ensure scalablity, security and robustness.
1. Problem Context
2. What is Event Sourcing
3. What is CQRS
4. Solving Problem Context with Event Sourcing and CQRS
5. Technology Choices
6. Addiotnal Notes
Problem Context #1
Client
IO Intensive
back-end
1
2
Client
Non
Blocking
Adapter
1
2
IO Intensive
back-end
How to decouple a blocking backend and
make a non-blocking system ?
Problem Context #2
How to implement a distributed transcation across microservice ?
MS 1
MS 2
Client
1
2
3 4
Problem Context – MS1 and MS2 should be part
of transaction.
MS1 updated data successfully.
MS2 got some error
How to roll back changes done by MS1 – 2 PC is
not an option.
Problem Context #3
How to scale a CRUD massively?
Client
Domain
Micro
service
RDBMS
1. Create
2. READ
3. Update
4. Delete
Update operations are
heavy and use locking.
Handling concurrency
may impact performance
No Audit log. Cant make a
UNDO.
Event Sourcing | Pattern
Event Sourcing is an approach
1. Every change in the application state is captured by a Event.
2. Log all such events in DB ( Event Store) in a sequence.
3. Event Store would be a append only data structure.
Client
Domain
Micro
service
Event Id Event Name State
1 Employee
Added
Employee{
id=1,Name=John ,,,,}
2 Employee
Updated
Employee{
id=1,Name=John ,,,,}
Event Store
CQRS | Pattern
1. Command(Write) Query(Read) Responsibility Segregation
2. Separate Read and Write Model – Different read and write Stores
3. May be different API ( Microservices) for Command( Write) and Query
(Read)
Client
Read
service
Command
Service
Read DB
Write DB
Sync Read DB with Write DB using
different mechanisms like events etc.
Applying Event Sourcing | PC #1
● How to decouple a
blocking backend and
make a non-blocking
system ?
● Applying Event Sourcing
Client
IO Intensive back-
end
1
2
Client
1
2
IO Intensive back-
end
Wrapper/
Adapter
Service
Event
Bus
Event DB
Event
Listener
API
Applying Event Sourcing & CQRS | PC #3
● How to scale
CRUD
massively.
● Applying Event Sourcing and CQRS
Client
Write Microservice
Event Bus Event DB
API
Client
Microservice
Database
Read API
Event Handler
Read DB
Event
Store
Read
Write

Advance Microservice Patterns - Event Souring , CQRS

  • 1.
    Event Sourcing &CQRS By Mohit Kumar Mittal
  • 2.
    About me ● SeasonedEnterprise/Solution/Technical Architect. ● Responsible for providing solutions , proposals and technical leadership. ● Consulted various organizations on Microservices and applied patterns like CQRS, Event Sourcing etc. ● Founder of www.javaarchitectforum.com
  • 3.
    Agenda Event Sourcing andCQRS are modern and very popular architecure patterns to Ensure scalablity, security and robustness. 1. Problem Context 2. What is Event Sourcing 3. What is CQRS 4. Solving Problem Context with Event Sourcing and CQRS 5. Technology Choices 6. Addiotnal Notes
  • 4.
    Problem Context #1 Client IOIntensive back-end 1 2 Client Non Blocking Adapter 1 2 IO Intensive back-end How to decouple a blocking backend and make a non-blocking system ?
  • 5.
    Problem Context #2 Howto implement a distributed transcation across microservice ? MS 1 MS 2 Client 1 2 3 4 Problem Context – MS1 and MS2 should be part of transaction. MS1 updated data successfully. MS2 got some error How to roll back changes done by MS1 – 2 PC is not an option.
  • 6.
    Problem Context #3 Howto scale a CRUD massively? Client Domain Micro service RDBMS 1. Create 2. READ 3. Update 4. Delete Update operations are heavy and use locking. Handling concurrency may impact performance No Audit log. Cant make a UNDO.
  • 7.
    Event Sourcing |Pattern Event Sourcing is an approach 1. Every change in the application state is captured by a Event. 2. Log all such events in DB ( Event Store) in a sequence. 3. Event Store would be a append only data structure. Client Domain Micro service Event Id Event Name State 1 Employee Added Employee{ id=1,Name=John ,,,,} 2 Employee Updated Employee{ id=1,Name=John ,,,,} Event Store
  • 8.
    CQRS | Pattern 1.Command(Write) Query(Read) Responsibility Segregation 2. Separate Read and Write Model – Different read and write Stores 3. May be different API ( Microservices) for Command( Write) and Query (Read) Client Read service Command Service Read DB Write DB Sync Read DB with Write DB using different mechanisms like events etc.
  • 9.
    Applying Event Sourcing| PC #1 ● How to decouple a blocking backend and make a non-blocking system ? ● Applying Event Sourcing Client IO Intensive back- end 1 2 Client 1 2 IO Intensive back- end Wrapper/ Adapter Service Event Bus Event DB Event Listener API
  • 10.
    Applying Event Sourcing& CQRS | PC #3 ● How to scale CRUD massively. ● Applying Event Sourcing and CQRS Client Write Microservice Event Bus Event DB API Client Microservice Database Read API Event Handler Read DB Event Store Read Write