The need for API Gateways and how to solve it with KrakenD

Bugra Kocabay
Insider Engineering
9 min readMay 6, 2024
Photo by Yaselyn Perez on Unsplash

In today’s rapidly evolving digital landscape, the complexity of managing numerous microservices has underscored the critical need for efficient API Gateways. The gateways help, among other things, client applications and the backend services communicate. They also improve performance and security, and even enable the orchestration of the microservices.

This blog post dives deeper into the growing need of having a robust API Gateway in modern software architectures. It explains how KrakenD helps us effortlessly adopt microservices by offering great performance optimizations, security features, and easy configuration that really capture the development constraints of present days.

The Growing Need for API Gateways

In modern software architectures, the need for API Gateways is more than a trend; it’s a strategic necessity that addresses some of the core challenges of microservices-based development. I’ve listed below the most important reasons for using an API Gateway.

Complexity of Microservices

Successful systems in growing companies often turn into an enormous monolithic big ball of mud. Then to improve scalability and ease of development, the monolith is transformed into a large number of microservices. This increases fragmentation, especially in managing communication between services. API Gateways make such interactions simple by having one entry point for all external requests.

Performance Optimization

With such a strong demand for snappy, responsive applications, the communication between services has to be optimized for speed. The API Gateway minimizes the latency and helps in efficient routing of requests, sometimes by caching the responses of frequently made requests.

Security

Since every single one of those microservices might expose its endpoint to the outside world, API Gateways offer security capabilities. API Gateways aggregate and expose a security layer with typical demands on the services to be used for authentication, authorization, and encryption to protect from unauthorized access to resources and information.

Service Orchestration and Aggregation

Typically, demand for services will grow in number and in complexity. With the development of applications, they will have to be orchestrated, a series of service calls or aggregation from the response of many services into one. This is where API Gateways would be most efficient, with minimized round trips required and simplified client-side logic.

Monitoring and Management

An overview of system health and traffic patterns will always be key to maintaining the system's performance and availability. Centralized logging, monitoring, and analytics allow teams to quickly identify problems and promptly take necessary action.

Developer Productivity

API Gateways abstract the intricacies of the underlying microservices away from the developer, allowing him to focus on feature implementation rather than inter-service communication. It also reduces onboarding friction and speeds up development.

Solving API Gateway Needs with KrakenD

KrakenD stands out in the crowded space of API Gateways by offering a unique blend of performance, flexibility, and security. Its design is tailored to solve the common and emerging challenges faced by modern software architectures, making it a standout choice for developers and organizations alike.

KrakenD is a stateless, distributed, high-performance API Gateway that helps you effortlessly adopt microservices.

This is how KrakenD is defined on its website. This is a broad explanation, but I want to talk a little bit about the adjective “stateless”, because it is an important aspect for an API Gateway.

Being “stateless” means that the gateway does not store any client session data or information about consecutive client requests. Each request is processed independently, without relying on the previous state or session data stored by the gateway. With this, the scaling of the cluster will be much easier since every node will be able to respond to client requests independently. No coordination between them will be necessary.

Now that we have some grasp of what KrakenD is like, let’s dive into how KrakenD addresses these needs and what sets it apart from other API gateways in the market.

Simplifying Service Communication

KrakenD acts as a centralized point of entry for all client applications, streamlining the communication with various backend services. This consolidation simplifies the architecture by reducing the number of direct connections needed between clients and services. By serving as a single endpoint, KrakenD eliminates the complexity that comes with managing multiple service interactions, making the system more manageable and less prone to errors.

Performance Enhancements

KrakenD boosts performance by bundling multiple requests into one and using smart caching to skip redoing requests, speeding things up. It also lets developers tweak responses to be just right, making everything run smoother and faster.

Here is a benchmark, which can be found on KrakenD’s website.

https://www.krakend.io/docs/benchmarks/

Also, here is a comparison of KrakenD with some API Gateways in the market. It represents the amount of requests that each can handle in one minute.

https://www.krakend.io

Security and Management

