CQRS by Example
CQRS by Example
Command-Query Responsibility Segregation is an architectural style for developing applications that split the Domain Model in Read and Write operations in order to maximize semantics, performance, and scalability. What are all the benefits of CQRS? What are the drawbacks? In which cases does it worth applying it? How does it relate to Hexagonal Architecture? How do we properly implement the Write Model and Read Models? How do we keep in sync both sides? What are the following steps to move towards Event Sourcing? This book will answer all these questions and many more, guided through lots of practical examples. Brought to you by the same authors behind "Domain-Driven Design in PHP".
About the Book
Four years have passed since our first publication, we established what we think is a solid ground for DDD building blocks and Hexagonal Architecture. It is time to explore how to solve the limitations some projects may face at scale.
We think CQRS is the next natural step forward and we want to guide you through it.CQRS is usually discussed along with Event Sourcing, a pattern where application state is a projection of the Domain Events that happen through its lifetime. Event Sourcing relies on having a stream of Domain Events to reconstitute state. Something convenient for operations that modify the state of the application, known as Commands; however, querying the system requires some special mechanisms that aren't that trivial. We can say than in Event Sourced systems CQRS is mandatory in order to generate the required information to be able to query later, but the opposite isn't true. We can develop a CQRS system without Event Sourcing, so worry not, this book won't cover Event Sourcing.
In this book, we'll explore Hexagonal Architecture drawbacks, and we'll dive into CQRS by exploring plenty of real examples that you can use in your projects. Even though code examples are written in PHP, the patterns and techniques described in this book are applicable to any programming language and likely any paradigm you may be using. We can't thank you enough for purchasing this book and be an active contributor of Domain-Driven Design, Hexagonal Architecture, and CQRS!
Table of Contents
- Foreword by Marco Pivetta
-
Preface
- Domain-Driven Design in PHP
- Domain-Driven Design Acceptance Has Rapidly Grown
- CQRS by Example
- Who Should Read This Book
- Summary of Chapters
- Code, Typos, and Examples
- Acknowledgements
-
About the Authors
- Carlos Buenosvinos
- Christian Soronellas
- Keyvan Akbary
-
CQRS and Domain-Driven Design
- What’s All the Fuss About?
- Why Domain-Driven Design Matters
- The Three Pillars of Domain-Driven Design
- Considering Domain-Driven Design
- The Tricky Parts
- Wrapup
-
A Journey Toward CQRS
- Introducing Cheeper, a Twitter Clone
- Architectural Styles
- A Brief Analysis
- Potential Limitations of Hexagonal Architecture
- Wrapup
-
Anatomy of CQRS
- Cheeper Use Case Analysis
- Cheeper à la CQRS
- CQRS Overview
- Other CQRS Components
- Two Sides of the Same Coin
- The Command Side
- The Query Side
- Syncing the Command and Query Sides
- Wrapup
-
Command Side and the Write Model
- Commands
- Command Handlers
- Command Bus
- Putting It All Together
- Wrapup
-
Query Side and the Read Models
- Queries
- Query Handlers
- Query Bus
- Putting It All Together
- Wrapup
-
Synchronizing the Write and the Read Models
- Multiple Read Models
- Synchronization Strategies
- Wrapup
-
The Full Picture
- The Timeline Use Case
- Posting a Cheep
- Command
- Command Bus
- Command Handler
- Domain Event
- Event Bus
- Event Handler
- Projection
- Projection Bus
- Projection Handler
- Fetching the Timeline
- Query
- Query Bus
- Query Handler
- Wrapup
-
Optimizations and Edge Cases
- Relying on Multiple Read Models
- Incremental Projections
- Race Conditions
- Trouble with Events
- Duplicated Messages
- Missing Events
- Dealing with Inconsistencies
- Wrapup
-
CQRS and Event Sourcing
- CQRS’ Biggest Challenge
- Recreating State from Past Events
- Introducing the Event Store
- Changes in Repositories
- Changes in Entities or Aggregates
- Changes in Projections
- Event Sourcing Tradeoffs
- Wrapup
-
Demo Time
- Getting Started
- Starting the Application
- Nothing Up My Sleeve
- Signing Up New Authors
-
Consuming
NewAuthorSigned
Events - Following Other Authors
-
Consuming
FollowCommand
Commands - Verifying an Author’s Followers
-
Consuming
AuthorFollowed
Events - Posting Cheeps
-
Consuming
PostCheepCommand
Commands -
Consuming
CheepPosted
Events -
Consuming
AddCheepToTimelineProjection
Projections - Verifying an Author’s Timeline
- Wrapup
- The End
- Bibliography
The Leanpub 60 Day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.
You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!
So, there's no reason not to click the Add to Cart button, is there?
See full terms...
Earn $8 on a $10 Purchase, and $16 on a $20 Purchase
We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earnedover $13 millionwriting, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them