CQRS by Example
With Membership
Suggested price

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!

About the Authors

Carlos Buenosvinos
Carlos Buenosvinos

I am an Extreme Programmer (XP) and DevOps with more than 20 years of experience in developing Web and Mobile Applications. For the last ten years, I have played various leading roles such as Tech Lead, VP of Engineering and CTO. I have mentored engineering and product teams up to 150 members in multiple different markets such as E-commerce, E-Learning, Payment Processing, Classifieds, and Recruiting Market.

As an employee and consultant, I have contributed to the success of start-ups and well-established brands. Some examples are SEAT, XING, Atrápalo, PCComponentes, Emagister, eBay, Lowpost, Vendo, Riplife, Universitat International de Catalunya (UIC), and many more.

I am the happy creator of Ansistrano, the most starred Ansible Galaxy role. I am also the author of the book Domain-Driven Design in PHP. I am also a conference speaker, and since 2016, I have a video blog about Development Best Practices called Rigor Talks. I organized the DevOps Barcelona Conference and the PHP Barcelona Conference.

My main areas of expertise are the Agile Team Management (Scrum and Kanban), Best Development Practices (Extreme Programming, Domain-Driven Design, and Microservice Architectures) and Digital Transformation (Agile, XP, and DevOps).

Christian Soronellas
Christian Soronellas

Christian is an Extreme Programmer and has over 15 years of experience helping tech companies succeed from a broad variety of roles, from Software Engineer to CTO. He has helped companies such as a Privalia, Emagister, Atrápalo, Enalquiler, PlanetaHuerto, PcComponentes or Opositatest. He is the author of the book Domain-Driven Design in PHP as well as a conference co-organizer of DevOps Barcelona Conference and PHP Barcelona Conference

Keyvan Akbary
Keyvan Akbary

Keyvan is an Engineering Leader and programmer with more than 15 years of experience crafting products customers love and helping teams succeed. He understands technology as a medium for providing value, not the end itself. He has a passion for Distributed Systems, Software fundamentals, SOLID principles, Clean Code, Design Patterns, Domain-Driven Design, and Testing; as well as being a sporadic Functional Programmer. For the last 7 years, he has also focused on growing teams in high scale-up product companies, advocating for customer-centric product development, Extreme Programming, DevOps, Lean and Kanban.

He has worked on countless projects as a freelancer, on video streaming at Youzee, tradesman marketplace at MyBuilder, in addition to founding his own crowdfunding startup Funddy, and leading FinTech teams at Wise. Currently, he is leading engineering in the ride-hailing space as Head of Engineering at Cabify.

He is also the author of Domain-Driven Design in PHP and CQRS by Example.

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...

80% Royalties. Earn $16 on a $20 book.

We pay 80% royalties. That's not a typo: you earn $16 on a $20 sale. If we sell 5000 non-refunded copies of your book or course 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

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub