DEV Community

Mahi Mullapudi
Mahi Mullapudi

Posted on

Implementing CQRS and Event Sourcing in Distributed Systems

Introduction

As software systems grow in complexity, the need for scalable and maintainable architectures becomes paramount. Two powerful patterns that address these needs are Command Query Responsibility Segregation (CQRS) and Event Sourcing. By leveraging these patterns, along with reactive principles, you can build systems that are highly responsive, resilient, and flexible. In this article, we will explore the concepts of CQRS and Event Sourcing, and provide detailed examples of how to implement them in a distributed system using Java and Spring Boot.

Understanding CQRS

What is CQRS?

CQRS stands for Command Query Responsibility Segregation. It is a design pattern that separates the operations of reading data (queries) from the operations of modifying data (commands). This separation allows for more optimized and scalable solutions, as each operation can be handled differently and optimized independently.

Benefits of CQRS

  • Scalability: Separating reads and writes allows each to be scaled independently, improving overall system performance.
  • Optimized Performance: Queries can be optimized for read performance, while commands can be optimized for write performance.
  • Simplified Complexities: By separating responsibilities, the complexities of each operation type are reduced.

Key Concepts in CQRS

  • Commands: Represent operations that change the state of the application. Examples include creating, updating, or deleting records.
  • Queries: Represent operations that read the state of the application without modifying it. Examples include fetching records or aggregating data.

Command Query Responsibility Segregation (CQRS) pattern

Introduction to Event Sourcing

What is Event Sourcing?

Event Sourcing is a pattern where state changes in an application are stored as a sequence of events. Instead of storing just the current state, all changes are captured and stored, allowing the system to reconstruct any past state by replaying the events.

Benefits of Event Sourcing

  • Auditability: Complete history of changes is maintained, providing a full audit trail.
  • Reproducibility: Any past state can be reconstructed by replaying the sequence of events.
  • Scalability: Event logs can be partitioned and processed independently, improving scalability.

Key Concepts in Event Sourcing

  • Events: Represent state changes that have occurred in the system. Examples include order placed, payment received, or item shipped.
  • Event Store: A storage system that captures and persists events. It acts as the single source of truth for the application's state.

Combining CQRS and Event Sourcing

The Synergy Between CQRS and Event Sourcing

Combining CQRS with Event Sourcing provides a powerful architecture for building distributed systems. CQRS allows for separate optimization of read and write operations, while Event Sourcing ensures a complete history of state changes. Together, they enable systems that are scalable, maintainable, and resilient.

Event Sourcing pattern

Architecture Overview

  1. Command Model: Handles write operations and generates events.
  2. Event Store: Persists events generated by the command model.
  3. Read Model: Builds optimized views for query operations by subscribing to events.

Use Case: E-commerce Order Management

Why Choose E-commerce?

E-commerce systems require handling a large number of transactions and maintaining a comprehensive history of operations. Implementing CQRS and Event Sourcing in an e-commerce order management system can showcase the benefits of these patterns in a real-world scenario.

Features of the Order Management System

  • Handling orders, payments, and shipments.
  • Maintaining a complete history of all operations.
  • Providing optimized views for order queries.

Implementation with Java and Spring Boot

