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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Resolving Parameter Sensitivity With Parameter Sensitive Plan Optimization in SQL Server 2022
  • Comparing Managed Postgres Options on The Azure Marketplace
  • Useful System Table Queries in Relational Databases
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

Trending

  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • IoT and Cybersecurity: Addressing Data Privacy and Security Challenges
  • Prioritizing Cloud Security Risks: A Developer's Guide to Tackling Security Debt
  • Introduction to Retrieval Augmented Generation (RAG)
  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 user avatar
Michele Ferracin
·
Nov. 06, 18 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
46.5K Views

Join the DZone community and get the full member experience.

Join For Free

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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Resolving Parameter Sensitivity With Parameter Sensitive Plan Optimization in SQL Server 2022
  • Comparing Managed Postgres Options on The Azure Marketplace
  • Useful System Table Queries in Relational Databases
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!