Monolith

alt text

This is a monolithic server we are familiar with.

a monolith contains routing, middlewares, business logic and database access to implement all features of our app.

Microservices

alt text

a single microservice contains routing, middleware, business logic and database access to implement one feature of our app.

PROS: if for some reasons, some services go down, the rest of services still work.

The big challenge with microservices: Data management between services

1: we want each service to run independently of other services.

avoid this scenario.

CONS if one database shutdowns, all services crush immediately.

alt text

2:  Database schema/structure might change unexpectedly.

avoid this scenario.

CONS: we introduced dependency between services A et B.

alt text

3:  Some services might function more efficiently with different types of DB's.

Practical case

Look at the diagram below, how do we design the database for the Service D

alt text

There are 2 ways to design

  • Sync
  • Async

I am going to show the async way to design the database The database D has 2 tables , products and user_products. They are created passively.

alt text

This microservice uses Event Bus architecture.

Here is the workflow

  • When a requests comes, it creates an order in the DB for C
  • Then it sends an event to the Event-Bus.
  • The Event-Bus sends back this event to the DB for service D

alt text

Note on async communication

  • PROS
    • Service D has zero dependencies on other services!
    • Service D will be fast
  • CONS
    • Data duplication.
    • Harder to understand.