How do Microservices work?
Microservices work by breaking down a large application into smaller, independent services that communicate with each other through APIs. Each service runs in its own process & has its own database. When a request comes from the user via an API gateway to the application then it’s routed to the relevant service by API gateway
Here is how microservices work together:
-
The User sends application a request to view some product;
-
This request goes through API Gateway which forwards it to the Product Catalog Service.
-
The Product Catalog Service retrieves information about the product from its database and returns that back to API Gateway.
-
If user decides to add that product in his/her cart, the API Gateway will send a request to Shopping Cart Service.
-
This service adds this product into User’s cart in its own database.
-
When one wants to check out, he goes through API gateway and then it sends a request to Payment Service
- The Payment Service processes payment and it sends the result back to the API Gateway
During all this process each of these services work autonomously without having knowledge about each other’s internal working. They communicate using well-defined APIs which allow them to be developed separately and deployed independently.
What are the main components of Microservices Architecture?
-
Services: These are individual small autonomous components constituting an application. Each service is responsible for one business capability & has its own database.
-
API Gateway: This acts as the entry point for all client requests. It handles request routing, composition, & protocol translation. It can also perform authentication, rate limiting, & logging.
-
Service discovery: is important in a microservices architecture because the services need to find one another and communicate. With service discovery, services can discover dynamically other services’ network locations.
-
Database per Service: Every single service has its own database; hence it can be deployed independently. The types of this database like SQL or NoSql are chosen based on requirements of the service.
-
Event Bus: Services may need to talk to one another asynchronously. Moreover, they also publish and subscribe to events through an event bus; leading to an event driven architecture.
-
Containerization: Microservices are usually implemented as containers that provide a lightweight portable runtime environment. Docker is frequently used for containerization purposes.
- Orchestration: This becomes more apparent when you have a lot of services running inside containers that need managing. Orchestration platforms like Kubernetes will automate the deployment, scaling, and management of containerized applications.
This would mean that User Service, Product Catalog Service, Shopping Cart Service and Payment Service would each be separate components in this online shopping application we discussed earlier.
These services could then discover one another using this Service Discovery mechanism. Again, every service should have its own database with them. Consequently, the Event Bus would allow these Services to work independently conversing such as payment messages from Payment Service which notify Shopping Cart Services about successful payments made by users.
Which Design Patterns Can Be Used In Microservices?
Design patterns are templates for solving common software design problems while developing software systems. For microservice-based architectures there exist design patterns for scalability, stability and maintainability across multiple hosts or regions . Some popular examples include:
Decomposition Patterns
a) Decompose by Business Capability: This pattern arranges services according to business capabilities such as user management ,product catalogues ,order processing etc .
b) Decompose by Subdomain : This pattern groups together different subdomains based on domain-driven design (DDD).
Database Patterns
a) Database per Service: Loose coupling among other things independence in scalability requires every service having its own database.
b) Shared Database: This pattern arises when services are connected to one database as it happens in the monolithic architecture.
Observability Patterns
a) Log Aggregation: As a result, logs from all services are centrally collected for ease of troubleshooting and monitoring purposes.
b) Distributed Tracing: It helps to trace requests moving through several services in order to figure out the path taken & potential bottlenecks.
c) Health Check API: Create an endpoint that reports the health of your service; this can be used by orchestrators for discovery and auto-healing purposes.
Cross-Cutting Concern Patterns
a) External Configuration: This allows configuration to be stored outside of the deployment artifact, for example in a central configuration server or vault, thereby making it possible to alter configuration without redeployment of the application.
b) Service Mesh: Instead, such infrastructural layer exclusively manages inter-service communications involving discovery mechanisms, encryption and load balancing among others.
For example, Decompose by Business Capability approach could be employed in our e-commerce system. In this case User Management is classified as a business capability while Product Catalog ,Shopping Cart and Payment would fall under other different categories .Therefore , each one of them could have its own Database per Service. Furthermore , we could employ Log Aggregation pattern for collecting logs from all these services at once just like Health Check API pattern.
What are the Microservices’ Anti-Patterns?
Anti-patterns in microservices are patterns that are considered as bad practice or common pitfalls leading to trouble in a microservice based architecture. It’s useful to be aware of these anti-patterns in order to avoid them. Some of the most common anti-patterns include:
-
Nano Services: When services become too fine grained, this increases complexity & redundancy among services. The size of services should be small but not too small.
-
Mega Service: In contrast, having a service do everything can lead to problems similar to those found in monolithic applications. There is need for striking the right balance of service size.
-
Shared Libraries: However sharing code between services may seem like a good idea, it ends up causing tight coupling. This means that any changes made in the shared library may require all services being updated and redeployed.
-
Shared Database: Sharing a database by more than one service also leads to tight coupling. All services using it will be affected by changes made on the database schema.
-
Lack of Decentralized Governance: Each team must select its own technology for their service within microservices architecture. Imposing uniform technology stack across all other services stifles innovation & optimization.
-
Lack of Monitoring & Logging: With many tiny services, solid monitoring & logging must be put into place otherwise troubleshooting can be extremely difficult.
- Lack of Automated Testing: For every service there has to have automated tests so that it functions correctly. Without automated testing, bugs can easily slip through the cracks.
For example, let’s say we decide on having a single “ProductService” handling products, reviews, recommendations and search in our online shopping application. This would be an example of Mega Service anti-pattern; instead we should break them down by business capabilities into separate services.
Real-Life Example of Microservices
Let us take an actual case study and thus get insight on how microservices are used in practice. Netflix is a very good example of a company, which has successfully adopted the microservices pattern.
Before adopting microservices, Netflix had a monolithic architecture. As they increased their number of users and expanded to different countries, they faced scaling & maintainability issues with their application. To solve these problems, they switched to a microservices architecture.
Here is a simplified breakdown of some of Netflix’s microservices:
-
User Service: Handles user registration, authentication and profile management.
-
Recommendation Service: Offers personalized movie and TV show recommendations based on the user’s viewing history.
-
Search Service: Enables users to search for movies and TV shows.
-
Playback Service: Will stream video content to the user device.
-
Billing Service: bills customers for their subscriptions or payments
Each off these services can be developed independently deployed & scaled. For instance, changes can be made to Recommendation service algorithms without affecting other services if any.
Netflix also relies heavily upon tools such as Hystrix for fault tolerance, Eureka for service discovery, as well as Zuul for API gateway. Many of these tools are now available as open source projects benefiting the wider community of microservices developers.
Through adoption of a microservice model however, Netflix has been able to scale up its platform to handle billions of requests per day while maintaining seamless streaming experience across all geographies.
In the monolithic style, the whole application is designed as a one big unit. The entire functionalities are highly coupled and executed in single service. Any changes to the system requires building and deploying the whole application again.
Microservices vs. Monolithic Architecture
Below are some of the key differences between microservices and monolithic architecture:
Structure
Monolithic: Application built as one large piece.
Microservices: Application divided into smaller independent services.
Scalability
Monolithic: Scaling means scaling all of it even if only part of it needs more resources.
Microservices: Every service can scale independently based on its needs.
Deployment
Monolithic: All changes require a redeployment of the entire application.
Microservices: Services can be deployed without affecting the rest of the application.
Technology
Monolithic: The entire application has to use same technology stack.
Microservices: Each service may go with any technology stack that best suits its requirements.
Team Structure
Monolithic: A single huge team develops this software.
Microservices: One or two people can develop each service independently.
Let’s take our online shopping application again as an example. In a monolithic architecture, our user management, product catalog, shopping cart & payments would all be part of one application. In case there was need to scale payment functionality due to traffic increase; we would have scaled up everything. However in microservice architecture you could have scaled only Payment Service not affecting other services at all.
How to move from Monolithic to Microservices?
This transition from monoliths into micro-services is no piece of cake; it entails careful planning and execution because it is costly operation itself. Here are steps involved:
-
Identify Business Capabilities – Begin by identifying various business capabilities within your existing monolithic applications that could underpin your future micro-services .
-
Prioritize Services – Determine which services should be extracted first based on their complexity , business value ,and dependencies .Often its better beginning with those having few dependences
-
Define Service Boundaries – Clearly define what each service is responsible for doing and where its boundaries lie. This also includes defining APIs that service uses to communicate with others.
-
Establish Communication Mechanisms – Choose the communication strategies for your services such as Rest APIs, Messaging Queues etc.
-
Set Up Infrastructure – Set up infrastructure required to run microservices including containerization platforms (such as Docker) and orchestration tools (e.g. Kubernetes).
-
Develop & Test Services -Each service must be developed in isolation and thoroughly tested until it works correctly. Each service should have its own automated test suite.
-
Establish Deployment Pipelines – Each service needs a separate CI/CD pipeline for continuous integration and continuous deployment.
-
Monitor & Manage – Implement robust monitoring and logging for all services. Prometheus, Grafana can be used respectively for Monitoring/ Visualizing
- Refactor & Optimize - Refactor your microservices continuously based on how they are being used in real life and their performance requirements.
For example, let’s say our monolithic online shopping application has become hard to maintain or scale. We would begin by identifying different business capabilities like user management, product catalogue, shopping cart, payments e.t.c.. For instance ,we may prioritize pulling out payment functionality because it has less number of dependencies .We will define an API for Payment Service; set up infrastructure needed to run it separately ,develop/test it and establish its deployment pipeline then we proceed with other services .
Service-Oriented Architecture (SOA) vs. Microservices Architecture
While both are architectural styles that structure applications as collections of services, Service-Oriented Architecture (SOA) and microservices architecture have key differences.
SOA is an approach where services revolve around business processes & use a centralized Enterprise Service Bus (ESB). Services in SOA are usually bigger & more complicated, and they may have shared data storage.
On the other hand, microservices are built around business capabilities & communicate through lightweight protocols such as HTTP/REEST. They’re smaller, centered on one functionality; thus can be deployed independently with their own data storage.
Here are some key differences:
Size & Scope
-
SOA: Services are often larger and may span multiple business functions.
- Microservices: Service sizes are small targeting unique business capabilities.
Communication
-
SOA: ESBs typically facilitate service communication which makes it complex.
- Microservices: Instead, services use light-weight protocols like HTTP/REST
Data Storage
-
SOA: There might be data sharing among the services just mentioned.
- Microservices: Typically each service would have its own data store.
Governance
-
SOA: Governance is often centralized and sometimes difficult to manage.
- Microservices: On the contrary, governance is decentralized and each service has its own governance.
Scalability
In SOA scaling can be more complex because of the centralised ESB and shared data storage system
In contrast to this Every single can scale up
For example, in an SOA approach, our online shopping application might have services for "Order Processing" & "Customer Management". The Order Processing service would do things like place orders, update inventory levels, or initiate shipping while the Customer Management service does tasks like customer registration, authentication or profile management. All these would rely on an ESB for communication purposes
However if we took a microservice approach we would have more granularly shaped services like the “Shopping Cart”, “Payment” or “User Profile”. Each of these would have their own data storage and interact with each other via APIs.
Cloud-native Microservices
Cloud-native microservices make maximum use of the capabilities offered by cloud computing platforms. Such systems are designed to be built and deployed in ways that leverage scalability, flexibility and resilience provided by the cloud.
Here are some key characteristics of cloud-native microservices:
-
Containerization: Microservices run inside containers (e.g., Docker containers) which provides a uniform & isolated runtime environment; thus can be easily deployed across different cloud environments.
-
Orchestration: Containers are managed and orchestrated through platforms such as Kubernetes enabling automatic scaling, self-healing, and efficient utilization of resources.
-
API-Driven: Microservice functionality is exposed using well-defined APIs that typically use REST or gRPC for loose service coupling.
-
Stateless: Wherever possible, Cloud-native microservices are designed to be stateless. All states are externalized in cache or database allowing for easier failover & scaling.
-
Resilient: They should be able to gracefully handle failure. For example they may use circuit breakers, timeout and retry etc so that overall system remains functional even if individual services fail.
- Automated CI/CD: Typically, cloud-native microservices utilize automated continuous integration & continuous deployment (CI/CD) pipelines for frequent and reliable updates.
To illustrate, we can take our online shopping application as an example. Each service (User Service, Product Catalog Service, Shopping Cart Service, Payment Service) would have its own Docker container. Those containers could be managed by Kubernetes that is scaling the services according to demand. Among the REST APIs would be used for communication among the services. For example, Payment Service would be stateless and all transaction data will be saved in a database. Every service has its own CI/CD pipeline for automated testing &deployment.
Role of Microservices in DevOps
Microservices and DevOps are two practices that can work together well because they complement each other. The objective of DevOps is to promote rapid, reliable and incremental software delivery through collaboration between development and operations teams. Most of DevOps objectives are supported by Microservices architecture.
These are some ways microservices help achieve and support DevOps:
-
Continuous Integration & Deployment (CI/CD): There is frequent updating and deployment using microservices. Any changes can be made on either service without affecting the entire application therefore this fits well with continuous integration & deployment culture of DevOps.
-
Autonomous Teams: Small independent teams tasked with particular services can easily be created with the use of microservices. This corresponds to cross-functional team approach advocated by DevOps where every team is responsible for their end-to-end applications.
-
Scalability & Resilience: On-demand scaling based on need can independently happen on microservices thereby it enables resource optimization hence improved performance efficiency also microservices were built with fault tolerance thus facilitating high availability goals of DevOptics.
-
Automation: Microservices depend heavily on automation such as testing, deploying and management in general infrastructure which is one of the major tenants underpinning modern day project called devops.
- Monitoring & Logging: In case of a microservice architecture, you need strong monitoring & logging mechanisms that provide visibility to enable the drivers fix problems quickly.
For example, let’s consider having microservices in our online shopping application that could serve DevOps. Each team could develop and deploy its own microservice. There would be end-to-end responsibility of each team regarding the service from development, deployment to operation. Consequently, they would automate much of the testing and deployment. The DevOps team would use monitoring & logging tools to gain visibility into the health & performance of each service. Whenever a certain service attracted high demand it could be scaled up without affecting others.
Benefits of using Microservices Architecture
Microservices architecture has several important advantages that make it appealing for many applications. Here are some of the major benefits:
-
Scalability: It enables fine-grained scaling - individual services can be independently scaled depending on their specific resource requirements and demand patterns leading to effective resource utilization and cost optimization.
-
Flexibility: Under the microservices approach, each team is at liberty to choose what technology stack works best for them and their specific service. This means flexibility as well as allowing teams to employ languages or tools that are suited to particular tasks assigned them.
-
Resilience: A failure in one microservice does not necessarily bring down the whole application; other services keep working normally, thus making entire system more reliable against failures.
-
Easier Maintenance & Updating: As a result of being small and focused, microservices become easier for understanding, maintaining and updating purposes in comparison with monolithic systems. Changes may be made within single services without modifying entire systems completely s
-
Microservices are an improvement to Organizational Alignment in that they enable technological architecture to match organizational structure. Several services can be owned independently by each team hence allowing for autonomy and quicker decision-making.
-
Time to market is reduced: This means that microservices are capable of developing new features and deploying them at a quicker rate. Faster innovation and release cycles can, therefore, be achieved if teams engage in parallel work on different services.
- Reusability: These services have been developed in such way as to encourage reusability. A single service may be made use of by multiple other applications or services thereby making development more efficient.
If we were using a microservices model with our online shop application, we would then scale the Payment Service only during peak shopping times while the rest of the application does not need scaling. If the other services had no effect when changing from one machine learning algorithm to another recommended any service it was linked to. For example, everything became more dynamic and faster after giving different teams their own microservice so they could start improvising even without talking about it.
Challenges of using Microservices Architecture
In spite of the many benefits they provide, microservices architecture also have their own challenges. Here are some of the main challenges when using a microservices architecture:
-
Complexity: The composite nature of the system is more complex than individual microservices. There are more moving parts to manage,& interactions among services can be intricate.
-
Network Latency & Reliability: There is a lot of inter-service communication that occurs in microservices. This leads to network latency,& in case of unreliable network, it can impact the entire system.
-
Data Consistency: Normally each microservice has its own database. It could be hard to keep data consistent across these separate databases particularly where transactions cover several services.
-
Testing & Debugging: Microservices make testing and debugging harder. Things can go wrong during service interaction thereby making it difficult to identify cause and effect relationship of any particular issue.
-
Operational Complexity: Managing a microservices architecture can be operationally complex. There are many more deployments to manage,& robust monitoring and automation are needed to make sure all services run smoothly.
-
Skill Requirements: Developing& managing microservices requires a broad set of skills. Teams need expertise in areas like distributed systems, networking, &DevOps apart from programming skills.
- Organizational Alignment: Although better organizational alignment may be achieved through microservices they mean teams must work differently. Effective communication & coordination between teams is crucial here.
For instance, our e-commerce website may face the challenge of ensuring there is data consistency between Shopping Cart Service and Payment Service. If a user adds an item into their cart but does not update immediately by Payment Service then there could be differences. Debugging an issue that arises from interaction between Product Catalog Service and Recommendation Service could prove challenging.Maintaining all these services would entail significant operational effort on deployment and monitoring sides alone.
Real-World Examples of Companies using Microservices Architecture
Several large corporations have utilized microservices architecture to improve their software systems. Here are a few notable examples:
-
Amazon: One of the pioneers of microservices, Amazon moved from monolithic architecture to microservices in 2000s. Its various offerings including AWS and Amazon.com are today powered by thousands of microservices.
-
Netflix: Netflix is another well-known adopter of microservices as mentioned earlier on. It has over 700 microservices that range from user authentication to movie recommendations.
-
Uber: The Uber’s software architecture operates through microservices. They have different services for passenger management, driver management, trip management, billing,& others. As such it enables scaling & adapting its services independently
-
Spotify: Spotify employs microservices for its music streaming platform. Their services include user management, playlist management, music metadata,& more. This allows them to scale & innovate rapidly.
-
eBay: eBay initiated its transition to microservices back in 2011.It runs over 1000 micro-services on its platform which handle product listings and payment processing among other things.
- Zalando: Zalando is a European e-commerce company that adopted microservices to sustain their fast growth rate. There are in excess of150 separate unit teams each managing a single service thus allowing themto quickly scale& innovate
These are just a few examples. Many other companies like Twitter, Airbnb, Coca-Cola or Walmart have also employed a degree of this strategy called ‘microservice’.
All of these companies have one thing in common: they operate on a large scale and thus need the ability to develop and release new features fast. Microservices give them the flexibility, scalability, and agility they require to stay competitive.
Technologies that enables microservices architecture
Microservices architecture is supported by numerous technologies. Here are some of the main technologies that enable and support microservices:
-
Containerization: Docker for example offers technology that can be used to containerize micro services. Containers provide a dependable, isolated runtime environment for each service.
-
Container Orchestration: These are platforms such as Kubernetes or Docker Swarm which manage and orchestrate containers. They include scaling, load balancing plus self-healing among other things.
-
API Gateways: Kong or Apigee are API gateways that become the single entry point for all client requests. For instance request routing, composition, protocol translation etc.
-
Service Mesh: Some examples include Linkerd as well as Istio which serves as a dedicated infrastructure layer meant for facilitating communication amongst different services hence their name Service Meshes. It helps with service discovery, load balancing among other things this include encryption.
-
Messaging & Queuing: This is exemplified by Apache Kafka & RabbitMQ that provides asynchronous communications between Microservices in order to decouple services & handle high volumes of traffic.
-
Serverless Computing: One example is AWS Lambda or Google Cloud Functions allowing execution of code without having to worry about managing servers and this has specific use cases within certain types of Micro Services.
-
Monitoring & Logging: Prometheus & ELK stack (Elasticsearch, Logstash, Kibana) tools serve monitoring & logging purposes in microservice architectures providing visibility into service health & performance.
-
Continuous Integration & Deployment (CI/CD): CI/CD tools such as Jenkins or GitLab CircleCI automate build/test/deploy processes for micro-services.
-
Programming Languages & Frameworks: The implementation of Micro Services can be done through multiple programming languages & frameworks. Some of highly used ones include Java (with Spring Boot), Node.js (using Express) and Python (developed with Flask).
For instance, let’s consider a hypothetical online shopping app. In this case, Docker may be employed to place each microservice into their own containers. Kubernetes can be utilized for managing and scaling these containers. In addition, Kong can act as an API gateway while Istio plays the role of service mesh.
For example, Apache Kafka can be employed for inter-service messaging. To monitor and log events in the system, we will need to use Prometheus and the ELK stack. Again Jenkins could then be used for CI/CD processes while Java with Spring Boot would be used to create the services themselves.
Frequently Asked Questions
What is the primary benefit of using microservices architecture?
The primary benefit of using microservices architecture is its ability to enable scalability & flexibility. Each service can be scaled independently based on demand, & issues in one service do not necessarily impact the entire application, enhancing resilience.
How do microservices communicate with each other?
Microservices typically communicate through APIs, often RESTful or using messaging systems like Kafka for asynchronous communication. This allows services to remain loosely coupled & maintain high levels of service autonomy.
Are microservices suitable for all types of projects?
Microservices are best suited for complex applications that require high scalability & flexibility. They may not be the best choice for small, simple applications where a monolithic architecture could be more efficient in terms of development & maintenance.
Conclusion
The article explains the topic of microservices architecture, which is building applications as sets of small, independently deployable services. It talks about such things as the advantages and disadvantages of microservices (scalability and flexibility, complexity and operational overhead). The comparison of Microservices with other architectures in terms of their role in DevOps, real-world examples that use them as well as technologies involved was also mentioned. However, designing and managing them properly can only be successful in building scalable systems that are robust.
You can refer to our guided paths on Code 360. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.