DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Two AI Agents, One Database: Solving Concurrent Updates With Optimistic Locking
  • MySQL Instant DDL: Metadata Locks, Waiting Writers, and Query Pileups
  • Should Developers Switch From SQLite to DuckDB?
  • Adding Graph Retrieval to a RAG Pipeline With Python

Trending

  • How Performance Engineers Find and Fix Hidden System Bottlenecks
  • 5 Proven Strategies for Modernizing Legacy .NET Applications
  • Why Your Test Automation Is Always Behind the Code And the Architecture That Fixes It
  • Agentic Test Creation: From Plain-Language Requirements to End-to-End Test Cases

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. CQRS by Example (Introduction)

CQRS by Example (Introduction)

CQRS can be a confusing pattern, but it can also be very useful. Let's take a quick look at it as well as the queries and commands that make up the pattern.

By 
Michele Ferracin
·
Nov. 06, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
46.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

I'm doing some R&D for a future project at work and I'm currently exploring some architectural prototypes. In the last few weeks, I explored the CQRS pattern.

CQRS thanks to the separation of the read/query services from the action/command services enables us to do many things.

At a very basic level applying CQRS means something like this (in psuedo-code).

OrderingService:
void Ship(OrderId)
Order GetOrder(OrderId)
void ChangeOrderShipmentAddress(OrderId, NewAddress)
void CreateOrder(Order)
void ChangeOrderPaymentMethod(OrderId, PaymentMethod)


Applying CQRS on this would result in two services:

OrderingWriteService:
void Ship(OrderId)
void ChangeOrderShipmentAddress(OrderId, NewAddress)
void CreateOrder(Order)
void ChangeOrderPaymentMethod(OrderId, PaymentMethod)


OrderingReadService:
Order GetOrder(OrderId)


Done! That is the whole CQRS pattern applied.

This simple separation enables us to do many interesting things from an architectural point of view. This means that the data models used for querying and updates can be different. They can stay in the same data store or be completely different.

Command

A Command is the only way to change the state of our system. Commands are responsible for introducing all changes to the system. Commands should not return any value.

Query

A Query is a read-only operation. With a Query, we read the state of a part of our system. We can do filters, projections, and all sorts of data transformations to deliver data to the UI in the most useful format. A Query does not modify the state of our system. It can be run many times without side-effects.

Summary

With this first blog post about CQRS, we explored the fundamentals and some basic terms of the pattern. In the next blog posts, we'll write some code to practice the implementation of such a pattern and get our hands dirty!

References

  • Microsoft Docs - https://docs.microsoft.com/en-us/azure/architecture/patterns/cqrs
  • Martin Fowler - https://martinfowler.com/bliki/CQRS.html
  • Greg Young - http://codebetter.com/gregyoung/2010/02/16/cqrs-task-based-uis-event-sourcing-agh/
Database

Published at DZone with permission of Michele Ferracin. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Two AI Agents, One Database: Solving Concurrent Updates With Optimistic Locking
  • MySQL Instant DDL: Metadata Locks, Waiting Writers, and Query Pileups
  • Should Developers Switch From SQLite to DuckDB?
  • Adding Graph Retrieval to a RAG Pipeline With Python

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook