Blog

KESA – Event Sourcing

KESA - Event Sourcing Illustrated: A Jigsaw Puzzle Game Perspective

In the dynamic world of software development, managing and maintaining the state of an application poses significant challenges. Event sourcing, an innovative architectural pattern, steps in to revolutionize this process, which offers a robust foundation for understanding not only where the system stands, but also how it got there, significantly enhancing both transparency and traceability.

What is Event Sourcing?

Event sourcing is an architectural pattern that captures changes to an entity's state through a chronological series of events. This approach diverges from traditional methodologies that emphasize storing the current state, by instead focusing on the sequence of events that lead to that state.

Event Sourcing utilizes concepts such as events, event streams, event stores, and event handlers to manage and process these changes. Further, it offers benefits such as immutable event logs, auditability, event replay for state reconstruction, consistency, improved scalability, fault tolerance, and support for event-driven architectures.

Jigsaw Puzzle Game Project Overview

To illustrate the practical application of event sourcing, we present a simple, engaging example: a digital jigsaw puzzle game. This single-player game is developed to highlight the principles and advantages of event sourcing and event-driven architectures, where a player interacts with a web interface to assemble a jigsaw puzzle.

Play the game here: Fidenz Kesa

Find instructions on how to play here

Find the full project explainer video here: https://youtu.be/4O9VukIrtCI

Implementing Event Sourcing in Jigsaw Puzzle Game

The table below demonstrates the implementation of event sourcing features within the Jigsaw Puzzle Game application, which has been intentionally over-engineered to effectively illustrate event sourcing capabilities using a jigsaw puzzle game as an example.

Event Sourcing Feature Application Implementation Benefits in the Game Context
Entity The "JigsawPuzzleGame" entity is designed to represent the current state of a gaming session. Encapsulates the game logic and helps validating events for an ongoing gaming session
Event Each game action triggering an event sent from the front-end to the back-end such as: Game Started Event, Piece Added Event, Piece Removed Event. Facilitates real-time game interactions and state management
Event Stream A gaming session begins with a Start Game Event, marking the commencement of an event stream. Subsequent events, including Piece Added Events and Piece Removed Events, will be sequentially numbered in chronological order. Ensures ordered, reliable processing of game actions
Event Handlers Event handlers are designed to react to various events, taking actions such as processing data and updating “JigsawPuzzleGame” entity. Enables dynamic game state management based on player actions
Event Store Events are stored in the event store and can be retrieved as needed. Once saved, these events are immutable. Each event record includes essential information such as the unique game session ID, event type, event stream ID, and event data, among other details. Provides durability and historical tracking of game actions
Immutable Event Log Event Store implementation provide chronologically ordered immutable events of a gaming session Accurate tracking of puzzle progress
Event Replay A Player can use the Event List to go back in time to a specific point in the game. This can be done by selecting an event from the event list. The system then reverse all subsequent events by applying their opposite, thus restoring the puzzle to
the desired state
Ability to revert puzzle state and understand puzzle assembly
Consistency and Eventual Consistency Storing events in chronological order with event stream ID and event type Distinguishes between the various events that occur within a gaming session.

Conclusion

The jigsaw puzzle game serves as a practical and engaging example to illustrate the principles of event sourcing. By understanding how each action in the game translates into an event and contributes to the puzzle's state, we can appreciate the power and utility of event sourcing in software development. As technology evolves, it's exciting to ponder how event sourcing will shape the future of data management and application design.

Would you like to learn more? Contact Us!

If you have any additional questions about this or would like a word with our creators? Feel free to reach out to us.

📆 Talk to Us: Contact us - Fidenz Technologies

Keep an eye out for our upcoming content from Fidenz Technologies, as we embark on a journey through the intricate realms of technology.

Happy Exploring!

................................................................................................................................................................

Instructions and How to play this game

  • Watch the video below for instructions on how to play the game.
  • Interact with a dual-panel interface: left panel for puzzle assembly, right panel for displaying available jigsaw pieces.
  • First, choose a puzzle size from three game size options: 4x4, 5x5 (default), or 6x6.
  • Start the game using start button
    • Upon start four puzzle pieces will be available in the piece viewer for selection.
  • Add pieces by dragging them from the piece viewer to the grid.
  • Remove pieces by clicking on a piece in the grid and confirming the subsequent pop-up.
    • Removed pieces won't be added back to the piece viewer but will be available for future selection.
  • Use the “Re-shuffle” button to refresh the piece selection with a new set of random pieces, useful when the piece viewer is empty.
  • Reset the game at any time using the “Reset” button.
  • Event List Panel
    • To view the events occurred while playing the game
    • To Revert puzzle to a previous state by selecting an event in the event list and confirming the prompted pop-up.

The game concludes when the puzzle is fully assembled, with completion time affecting the player's score.