A “.js” API Gateway for the masses

Rolando Santamaría Masó
5 min readApr 7, 2020

Introduction

In this article, we present a Node.js solution for API Gateways implementation. From a developer perspective, we describe how we can easily implement the most common API gateway functionalities using only Node.js and JavaScript ❤️

API Gateway analogy ;) — photo courtesy of https://pexels.com

API Gateway?

An API Gateway encapsulates the internal architecture of your application, by exposing a single, client friendly API through one and only URL; a facade that acts as a single application entry point.

For most distributed applications, it makes sense to implement an API Gateway, which acts as a single entry point into a system. The API Gateway is responsible for request routing, composition, and protocol translation. It provides each of the application’s clients with a custom API. The API Gateway can also mask failures in the backend services by returning cached or default data.

https://www.nginx.com/blog/building-microservices-using-an-api-gateway/

Common responsibilities usually placed at API Gateway level

  • Obfuscation of the internal architecture and distribution of microservices-based applications. Usually reducing configuration overhead.
  • Non-functional requirements and cross-cutting concerns, making the target services implementation as lightweight and thiner as possible:
    — SSL Termination
    — Logging
    — Application Metrics
    — Load Balancing
    — Authentication & Authorisation
    — Content Negotiation
    — Caching***
    — Content Compression
    — Rates Limit
    — …
  • Data aggregation and transformation to simplify clients interaction with the distributed microservices (as usually multiple backend calls are required for the simplest frontend view rendering). Data aggregation also reduces the number of requests/round-trips from the internet.

fast-proxy

Node.js framework agnostic library that enables you to forward an http request to another HTTP server at maximum performance. Supported protocols: HTTP, HTTPS, HTTP2

When we talk about HTTP requests proxying in Node.js, we need to consider the fast-proxy library. It was born using fastify-reply-from as a base, but without all the fastify framework friction and dependencies .

After all performance optimisations, this library can reach up to 46 times the performance of the Node.js widely used http-proxy module:

http-lambda-proxy

Now you can proxy HTTP requests to AWS Lambda functions. 🚀

The http-lambda-proxy module allow us to forward HTTP requests to services running inside AWS Lambda functions. This feature enable many awesome enterprise features:

  • Very low cost for computing instances
  • Unprecedented scalability with rather constant execution times
  • and many more
Forwarding HTTP requests on port 8080 to AWS Lambda function

Also, by using the great serverless-http module, developers can run full-size REST APIs as lambdas…, let me say this again:

You can safely run your stateless Node.js REST APIs inside AWS Lambda functions!

Meet fast-gateway

fast-proxy” and “http-lambda-proxy” libraries are too low level, let’s provide a friendly, all in one API Gateway solution for HTTP and Serverless based distributed applications:

Basic HTTP requests forwarding example. Find much more at: https://github.com/jkyberneees/fast-gateway

By the way, your API Gateway can run inside a lambda function as well!

Is fast-gateway actually fast?

Here a basic reverse HTTP proxy benchmark on a MacBook Pro 2019,2,4 GHz 8-Core Intel Core i9, 32 GB 2400 MHz DDR4 using:

wrk -t8 -c8 -d5s http://127.0.0.1:8080/service/get
Running 5s test @ http://127.0.0.1:8080/service/get
8 threads and 20 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 559.62us 215.40us 5.41ms 84.56%
Req/Sec 3.59k 404.46 4.91k 87.96%
145491 requests in 5.10s, 15.54MB read
Requests/sec: 28528.35
Transfer/sec: 3.05MB

fast-gateway is built on top of restana, faster Node.js HTTP server in town!

A guide through examples

Basic reverse proxy

API Gateway on the left will route all traffic with prefix “/service” into the service on the right

Middlewares

Global and route level connect-like middlewares allow developers to easily handle cross-cutting concerns at gateway level, in this example: CORS, Helmet and Authentication

Caching

You can also easily implement gateway level caching for all your services. Drastically reducing the latency of your response times to one digit milliseconds.
You can read more about it here:
https://www.npmjs.com/package/fast-gateway#gateway-level-caching

Hooks

You also have the ability to optionally intercept requests and responses passing through your routes, let’s analyse the following example:

Conclusions

In this article we have presented a Node.js based solution for API Gateways implementation. You have learned how to easily setup your own reverse proxy and put some logic on top of it, so your distributed services don’t have to repeat. It was also briefly described how to centrally intercept and transform requests and responses coming to and from your remote services.

If Node.js is your favourite backend stack, to improve your distributed solutions architecture you don’t have to be limited to Nginx, or just carry on expensive costs for the enterprise API Gateways solutions out there.

In the same way you build your REST API services using Node.js, you can build your API Gateways too…

Yes, you can do!

--

--