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

CQRS meets modern Java

CQRS meets modern Java

Command Query Responsibility Segregation (CQRS) is a proven design pattern that enables the clear separation of data changes (commands) and data queries (queries). This separation allows these responsibilities to be scaled and optimized independently. The application code is also structured more understandably, which improves maintainability.

This talk will deepen our understanding of the practical implementation of the CQRS principles using modern Java language features such as records, sealed classes, switch expressions, and pattern matching. An example application will demonstrate how the theoretical foundations of CQRS can be applied in practice and how this affects application design while increasing data access efficiency.

Simon Martinelli

May 14, 2024
Tweet

Video

More Decks by Simon Martinelli

Other Decks in Programming

Transcript

  1. About Me • 30 years in software engineering • 25

    years with Java • Self-employed since 2009 • Teacher at two universities in Switzerland • JUG Switzerland
  2. Issue 1: Too Much • Over fetching • Loading data

    that is not needed • Not all data can be changed • Too much data is sent in the update request • Potential bugs if blindly mapped
  3. Issue 2: Too Little • Under fetching • Results in

    multiple requests • Produces the N1 select problem on the client-side • Under storing • Updates/inserts not within the same transaction
  4. Modern Java: Records • Provide a compact syntax for declaring

    classes as transparent holders for immutable data Preview: 14 Final: 16 record CreateOrderCommand(long customerId) { }
  5. Modern Java: Sealed Classes • Sealed classes and interfaces restrict

    which other classes or interfaces may extend or implement them Preview: 15 Final: 17 abstract sealed class Shape permits Circle, Rectangle, Square { }
  6. Modern Java: Switch Expression • Exhaustive: the switch expression must

    cover all cases • Preview in Java 12 and finalized in Java 14 String result = switch (color) { case RED -> "Red"; case GREEN -> "Green"; case BLUE -> "Blue"; }; Preview: 12 Final: 14
  7. Modern Java: Pattern Matching • instanceof operator • switch expression

    and statement double area = switch (shape) { case Circle c -> Math.PI * c.radius * c.radius; case Rectangle r -> r.length * r.width; default -> throw new IllegalArgumentException(); }; Preview: 14/17 Final: 17/21
  8. Modern Java: Record Patterns • Use to test whether a

    value is an instance of a record class type and, if it is, recursively perform pattern matching on its component values record Pair(int left, int right) {} if (p instanceof Pair(int left, int right)) { ... } Preview: 20 Final: 21
  9. Why jOOQ? • Commands • Usually, insert, update, or delete

    data in the database • Use jOOQ to generate minimal updates • Queries • Often return nested structures like Order à Item • jOOQ provides the MULTISET value constructor
  10. Conclusion Separation of commands and queries • Improves understandability •

    Helps to avoid over- and under-fetching • Can make mapping Entity ↔ DTO obsolete • Enables an event-driven approach
  11. Thank you! • Web https://martinelli.ch • EMail [email protected] • Bluesky

    @martinelli.ch • LinkedIn https://linkedin.com/in/ simonmartinelli