Build a Serverless Histogram API with Redis
This tutorial shows how to build a histogram API with Redis.
While developing the latency benchmark for the serverless databases (DynamoDB, FaunaDB, Upstash), I wished there was an API where I will record the latency numbers and get the histogram back. In this tutorial, I will build such an API where you can record your latency values from any application. It will be a REST API with following methods:
- record: Records numeric values into the histogram.
- get: Returns the histogram object.
Motivation
I will show how easy to develop a generic API using AWS Lambda and Serverless Redis.
See code.
1
Create a Redis (Upstash) Database
Create a database as getting started
2
Serverless Project Setup
If you do not have it already install serverless framework via:
npm install -g serverless
In any folder run serverless
as below:
See Using AWS SAM, if you prefer AWS SAM over Serverless Framework.
Inside the project folder create a node project with the command:
Then install the redis client and histogram library with:
Update the serverless.yml
as below. Copy your Redis URL from console and
replace below:
This example uses ioredis, you can copy the connection string from the Node tab in the console.
3
Code
Edit handler.js as below.
We have two serverless functions above. get
takes name
as parameter and
loads a list from Redis. Then builds a histogram using the values in the list.
The record
function takes name
and values
as parameters. It adds the
values
to the Redis List with name name
.
The get
function calculates the histogram over the latest 10000 latency
records. Update the SIZE parameter to change this number.
The fixUrl
is a helper method which corrects the Redis url format.
4
Deploy and Try the API
Deploy your functions with:
The command will deploy two functions and output two endpoints. Try the endpoints with setting parameters as below:
Record latency numbers to perf-test-1
:
Get the histogram for perf-test-1
:
Batching
It can be costly to call a remote function each time for latency calculation. In your application, you should keep an array or queue as a buffer for the latency numbers, then submit them in batches to the API when the array reaches the batch size. Something like below:
Was this page helpful?