Shared Database Pattern in Microservices

2761 views Designing μ-services

Microservices need to communicate with each other, and one such way of doing it is through a shared database.

For example: While building a multi-user blogging application, say we have a Blogs service that manages all the blogs-related information and we have an Analytics service that takes care of all the analytics like Likes, Shares, Views, etc.

Analytics service updates the information asynchronously directly in the blog’s database; eg: total_views that happened on the blog. This can be easily achieved by sharing the database between Blogs and Analytics, and this pattern is the Shared Database pattern.

Advantages of sharing the database

  • the simplest way of integration
  • no middleman involved
  • no latency overhead
  • quick development time

Challenges with Shared Database

There are 4 challenges to using this pattern

External parties know internal details

By sharing the database across services, an external party (Analytics) would get to know the internal details of the Blogs service; eg: deletion practice, schema, etc.

This leads to a very tight coupling between the services; which then restrains the maintainability and performance of the system. For example, whenever the Blogs service changes the schema, the Analytics Service would have to be informed about the change.

Sharing the database is sharing the logic

To compute some information we need to query a set of tables; and say, this information is required by the Blogs, Analytics, and Recommendation service.

The business logic to compute the information has to be replicated across all the 3 services. Any change in the logic needs to be made across all the services.

Risk of data corruption and deletion

There is a risk that one of the services might corrupt or delete some data given that the database is shared between the services.

Abusing the shared database

One service firing expensive queries on the database will affect the performance of other services sharing the same database.

When to share a database?

A shared database pattern is helpful when you are seeking quick development time. Although it is not the best practice, sharing the database does reduce the development effort by a massive margin.

Sharing the database is also seen where it is inconvenient to have a middleman for the communication; for example: sending a notification to a million followers of a person is simple when the Relationship database is shared with the notification fan-out service; instead of iterating the millions of followers through some middleman API.

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

2225 views 114 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

850 views 50 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

856 views 35 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?

631 views 27 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.

Enrolled by 700+ learners

Details →

Designing Microservices

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

Enrolled by 17+ learners

Details →

GitHub Outage Dissections

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

Enrolled by 67+ learners

Details →

Hash Table Internals

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

Enrolled by 25+ learners

Details →

BitTorrent Internals

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

Enrolled by 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.4.4
  • © Arpit Bhayani, 2022

Powered by this tech stack.