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.
I will show how easy to develop a generic API using AWS Lambda and Serverless Redis.
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:
serverless.yml as below. Copy your Redis URL from console and replace below:
Edit handler.js as below.
We have two serverless functions above.
name as parameter and loads a list from Redis. Then builds a histogram using the values in the list.
record function takes
values as parameters. It adds the
values to the Redis List with name
get function calculates the histogram over the latest 10000 latency records. Update the SIZE parameter to change this number.
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
Get the histogram for
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: