<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: William Boxhall</title>
    <description>The latest articles on Forem by William Boxhall (@williamboxhall).</description>
    <link>https://forem.com/williamboxhall</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F860281%2Fda93a5c4-7c10-4772-bd88-48f06bae604c.jpeg</url>
      <title>Forem: William Boxhall</title>
      <link>https://forem.com/williamboxhall</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/williamboxhall"/>
    <language>en</language>
    <item>
      <title>Event sourcing - basic concepts</title>
      <dc:creator>William Boxhall</dc:creator>
      <pubDate>Fri, 13 May 2022 06:01:46 +0000</pubDate>
      <link>https://forem.com/cultureamp/event-sourcing-basic-concepts-52ik</link>
      <guid>https://forem.com/cultureamp/event-sourcing-basic-concepts-52ik</guid>
      <description>&lt;p&gt;At Culture Amp we are increasingly embracing event sourced systems for a few reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Architectural modularisation and flexibility&lt;/li&gt;
&lt;li&gt;The strong emphasis on Domain Driven Design and modelling, best representing the business domain &lt;/li&gt;
&lt;li&gt;The semantically meaningful append-only event streams mean we never have to throw away data, allowing us to leverage this highest-possible-fidelity historical data in new and interesting ways across our systems&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We have two small open-source event-sourcing micro-frameworks,&lt;br&gt;
one named &lt;a href="https://github.com/cultureamp/event_framework" rel="noopener noreferrer"&gt;Event Framework&lt;/a&gt; written in Ruby, and another written in Kotlin named &lt;a href="https://github.com/cultureamp/kestrel" rel="noopener noreferrer"&gt;Kestrel&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdjybskbqydtbrevqx3lh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdjybskbqydtbrevqx3lh.png" alt="Kestrel"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we hire more and more people and do more and more event sourcing, we're looking for easy ways for newcomers to gain a grasp of the basics.&lt;/p&gt;

&lt;h2&gt;
  
  
  An example domain
&lt;/h2&gt;

&lt;p&gt;I was searching for an example domain that could represent the basic concepts of event sourcing with: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Little-to-no text, favouring shapes, symbols, boxes and arrows, in order to emphasise the &lt;em&gt;mechanics&lt;/em&gt; of how things sit together&lt;/li&gt;
&lt;li&gt;A well-known, bounded domain&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Tetris
&lt;/h2&gt;

&lt;p&gt;I realised Tetris ticks both of these boxes, with its minimalist, iconic shapes and sheer oldschool ubiquity - everyone has played Tetris!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffu07p5jtb7xocgiqt7sb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffu07p5jtb7xocgiqt7sb.png" alt="Tetris Playfield"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Tetris Playfield also has the benefit of being an entity that is subject to forces other than just the Player, it is affected by new blocks coming in from outside (in reaction to the previous block landing), and subject to the ticking of time (gravity moving the blocks down the Playfield).&lt;/p&gt;

&lt;p&gt;Here is an example of how an event sourced implementation of Tetris might look, and below we will unpack it, piece by piece. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqyjg0ll401x7gf3gp5lv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqyjg0ll401x7gf3gp5lv.png" alt="Complete Tetris system"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Aggregates
&lt;/h2&gt;

&lt;p&gt;An aggregate instance is the central entity to which things happen. In many event sourced systems there can be many instances of many different types of aggregates. Aggregates tend to have a life-cycle: a creation event, many update events, and, often but not always, an ending event.&lt;/p&gt;

&lt;p&gt;In the Tetris example, the &lt;code&gt;Tetris Playfield Aggregate&lt;/code&gt; is instantiated every time a new game is started. In this implementation, the first thing that happens (the creation event) to it is a &lt;code&gt;New Block Appeared&lt;/code&gt; at the top of the Playfield. Each instance of the aggregate has an id, in this case, ids &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzvaxxmp9yykl75oduy9u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzvaxxmp9yykl75oduy9u.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Commands
&lt;/h2&gt;

&lt;p&gt;One form of input are user commands, which represent the user intention to do something to an aggregate, and which may succeed or be rejected. &lt;/p&gt;

&lt;p&gt;In the Tetris example, the user may attempt to nudge the block left or right, rotate it clockwise or anti-clockwise, or send it immediately to the bottom of the Playfield. If the user tries to move the block through a wall, the user command will be rejected.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi4o0vblgzgf7dcktsu8m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi4o0vblgzgf7dcktsu8m.png" alt="User commands"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Events
&lt;/h2&gt;

&lt;p&gt;When an aggregate doesn't reject a command, it sinks one or more events to the event store. The aggregate can then use these events to build up "just enough" state in order to accept or reject subsequent commands.&lt;/p&gt;

&lt;p&gt;In the Tetris example, when the movement commands are accepted by the &lt;code&gt;Tetris Playfield Aggregate&lt;/code&gt; based on the current state of the Playfield, the "update" events &lt;code&gt;Moved Right&lt;/code&gt;, &lt;code&gt;Moved Left&lt;/code&gt;, &lt;code&gt;Rotated Clockwise&lt;/code&gt;, &lt;code&gt;Rotated Anti-clockwise&lt;/code&gt; and &lt;code&gt;Sent To Bottom&lt;/code&gt; are saved as events in a time ordered append-only fashion.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmplzt6zunbs22img7g77.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmplzt6zunbs22img7g77.png" alt="Events"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Non-user Actors
&lt;/h2&gt;