KrakenD provides robust security features designed to protect backend services from a variety of threats. Rate limiting prevents clients from overloading services by making too many requests in a short period, which is crucial for maintaining the stability and availability of the services. CORS (Cross-Origin Resource Sharing) support ensures that web applications can securely access resources from different domains, a common requirement in today’s interconnected digital ecosystems. Furthermore, KrakenD includes bot detection capabilities, helping to mitigate the risk of automated attacks and scrapers that can disrupt service operations.

Ease of Configuration

Flexibility and ease of use are at the heart of KrakenD’s design. The gateway can be easily configured and deployed, making it accessible for teams of all sizes and skill levels. Its configuration system is designed to be both powerful and straightforward, allowing for rapid adjustments to the gateway’s behavior without the need for extensive coding.

Discovering some of the features of KrakenD

As we venture further into the heart of KrakenD, it becomes pretty obvious that the real power isn’t just performance-based, but the ability to be super flexible and easy to set up. KrakenD depends on a configuration file to initialize, from which it gets all the information needed to do its work.

Here is a very basic configuration in JSON format (you can also use YAML):

{
"$schema": "https://www.krakend.io/schema/v2.6/krakend.json",
"version": 3,
"endpoints": [
{
"endpoint": "/v1/foo-bar",
"backend": [
{
"url_pattern": "/foo",
"host": [
"https://some-host:8080.com"
]
}
]
},
{
"endpoint": "/v1/bar-foo",
"backend": [
{
"url_pattern": "/bar",
"host": [
"https://some-host:8080.com",
"https://some-host:8081.com"
]
}
]
}
],
"extra_config": {}
}

In this example, let’s focus on endpoints array. The first object is a direct usage, where we are creating the endpoint /v1/foo-bar and it directs the requests to a given host in the host array. In the second object, the usage is almost the same, except there are two hosts to redirect requests. This means that KrakenD will load balance requests to these hosts in Round Robin fashion.

Basic response transformation

KrakenD can allow or deny some parameters inside the response. Allowing will only return the given parameters inside the response and denying will block the given parameters to be forwarded.

Here are two examples:

{
"endpoint": "/posts/{user}",
"method": "GET",
"backend": [{
"url_pattern": "/posts/{user}",
"host": [
"https://some-host.com"
],
"allow": [
"userName",
"email"
]
}]
}
{
"endpoint": "/posts/{user}",
"method": "GET",
"backend": [
{
"url_pattern": "/posts/{user}",
"host": [
"https://some-host.com"
],
"deny": [
"password",
"birthDate"
]
}
]
}

Also, it can group the parameters, i.e. it gets data from two different backends, but one of the responses is encapsulated inside the given parameter.

Here is an example configuration with a response:

{
"endpoint": "/users/{user}",
"method": "GET",
"backend": [
{
"url_pattern": "/users/{user}",
"host": ["https://some-host.com"]
},
{
"url_pattern": "/posts/{user}",
"host": ["https://some-host.com"],
"group": "last_post"
}
]
}
{
"id": 1,
"name": "John Doe",
"username": "johnDoe",
"email": "johndoe@mail.com",
"address": {
"street": "High St",
"district": "Harlesden",
"city": "London"
},
"last_post": {
"id": 124,
"userId": 1,
"title": "Magna sit amet purus gravida quis blandit",
"body": "Risus quis varius quam quisque id diam vel. Senectus et netus et malesuada fames ac turpis egestas maecenas."
}
}

Backend Cache

An important aspect for an API Gateway is having a cache, which reduces the network traffic and alleviates the pressure on your services. KrakenD will cache the responses of configured endpoints in the memory. You have to be careful using this, as the larger your cache the more memory you will need.

You can adjust different aspects of caching like TTL etc. However we will not dive deep into that in this blog post.

A simple usage to cache responses for given endpoint, which will stay in memory for 5 minutes by default, is like this:

{
"endpoint": "/cached",
"backend": [
{
"url_pattern": "/",
"host": [
"https://some-host.com"
],
"extra_config": {
"qos/http-cache": {}
}
}
]
}

