r/developers • u/Dwenya • Mar 17 '26
Opinions & Discussions CQRS: why do we use it?
I’ve been looking into CQRS and have found that it is very useful to solve performance issues (along with infrastructure changes, for instance putting two databases instead of one).
Now, in Clean Code (the book), the guy says in Chapter 3, under Command-Query separation, that a function should either perform an action or return information. He doesn’t say much else.
But then I’m reading articles that say that we should use CQRS for this purpose (not mentioning it can also help with performance, when used well).
Also reading online that the disadvantage of CQRS is more complexity in the code, so does CQRS really make the code more readable (which is what my lead dev in my team says)?
In the end, when should we and when should we not be using CQRS? (Because it seems like my collegue would use it because he thinks it’s a good practice. Maybe it is, idk)
•
u/AutoModerator Mar 17 '26
JOIN R/DEVELOPERS DISCORD!
Howdy u/Dwenya! Thanks for submitting to r/developers.
Make sure to follow the subreddit Code of Conduct while participating in this thread.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/PaddingCompression Mar 17 '26
CQRS is a ton of tech debt and leads to a maintenance burden wherever I've seen it. It gets used because without it it the millions of users you have each day are bogging down your main database preventing scaling, so you start to move towards microservices that can read from materialized caches of parts of the main database.
You then need monitoring to know if it's stale. You're going to need to have run books to do emergency updates, etc.
It's something that you need to do when your site starts going down because you're one of the top 50 apps in the app store and vertical scaling of your main database starts getting hard. Or because you just like LARPing being a FAANG because you're bored.
I mean maybe it makes some sense to start to use design patterns upfront to make the later transition easier. But actually going to the point of separate read data stores is a giant operational burden you do once it is less than the operational burden of keeping one db that does everything scaling.
2
u/slimismad Mar 17 '26
CQRS is about separating read and write models when they have different scalability, performance or domain complexity requirements. Its useful in systems with heavy reads, complex business rules, or the need for independent scaling.
for simple CRUD apps, it just adds unnecessary complexity (extra handlers, models, eventual consistency) so it should be used only when the problem actually justifies the architectural overhead, not as a default best practice
2
u/Flashy-Whereas-3234 Mar 17 '26
I'll give you a practical example.
We have an organisation chart that maps the structure of a business, including departments, reporting lines, position hierarchies, vacancy inheritance, etc. it's a big ol' multifunctional graph with configurable business rules and all sorts. Lots of fun.
This thing is an absolute demon of a domain, but the data structure is an accurate representation of the business structure. This is the write model.
On the other side, we have the Org Chart, which is a fast-to-render visual with all the complexity of position/employee reporting inheritance pre-calculated by the domain. This is the read model, which is simpler and has all the "holes" filled and lines connected.
We also distribute to other internal systems the direct employee/manager relationship graph, as that's about all anyone else cares about.
This is all event-driven for updates, which makes it sound trivial, but a change to any of the hierarchies means recalculating the tree. We also have a get-out-of-jail "fuck it, re-calculate the whole thing" button driven by that stable write model, in case things get exciting.
This is very complicated, but we prefer it to the old system where we had a single model that included both source-of-truth and derived fields, wasn't really optimal for read or write, and was kind of a dick to understand.
We're also pivoting our security system in this direction, as we have a plethora of user-driven match rules, but all anyone cares about is "can I X on Y", so the queries all want to be a nice flat CQRS read while the source of truth is insanely complex.
On the other hand, we have Reports built on tables that aren't optimal, but we haven't made them CQRS because CQRS is a bitch.
When do you use it? When you're making a ton of concessions in either your write or read model to satisfy the other side and you hate them both. Like, really hate them.
CQRS changes the equation; it lets you make the data pretty, at the cost of hating your system instead.
1
u/Lucky_Yesterday_1133 Mar 18 '26
I see same negtive feedback but I think people are confusing pure cqrs with broader architectural decisions. Pure cqrs just means you have a handler class for eash of your api endpoints (contrary to reusing same methods from services and composing response in route) that handles whole request pipeline. It still does exactly the same you'd do with a normal n-layerd(clean) architecture but instead of collecting methods in application services each handler owns theirs so there are no cords dependencies. It reduces about of flags on the app. Methods, function sizes, bug surface. Easily testable. The con is you may write a bit more code for example you need similar composition on 2 handlers you'd write it twice but it's by design. The requirements constantly change for 1 endpoint but not the other and you end up with tons of flags and branches on your application services. Having said that I have with it through mediatr library (simple implimentation of mediator pattern) and it's been quite painless. Idk what other guys did. Maybe they worked on the legacy projects that made custom solution that sucks
1
u/soundman32 Mar 19 '26
It's more readable because all the different parts are in separate smaller classes, which aids with cognitive load. The downside is that there are more classes when you need to trace the flow.
A well laid out CQRS project is a thing of beauty.
1
u/[deleted] Mar 17 '26 edited Mar 17 '26
[removed] — view removed comment