&lt;p&gt;Apart from user commands, scheduled commands, reactors or other actors may act upon an aggregate.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scheduled commands
&lt;/h3&gt;

&lt;p&gt;It's a common pattern to have "scheduled commands" in event sourced systems.&lt;/p&gt;

&lt;p&gt;In the Tetris example, we have &lt;code&gt;Time&lt;/code&gt; running on a schedule (once per second), interacting with the aggregate. It's the ticking of &lt;code&gt;Time&lt;/code&gt; that allows blocks to move down the playfield, and when the aggregate detects that the block has reached the ground, the aggregate can emit an additional &lt;code&gt;Landed&lt;/code&gt; event following the &lt;code&gt;Fell&lt;/code&gt; event.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwivjm2s1skttu191efic.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwivjm2s1skttu191efic.png" alt="Time"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Reactors
&lt;/h2&gt;

&lt;p&gt;Reactors have the additional feature that they can listen to the events that have happened so far, and react to those events by sending new commands back into aggregates.&lt;/p&gt;

&lt;p&gt;In the Tetris example, we want the &lt;code&gt;Landed&lt;/code&gt; event to trigger a &lt;code&gt;New Block Appeared&lt;/code&gt; at the top of the Playfield.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5y9bllme9mqmky7qgq1t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5y9bllme9mqmky7qgq1t.png" alt="Reactor"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the combination of blocks being able to "land" and new blocks being able to enter the Playfield, this means the aggregate can now sink &lt;code&gt;Line Cleared&lt;/code&gt; or &lt;code&gt;Tetris Cleared&lt;/code&gt; events, and of course &lt;code&gt;Game Over&lt;/code&gt; "ending event" when the Playfield gets full.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsd6std3qrz3vdwaiqoct.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsd6std3qrz3vdwaiqoct.png" alt="Reactors or other actors"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Projectors, projections and queries
&lt;/h2&gt;

&lt;p&gt;So far we've only spoken about "write" actions, things that attempt to change the system and sink events. In event sourced systems, we usually want to be able to view or "query" the state of the system in various context-specific forms.&lt;/p&gt;

&lt;p&gt;"Projectors" allow us to "project" the events in the event store into different views or "projections". Projectors (and Reactors) only need to listen for events with relevant types and can skip the rest, as indicated in the diagram.&lt;/p&gt;

&lt;p&gt;In the Tetris example, the Player needs to be able to view the current state of the Playfield so that they can decide which further input commands they wish to send. Once the game is over, they can see a summary of what happened during the game, including their final score. The act of requesting data from a projection is called a "query"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgxhd25g4b1n74443w9xi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgxhd25g4b1n74443w9xi.png" alt="Projectors"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The whole picture
&lt;/h2&gt;

&lt;p&gt;With the following basic building blocks we have a full end-to-end eventsourced system:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Commands&lt;/li&gt;
&lt;li&gt;Aggregates&lt;/li&gt;
&lt;li&gt;Events&lt;/li&gt;
&lt;li&gt;Reactors&lt;/li&gt;
&lt;li&gt;Projectors, projections and queries&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqyjg0ll401x7gf3gp5lv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqyjg0ll401x7gf3gp5lv.png" alt="Complete Tetris system"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Tetris is a great domain to illustrate the fundamentals of event sourcing, but in reality, one probably wouldn't build a video game using event sourcing because video games need to optimise for very low latency throughput. Event sourced systems optimise instead for domain richness, data fidelity, architectural modularisation and flexibility. In future articles we will dive into how to to actually build these systems with code.&lt;/p&gt;

&lt;p&gt;Thanks to &lt;a href="https://www.flickr.com/photos/mache/" rel="noopener noreferrer"&gt;Mache&lt;/a&gt; for the &lt;a href="https://www.flickr.com/photos/mache/166940673/in/photolist-evvHbt-fKBDt-4fzLhc-trfjV3-5BLtga-2fv2Tz-xCPL-5nzeLZ-c1v2QS-yKGHd3-pwJY7G-84HMA7-bs7ewx-2mp6gfB-xR8c9R-4FtYjb-o9kG45-8qTVfN-a6CCsY-yM24rd-5MJd9r-q8XGz7-4VBPv4-pSYsje-8niBDX-yKGyAJ-yvoBFq-8qQNdT-8qTUhL-JQrnDZ-7GJR7f-8Leu9w-4SssPy-8qQMEe-6nV4C-8qQMi8-5Gf6u2-xR6U88-5be7vA-8hA5CA-xQXMwE-wnRM1-2FCmJp-xR8CNM-pGG2r-8qTUCG-MGfmUf-8qTTYq-PUM5P-31apHH/" rel="noopener noreferrer"&gt;cover image&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>eventsourcing</category>
      <category>eventdriven</category>
      <category>cqrs</category>
      <category>tetris</category>
    </item>
  </channel>
</rss>
