Two weeks ago I wrote a blog post as an introduction to event sourcing. Many of them got the subject with great interest, there was a lot of reaction, and of course there were many questions. In today’s blog post, I raise many of these questions and answer them. Above all, you get another introduction to a concept related to event sourcing: today it is also about an architectural pattern called CQRS. What is that, for whom it is good how it works, what the application is, how you can combine it with event sourcing, and so on – you will learn all this today.
Advertisement
Golo Roden is the founder and CTO of the native web GMBH. It deals with web and cloud application as well as conception and development of APIs with web and cloud application focusing on event-powered and service-based distribution. Their guiding theory is that software development is not an end in itself, but always has to follow an underlying professionalism.

With or without event sourcing with or without CQRS?
Let’s start, very briefly, with an explanation: maybe you have heard about CqRS and now you would like to know what it really is, but perhaps you have you blog about event sourcing two weeks ago Did not read the post. Now the question arises: Can you read today’s post “Just”, or should you first deal with the subject of event sourcing?
Well, this is very important: CQRS and event sourcing are two concepts that exist independently. This means that you can apply CQRS without using event sourcing, and you can also apply event sourcing without using CQRS, and in this regard you can apply this blog post to a self -respecting subject Can also see in form.
But: Two concepts complement each other very well. In this regard, they will rarely find them in separation in behavior, but usually together. This is the reason that I build on what I wrote in this blog post two weeks ago today. So if you have not yet seen that blog post, I would advise you to make it for it. Alternatively, you can Also as a video Look
What is CQRS?
But what is CQRS anyway? The brief name is for “Command Query responsibility isolation”, namely to separate the responsibilities for orders and questions.
In fact, it is much easier than it seems at first. The main idea of ​​CQRS is that there are only two types of interaction patterns for access to applications: either the user takes an action with the intention of changing something in the system, or he wants to know something. There are no other options. This means that you either share a software that you should do this or for you, then give it a command (this is such a “command”) – or you ask the software for information, so you information a information Provide questions or take out a query (and this is such a “querry”).
Recommended editorial material
With your consent, an external YouTube video (Google Ireland Limited) has been invited here.
Always load youtube videos
CQRS – the only video you need // German
One, that is, one command, usually draws a change and thus reaches the reading process, the other, ie a query, only. So you can also say: Using writing and reading. The CQRS design pattern now says that you should use your application in two: the writing processes in the part, and in the second part that takes care of the reading processes. So they should, and it was really my original statement that separated the responsibility to write from her to read.
What this design pattern says what this separation is. So it is completely open whether it means that you are mentally different between writing and reading API routes, or do you build two separate APIs, or whether these two different services Are, or something else. As it says in English, it is “for interpretation”. However, we later come to speak in more detail.
Why the whole thing?
The first question is why should you do this? So why is this recommended CQRS pattern?
Well, there is a very simple idea behind it, and it has little to do with API, but with a database too much: When we talk about classic architecture, we usually have a UI. , Somehow a kind of API and finally a database. Although the database is not literally focused on development, we usually deal with it in detail in software development, and often the data models selected in the database have a significant impact of how business logic in API is made. This means that an essential question is: What does a good data model look like?
There is a suitable answer to the academic world: database theory has different -common forms. You can imagine a little bit like a sentence of rules on how to design data models. In particular, five of them are different variants, and the fifth normal form matches a little with the sand of the holy comb: if you apply it, the data does not have excess, and so on. This is desirable because it can be applied very easily. From this point of view, the fifth normal form is a dream when it comes to writing data.
Where light is also a shadow
It is a bit sad that you usually have to read the data again. This is certainly possible in the fifth general form, only you usually have to complete very complex questions, and then you need 27 joins, for example, for some simple reading like the user’s master data. This means that reading is quite possible, but it is completely disabled and slow. Of course, it is very impractical and not very useful in everyday life. So, briefly: fifth normal form is well suited to write, but a disaster to read.
The other extreme will be the first common form: Here is a suitable table for every view that is available in the UI, and the data is distributed wherever you need it. This means that the data is completely lifeless, which reads fast: because more than one SELECT * FROM xy
It is usually not necessary, after all, according to the definition there is a suitable table for each view. In this regard, the first general form is a dream when it comes to reading data.
However, writing in this case is difficult, because you have to keep the same information many times, you have to maintain it many times, and therefore maintaining continuity and integrity is very complicated. So you can tell that the first common form is the opposite of fifth: the first is good for reading, but writing is bad.