Rate Limiting

KrakenD allows different kinds of rate limiting and throttling, which give you more control in the usage of your infrastructure. For simplicity of this guide, we will check out two most used ones.

First, we have endpoint rate-limiting, which regulates the number of requests that a single endpoint is able to process at any given time. It is the kind of limit that safeguards the service for all customers, and at the same time, it reduces abusive actions, such as fast writing of content, aggressive polling, or too many API calls.

{
"endpoint": "/rate-limited",
"extra_config": {
"qos/ratelimit/router": {
"@comment":"Rate limit to 10k requests per 30 minutes",
"max_rate": 10000,
"every": "30m"
}
}
}

The second rate limit is the client or user rate limit, which is applied per individual user and endpoint. Though the limit rates can differ between each endpoint, all users are imposed with the same rate.

{
"endpoint": "/rate-limited",
"extra_config": {
"qos/ratelimit/router": {
"@comment":"Rate limit the client to 20 requests every 10 seconds",
"client_max_rate": 20,
"every": "10s"
}
}
}

Concurrent calls

This is an interesting feature of KrakenD, which makes the same request to multiple backend services and returns the first response to the client. Obviously you need to have multiple instances of the same service to make use of this feature. By putting more pressure on your services, KrakenD will always use the fastest response, and people have reported up to %75 improvement in response times. As this technique adds more pressure to the backends, you need to ensure that your services are able to handle this load.

Here is how you enable this feature:

{
"endpoints": [
{
"endpoint": "/bar",
"method": "GET",
"concurrent_calls": 3,
"backend": [
{
"host": [
"http://some-host-1.com:8000",
"http://some-host-2.com:8000"
],
"url_pattern": "/foo"
}
]
}
]
}

In the above example, KrakenD will make 3 requests to two given hosts, returning only the fastest one.

Sequential calls

Another very useful feature of KrakenD which will allow your clients to have less repetitive code. Let’s say you need to make two different requests to your backend services to get a result, and you need to use a value from the first one in order to make the second request. This kind of scenarios is very common in front-ends, and sequential calls help us with these. It can make sequential calls by using a value from the first request and using it in the second one.

Here is a sequence diagram that outlines a sample sequential call:

https://www.krakend.io/docs/endpoints/sequential-proxy/#do-you-really-need-a-sequential-proxy

This is how you would configure such a scenario:

{
"endpoint": "/hotel-destinations/{id}",
"backend": [
{
"@comment": "This is the index position 0",
"host": [
"https://hotels.api"
],
"url_pattern": "/hotels/{id}"
},
{
"@comment": "This is the index position 1",
"host": [
"https://destinations.api"
],
"@comment2": "resp0_ is the response of index position 0",
"url_pattern": "/destinations/{resp0_destination_id}"
}
],
"extra_config": {
"proxy": {
"sequential": true
}
}
}

The list goes on and on with many other features of KrakenD, which will not fit inside a single blog post.

Conclusion

As we wrap up our exploration of API Gateways, with a spotlight on KrakenD, it’s clear that the tool brings a really good mix of features to handle most of the problems modern software architectures have. KrakenD abstracts complex interactions between client applications and backend services, giving your system a performance boost while providing an extra layer of security. Its stateless nature and the ability to perform efficient request aggregation, caching, and response manipulation point to the role it plays not as a bridge but more of a catalyst towards seamless, secured high-performance digital systems.

Moreover, the ease of configuration that KrakenD brings to the table ensures that organizations can adapt quickly to changes, making it a strategic asset in the rapidly evolving digital landscape. Whether you’re trying hard for API Gateway deployment or looking to elevate your existing infrastructure, KrakenD is a powerful partner. It brings simplicity and efficiency in such a way as to make architectural complexity not a problem but an opportunity for your organization.

If you enjoyed this article, feel free to check out Insider Engineering Medium Page learn about latest software development technologies. Here are some of the stories that you may also like.

--

--