Database per Service Pattern in Microservices



2909 views Designing μ-services



Should every microservice have its own database?

Microservices need some persistence to store the state of the application. They also need to be loosely coupled with others so as to be autonomous and having a separate database for itself really helps, and this is the Database Per Service pattern.

Modeling a social network

The importance of having a database per service can be seen when we model a social network. For every usecase, we would need a specialized database to keep our latencies to a bare minimum.

  • Chat: a partitioned non-relational database like Cassandra
  • Auth: a simple relational database with replicas like MySQL
  • Profile: a nonrelational schemaless database like MongoDB
  • Analytics: a massively scalable columnar storage like Redshift
  • Videos: blob storage like S3

Advantages of Database Per Service Pattern

Loosely coupled components

Because the databases are separate, the services could not connect to other databases and would have to directly talk to each other making them loosely coupled.

Specific DB for specific usecase

Services can pick the best database for their usecase making them super performant and efficient. For example, picking a Graph database to model relations in social networks instead of relational.

Granular control and scaling

Services can choose their scaling strategies as per the load it is getting; be it vertical, horizontal, replicas, partitioned, or decentralized.

Smaller blast radius

When any database is experiencing an outage only the services that are directly or indirectly dependent on it get affected and everything else continues to operate normally. For example, we can continue to accept the payments even when the profile service is down.

Compliance

Compliance requires us to make changes in how our data is stored and moved across. Separate databases would help us in implementing changes on a fraction of data instead of the whole.

Downsides of Database Per Service Pattern

Transactions are distributed and expensive

If we need strong consistency across, we would need distributed transactions and those are expensive and complex to implement.

Conveying updates requires brokers

Conveying updates from one service to another would require us to have message brokers, thus adding more things to manage and maintain.

More infra to manage

Having a database per service bloats up our infra and we would need to build expertise in maintaining and managing them.


Arpit Bhayani

Arpit's Newsletter

CS newsletter for the curious engineers

❤️ by 17000+ readers

If you like what you read subscribe you can always subscribe to my newsletter and get the post delivered straight to your inbox. I write essays on various engineering topics and share it through my weekly newsletter.




Other essays that you might like


BFF - Backend for Frontend - Pattern in Microservices

2635 views 131 likes 2022-07-04

As your application evolves, supporting multiple types of clients like Desktop, Mobile apps, etc becomes tricky. The bac...

Best practices that make microservices integration easy

938 views 52 likes 2022-06-27

Running microservices in isolation does not make any sense. To get something done, multiple microservices need to talk t...

Things to remember while building Microservices

938 views 41 likes 2022-06-20

An engineer working on Microservices should not only just focus on engineering; there are so many other aspects to look ...

Why should we have a standard way of building Microservices?

701 views 29 likes 2022-06-17

We all love creating microservices, but what if every team creates its own microservice uniquely and uses its own conven...


Be a better engineer

A set of courses designed to make you a better engineer and excel at your career; no-fluff, pure engineering.


System Design Masterclass

A masterclass that helps you become great at designing scalable, fault-tolerant, and highly available systems.

800+ learners

Details →

Designing Microservices

A free playlist to help you understand Microservices and their high-level patterns in depth.

17+ learners

Details →

GitHub Outage Dissections

A free playlist to help you learn core engineering from outages that happened at GitHub.

67+ learners

Details →

Hash Table Internals

A free playlist to help you understand the internal workings and construction of Hash Tables.

25+ learners

Details →

BitTorrent Internals

A free playlist to help you understand the algorithms and strategies that power P2P networks and BitTorrent.

42+ learners

Details →

Topics I talk about

Being a passionate engineer, I love to talk about a wide range of topics, but these are my personal favourites.




Arpit's Newsletter read by 17000+ engineers

🔥 Thrice a week, in your inbox, an essay about system design, distributed systems, microservices, programming languages internals, or a deep dive on some super-clever algorithm, or just a few tips on building highly scalable distributed systems.



  • v12.7.8
  • © Arpit Bhayani, 2022

Powered by this tech stack.