Microservice Design Pattern: Shared Database — Microservice Architecture

There are various ways to implement a shared database design pattern that is fully scalable and eventually consistent. Eventual consistency is what allows the database to scale and synchronize.

Photo by Campaign Creators on Unsplash

A microservice design pattern is a software design pattern that deals with distributed applications; several design patterns address various aspects of the microservice architecture. A microservice uses small, single-purpose applications that communicate with each other using language independent protocols such as HTTP.

The microservices use a shared database for storing data and events
One of the possible disadvantages of this technique is the need to synchronize data in multiple databases.

I consider this an advantage because it makes scaling possible.
Microservices are small, autonomous services that work together to accomplish a larger goal. A typical microservice system could be composed of hundreds or thousands of microservices.
A shared database is one way to design this type of architecture. A shared database eliminates the need for an expensive central database.

Design Pattern: Central Database

Let us begin with the traditional central database approach. Here a central database is accessed by a scalable collection of microservices to read and write. In the picture I am also showing a front-end service to deliver the static components. Pasting below are some screenshots taken during my tests.

Docker Container Deployment

docker-compose.yml

Initialize the database

API End-Points
Screenshots of the Running Application

Kubernetes Based Deployment — Single DB With Scalable Python API Front-end

In here I am deploying the above central database design in Kubernetes.

API End-Points and the Microservices
MySQL Deployment
MySQL Service
Replicated Python App Deployement
Replicated Python App Service
Commands used for the deployment and DB initialisation.

Design Pattern: Shared Database — ReadOnly

Kubernetes Scalable PHP-Web Application, Scalable Read Replica MySQL DB, and a Single Writable MySQL DB

Shared Database Architecture with Read-Replicas
Scalable Front-End PHP App Deployment
Scalable Front-End PHP App Service
Scalable Python API App Deployment
Scalable Python API App Service

Commands used for launching the application services. db-mysql-statefulset.yml from https://k8s.io/examples/application/mysql/mysql-statefulset.yaml

Design Pattern: Shared Database — Buffered Write and Event Source

(Web, API, Q and Read-Replica MySQL). Still with a Single Write Db (but The Q Will Buffer So Allowing Traffic)

Components of the Shared Database Architecture
Initializing the Database
RabbitMQ Deployment
RabbitMQ Service
Write Loop Python Service for DB Sync — Deployment
API Service to Read DB — Deployment
API Service to Read DB — Service
Python Service to Push Messages to the Queue — Deployment
Python Service to Push Messages to the Queue — Service
PHP Web Front-End — Deployment
PHP Web Front-End — Service

Again, there are various ways to implement a shared database design pattern that is fully scalable and eventually consistent. Eventual consistency is what allows the database to scale and synchronize. In the above design, the python service that loops for messages in the Q decides this consistency window! 10 seconds in this demo.

Happy microservice migrations!

Connecting SMEs (Subject Matter Experts) to businesses who need Help with Microservices, Machine Learning, Cloud, IaC, and DevOps and Training