Preparing for Java Interview?

My books Grokking the Java Interview and Grokking the Spring Boot Interview can help

Download PDF

Saturday, April 22, 2023

Database Per Microservice Pattern in Java - Example Tutorial

Hello guys, in the last article, I share with your 10 essential Microservice design patterns and principles and best Microservices courses for Java developers which many of you appreciated. At the same time, a lot of you asked me to go deep down into each of those patterns and explain them with examples. So I decided to create this new series of Microservice Pattern tutorials where I will explain each of those essential Microservice pattern with example every week. In this article, let's have a look at what is database per microservice. Microservice architecture is a combination of a set of loosely coupled microservices and each service can be developed independently in an agile manner. We can use this pattern and practices when designing microservices. So let's go deep into what is database per microservice is and why we use it in our programming. 

The loose coupling of services is a key feature of the microservices architecture. Each service must have its own private data store to accomplish this.

 As a result, building a database architecture for Microservices almost always necessitates adhering to the database-per-service pattern. At least until the application hasn't become too complex, with multiple services involved.

From the heading, we can come to the conclusion that one Microservice is using one database as shown in below diagram:

Database Per Microservice Pattern with Example in Java

Simply, this means each service has its own database. Unlike using one common database, this is using a separate database for its services. So through this pattern, there can achieve a loose coupling of microservices. 

So think you have an e-commerce application with customer management and order management. There when you have done one change to a customer management database will not affect the order management service. So these databases can not be accessed directly by the other microservices. 

Only through the REST APIs can be communicated. So the database per microservices provides a huge benefit to the massively scaling systems in the software industry.

So some of the main advantages that can be get using the database per microservice,

    1. Data within the service is encapsulated within the service.

    2. The database is available for only one microservice. so can scale independently.

    3. If one database is not functioning, there will not affect the parallel services within the microservice.

How to use Database Per Microservice Pattern in Java

So the above is a perfect example of microservice architecture for an e-commerce application. 

  1. The order management service is using sql to store the data to make relationships with different services in the order management database tables.

  2. The customer management service is using NoSQL database to store high volumes of reading operations. 

 3. The shopping cart is using the distributed cache to accommodate the rich relational structure of underlying data. 

So there using different kinds of databases for different kinds of purposes. There will not be any chance of one database within the service is depend on another service's database. The only communication channel is through the API.

As from the above example, Databases for each service do not always need to be provisioned separately. In the case of a relational database, we can implement the pattern in the following ways.

    1. Private Tables per service - Each Microservice can make use of a set of tables, which should be accessible only through the relevant microservice.

     2. Schema per Service - A separate schema can be defined per microservice.

     3. Database server per microservice - Entire database server can be configured per microservice.

Pros and cons of Database per Microservice Pattern

So let's have a look at what are the advantages and disadvantages that we can get through the database per microservice pattern in our applications.


Firstly, let's discuss its advantages of it.

  1. Can different people manage the different databases.  Ex- One services sql database is handled by a separate group while other services NoSQL database is handled by another group of people which is capable of handling them.

  2. There are no transactional conflicts between different kinds of services

  3. Very easy to scale in terms of storage capacity and performance. 

  4. Everyone likes to have different databases for different services as it reduces the risk of falling down.

Disadvantages of database per microservices.

  1. Transactions between the components are too hard.  

  2. There may be data duplication between different services. As each and every service uses a separate database, there might have data duplicate with services.

  3. There may be some confusion to get the data as communication with the different databases through APIs should be considered. 

So these are some of the pros and cons of having the database per microservice architecture. 

From the above explanations, we can come to the conclusion that, individual data stores are more easily scaled. Furthermore, the domain's data is contained within the Microservices. As a result, understanding the service and its data as a whole is simplified. 

This is especially true for new members of a development team. It will take less time and effort for them to fully comprehend the area for which they are responsible.

As a result, a failure protection mechanism is required in the event that communication fails. Assume we are sending payment requests from service A to service B. Service A is waiting for the response in order to take appropriate action based on the outcome. During this time, service B is unavailable. We must handle the situation and notify service A of the outcome once B is back online. The circuit breaker mechanism can be useful in this situation.

The following significant issue involves transactions. Consistency and atomicity may be significantly impacted by spanning transactions between microservices. Complex queries have a related disadvantage. There isn't an easy way to run join queries across several data storage.

Finally, processes involving data that span multiple microservices may be challenging to troubleshoot.

So in this tutorial, we discussed what are the use cases with using the database per microservices. There are plenty of advantages that we can from using the separate database per microservices. And also some drawbacks also. Within this article, we discussed most of them and hope you understand with how we can implement the individual database for each service. Hope to see you in the next tutorial. 

Other Java Microservices articles and tutorials you may like:

Thanks for reading this article so far. If you like this Database per  Microservice design pattern and when and how to use it then please share them with your friends and colleagues. If you have any questions, feedback, or other fee courses to add to this list, please feel free to suggest.

P. S. - If you want to learn more about Microservice Architecture and solutions from scratch and looking for free resources then I highly recommend you to check out my post about 7 free Microservice courses. It contains free Udemy and Coursera and courses to learn Microservice architecture from scratch.  

No comments :

Post a Comment