CQRS
Command Query Responsibility Segregation

Piotr Pelczar
me@athlan.pl
CQRS
• Greg Young

• different model to update information
• than the model you use to read information
• http://martinfowler.com/bliki/CQRS.html
CQRS is…
CQRS is…
NOT the design pattern
CQRS is…
architecture?
CQRS is…
architecture?
no
i should say
CQRS is…
architectural pattern
CQS
• Command–query separation
• every method should either be a command
that performs an action
• or a query that returns data to the caller
• but not both = no side effects
• Asking a question should not change the
answer.
Basic terms
• Queries – Read models
• Commands – Write models
CQRS
• Queries – Read models, two-way
• Commands – Write models, one-way

http://berb.github.io/diploma-thesis/community/091_archtrends.html
Why CQRS?
• Queries
(READ PERFORMANCE !)
1. can be routed to
•
•

fast in-memory
non-transactional databases
Operation
L1 cache reference
Branch mispredict
L2 cache reference
Mutex lock/unlock
Main memory reference

Latency
0.5 ns
5 ns
7 ns
25 ns
100 ns

Compress 1K bytes w/ cheap algorithm

3,000 ns

Send 2K bytes over 1 Gbps network

20,000 ns

Read 1 MB sequentially from memory

250,000 ns = 0.25ms

Round trip within same datacenter

500,000 ns

Disk seek

10,000,000 ns

Read 1 MB sequentially from disk

20,000,000 ns = 20ms (-80x)

Send packet CA->Netherlands->CA

150,000,000 ns
Why CQRS?
• Queries
(READ PERFORMANCE !)
2. can query well prepared denormalised datasets
(without relations, no JOINS’s – just raw data)
DATA DENORMALIZATION !!
Imagine ideal dataset you want to receive in
your application use case
… and prepare it in background.
Why CQRS?
• Queries
(READ PERFORMANCE !)
3. warehouse approach:
can select aggregated datasets/tables
for e.x. report tables that contains numbers
(metrics) groupped by the domain (dimension)
SELECT SUM(price), SUM(transactions)
FROM acc_by_day
WHERE g_year = 2013 AND g_month = 8
Why CQRS?
SELECT SUM(price), SUM(transactions)
FROM acc_by_day
WHERE g_year = 2013 AND g_month = 8
g_year

g_month

g_day

price

transactions

2013

08

11

7839

12

2013

08

12

12897

16

2013

08

13

5326

10

2013

08

14

2357

5

2013

08

15

3687

6

2013

08

16

5478

11
Why CQRS?
• Queries
(READ PERFORMANCE !)
1. can be routed to fast in-memory nontransactional databases
2. can query well prepared datasets
(without relations)
3. warehouse purpose:
can select aggregates datasets/tables
for e.x. report tables that contains numbers
(metrics) groupped by the domain (dimension)
Commands
• Commands are imperatives
• Produces Events
• Requests for the system
to perform a task or action
Events
• Produced by Commands
• Commands publishes
– one-way
– asynchronous messages
– that are published to multiple recipients

• Events are descriptions of actions to be
happened (for e.x. item’s description changed)
• Events are handeled by handlers
to perform some actions
Event handlers
• Subscribes for Events occurence
• Performs some actions:
– Persists new state of object
– Updates aggregates
Processing
• Events can be catched by handlers
in two ways:
– Synchronously
(immediately after perform in the same thread or
sub-thread)
like: myListener.onSthChanged(Event e)

– Asynchronously
(added to queued system to futher process)
messageBroker.publish(Event e)
Embracing eventual consistency
• Database systems:
– atomicity
(the action is one trasactional operation),

– consistency,
– isolation
(other processes cannot access local variables),

– durability
(all data is persisted)

• (ACID) properties of transactions
Data consistency
• Data on the read side will be eventually
consistent with the data on the write side
• When you query data, it may be out of date

• Don’t say: data are not consistent –
that sounds bad …
data is temporarly out of date… - better
Data consistency
• You can deal with consistency by using
distributed transactions
• Distributed transaction = performance issues
with synchronization
• The faster side waits to another, but is ready
http://msdn.microsoft.com/en-us/library/jj591577.aspx
Data consistency
• Distributed transaction = performance issues
with synchronization
• The faster side waits to another,
unless it is ready
http://msdn.microsoft.com/en-us/library/jj591577.aspx
Data consistency
• That’s why use queues – write performance
• Messaging and CQRS:
– Commands (one recever)
– Events (many receivers)
CQRS messaging problems
Consider system bottlenecks:

versioning

handshake and
persistence

•
•
•
•

Duplicated messages
Unordered messages
Lost messages
Unprocessed messages
We need messaging system
• Simple queue
• Work queues
(one consumer)

• Publish/Subscribe
(many consumers)
RabbitMQ

• Open source message broker
• written in the Erlang
• Implements the Advanced Message Queuing
Protocol (AMQP)
– also: Apache ActiveMQ, Windows Azure Service Bus
RabbitMQ
• Decopules publishers and consumers
Producer

Consumer

• Queueing for later delivery
• Load balancing (round-robin) and scalability
RabbitMQ reliability
• Round-robin dispatching
• Message acknowledgment (after processing)
– message had been received, processed and that
RabbitMQ is free to delete it
– no message timeouts - RabbitMQ will redeliver
the message only when the worker connection
dies
RabbitMQ reliability
• Acknowledgments are turned on by default
• You have to disable ack and send
programmatically!

$ sudo rabbitmqctl list_queues name
messages_ready
messages_unacknowledged
RabbitMQ reliability
• Durable Queues
1. Make sure that RabbitMQ will never lose
our queue:
durable = true
2. Send message in persistent mode:

delivery_mode = 2
# make message persistent
RabbitMQ + Node.js
• https://github.com/postwait/node-amqp
$ npm install amqp

• I have reported issue in releasing, so include in
your packages.json to have the newest
version directly from github tarball:
"dependencies": {
"amqp": "https://github.com/postwait/node-amqp/tarball/master"
}
RabbitMQ + Node.js
Examples…
CQRS in Grails
• Install plugins:
– JMS
– ActiveMQ

• In your BuildConfig.groovy
plugins {
compile ":jms:1.2",
compile ":activemq:0.4.1" // mvn
}
CQRS in Grails
Examples…
NCQRS (.NET)
• Framework for .NET
– command handling,
– domain modeling,
– event sourcing

• Provides interfaces
NCQRS (.NET)
Examples…
Literature
• http://msdn.microsoft.com/en-us/library/jj554200.aspx
• https://github.com/ncqrs/ncqrs
Literature
• http://www.slideshare.net/rabbitmq/amqp-and-rabbitmq
• http://www.udidahan.com/2011/10/02/why-you-should-beusing-cqrs-almost-everywhere%E2%80%A6/
• http://simon-says-architecture.com/tag/cqrs/
CQRS

Q&A?
http://slideshare.net/piotrpelczar/cqrs

Piotr Pelczar
me@athlan.pl

CQRS