Microservices

Microservices

How is a microservice architecture created?

  1. In microservice architecture, each service has its own middleware and database so that it is independent of other services running on the system.

  2. This means that service 1 could not access the database of service 2.

  3. Event bus: each service can emit or receive an event from an event bus.

How do the microservices communicate between each other?

Every time a microservice (let’s call it “Service B”) saves data in its dedicated database (“Database B”), it emits an event (let’s call it “Event B”) onto the event bus. The event bus serves as a central hub, responsible for receiving and forwarding events to interested parties. In this case, “Service C” has subscribed to events of type “Event B” on the event bus.

Upon receiving “Event B,” Service C processes the data and stores it in its own database (“Database C”). This asynchronous communication model ensures that each service can evolve independently without being tightly coupled to others. It also prevents the need for direct service-to-service communication, enhancing system resilience and scalability.

how microservices communicate

Please note that there is a step between step 2 and step 3, which is that microservice C will query its own database and increment the “times played” value by 1 for “song id” 10.

What happens if microservice C in the depicted diagram experiences downtime?
1. If microservice C is offline for a period, it will fail to capture certain instances of “event B,” resulting in the non-update of database C. So, how can we address this issue?

2. To avert the aforementioned scenario in a microservice architecture, we establish an event bus database. This database serves as a repository for all events emitted by each microservice.

3. Hence when microservice C comes back online after some time, it will query the event bus and fetch all the missed instances of “event B” as shown in step 3 below.

References:

Below is a very good course on microservices by Stephen Grider, which I referred to for writing this post.

https://www.udemy.com/course/microservices-with-node-js-and-react/