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.
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
Single DB With Scalable Python API Front-End
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
Initialize the database
docker-compose exec db mysql -u root -psree123 sree_db -e "CREATE TABLE vote (id INT(6) AUTO_INCREMENT PRIMARY KEY,votes VARCHAR(30) NULL);insert into vote values(1,1);"docker-compose exec db mysql -u root -psree123 sree_db -e "select * from vote;"
Kubernetes Based Deployment — Single DB With Scalable Python API Front-end
In here I am deploying the above central database design in Kubernetes.
Design Pattern: Shared Database — ReadOnly
Kubernetes Scalable PHP-Web Application, Scalable Read Replica MySQL DB, and a Single Writable MySQL DB
Commands used for launching the application services. db-mysql-statefulset.yml from
Design Pattern: Shared Database — Buffered Write and Event Source
Kubernetes with RabbitMQ, PHP, Python, MySQL — a Fully Scalable Microservice Architecture
(Web, API, Q and Read-Replica MySQL). Still with a Single Write Db (but The Q Will Buffer So Allowing Traffic)
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!