Why are API gateways so damn important?🤔
Tags: webdev, beginners, discuss, devops
Published: 2023-11-22 17:27:52

You may heard about **"API Gateway"** term somewhere in your software engineering journey. 
You probably should've Googled and learned that back then, or probably not. I guess that concept was kinda tricky for you, even though it's pretty basic. 
Hope this article helps you pick up some new knowledge or brush up on what you already know. Let's dive into this 🙌.

## What the heck is this API gateway thing?
Okay, calm down. If you are total noob here, just forget the word "API" for now. You all know what is a gate, yeah? Think of this whole API gateway situation as your old gate.
<img width="100%" style="width:100%" src="https://media.giphy.com/media/8bX8xnLxFs3h6/giphy-downsized-large.gif">
What does this gate actually do? It's like the entry point to your property, and separates your stuff from everybody else's, yeah?
That's what these API gateway things are doing too. It acts as a single entry point to all the APIs. It uses to separate client APIs from internal services(microservices).


![How API gateway works](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q4mcjiym6i31g4cq1jwa.png)

## What's the point of using these API gateways anyway?

**1. Handling cross-cutting concerns.**
Let me take a guess, you have no idea what this "cross-cutting concerns" means, yeah? Just think of it as like the common stuff that every API endpoint needs, like authentication, authorization, rate limiting, caching etc...
Let me explain what are the benefits of using API gateway for cross cutting concerns.

- **Reduce the duplications** - Let's use authorization as an example. If we don't have an API gateway, we need to duplicate the authorization implementation in every microservice. And that means a bunch of duplicate codes.

- **Reduce the inconsistent implementations** - Different engineering teams may work on each (different) microservice and their implementations differently from each other. With an API gateway though, we can reduce that mess.

- **Reduce the code complexity** - So having less duplicate code and keeping things consistent makes the overall code way easier to understand.

**2. Decouples the client applications from the internal microservices.**
Microservice architectures consist with set of little services. Without an API gateway, we would need to call each service directly. So if anything changes, we have to change the client app code too.

> For an example, let's assume we deploy our application on AWS Beanstalk or EC2. If we move the app to another Beanstalk or EC2, we would need to update the client endpoint too.

But with an API gateway, we don't need to mess with the client code. It just maps the external APIs to the new internal services.

**3. Aggregate data from different services to a single response.**
I think you got the idea. Let's imagine a client requests some data from the server. They would typically need to navigate multiple microservices to gather the full data required (You can think this as something like database query concept). In these types of situations, an API gateway can aggregate the necessary information from the various services, and deliver it to the client as a single response.

**4. Request sharping.**
Okay, what is this request sharping doing? It helps to optimize backend performance by managing amount of requests received. We can take some examples like rate limiting, caching, queueing for this. (we will discuss more about these features in our future articles.)

## API gateway patterns.
I'm going to discuss about 3 most popular gateway patterns in briefly through this article. We will be discussing more details about these patterns in our upcoming articles and projects.

**1. Gateway Routing Pattern.**
Gateway routing pattern routes incoming API requests to the relevant microservice.

**2. Gateway Aggregation Pattern.**
API gateway aggregate data from multiple microservices into single response.

**3. Gateway Offloading Pattern.**
API gateway handle cross cutting concerns before passing the requests to the relevant microservice.

Alright, at this point I’d like to explore some useful applications of API gateways.

**1. Backend for Frontend Pattern.**
Here we use separate API gateways for specific client applications.
> For an example, One API gateway for web client, another API gateway for mobile client so on.

**2. Service Aggregator Pattern.**
Here what this does, when API gateway receives requests from the clients, dispatch the request of multiple internal backend services, combine the results and responds back to the client.

**3. Service Registry and Discovery Pattern.**
This allows to find the network locations of microservices without injecting or coupling. Service registry uses to update service locations. Using service discovery pattern, client find network locations of relevant microservices.

That covers the main points of API gateway I wanted to share through this article. If you're new to these concepts, hopefully this article has helped sharp your knowledge. Feel free to drop any questions in the comments. Look forward to connecting again in a future article with you all!