My experiences building an Event Sourced, CQRF Spring application.
The fact that it ultimately leads to a mature Microservices implementation is just gravy.
. . . . CQRS and Event Sourcing with Spring HI • My name is Eugen Paraschiv (don’t try to pronounce it) • Created my first Event Sourced Architecture in early 2014 • Got excited about CQRS and DDD • Working on my 4th real-world implementation now • Made lots of mistakes and learned from them • The current implementation is well positioned to become the largest ElasticSearch installation in the world
. . . . CQRS and Event Sourcing with Spring • Show you how this way of architecting a system – Event Sourcing – is so highly powerful and useful PRESENTATION GOAL
. . . . CQRS and Event Sourcing with Spring • CRUD? No! CQRS! • Why Event Sourcing? • What are Projections? • How does Spring fit in? • Microservices, the natural conclusion... AGENDA
. . . . CQRS and Event Sourcing with Spring • The Lead Management module • The Lead Scoring module • The Communications module • The Risk module • The Promotions module • The Email Automation module THE CRM APPLICATION
. . . . CQRS and Event Sourcing with Spring TRADITIONAL ARCHITECTURE DIAGRAM Browser / Client WAR CRM UI Lead Scoring module Lead Management module Communication module XML, JSON RDBMS Promotions module Email Automation module
. . . . CQRS and Event Sourcing with Spring HOW DO YOU SCALE OUT A MONOLITH? Browser / Client WAR CRM UI Lead Scoring module Lead Management module Communication module XML, JSON RDBMS Promotions module Email Automation module WAR
. . . . CQRS and Event Sourcing with Spring A MODULAR ARCHITECTURE Browser / Client W WAR HTML, REST, JSON Lead Management module CRM UI Lead Scoring module Promotions module LEAD MANAGEMENT DB LEAD SCORING DB PROMOTIONS DB WAR WAR
. . . . CQRS and Event Sourcing with Spring • Simple Domain Models - CRUD is OK • Complex Domain Models – there’s now a difference between: – Write Operations – Read Operations FIRST STEP TO MODULARIZATION…
. . . . CQRS and Event Sourcing with Spring • Simple Domain Models - CRUD is OK • Complex Domain Models – there’s now a difference between: – Write Operations – Read Operations • => CRUD? NO! CQRS! FIRST STEP TO MODULARIZATION…
. . . . CQRS and Event Sourcing with Spring • AN EVENT = something that: – occurred in the past – produced a change in the system • ex: LeadCreated, LeadBecameCustomer, EmailSentToLead THE EVENT DRIVEN ARCHITECTURE
. . . . CQRS and Event Sourcing with Spring LET’S PROCESS THE COMMAND IN SPRING @RequestMapping(value = "/api/lead", method = POST) @ResponseStatus(HttpStatus.ACCEPTED) public void createLead(@Valid @RequestBody CreateLead command) { // ... extra validation this.publisher.publishEvent(new LeadCreated (...)); }
. . . . CQRS and Event Sourcing with Spring • The Official System of Record • All Events are stored in sequence with a strict global ordering STORING EVENTS – THE EVENT STORE
. . . . CQRS and Event Sourcing with Spring • The Official System of Record • All Events are stored in sequence with a strict global ordering • Events are immutable STORING EVENTS – THE EVENT STORE
. . . . CQRS and Event Sourcing with Spring • The Official System of Record • All Events are stored in sequence with a strict global ordering • Events are immutable • Updating or Deleting data is a new Event STORING EVENTS – THE EVENT STORE
. . . . CQRS and Event Sourcing with Spring • Traditional Architecture: • The Current State: the ONLY thing you know about your data is what it looks like RIGHT NOW WHAT DATA DO WE CARE ABOUT?
. . . . CQRS and Event Sourcing with Spring • Traditional Architecture: • The Current State: the ONLY thing you know about your data is what it looks like RIGHT NOW • Event Sourcing Architecture: • we KNOW the state of our data at ANY POINT IN TIME WHAT DATA DO WE CARE ABOUT?
. . . . CQRS and Event Sourcing with Spring • We can now answer new questions: – What was the score of this lead last week? – Who’s the original author of this promotion campaign? – How many leads did we have 3 months ago? – Who has added a product to their card and removed it in the next 5 minutes? EVENT SOURCING IN OUR CRM APP
. . . . CQRS and Event Sourcing with Spring • A Projection reacts to the Events in the System • A Projection answers a single question WHAT IS A PROJECTION
. . . . CQRS and Event Sourcing with Spring • A Projection enables us to do work while the User isn’t waiting! • A Projection makes data trivial to query! WHY USE A PROJECTION
. . . . CQRS and Event Sourcing with Spring • Lead Management Projection => keep track of lead data • Lead Scoring Projection => calculate a score for the lead EX: LET’S PROJECT: LeadCreated
. . . . CQRS and Event Sourcing with Spring • Lead Management Projection => keep track of lead data • Lead Scoring Projection => calculate a score for the lead • Communication Projection -> email / SMS the lead EX: LET’S PROJECT: LeadCreated
. . . . CQRS and Event Sourcing with Spring • Projection data is trivial to query • Projection data = throwaway data • Projection data can be re-created at any point PROJECTION DATA
. . . . CQRS and Event Sourcing with Spring • Events usually cluster into families of events (with a common base class) • The Projection is a simple Listener (listening on that base class) EVENTS, PROJECTIONS - IMPL
. . . . CQRS and Event Sourcing with Spring EX: PROJECTION WITH EVENTS (>4.2) @Component public class LeadProjection{ @EventListener public void on(LeadCreated event) { ... } @EventListener public void on(LeadBecameCustomer event) { ... } }
. . . . CQRS and Event Sourcing with Spring • A Projection cannot undo an Event • The Projection can only react to the Event • It can project the Event or Ignore It UNDO? NO! JUST REACT.
. . . . CQRS and Event Sourcing with Spring • Projecting the full Event stream is slow and not always practical • Save Points are a great tool to be able to only project the latest Events SAVE POINTS (NOT JUST FOR GAMES)
. . . . CQRS and Event Sourcing with Spring • We have data for everything that ever happened in the system • => • We can build new functionality as if we had it since the beginning ADD A NEW PROJECTION
. . . . CQRS and Event Sourcing with Spring • An Audit Log • Versioning of Objects • An “Undo” Action • Time Series Data • New Reports WE DON’T NEED TO MANUALLY BUILD
. . . . CQRS and Event Sourcing with Spring • A lot of mature systems already use Event Sourcing and Projections: – Banking – Search is a Projection – OLAP – Audit Logs – Git – the reflog YOU’RE ALREADY DOING IT
. . . . CQRS and Event Sourcing with Spring • Visualize and simulate exactly how a bug occurred: – We start with a blank System – We replay / project the Event Store up until the occurrence of the Bug THINGS YOU COULDN’T DO BEFORE
. . . . CQRS and Event Sourcing with Spring • No longer lose information • Update a Projection and then replay the Event Stream THINGS YOU COULDN’T DO BEFORE
. . . . CQRS and Event Sourcing with Spring • No longer lose information • Update a Projection and then replay the Event Stream • Trivially do migrations by simply re-projecting all Events into a new Projection THINGS YOU COULDN’T DO BEFORE
. . . . CQRS and Event Sourcing with Spring • No longer lose information • Update a Projection and then replay the Event Stream • Trivially do migrations by simply re-projecting all Events into a new Projection • Cache an entire Projection in memory without worrying about losing data THINGS YOU COULDN’T DO BEFORE
. . . . CQRS and Event Sourcing with Spring • Projections naturally evolve into self-contained systems, deployed separately from each other and from the Event Store DEPLOY PROJECTIONS
. . . . CQRS and Event Sourcing with Spring • Large Mental Shift • Important to Pick the right Events • The Application must handle Eventual Consistency POTENTIAL CONS
. . . . CQRS and Event Sourcing with Spring • We haven’t set out to implement a Microservice Architecture • But ... • Replace the word “Projection” with the word “Microservice” MICROSERVICES (FINALLY)