How to Choose a Database for Microservices — CAP Theorem

In this article, we are going to discuss How to choose database for Microservices in order to understand data considerations for microservices with learning practices and patterns about Microservices Data Design patterns. And we will use these pattern and practices when designing e-commerce microservice architecture.



By the end of the article, you will learn how to choose database in Microservices Architectures with applying CAP Theorem in Microservices Data Design patterns and principles.


How to Choose a Database for Microservices ?

This really important question and there are several way to understand your database requirements as per microservices.


There are several key points when we decide databases. First of all is the consider the “consistency level” that we need. Do we need Strict consistency or Eventual consistency ? If we are working on banking industry then Strict consistency should use for example debit or withdraw on bank account.


And if we need to Strict consistency, we should select relational databases in order to perform acid in transactional scopes. But mostly if possible we should follow Eventual consistency in microservices architecture in order to gain scalability and high availability.


Another key point is the “high scalability”. If our application need to accommodate millions of request than it should scale fast and easily. But in order to provide this, we should sacrifice strict consistency, because since we distribute the data in different servers, its imposible to make strict consistency due to network partitioning nature.


So another key point could be “high availability”. In order to perform “high availability”, we should separate our data center, split them into different nodes and partitions. But again it results to sacrificing consistency.


So as you can see that we have several key points and they result some benefits and drawbacks when we deciding database in microservices architecture.


For combining all these key points, it becomes CAP Theorem that explains better to this situation. So that means, when we try to decide databases in microservices, we should check the CAP Theorem.