What is Microservices ?
Microservices (MS) is an architectural style for constructing applications. Its collection of small, loosely coupled collaborating services that works independently.
Lets understand by example: You are using a social media platform with features as Profile, News feed, Chat, Login, Share/broadcast etc. In Microservice style architecture, every services will be designed and developed separately irrespective of development language preference and technology stack. So here News feed will be a service may be developed in C++, Chat will be another service may be developed in another language Scala or Share/broadcast will be another services may be developed in Go or other languages. It gives flexibility to design and develop based on programming language of own preference. Every services will have their own sets of Database and controlled and managed separately but interact with each other to fulfil service requirement.
The Services communicate using either synchronous protocols such as HTTP/REST, WebSockets or asynchronous protocols such as AMQP.
As the every microservices will have their own sets of Databases and they may have to interact to fulfill service requirement, the data consistency between services is maintained using the Saga Pattern.
The advantage of using microservice based architecture are;
1- Every services will run and managed independently, hence if once services will be down then other services will remain active. This will turn outages of entire applications.
2- The maintenance, deployment and upgrade of each microservices will be independent and no dependency on another team/process.
3- Loosely coupled, highly maintainable and testable that enables rapid and frequent development and deployment (CI/CD).
Challenges with Microservices based architecture
1- Developers must deal with the additional complexity of distributed system:
Lets say, You have to get profile info of person using their profile ID then (i) in monolithic architecture developer will usually call a function similar like and pass on explicitly: getProfile (int profileID) and parse through single codebase.
In case datatype of profileID changed from int to string or char due to some business requirement then all the respective services which is using profileID as integer must change at a time of compilation otherwise it will throw compilation error.
However in Microservice based architecture, as all services are independent and you made changes of datatypes for profileID in only profile services then at the time of execution the service chain due to inter communication will break.
2– Increased memory consumption to to multiple service functions
How to Design Microservices based Architecture : Building blocks ?
1- Define Clients : Every microservices has its own clients or contracts. Lets say if profile microservice has to interact with News feed microservices to get news feed information about respective profile then it will interact with client of news feed and construct the object to get relevant news feed.
2– Define Mechanism for Routing the request : Here we can deploy load balancer or Service Registry (SR) to route the service request for subsequent microservices.
3– Define Continuous Integration and Continuous Deployment (CI/CD) process. As the deployment of each microservices are independent, we should have a automated CI/CD process. Using Jenkin or similar tools we can achieve this.
4– Define Service to Service communication establishment : Every microservices may have different way to interact between each other or with client. Lets say some microservices can be communicated with Message queue, some may be request-response based etc.
5- Define Distributed Logging : Each microservices have their own way to serving request and generating logs. In distributed system tracking of logs at every place will be challenging while troubleshooting for issues as services are interconnected. We should have distributed logging platform such as Elasticstack to capture all types of logs from different sets of microservices.