How To Optimize API Gateways with Zipkin and Express

Optimize your APIs

API Gateways are undergoing a beautification.

API Gateways are a means to minimize or optimize bandwidth usage by application developers requesting micro services.  Additionally, API Gateways become a conduit to synchronize and streamline the ongoing flow of requests for services for an application. Upon further examination, we see the growing and urgent need for optimization, but where to start?

Key Factors to Optimize an API Gateway

Before you beautify your API Gateway, it’s important to understand some key factors to consider.

Non-blocking calls maximize traffic flow for the available bandwidth.

Sometimes, non-blocking calls and asynchronous are just two different words for the same thing. However, in other instances, they are completely different.

Let’s begin with a straight forward explanation. Since an answer can’t be returned rapidly. a non-blocking call returns an immediate error. In this case, it does not go on to do anything else.  Additionally, his also means that there must be a way to query whether or not the API is ready.t

On the other hand, asynchronous means the API always returns right away, initiating a “background” effort to fulfill your request, so there must be a related way to obtain the result.

Group calls for services by the type of client, framework, or protocol minimizes the clutter of single service requests.

As a result, grouping your calls by the type of client, framework, or even protocol can help keep everything more organized and reduce chatter.

The Case for Measuring Performance

Previously, we’ve outlined that staying true to how REST has normalized around “resources”, makes it more natural to structure your backend based on domains when you’re thinking of migrating your monolithic app to a set of discreet microservices.

Due to web scale microservices usage, volumes can spike unexpectedly resulting in a choke point. In order to address this, cloud-based API Gateways are flexible. This flexibility and the capacity to adapt with automated policy management makes cloud based API Gateways an excellent choice. In addition to flexibility, real-time analytics can help time the requests and their flow rate to match the available bandwidth.

Application Metrics for API Invocation

Optimization of traffic flow on the API Gateway begins with the generic metrics of the flow rate of requests and the overall volumes.  Then, a request is sub-divided into categories such as calls by client-type.

If it is a mobile client, the API gateway provides APIs that help to segregate the services it needs and the API Gateway segregates the most efficient load distribution among end points. To gain efficiency, you should avoid searching of an entire registry of services.

Finally, you can optimize bandwidth usage by calling a package of services concurrently, and not a sequentially. Yet, applications can also have protocol translation needs in a polygot application development environment. Hence, analytics help you, the developer, find translation patterns and automate these processes. Save time AND your network resources!

Express & Zipkin 

We believe that Express is an industry leading web applications framework for Node.js web and mobile applications development. In a very simple way, you can create an API Gateway with Express.  Additionally, Express has a very large ecosystem of middleware, robust application performance metrics (APM) and tracing. Further optimization can be attained by introspecting each API invocation after grouping API.

While well known commercial providers like:  New Relic, AppDynamics, Datadog, and many others, there are now open source solutions that give similar basic tracing functions.

First, we’re going to examine ZipkinAs a Java based open source tracing framework, Zipkin is geared toward microservices. Additionally, it has instrumented libraries including JavaScript/Node.js as well as many popular transports including HTTP, Kafka, Scribe to take data from the tracers to one of their collectors.

Next, in order to get an idea of what Zipkin looks like, here is an example trace diagram:

With Microservices in mind, Zipkin’s UI has a general “services” label.  For developers, this is a great way to break down a monolithic infrastructure through all of its different layers and domain functionality using a series of global events or markers.

Also, you can create tracers that are instrumented into each service and see how each of the services is performing.

Instrumenting Microservices with Express as an API Gateway

Zipkin-instrumentation-express, a popular NPM module contains Express middleware. By using this module, you add tracing capabilities through the standard Express app.use(…) interface and it also comes with a library to add headers for tracing to express-http-proxy for downstream microservice calls. Furthermore, Express can act as an API Gateway. 

Here’s the config example listed from npmjs.org:

const express = require('express');

const {Tracer, ExplicitContext, ConsoleRecorder} = require('zipkin');

const zipkinMiddleware = require('zipkin-instrumentation-express').expressMiddleware;

const ctxImpl = new ExplicitContext();

const recorder = new ConsoleRecorder();

const tracer = new Tracer({ctxImpl, recorder}); 
//Next, configure your tracer properly here 

const app = express();

//Now, Add the Zipkin middleware 

app.use(zipkinMiddleware({

  tracer,

  serviceName: 'service-a' // name of this application 

}));

Additionally, downstream microservices can be assigned their own tracer with serviceName label by specifying the route.

//Next, you can add the Zipkin middleware to cart microservice

app.use('/v1/api/cart',  zipkinMiddleware({

  tracer,

serviceName: microservice-cart' // name of this microservice 

}));

// This is where you add the Zipkin middleware to catalog microservice

app.use('/v1/api/catalog', zipkinMiddleware({

  tracer,

  serviceName: microservice-catalog// name of this microservice 

}));

Additionally, now you’ve had a chance to understand more about Express and Zipkin, we encourage you to explore more open-source tools you can use to optimize your tech stack.


  • Sign up for our private beta – your feedback helps prioritize our roadmap with the most value realized within the shortest amount of time
  • Learn about the inaugural feature set we’re striving for to make APIs repeatedly fast, easy and manageable as you evolve through the API lifecycle itself.
  • Sign up for the latest development on APIs and microservices.