Doing CQRS + Event Sourcing without building a spaceship

  • Event Stream: sequence of events produced by the system.
  • Projections: are event consumers, they process events from the event stream.
  • Read Models: it’s the cached result of the event processing by the projection.
  • Do it in PHP because that’s the language I love.
  • Write events in a cheap, immutable, append only fashion.
  • Use projections to read from the event stream and calculate read models state.
  • Be able to change how events are processed without breaking anything.

The ingredients

  • Event: represents something that has already happened, has a root ID property the projections will use to aggregate the events, and also has actions to be applied by the reducers. It’s actions in the plural because an action can have many versions.
  • Action: has a name and version, does the mapping between reducers and events.
  • Projection: as consumers they interpret and extract information from the events stream. Has the reducers that will change the state of the read models. For simplicity sake projections are 1–1 with read models.
  • Reducers: basically an array of versioned callables that live in the projections. Because events are versioned, reducers are also versioned, it’s a form of mapping. If a new version of the event is created a new reducer version also needs to be created to process the changes.
  • Event Repository: persists and retrieves events.
  • Read Model Repository: persists and retrieves read models.




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store