{"id":1953,"date":"2019-10-28T12:00:00","date_gmt":"2019-10-28T11:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/7-database-patterns-for-microservices-explained-with-diagrams\/"},"modified":"2026-07-05T03:24:42","modified_gmt":"2026-07-05T01:24:42","slug":"7-database-patterns-for-microservices-explained-with-diagrams","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/7-database-patterns-for-microservices-explained-with-diagrams\/","title":{"rendered":"7 Database Patterns for Microservices \u2013 Explained With Diagrams"},"content":{"rendered":"<p>We would learn about 7 database patterns for Microservices. The include:<\/p>\n<ol>\n<li><a href=\"#t1\">Database per Service<\/a><\/li>\n<li><a href=\"#t2\">Shared database<\/a><\/li>\n<li><a href=\"#t3\">Saga<\/a><\/li>\n<li><a href=\"#t4\">API Composition<\/a><\/li>\n<li><a href=\"#t5\">CQRS<\/a><\/li>\n<li><a href=\"#t6\">Domain event<\/a><\/li>\n<li><a href=\"#t7\">Event sourcing<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h5 id=\"t1\"><strong>1. Database per Service<\/strong><\/h5>\n<p>In this pattern, the microservice&#8217;s persistent data are kept private to that service and is can only be accessed through its API. A transaction by a service involves only it&#8217;s database.<\/p>\n<p>Therefore, the database for that service is essentially part of the implementation of that microservice and thus cannot be accessed by external services.<\/p>\n<p>Ways to keep a microservice&#8217;s data private includes:<\/p>\n<ul>\n<li>Private-tables-per-service<\/li>\n<li>Schema-per-service<\/li>\n<li>Database-server-per-service<\/li>\n<\/ul>\n<p><strong>Benefits of Database-per-service<\/strong><\/p>\n<ul>\n<li>Ensures loose-coupling between the services<\/li>\n<li>Different service can used a suitable database based on it&#8217;s needs<\/li>\n<\/ul>\n<p><strong>Some Challenges of Database-per-service<\/strong><\/p>\n<ul>\n<li>Some complexity due to creating transactions that span multiple services<\/li>\n<li>Several join queries has to be written<\/li>\n<li>Hard to cordinate multiple SQL and NoSQL databases<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t2\">2. Shared database<\/strong><\/h5>\n<p>The shared-database pattern\u00a0 allows different services to access the same database. The approach is to use a single database that is shared by multiple services. Thus each service can freely access the data that is owned by other services.<\/p>\n<p><strong>Benefits of Shared Database<\/strong><\/p>\n<ul>\n<li>Use of ACID transaction to enforce consistency is very clear and straighforward<\/li>\n<li>It is easier to implement a single database<\/li>\n<\/ul>\n<p><strong>Challenges of Shared Database<\/strong><\/p>\n<ul>\n<li>Just one database could not fill the data requirements of all the services<\/li>\n<li>Additional coordination during development time between different teams working on different services as they need to access the same data storage<\/li>\n<li>Runtime couple could impede performance<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t3\">3. Saga\u00a0<\/strong><\/h5>\n<p>A saga in defined as as sequence of local transactions. In this case, these are transactions that span multiple services. In\u00a0 a saga, each local transaction executes on a database, then publishes a message\/event that triggers the next local transaction in the sequence.<\/p>\n<p>If however, one transaction fails in the saga, then a series of\u00a0 compensating transactions in executed to rollback the changes made by the preceding transactions. This is illustrated in the figure below. The Patient service holds information about patients admission into a hospital while the Patient service holds the patient&#8217;s details.<\/p>\n<figure id=\"attachment_1278\" aria-describedby=\"caption-attachment-1278\" style=\"width: 735px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Saga.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1278 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Saga-1024x538.jpg\" alt=\"How Saga Works\" width=\"735\" height=\"386\" \/><\/a><figcaption id=\"caption-attachment-1278\" class=\"wp-caption-text\">How Saga Works<\/figcaption><\/figure>\n<p><strong>Benefits of Saga<\/strong><\/p>\n<p>It allows an application to maintain consistency in its data across multiple services without having to use distributed transactions<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Challenges of Saga<\/strong><\/p>\n<p>Has a complex programming model<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t4\">4. API Composition<\/strong><\/h5>\n<p>This is a way to implement queries that access data across multiple services.<\/p>\n<p>The API Composition is achieved by creating an API Composer (which could be another service) that invokes that services that owns the data. Then it performs joins of the results in-memory. This is illustrated below.<\/p>\n<figure id=\"attachment_1280\" aria-describedby=\"caption-attachment-1280\" style=\"width: 735px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/API-Composer.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1280 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/API-Composer-1024x561.jpg\" alt=\"API Composer\" width=\"735\" height=\"403\" \/><\/a><figcaption id=\"caption-attachment-1280\" class=\"wp-caption-text\">API Composer<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p><strong>Benefits of API Composer<\/strong><\/p>\n<p>Straightforward way of querying data across services<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Challenges of API Composer<\/strong><\/p>\n<p>Some queries may perform inefficiently due to joining large resultsets.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t5\">5. CQRS<\/strong><\/h5>\n<p>CQRS stands for Command Query Responsibility Segregation.<\/p>\n<p>In this pattern, we define a view database. This is a read-only copy of the database that is made to support the particular query. So the application maintains this copy up to date\u00a0 by subscribing to the domain events emitted by the service that owns the data.<\/p>\n<figure id=\"attachment_1281\" aria-describedby=\"caption-attachment-1281\" style=\"width: 735px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Command-Query-REquest-Segregation.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1281 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Command-Query-REquest-Segregation-1024x916.jpg\" alt=\"Command Query REquest Segregation\" width=\"735\" height=\"657\" \/><\/a><figcaption id=\"caption-attachment-1281\" class=\"wp-caption-text\">Command Query Request Segregation<\/figcaption><\/figure>\n<p><strong>Benefits of CQRS<\/strong><\/p>\n<ul>\n<li>Has support for multiple denormalized views which has good performance and are scalable as well<\/li>\n<li>Separation of Concerns is improved due to simpler command and query models<\/li>\n<li>Can be used in an event sourced design<\/li>\n<\/ul>\n<p><strong>Challenges of CQRS<\/strong><\/p>\n<ul>\n<li>Complexity is increased<\/li>\n<li>Possible code duplication<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t6\">6. Domain event<\/strong><\/h5>\n<p>In this pattern, business logic is organized as a collection of domain-driven-design(ddd) modules. These module emit domain events anytime they are created or updated. The service then published these events so that the can be consumed by other microservices.<\/p>\n<p>Benefits and challenges are similar to Command Query Request Segregation<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t7\">7. Event sourcing<\/strong><\/h5>\n<p>In this pattern, the state of the business entity such as Patient or Admission\u00a0 is persisted as a sequence of state-changing events.<\/p>\n<p>Each time the state of a business object changes, a new event is added to the queue of events. In this way atomicity can be guaranteed. The figure below illustrates Event sourcing<\/p>\n<p><a href=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Event-Sourcing.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1282  aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Event-Sourcing-300x276.jpg\" alt=\"\" width=\"353\" height=\"325\" \/><\/a><\/p>\n<p><strong>Benefits of Event Sourcing<\/strong><\/p>\n<ul>\n<li>It ensures that events are reliably published each time there is a state change<\/li>\n<li>It avoids object-relational inefficiencies since events rather than domains are persisted<\/li>\n<li>It provides an audit log of changes made to business entities<\/li>\n<li>Allows for implementation of temporal queries<\/li>\n<li>Supports loose-coupling of business entities<\/li>\n<\/ul>\n<p><strong>Challenges of Event Sourcing<\/strong><\/p>\n<ul>\n<li>It is an unfamiliar programming model and therefore has some learning curve<\/li>\n<li>Event stores could be inherently difficult to query<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>We would learn about 7 database patterns for Microservices. The include: Database per Service Shared database Saga API Composition CQRS Domain event Event sourcing &nbsp; &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[414],"tags":[],"class_list":["post-1953","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1953","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1953"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1953\/revisions"}],"predecessor-version":[{"id":2121,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1953\/revisions\/2121"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}