Setting Up the Project

  1. Create a Spring Boot Project: Use Spring Initializr to create a new Spring Boot project with the necessary dependencies. ```shell

spring init --dependencies=data-jpa,web,actuator e-commerce-system


2. **Project Structure**:
Enter fullscreen mode Exit fullscreen mode

├── src
│ ├── main
│ │ ├── java
│ │ │ └── com.example.ecommerce
│ │ │ ├── EcommerceApplication.java
│ │ │ ├── config
│ │ │ │ └── EventStoreConfig.java
│ │ │ ├── controller
│ │ │ │ └── OrderController.java
│ │ │ ├── model
│ │ │ │ ├── Order.java
│ │ │ │ ├── OrderEvent.java
│ │ │ │ └── OrderStatus.java
│ │ │ ├── repository
│ │ │ │ ├── OrderRepository.java
│ │ │ │ └── EventStoreRepository.java
│ │ │ ├── service
│ │ │ │ ├── OrderService.java
│ │ │ │ └── EventStoreService.java
│ │ │ └── event
│ │ │ ├── OrderCreatedEvent.java
│ │ │ ├── OrderPaidEvent.java
│ │ │ └── OrderShippedEvent.java
│ │ ├── resources
│ │ │ └── application.properties


Command Model: Handling Write Operations

Implement the command model to handle write operations and generate events.



package com.example.ecommerce.service;

import com.example.ecommerce.model.Order;
import com.example.ecommerce.model.OrderStatus;
import com.example.ecommerce.event.OrderCreatedEvent;
import com.example.ecommerce.repository.OrderRepository;
import com.example.ecommerce.repository.EventStoreRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrderService {

    private final OrderRepository orderRepository;
    private final EventStoreRepository eventStoreRepository;

    public OrderService(OrderRepository orderRepository, EventStoreRepository eventStoreRepository) {
        this.orderRepository = orderRepository;
        this.eventStoreRepository = eventStoreRepository;
    }

    @Transactional
    public void createOrder(Order order) {
        order.setStatus(OrderStatus.CREATED);
        orderRepository.save(order);
        OrderCreatedEvent event = new OrderCreatedEvent(order.getId(), order.getTotal());
        eventStoreRepository.save(event);
    }

    @Transactional
    public void payOrder(Long orderId) {
        Order order = orderRepository.findById(orderId).orElseThrow();
        order.setStatus(OrderStatus.PAID);
        orderRepository.save(order);
        OrderPaidEvent event = new OrderPaidEvent(orderId);
        eventStoreRepository.save(event);
    }

    @Transactional
    public void shipOrder(Long orderId) {
        Order order = orderRepository.findById(orderId).orElseThrow();
        order.setStatus(OrderStatus.SHIPPED);
        orderRepository.save(order);
        OrderShippedEvent event = new OrderShippedEvent(orderId);
        eventStoreRepository.save(event);
    }
}


</code></pre></div><h3>
  <a name="explanation-of-keywords" href="#explanation-of-keywords">
  </a>
  Explanation of Keywords
</h3>

<ul>
<li>
<strong>@Service</strong>: Indicates that the class is a service component in the Spring framework.</li>
<li>
<strong>@Transactional</strong>: Indicates that the method should be executed within a transactional context.</li>
<li>
<strong>OrderCreatedEvent</strong>: Event representing the creation of an order.</li>
<li>
<strong>OrderPaidEvent</strong>: Event representing the payment of an order.</li>
<li>
<strong>OrderShippedEvent</strong>: Event representing the shipment of an order.</li>
</ul>
<h3>
  <a name="event-store-persisting-events" href="#event-store-persisting-events">
  </a>
  Event Store: Persisting Events
</h3>

<p>Configure an event store to persist events generated by the command model.</p>
<div class="highlight"><pre class="highlight java"><code>

<span class="kn">package</span> <span class="nn">com.example.ecommerce.repository</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">com.example.ecommerce.event.OrderEvent</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.data.jpa.repository.JpaRepository</span><span class="o">;</span>

<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">EventStoreRepository</span> <span class="kd">extends</span> <span class="nc">JpaRepository</span><span class="o">&lt;</span><span class="nc">OrderEvent</span><span class="o">,</span> <span class="nc">Long</span><span class="o">&gt;</span> <span class="o">{</span>
<span class="o">}</span>


</code></pre></div><h3>
  <a name="explanation-of-keywords" href="#explanation-of-keywords">
  </a>
  Explanation of Keywords
</h3>

<ul>
<li>
<strong>JpaRepository</strong>: A JPA-specific extension of the Repository interface that provides JPA related methods for standard CRUD operations.</li>
</ul>
<h3>
  <a name="read-model-building-optimized-views" href="#read-model-building-optimized-views">
  </a>
  Read Model: Building Optimized Views
</h3>

<p>Implement the read model to build optimized views for query operations by subscribing to events.</p>
<div class="highlight"><pre class="highlight java"><code>

<span class="kn">package</span> <span class="nn">com.example.ecommerce.service</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">com.example.ecommerce.model.Order</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">com.example.ecommerce.repository.OrderRepository</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">com.example.ecommerce.event.OrderCreatedEvent</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">com.example.ecommerce.event.OrderPaidEvent</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">com.example.ecommerce.event.OrderShippedEvent</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.context.event.EventListener</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.stereotype.Service</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">java.util.List</span><span class="o">;</span>

<span class="nd">@Service</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">ReadModelService</span> <span class="o">{</span>

    <span class="kd">private</span> <span class="kd">final</span> <span class="nc">OrderRepository</span> <span class="n">orderRepository</span><span class="o">;</span>

    <span class="kd">public</span> <span class="nf">ReadModelService</span><span class="o">(</span><span class="nc">OrderRepository</span> <span class="n">orderRepository</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">this</span><span class="o">.</span><span class="na">orderRepository</span> <span class="o">=</span> <span class="n">orderRepository</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="nd">@EventListener</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">handleOrderCreated</span><span class="o">(</span><span class="nc">OrderCreatedEvent</span> <span class="n">event</span><span class="o">)</span> <span class="o">{</span>
        <span class="nc">Order</span> <span class="n">order</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Order</span><span class="o">();</span>
        <span class="n">order</span><span class="o">.</span><span class="na">setId</span><span class="o">(</span><span class="n">event</span><span class="o">.</span><span class="na">getOrderId</span><span class="o">());</span>
        <span class="n">order</span><span class="o">.</span><span class="na">setTotal</span><span class="o">(</span><span class="n">event</span><span class="o">.</span><span class="na">getTotal</span><span class="o">());</span>
        <span class="n">order</span><span class="o">.</span><span class="na">setStatus</span><span class="o">(</span><span class="nc">OrderStatus</span><span class="o">.</span><span class="na">CREATED</span><span class="o">);</span>
        <span class="n">orderRepository</span><span class="o">.</span><span class="na">save</span><span class="o">(</span><span class="n">order</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="nd">@EventListener</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">handleOrderPaid</span><span class="o">(</span><span class="nc">OrderPaidEvent</span> <span class="n">event</span><span class="o">)</span> <span class="o">{</span>
        <span class="nc">Order</span> <span class="n">order</span> <span class="o">=</span> <span class="n">orderRepository</span><span class="o">.</span><span class="na">findById</span><span class="o">(</span><span class="n">event</span><span class="o">.</span><span class="na">getOrderId</span><span class="o">()).</span><span class="na">orElseThrow</span><span class="o">();</span>
        <span class="n">order</span><span class="o">.</span><span class="na">setStatus</span><span class="o">(</span><span class="nc">OrderStatus</span><span class="o">.</span><span class="na">PAID</span><span class="o">);</span>
        <span class="n">orderRepository</span><span class="o">.</span><span class="na">save</span><span class="o">(</span><span class="n">order</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="nd">@EventListener</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">handleOrderShipped</span><span class="o">(</span><span class="nc">OrderShippedEvent</span> <span class="n">event</span><span class="o">)</span> <span class="o">{</span>
        <span class="nc">Order</span> <span class="n">order</span> <span class="o">=</span> <span class="n">orderRepository</span><span class="o">.</span><span class="na">findById</span><span class="o">(</span><span class="n">event</span><span class="o">.</span><span class="na">getOrder</span>

<span class="nf">Id</span><span class="o">()).</span><span class="na">orElseThrow</span><span class="o">();</span>
        <span class="n">order</span><span class="o">.</span><span class="na">setStatus</span><span class="o">(</span><span class="nc">OrderStatus</span><span class="o">.</span><span class="na">SHIPPED</span><span class="o">);</span>
        <span class="n">orderRepository</span><span class="o">.</span><span class="na">save</span><span class="o">(</span><span class="n">order</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="kd">public</span> <span class="nc">List</span><span class="o">&lt;</span><span class="nc">Order</span><span class="o">&gt;</span> <span class="nf">getAllOrders</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="n">orderRepository</span><span class="o">.</span><span class="na">findAll</span><span class="o">();</span>
    <span class="o">}</span>
<span class="o">}</span>


</code></pre></div><h3>
  <a name="explanation-of-keywords" href="#explanation-of-keywords">
  </a>
  Explanation of Keywords
</h3>

<ul>
<li>
<strong>@EventListener</strong>: Indicates that the method should be invoked when an application event is published.</li>
<li>
<strong>OrderCreatedEvent</strong>: Event representing the creation of an order.</li>
<li>
<strong>OrderPaidEvent</strong>: Event representing the payment of an order.</li>
<li>
<strong>OrderShippedEvent</strong>: Event representing the shipment of an order.</li>
</ul>
<h3>
  <a name="clientside-implementation" href="#clientside-implementation">
  </a>
  Client-Side Implementation
</h3>

<p>Implement a simple client-side interface to interact with the order management system.</p>
<div class="highlight"><pre class="highlight html"><code>

<span class="cp">&lt;!DOCTYPE html&gt;</span>
<span class="nt">&lt;html&gt;</span>
<span class="nt">&lt;head&gt;</span>
    <span class="nt">&lt;title&gt;</span>E-commerce Order Management<span class="nt">&lt;/title&gt;</span>
    <span class="nt">&lt;script&gt;</span>
        <span class="k">async</span> <span class="kd">function</span> <span class="nf">createOrder</span><span class="p">()</span> <span class="p">{</span>
            <span class="kd">const</span> <span class="nx">response</span> <span class="o">=</span> <span class="k">await</span> <span class="nf">fetch</span><span class="p">(</span><span class="dl">'</span><span class="s1">/orders</span><span class="dl">'</span><span class="p">,</span> <span class="p">{</span> <span class="na">method</span><span class="p">:</span> <span class="dl">'</span><span class="s1">POST</span><span class="dl">'</span><span class="p">,</span> <span class="na">headers</span><span class="p">:</span> <span class="p">{</span> <span class="dl">'</span><span class="s1">Content-Type</span><span class="dl">'</span><span class="p">:</span> <span class="dl">'</span><span class="s1">application/json</span><span class="dl">'</span> <span class="p">},</span> <span class="na">body</span><span class="p">:</span> <span class="nx">JSON</span><span class="p">.</span><span class="nf">stringify</span><span class="p">({</span> <span class="na">total</span><span class="p">:</span> <span class="mi">100</span> <span class="p">})</span> <span class="p">});</span>
            <span class="kd">const</span> <span class="nx">order</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">response</span><span class="p">.</span><span class="nf">json</span><span class="p">();</span>
            <span class="nf">displayOrder</span><span class="p">(</span><span class="nx">order</span><span class="p">);</span>
        <span class="p">}</span>

        <span class="k">async</span> <span class="kd">function</span> <span class="nf">displayOrder</span><span class="p">(</span><span class="nx">order</span><span class="p">)</span> <span class="p">{</span>
            <span class="kd">const</span> <span class="nx">orderElement</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nf">createElement</span><span class="p">(</span><span class="dl">'</span><span class="s1">div</span><span class="dl">'</span><span class="p">);</span>
            <span class="nx">orderElement</span><span class="p">.</span><span class="nx">textContent</span> <span class="o">=</span> <span class="s2">`Order ID: </span><span class="p">${</span><span class="nx">order</span><span class="p">.</span><span class="nx">id</span><span class="p">}</span><span class="s2">, Total: </span><span class="p">${</span><span class="nx">order</span><span class="p">.</span><span class="nx">total</span><span class="p">}</span><span class="s2">, Status: </span><span class="p">${</span><span class="nx">order</span><span class="p">.</span><span class="nx">status</span><span class="p">}</span><span class="s2">`</span><span class="p">;</span>
            <span class="nb">document</span><span class="p">.</span><span class="nf">getElementById</span><span class="p">(</span><span class="dl">'</span><span class="s1">orders</span><span class="dl">'</span><span class="p">).</span><span class="nf">appendChild</span><span class="p">(</span><span class="nx">orderElement</span><span class="p">);</span>
        <span class="p">}</span>
    <span class="nt">&lt;/script&gt;</span>
<span class="nt">&lt;/head&gt;</span>
<span class="nt">&lt;body&gt;</span>
    <span class="nt">&lt;h1&gt;</span>E-commerce Order Management<span class="nt">&lt;/h1&gt;</span>
    <span class="nt">&lt;button</span> <span class="na">onclick=</span><span class="s">"createOrder()"</span><span class="nt">&gt;</span>Create Order<span class="nt">&lt;/button&gt;</span>
    <span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">"orders"</span><span class="nt">&gt;&lt;/div&gt;</span>
<span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>


</code></pre></div><h3>
  <a name="explanation-of-keywords" href="#explanation-of-keywords">
  </a>
  Explanation of Keywords
</h3>

<ul>
<li>
<strong>fetch</strong>: A JavaScript function to make HTTP requests.</li>
<li>
<strong>async/await</strong>: JavaScript syntax for handling asynchronous operations.</li>
</ul>
<h2>
  <a name="testing-and-debugging" href="#testing-and-debugging">
  </a>
  Testing and Debugging
</h2>
<h3>
  <a name="testing-cqrs-and-event-sourcing" href="#testing-cqrs-and-event-sourcing">
  </a>
  Testing CQRS and Event Sourcing
</h3>

<ul>
<li>Use integration tests to validate the command and read models.</li>
<li>Write unit tests to ensure events are correctly generated and handled.</li>
</ul>
<h3>
  <a name="debugging-tips" href="#debugging-tips">
  </a>
  Debugging Tips
</h3>

<ul>
<li>Enable detailed logging for commands, events, and queries.</li>
<li>Use monitoring tools to track event flow and system performance.</li>
</ul>
<h2>
  <a name="conclusion" href="#conclusion">
  </a>
  Conclusion
</h2>

<p>In this article, we explored how to implement CQRS and Event Sourcing in a distributed system using Java and Spring Boot. We covered the essential concepts, set up a project, and implemented a real-world e-commerce order management system. By leveraging these patterns, you can build systems that are scalable, maintainable, and resilient. We encourage you to experiment with these technologies and explore additional features such as event replay, sagas, and eventual consistency.</p>
Enter fullscreen mode Exit fullscreen mode
Enter fullscreen mode Exit fullscreen mode

Top comments (0)