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.
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
— Application Metrics
— Load Balancing
— Authentication & Authorisation
— Content Negotiation
— 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.
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:
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
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
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
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
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:
You also have the ability to optionally intercept requests and responses passing through your routes, let’s analyse the following example:
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!