Node.js API Gateway? A developer perspective

Introduction

In this article we discuss a Node.js solution for API Gateways implementation. From a developer perspective, we will 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

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

https://www.npmjs.com/package/fast-proxy

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:

fast-gateway

fast-proxy is too low level, let’s make it a friendly, all in one API Gateway solution for HTTP based distributed applications: fast-gateway

Read more about it at: https://www.npmjs.com/package/fast-gateway

Is fast-gateway actually fast?

Here a basic reverse proxy benchmark on a MacBook Pro 2016, 2,7 GHz Intel Core i7, 16 GB 2133 MHz LPDDR3 using:

wrk -t8 -c8 -d20s http://127.0.0.1:8080/service/get

fast-gateway is built on top of restana, faster Node.js framework listed at: https://github.com/the-benchmarker/web-frameworks

NGINX is open source software for web serving, reverse proxying, caching, load balancing, media streaming, and more. It started out as a web server designed for maximum performance and stability. In addition to its HTTP server capabilities, NGINX can also function as a proxy server for email (IMAP, POP3, and SMTP) and a reverse proxy and load balancer for HTTP, TCP, and UDP servers.

https://www.nginx.com

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: CORSHelmet 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 start limited by Nginx, or just carry on expensive costs for the enterprise API Gateways solutions out there, in the same way you build your HTTP service using Node.js, you can build your API Gateway too.

Yes, you can do!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.