An Image with a text " Understanding microservice & api gateway"

In today's interconnected digital landscape, APIs (Application Programming Interfaces) have become the backbone of modern software development. They facilitate communication between software systems, enabling seamless interaction and data exchange. However, as the number of APIs within an ecosystem grows, managing them efficiently becomes increasingly challenging. This is where Microservices & an API gateway comes into play. In this article, we’ll understand what Microservices & an API gateway is, when we might want to use them, some existing tools that offer API gateway, and most importantly, in the end, I’ll link some resources on how to create Microservices & a custom API gateway using node.js.

Excited? Let’s dive IN!

Now, before we can talk about API gateways, we need to first talk about why we may need it because we can do without it - of course! So, why and when might we need an API gateway? We need it mainly when we easily want to manage a large API ecosystem, or when we’re following modern software architecture design such as microservices. So, what is a microservice?

What are microservices?

In a microservices architecture, microservices applications are composed of small, independently deployable services that communicate with each other over a network. Each service is responsible for a specific business capability and can be developed, deployed, and scaled independently.


To break it down, let’s use a very popular/real-life example:

Imagine you're building an e-commerce platform similar to Amazon or eBay. In a traditional monolithic (opposite of microservice) architecture, all functionality would be tightly integrated into a single application. However, in a microservices architecture, different aspects of the e-commerce platform are broken down into separate services, each responsible for a specific function such as;

  • User Service: This microservice handles user authentication, registration, and profile management. It stores user information such as usernames, passwords, addresses, and payment details. By separating user management from other functionalities, the User Service can scale independently based on user demand.

  • Product Service: The Product Service manages product catalog information, including product names, descriptions, prices, and inventory levels. It handles tasks such as adding new products, updating stock levels, and retrieving product details. By decoupling product management from other components, the Product Service allows for efficient inventory management and updates.

  • Order Service: This microservice is responsible for processing and managing orders placed by users. It handles tasks such as order creation, order fulfillment, payment processing, and order status updates. By separating order management from other functionalities, the Order Service can scale independently to handle peak order volumes during sales or promotions.

  • Payment Service: The Payment Service handles payment processing and transaction management. It integrates with external payment gateways to securely process credit card transactions, handle refunds, and manage payment authorization. By isolating payment processing into its own service, the Payment Service can ensure compliance with payment industry regulations and maintain transaction security.

  • Shipping Service: This microservice coordinates the shipment and delivery of orders to customers. It calculates shipping costs, generates shipping labels, and tracks package delivery status. By separating shipping functionality from other components, the Shipping Service can integrate with third-party shipping carriers and optimize delivery routes for faster order fulfillment.


Each of these microservices communicates with others through well-defined APIs, typically over HTTP or message queues. For example, when a user places an order, the Order Service may interact with the Product Service to verify product availability, the Payment Service to process payment, and the Shipping Service to schedule delivery. This loose coupling enables independent development, deployment, and scaling of each microservice, making the e-commerce platform more resilient, scalable, and maintainable.

In summary, a microservice is a small, independent component responsible for a specific business function. By breaking down the monolithic application into discrete microservices, developers can build and scale each component independently, leading to improved agility, scalability, and reliability of the e-commerce platform.

Now that we have some overview and understanding of what Microservices are, let’s talk about the next big thing - API gateway and how they connect to microservices

What exactly is an API gateway?

An API gateway serves as a single entry point for all incoming API requests. It acts as an intermediary between clients and backend services, handling tasks such as authentication, authorization, rate limiting, request routing, load balancing, and response caching. By consolidating these responsibilities into a centralized component, an API gateway simplifies the management and operation of APIs, promoting scalability, security, and reliability. Remember that in a Microservice architecture, each service has its own features of functions that it handles, and they communicate within themselves.

However, what happens if a user wants to communicate with them? What happens if the user wants to make an HTTP request to the payment service for example, or the order service? Of course, they can. But that would mean the user would need to make different API calls. We don’t want that. We don’t want to give the user loads of APIs to call - we need a single source of truth - and that’s what an API gateway offers.

Let’s take a look at the following sketch design:

An Image explaning what an API gateway is.

From the above sketch, you can see that the API gateway is the middleman between the client and your services. 

As the entry point for client requests, the API gateway provides a unified interface to the diverse array of microservices within the ecosystem. It abstracts the complexities of the underlying service infrastructure and exposes a simplified API to clients, shielding them from the intricacies of individual service implementations. Let's explore some specific ways in which API gateways support microservices architectures:

  • Service Discovery and Dynamic Routing: Microservices are typically deployed in dynamic environments where instances may come and go based on factors like auto-scaling and failure recovery. An API gateway can dynamically discover available service instances and route requests to them based on predefined rules and load-balancing strategies.

  • Aggregation and Composition:  In microservices architectures, it's common for client applications to require data from multiple services to fulfill a single request. An API gateway can aggregate data from multiple backend services and present it to clients in a single response, reducing the number of round-trips and improving performance.

  • Security and Access Control: Microservices often handle sensitive data and functionality, making security a paramount concern. An API gateway can enforce authentication, authorization, and encryption policies to ensure that only authorized clients can access protected resources. It can also offload tasks like token validation and user authentication, freeing up backend services to focus on business logic.

  • Rate Limiting and Throttling: To prevent overload and ensure fair resource allocation, API gateways can enforce rate limiting and throttling policies. By controlling the rate of incoming requests, API gateways help maintain system stability and prevent degradation of service quality during periods of high traffic.

  • Monitoring and Analytics:  Understanding the performance, usage, and health of microservices is critical for maintaining reliability and optimizing resource utilization. API gateways can collect metrics, logs, and traces from incoming requests, providing valuable insights into system behavior and identifying areas for improvement.

API gateway is a topic that requires more understanding. If you’re the type that likes using existing tools and software to build products, you may consider using tools such as AWS, NGINX web server, Tyk, and other API management tools. 

If you like exploring things, you may want to learn how to create a custom API gateway.

Conclusion:

So far we’ve spoken about what A Microservice is, the need for an API gateway and we’ve seen the benefit of both software designs.

The way you build your microservices or API gateway doesn’t matter. The programming language you use doesn’t matter. What matters most is how lean each service is, and how scalable they are. 

Microservices and API gateway are fun to learn, and I can only advise that you take the time to go beyond these basics to learn deeper.