- 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.
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.
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:
perf-test-1
:
perf-test-1
: