What are Microservices?
Microservices are a software architecture style that structures an application as a collection of loosely coupled services. A single microservice contains of all the routing, middlewares, business logic and database access necessary to implement one feature of our application
In Microservices
- Each service gets its own database if needed. Database per service
- Services will never reach into another services database
Problems in Microservices
- Data Management
Way in which we store data in a service and how data communicates between different services
Suppose you have an app with three services A,B and C with each having their respective databases. Now if you were to add new service D which uses the database of A,B and c , how would you do it? Remember one service cant connect to another service’s database directly
We have two communication strategies for this
- Sync
-Services communicate with each other in form of direct request - Async
- Services communicate with each other using events.
Here all the services are connected to something called event bus, which receives and emits events Thus when service D requests information from A,B and C it creates an event to event bus and routes to other services which can handle those requests.
Here event bus is single point of failure - Any time any services handle any request , it is registered to an event bus
Here service D will create its own databse enlisitng all the information it needs and thus when any other services A,B or C handles any request, it will be simultaneously sent to the event bus as well. Now those events in the bus will redirect the information to service D and thus service D will get its required data for the database.
- Services communicate with each other using events.