Microservice & Database Architecture

Microservice & Database Architecture

Let’s imagine you are developing an online store application using the Microservice architecture pattern. Most services need to persist data in some kind of database. For example, the Order Service stores information about orders and the Customer Service stores information about customers.

Monolithic Database

So what is the Problems With Monolithic Database Design?

1- The traditional design of having a Monolithic Database for multiple services creates a tight coupling and inability to deploy your service changes independently.

2- It is difficult to scale individual services with this design since you only have the option to scale out the entire monolithic database.

3- Most of the times you have a relational store as your monolith database. This constraints all your services to use a relational database. However, there will be scenarios where a No-SQL datastore might be a better fit for your services and hence you don’t want to be tightly coupled to a centralized datastore.

So How to Handle Your data in a Microservice Architecture?

Each microservice should have its own database and should contain data relevant to that microservice itself. This will allow you to deploy individual services independently.

No alt text provided for this image

Keep each microservice’s persistent data private to that service and accessible only via its API. A service’s transactions only involve its database.

Microservices should follow Domain Driven Design and have bounded contexts.

There are a few different ways to keep a service’s persistent data private. You do not need to provision a database server for each service. For example, if you are using a relational database then the options are:

  • Private-tables-per-service — each service owns a set of tables that must only be accessed by that service
  • Schema-per-service — each service has a database schema that’s private to that service
  • Database-server-per-service — each service has it’s own database server.

Private-tables-per-service and schema-per-service have the lowest overhead. Using a schema per service is appealing since it makes ownership clearer. Some high throughput services might need their own database server.

It is a good idea to create barriers that enforce this modularity.

Using a database per service has the following drawbacks:

  • Implementing business transactions that span multiple services is not straightforward. Distributed transactions are best avoided because of the CAP theorem. Moreover, many modern (NoSQL) databases don’t support them.
  • Implementing queries that join data that is now in multiple databases is challenging.
  • Complexity of managing multiple SQL and NoSQL databases.

There are various patterns/solutions for implementing transactions and queries that span services:

  • Implementing transactions that span services — use the Saga pattern.

Thanks for reading this article.

AHMED EMAD

Engineering Manager | Experienced Engineer & Mentor | Enterprise & Senior Java Solution Architect | Technical Consultant | PMP Certified | Cloud Architect (GCP & AWS) | Technology Advisor & Digital Transformation

5 年

Ehab Hegazy?check this?

回复
Ahmad H.

Solution Architect | Data Architect | Data Management

5 年

Good article

Anas Rabei

Java Team Lead @ Synechron - TAHAKOM | Java Development, Team Management, Software Architecture, Microservices

5 年

very nice article.?

要查看或添加评论,请登录

AHMED EMAD的更多文章

  • How to design AWS architecture ?

    How to design AWS architecture ?

    Cloud computing is one of the boons of technology, making storage and access of documents easier and efficient. AWS…

  • Passwordless Authentication

    Passwordless Authentication

    Passwords are just too easy to guess, hack, or intercept. What’s more, the legacy of password reuse is leading to…

  • Microservices & Domain Driven Design(DD)

    Microservices & Domain Driven Design(DD)

    Using Domain-Driven Design (DDD) in our microservices will help us meet our microservices principles, but DDD will also…

  • How To Become A Full Stack Developer

    How To Become A Full Stack Developer

    Before you jump into the learning the skills, let’s get clear on what a full stack developer actually is. Full stack…

  • Serverless and Microservices

    Serverless and Microservices

    Is there any conflict between Serverless & Microservices? To answer this question we need to understand what both? What…

    4 条评论
  • New App (??????) Help me (SMS)

    New App (??????) Help me (SMS)

    App (HelpMe SMS ) currently tracking you by sending your location by SMS messages to your friends or your family…

社区洞察

其他会员也浏览了