A microservice architecture enables large teams to build scalable applications that are composed of many loosely coupled services.
Microservices are independently releasable services that are modeled around a business domain. A service encapsulates functionality and makes it accessible to other services via networks — you construct a more complex system from these building blocks. One microservice might represent Accounts, another Cards, and yet another Loans, but together they might constitute an entire bank system.
Each microservice has its own database. For example, the product service has its own database, the inventory service has its own database, and the stock service has its own database.
In a microservices project, all the microservices are loosely coupled — meaning all the services are independent of each other, and each microservice should be developed independently, deployed independently, and scaled independently.
So basically, a microservice has the below characteristics:
- Each microservice can have its own database.
- Each microservice should be developed independently.
- Each microservice should be deployed independently.
- Each microservice should be scaled independently.
In microservices projects, services can communicate with each other — for example, the product service can communicate with the inventory service, and the inventory service can communicate with the stock service. A microservice can communicate with multiple services as well.
There are two types of communication styles: synchronous and asynchronous.
In the case of synchronous communication, we can use the HTTP protocol to make an HTTP request from one microservice to another.
And in the case of asynchronous communication, we have to use a message broker between multiple microservices — for example, RabbitMQ or Apache Kafka — and each microservice in a microservices project can expose REST APIs.
Key Components in a Microservices Architecture
-
The key component is the API gateway — whenever the client sends a request to the API gateway, the API gateway routes that request to the relevant microservice.
-
Another key component is the service registry. All the microservices have to register to a service registry, and the API gateway discovers the particular microservice’s hostname and port using the service registry, so it can route that request to the particular microservice.
-
One more key component is the config server — this component externalizes the configuration of microservices.
-
One more key component is distributed tracing — to maintain the logs or complete log hierarchy for a particular HTTP call from start to end, we can use distributed tracing.
-
One more key component is security — we can implement centralized security in the API gateway.