{"id":153,"date":"2020-02-12T12:58:32","date_gmt":"2020-02-12T12:58:32","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/microservices\/?p=153"},"modified":"2020-02-12T12:58:32","modified_gmt":"2020-02-12T12:58:32","slug":"understanding-the-architecture-of-event-based-cqrs-design","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/microservices\/understanding-the-architecture-of-event-based-cqrs-design\/","title":{"rendered":"Understanding the Architecture of Event-Based CQRS Design"},"content":{"rendered":"<p>In this article, I am going to explain the various components of a simple <strong>Event-based CQRS Architecture<\/strong>.<\/p>\n<p>&nbsp;<\/p>\n<p>Take a look at Figure 1<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_155\" aria-describedby=\"caption-attachment-155\" style=\"width: 640px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/Event-Based-CQRS-Architecture.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-155 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/Event-Based-CQRS-Architecture-1024x715.jpg\" alt=\"\" width=\"640\" height=\"447\" srcset=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/Event-Based-CQRS-Architecture-1024x715.jpg 1024w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/Event-Based-CQRS-Architecture-300x210.jpg 300w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/Event-Based-CQRS-Architecture-768x536.jpg 768w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/Event-Based-CQRS-Architecture.jpg 1061w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><figcaption id=\"caption-attachment-155\" class=\"wp-caption-text\">Figure 1: Event-Based CQRS Architecture<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p><strong>Controller<\/strong>: This is also called the UI Controller. It is responsible for\u00a0 intercepting both read and write operations comming from the client. So when you enter a url in your browser, and and hit &lt;Enter&gt;, the request immediately routes to the controller<\/p>\n<p><strong>Commands<\/strong>: Whatever request is coming from the controller could either be a command or a query.\u00a0 A command represents\u00a0 an intent to modify the state of an entity. This is normally in form of a DELETE, POST or PUT request<\/p>\n<p><strong>Command Gateway<\/strong>: A Command Gateway provides an interface for choosing how to dispatch your commands. Normally, the command coming from the controller ges to the CommandGateway. It could dispatch the command either synchronoously or asznchromously.<\/p>\n<p><strong>Command Handler<\/strong>:\u00a0 A command handler receives and responds to specific type of commands. It then executes some business rule or logic based on the command. It retrieves the domain enities and make the neccesary changes on them.<\/p>\n<p><strong>Domain Entity<\/strong>: Domain Entities are aggregate entities that are modeled to reprsent the state of the components in the domain. An <strong>aggregate<\/strong> is an entity or group of entities that are always kept in a consistent state. For example, an Employee entity could be an aggregate. It could have additional aggregates like Address, NextOfKin etc. If the state of an aggregate changes, then a dmain event is generated.<\/p>\n<p><strong>Repository<\/strong>: A repository just as you may know provides functionality for managing aggregates. For example, a repository helps you find, save or delete an aggregate. A repository, could interface with a persistent storage(like a database) to store the aggregates. A repository could also use an event store to call up and aggregate and recreate it\u2019s state at any point in history based on saved states.<\/p>\n<p><strong>Event Store:<\/strong> This is a storage of all the changes that have been applied to an entity. It is like a history of events. An event store can replay\u00a0these historical changes and therefore rebuild the state of an entity at any point in time. Event store may also use a database for event storage.<\/p>\n<p><strong>Event:<\/strong> An event is a notification that there has been a change to the state of an entity. So when an update is made, then an event is emitted to say, what changes actually took place.<\/p>\n<p><strong>Event Bus:<\/strong> This is like a channel for generated events. They are normally backed up by a messaging topic in a publish\/subscribe manner.<\/p>\n<p><strong>Event Handler:<\/strong> These are methods the receive events and handle the events. For example, when an event is emited for an update to an entity, then an event handler recieves this event from the event bus. It could then update the WriteDB by mirroring this changes to materialized views.<\/p>\n<p><strong>Query:<\/strong>\u00a0 Think of a query as the opposite of a command. A query is a request for data without making and changes. When a query comes from the controller, it is routed to the query manager. Then the query manager manager executes the query and returns the results the to client.<\/p>\n<p>&nbsp;<\/p>\n<p>I recommend you follow this tutorial on building an<a href=\"https:\/\/www.kindsonthegenius.com\/microservices\/cqrs-with-axon-tutorial-part-1-introduction-and-setup-in-intellij\/\">\u00a0CQRS-based application<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, I am going to explain the various components of a simple Event-based CQRS Architecture. &nbsp; Take a look at Figure 1 &nbsp; &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,27,2,1],"tags":[6,8],"class_list":["post-153","post","type-post","status-publish","format-standard","hentry","category-axon","category-axonframework","category-microservices","category-uncategorized","tag-cqrs","tag-event-sourcing"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/comments?post=153"}],"version-history":[{"count":3,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/153\/revisions"}],"predecessor-version":[{"id":157,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/153\/revisions\/157"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/media?parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/categories?post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/tags?post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}