Published on

What are Microservices? How do they work?

Authors
  • avatar
    Name
    Melody Koh
    Twitter

The other day as I delved into the concept of microservices, I found it an intersting topic to share of. To start of, lets first define what microservices are.

Microservice is a small, loosely coupled distributed service. Each microservice is designed to perform a specific business function and can be developed, deployed, and scaled independently. This definition might seem complex to understand, let's simplify it by considering what a typical application structure looks like.

Monolithic applications

Applications are made of multiple components. The components communicate with each other to transmit data, fulfill requests, and keep the application running. Suppose that we have an application with tightly coupled components, these components might include databases, servers, user interface, business logic and so on. This type of architecture can be considered a monolithic application.

Monolithic application

In this approach to application architecture, if a single component fails, other components fail, and possibly the entire application fails. Adding or improving a monolithic application’s features becomes more complex as the code base grows. This complexity limits experimentation and makes it difficult to implement new ideas. Monolithic architectures add risk for application availability because many dependent and tightly coupled processes increase the impact of a single process failure.

Microservices

To help maintain application availability when a single component fails, we can design your application through a microservices approach.

Monolithic application

In a microservices approach, application components are loosely coupled. In this case, if a single component fails, the other components continue to work because they are communicating with each other. For example, one service may manage user authentication, while another handles product catalog functions. The loose coupling prevents the entire application from failing. With a microservices architecture, an application is built as independent components that run each application process as a service. These services communicate via a well-defined interface using lightweight APIs.

Difference Between Microservices and Monolithic Architectures Difference between Microservices and Monolithic Architectures

Benefits of Microservices

Microservices have grown increasingly popular in the last few years as organizations adopt DevOps and continuous testing processes to become more agile. Leading online companies such as Amazon, eBay, Netflix, PayPal, Twitter, and Uber have dropped monolithic architectures and moved to microservices. So what are the benefits of microservices? lets take a look into it.

Agility

Microservices foster an organization of small, independent teams that take ownership of their services. Teams act within a small and well understood context, and are empowered to work more independently and more quickly. This shortens development cycle times.

Flexible Scaling

Microservices allow each service to be independently scaled to meet demand for the application feature it supports.

Reusable Code

Dividing software into small, well-defined modules enables teams to use functions for multiple purposes. A service written for a certain function can be used as a building block for another feature. This allows an application to bootstrap off itself, as developers can create new capabilities without writing code from scratch.

And lots more!!!

How to move from monolithic to Microservices?

Monolithic application

Now we have understood what microservices are, you might wonder how do we move from monolithic to microservices. We can break it down with an example of a Node.js API service.

  1. Evaluate the Monolith: Understand the existing monolithic application and identify components for migration.
  2. Break Down the Monolith: Divide the monolith into distinct business capabilities for microservices (e.g., Users, Threads, and Posts Service).
  3. Strangler Pattern: Gradually replace monolithic parts with microservices using a step-by-step migration approach.
  4. API Definition: Clearly define APIs and contracts to enable seamless communication between microservices.
  5. CI/CD Implementation: Set up Continuous Integration/Continuous Deployment (CI/CD) for automated testing and deployment.
  6. Decentralize Data: Transition to a database-per-service model, reducing dependencies on a central database.
  7. Service Discovery: Introduce mechanisms for dynamic communication between microservices.
  8. Logging and Monitoring: Implement centralized logging and monitoring to gain visibility into microservices’ performance.

Real-World Applications of Microservices

Now that we know the what, why and how, let's dive deeper into microservices with a real world example of an Amazon E-Commerce Application.

Amazon’s online store is like a giant puzzle made of many small, specialized pieces called microservices. Each microservice does a specific job to make sure everything runs smoothly. Together, these microservices work behind the scenes to give you a great shopping experience.

Monolithic application

Below are some examples of microservices involved in Amazon E-Commerce Application:

  1. User Service: Manages user accounts, authentication, and preferences. It handles user registration, login, and profile management, ensuring a personalized experience for users.
  2. Search Service: Powers the search functionality on the platform, enabling users to find products quickly. It indexes product information and provides relevant search results based on user queries.
  3. Catalog Service: Manages the product catalog, including product details, categories, and relationships. It ensures that product information is accurate, up-to-date, and easily accessible to users.
  4. Cart Service: Manages the user’s shopping cart, allowing them to add, remove, and modify items before checkout. It ensures a seamless shopping experience by keeping track of selected items.
  5. Order Taking Service: Accepts and processes orders placed by customers. It validates orders, checks for product availability, and initiates the order fulfillment process.
  6. Order Processing Service: Manages the processing and fulfillment of orders. It coordinates with inventory, shipping, and payment services to ensure timely and accurate order delivery.
  7. Payment Service: Handles payment processing for orders. It securely processes payment transactions, integrates with payment gateways, and manages payment-related data.
  8. Logistics Service: Coordinates the logistics of order delivery. It calculates shipping costs, assigns carriers, tracks shipments, and manages delivery routes.

Takeaway

Microservices break down monolithic applications into smaller, more manageable components that are easier to handle. While a microservices architecture has numerous benefits for the proper use case, the transition can be complex. The end goal of migrating to microservices is to implement a distributed, loosely coupled, and independent framework. To learn more, checkout microservices.io for more info, they explain a lot in microservices, some of the concepts written in this blog came from this site too. That's all for now. See you in the next blog :)