Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CQRS and Event Sourcing with Spring

CQRS and Event Sourcing with Spring

My experiences building an Event Sourced, CQRF Spring application.
The fact that it ultimately leads to a mature Microservices implementation is just gravy.

baeldung

May 13, 2015
Tweet

More Decks by baeldung

Other Decks in Programming

Transcript

  1. . . . . . . . . . .

    . . . . 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
  2. . . . . . . . . . .

    . . . . 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
  3. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • CRUD? No! CQRS! AGENDA
  4. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • CRUD? No! CQRS! • Why Event Sourcing? AGENDA
  5. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • CRUD? No! CQRS! • Why Event Sourcing? • What are Projections? AGENDA
  6. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • CRUD? No! CQRS! • Why Event Sourcing? • What are Projections? • How does Spring fit in? AGENDA
  7. . . . . . . . . . .

    . . . . 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
  8. . . . . . . . . . .

    . . . . Imagine you’re building a CRM application
  9. . . . . . . . . . .

    . . . . 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
  10. . . . . . . . . . .

    . . . . 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
  11. . . . . . . . . . .

    . . . . 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
  12. . . . . . . . . . .

    . . . . 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
  13. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • Simple Domain Models - CRUD is OK FIRST STEP TO MODULARIZATION…
  14. . . . . . . . . . .

    . . . . 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…
  15. . . . . . . . . . .

    . . . . 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…
  16. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • The 2 Concepts: – The Command - for Writes • ex: CreateLead COMMANDS AND QUERIES
  17. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • The 2 Concepts: – The Command - for Writes • ex: CreateLead – The Query - for Reads COMMANDS AND QUERIES
  18. . . . . . . . . . .

    . . . . Commands => naturally evolve into => Events
  19. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • Command: – handle(CreateLead command){ ... } EXAMPLE - COMMAND => EVENT
  20. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • Command: – handle(CreateLead command){ ... } • Event: – on(LeadCreated event){ ... } EXAMPLE - COMMAND => EVENT
  21. . . . . . . . . . .

    . . . . 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
  22. . . . . . . . . . .

    . . . . 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 (...)); }
  23. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • The Official System of Record STORING EVENTS – THE EVENT STORE
  24. . . . . . . . . . .

    . . . . 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
  25. . . . . . . . . . .

    . . . . 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
  26. . . . . . . . . . .

    . . . . 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
  27. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring Event • id: UUID • revision: int • type: String • date: Date • userId: UUID • date: JSON • aggregate: UUID • ---------------------- A SIMPLE EVENT
  28. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • Traditional Architecture: • The Current State WHAT DATA DO WE CARE ABOUT?
  29. . . . . . . . . . .

    . . . . 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?
  30. . . . . . . . . . .

    . . . . 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?
  31. . . . . . . . . . .

    . . . . => Current State is a Secondary Citizen
  32. . . . . . . . . . .

    . . . . 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
  33. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • A Projection reacts to the Events in the System WHAT IS A PROJECTION
  34. . . . . . . . . . .

    . . . . 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
  35. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • A Projection enables us to do work while the User isn’t waiting! WHY USE A PROJECTION
  36. . . . . . . . . . .

    . . . . 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
  37. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • Lead Management Projection => keep track of lead data EX: LET’S PROJECT: LeadCreated
  38. . . . . . . . . . .

    . . . . 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
  39. . . . . . . . . . .

    . . . . 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
  40. . . . . . . . . . .

    . . . . PROJECTION PERSISTENCE “One Size Doesn’t Fit All”
  41. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • Projection data is trivial to query PROJECTION DATA
  42. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • Projection data is trivial to query • Projection data = throwaway data PROJECTION DATA
  43. . . . . . . . . . .

    . . . . 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
  44. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • Events usually cluster into families of events (with a common base class) EVENTS, PROJECTIONS - IMPL
  45. . . . . . . . . . .

    . . . . 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
  46. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring EX: PROJECTION WITH EVENTS (<4.2) public class LeadProjection implements ApplicationListener<BaseLeadEvent>{ public void onApplicationEvent(BaseLeadEvent event) { if( event instanceof LeadCreated ){ on((LeadCreated)event); } else if( event instanceof LeadBecameCustomer){ on((LeadBecameCustomer)event); } } }
  47. . . . . . . . . . .

    . . . . 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) { ... } }
  48. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • A Projection cannot undo an Event UNDO? NO! JUST REACT.
  49. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • A Projection cannot undo an Event • The Projection can only react to the Event UNDO? NO! JUST REACT.
  50. . . . . . . . . . .

    . . . . 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.
  51. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • Projecting the full Event stream is slow and not always practical SAVE POINTS (NOT JUST FOR GAMES)
  52. . . . . . . . . . .

    . . . . 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)
  53. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • We have data for everything that ever happened in the system ADD A NEW PROJECTION
  54. . . . . . . . . . .

    . . . . 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
  55. . . . . . . . . . .

    . . . . 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
  56. . . . . . . . . . .

    . . . . 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
  57. . . . . . . . . . .

    . . . . 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
  58. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • No longer lose information THINGS YOU COULDN’T DO BEFORE
  59. . . . . . . . . . .

    . . . . 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
  60. . . . . . . . . . .

    . . . . 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
  61. . . . . . . . . . .

    . . . . 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
  62. . . . . . . . . . .

    . . . . 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
  63. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • Large Mental Shift POTENTIAL CONS
  64. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • Large Mental Shift • Important to Pick the right Events POTENTIAL CONS
  65. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • Large Mental Shift • Important to Pick the right Events • The Application must handle Eventual Consistency POTENTIAL CONS
  66. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • We haven’t set out to implement a Microservice Architecture • But ... MICROSERVICES (FINALLY)
  67. . . . . . . . . . .

    . . . . 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)
  68. . . . . . . . . . .

    . . . . CQRS and Event Sourcing with Spring • CRUD => • CQRS (Commands) => • EVENTS => • EVENT SOURCING => • PROJECTIONS => • MICROSERVICES BUZZWORD CONCLUSION
  69. . . . . . . . . . .

    . . . . THANK YOU It’s Q&A Time