Reputation: 2810
Let's say I need to query for all entities which comply with given criteria. My biggest confusion is about where the data should be taken from. I guess we have two option, either query event store itself or ask some other projection storage solution(have no idea - maybe some DB or what). It maybe depend on the amount of events processed. How will the implementation look like?
What about performance?.
In event store the best one can do is probably to create some snapshots, not sure anyway how to list complex so called "aggregates". In DB we will have nearly the same count of rows as events, which will end up in extensive DB optimalization. Not to mention that this DB can be dropped anytime and filled again which I completely cannot imagine. Do I get the things right or am I completely of the track?
Upvotes: 2
Views: 3350
Reputation: 2542
You don’t quite have the proper mental view of CQRS yet. The C of CQRS is for commands which only update data. They instantiate an entity in memory using events in the event store, make the proper changes, then store those changes back to the event store. This side is not meant for adhoc queries. Think of the Command side as data you are only allowed to access via the primary key.
The read side is where you would query for all entities with a particular attribute. The read side is built in a DB (not an event store) by special code that listens to the event store and adds/updates rows in the read side DB to keep it up to date. Note that the read side DB is not third normal form – data is often duplicated and there are very few foreign keys.
So, while it is technically possible to query most event stores for specific attributes, but it’s clumsy since that’s not what they were designed for. To get collections of objects you query the read side.
Walking through a typical example
If you keep the two sides separate in your mind, many questions of how to implement something just fade away.
Upvotes: 3
Reputation: 291
Event Sourcing: is a way of developing an application in which all information is persisted as events. For more info refer here
Example: If we are developing a banking software, the events that we store will be something like below
Now if the User1 wants to debit his/her account, then we pull all the events from his/her stream and replay to get the balance. And if he/she has sufficient balance then we debit his/her account by adding one debit event.
This process takes a bit of time, and it is generally done only when there is a need to write in to the system (Writes are less common than reads in most systems). Though this takes a bit of time, it gives us the benefit of audit-ablity, security and running analytics.
The difficulties in a event sourced system are for querying. If we need to get a list of users who deposited more than 10000 units in a quarter, then its going to be difficult and time consuming as we need to replay the every users stream. To solve this we can use CQRS.
CQRS: is a pattern of developing software where the writes are done by one application and reads are done by another application. More information available here. The write application and read application could communicate with each other (may be by messaging). The read application could maintain it's own database and store the information in the shape that is comfortable for querying. So when the read application gets a message, it converts the message that is suitable for it's own db schema and store it.
The read application will eventually (probably in couple of seconds) be consistent with the write application. The read application will be able to handle queries quickly very quickly as its database schema is designed for it.
Now answering your question:
Upvotes: 0