Have you ever been curious about what goes on behind the scenes when you request a ride on Uber, search for a product on Amazon, or send a message on WhatsApp? I was always curious about knowing these things and always had a question in my mind how do they integrate the backend, frontend, the AI in the app, authentication and all other services? When I was in school, I enjoyed building small apps like Flappy Bird in Python, AI chatbots, and handwriting detection apps. However, these apps only consisted of a few features, and the process of building and handling them was relatively straightforward (it took me 5-6 hours to build that flappy bird game in Python🥲). On the other hand, when we look at popular apps like Instagram, Uber, and Spotify, we see that they have countless features and services running simultaneously. These apps must handle thousands of users logging in, clicking on various links and buttons, and generating high traffic on their websites.
So, how do companies and developers handle such a complex structure of integration of all the services and managing them within an app or a website? The answer is by building Microservices!
What are these Microservices and Why do we use them?
We talked about building apps and websites and got to know that as the number of features and services in an app or website increases, integrating them and managing incoming data, as well as updating it while the app is running, becomes a tedious task.
Let's take an example of building a game like Flappy Bird. In a monolithic architecture, all the different parts of the game, such as the graphics, sounds, and gameplay, are built together as one big piece. If we need to change something in the graphics, we would have to change the whole game code, which includes all the different parts. This can be a problem because it makes it harder to update and change parts of the game without affecting the whole game. A "monolithic" software application is built as one big piece, where all the different parts are connected. Just like while building Lego toys where all the pieces are connected, in a monolithic software application, all the different parts of the application are connected.
So Developers and Computer Scientists came up with the Microservice architecture.
Microservices is an architectural approach to building software applications by breaking down large, complex applications into smaller, independent services that work together to perform a specific function or task. Each service is designed to perform a specific business function and can be developed, deployed, and scaled independently of the other services. For example, let's say we are building a shopping app like Amazon. Instead of building the entire app as one big piece, we can break it down into smaller services such as product catalog, user authentication, order processing, payment processing, and so on. Each service can be developed, tested, and deployed independently, allowing for more agility and faster updates.
Instagram is a popular social media platform that uses a Microservices architecture to handle the millions of users and their interactions with the platform. Here's how it works:
Firstly, Instagram has broken down its platform into several smaller services, each responsible for a specific function, such as user authentication, profile management, messaging, and media management etc. Each service is developed and deployed independently, using different programming languages, tools, and frameworks, depending on the requirements and the preferences of the developers.
i) User authentication: This Microservice is responsible for authenticating users who are trying to log in or access the platform. It uses a combination of different authentication methods, such as email verification, two-factor authentication, and social media log in, to ensure the security and privacy of the users.
ii) Profile management: This Microservice handles the user profile, such as profile picture, bio, and contact information. It also allows users to customize their profiles, follow other users, and manage their followers.
iii) Messaging: The messaging Microservice enables users to communicate with each other via private messages. It handles features such as message encryption, message history, message notifications, and message delivery status.
iv) Media management: This Microservice is responsible for managing the user-generated content on the platform, such as photos, videos, stories, reels, etc. It handles features such as media uploading, media storage, media compression, and media filtering.
There can be hundreds of such Microservices working in the background.
Authorization Microservice - an example
Let's understand more briefly how an authorization microservice works in Instagram.
The authorization Microservice in Instagram is responsible for managing user authentication and authorization. When a user logs in to Instagram, the authorization Microservice first verifies the user's identity by checking their credentials against the user database. If the credentials are valid, the user is authenticated and granted access to the platform.
Once the user is authenticated, the authorization Microservice generates a JSON Web Token (JWT) that contains the user's information and permissions. The JWT is signed by a private key and sent back to the user's device as a response to the login request.
From then on, the user includes the JWT in the header of all subsequent requests to Instagram's API. When a request is received, the authorization Microservice checks the validity of the JWT by verifying its signature against the public key stored on the server. If the JWT is valid, the authorization Microservice extracts the user's information and permissions from the token and grants access to the requested resource if the user has the necessary permissions.
The authorization Microservice also handles the revocation of tokens in case a user logs out or their account is deactivated or suspended. When a token is revoked, the user is required to log in again to generate a new token and regain access to the platform.
By separating the authorization logic into a Microservice, Instagram can ensure the security and scalability of its authentication system. The authorization Microservice can be developed, tested, and deployed independently, allowing for faster iteration and easier maintenance. Additionally, it can be scaled horizontally to handle millions of concurrent authentication requests without affecting the performance of other Microservices.
Managing Microservices
Managing Microservices can be a complex task, as it involves handling multiple independent services that work together to deliver a software solution. Here are some key aspects of managing Microservices:
Service Discovery: In Microservices architecture, different services need to communicate with each other. Service discovery is the process of identifying the location and availability of services. Tools like Consul, ZooKeeper, and Eureka are commonly used for service discovery.
Load Balancing: With Microservices, multiple instances of the same service may be running simultaneously. Load balancing is the process of distributing requests across these instances to optimize performance and ensure high availability. Tools like NGINX, HAProxy, and Istio can be used for load balancing.
Monitoring: Monitoring Microservices is crucial for identifying issues and ensuring that services are functioning optimally. Tools like Prometheus, Grafana, and Elasticsearch are commonly used for monitoring Microservices.
Scaling: Microservices are designed to be scalable, which means that services can be easily scaled up or down based on demand. Container orchestration tools like Kubernetes and Docker Swarm can be used to manage the deployment and scaling of Microservices.
Security: Managing security in Microservices architecture is important, as the different services may have different security requirements. Tools like Vault and Istio can be used to manage security and access control in Microservices.
Microservices and Future
Microservices architecture has been gaining popularity due to its numerous benefits such as improved scalability, increased agility, and faster time to market. However, managing microservices can be a challenging task, especially as the number of services and the complexity of the system grow. Automating microservices using AI/ML can significantly reduce the time and effort required to manage complex systems.
Auto-scaling: One of the most significant advantages of microservices architecture is its ability to scale services independently. However, manually adjusting the scaling of each service can be a tedious and time-consuming task. Machine Learning algorithms can be used to monitor the system's resource usage and automatically adjust the scaling of each service to ensure optimal performance.
Fault detection and recovery: As microservices are distributed across multiple servers, detecting and recovering from faults can be a challenging task. AI/ML can be used to analyze system logs and detect anomalies that indicate a fault. It can then take the necessary steps to recover the service or switch to a backup instance.
Microservices can have complex dependencies that can affect the system's performance. Machine Learning algorithms can be used to analyze the system's performance and identify bottlenecks. It can then take steps to optimize the system's performance by adjusting resource allocation or fine-tuning service configurations.
In conclusion, Microservices architecture is an innovative and effective approach to managing large and complex software applications. By breaking down the application into smaller, independent services, developers can work more efficiently, deploy services independently, and scale individual services as needed.