This sample shows how to define a Lambda function which uses Redis-om via AWS Cloud Development Kit. This is a fork of prisma-lambda-cdk sample repo which replaces mysql/postgres with redis stack & prisma with Redis-om.
Redis OM is an open source object mapping, search etc library for Redis written in Typescript & very useful with its developer friendly API.
By this sample, you can see how a Lambda function which uses Redis OM can be deployed with CDK, how indexes can be created from Lambda, and how Redis OM works in Lambda.
This sample consists of the following AWS services:
- Amazon VPC
- Redis Cloud (Amazon doesn't offer redis stack yet)
- AWS Lambda
There're two Lambda functions:
- a function that reads and writes records from/to Redis
handler.ts
- a function that sets up the index for Request entity
migration-runner.ts
class Request extends Entity {
public awsRequestId: string;
public createdAt: Date;
}
The Request data is stored in JSON format using RedisJSON component where each entry contains the following values:
awsRequestId
: unique request id generated by awscreatedAt
: Timestamp when this request was received
It uses the repository.createAndSave()
function which internally uses JSON.SET
command.
Code Example:
requestRepository.createAndSave({
awsRequestId: context.awsRequestId,
createdAt: new Date(),
});
- The request data is accessed via
Request:{entityId}
key. RedisSearch module is used to fetch the data from Redis.
It uses the repository.search.all()
function which internally uses FT.SEARCH
command.
Code Example:
requestRepository.search().all();
Note: All the benchmarking was done using k6
Terminology:
- VU Count: Number of concurrent requests being made
- Duration: The duration for which test was run
- Function type: Type of Lambda function executed
- Prisma p95: p95 response time of prisma lambda function
- Redis OM p95: p95 response time of redis om lambda function
- Prisma p90: p90 response time of prisma lambda function
- Redis OM 90: p90 response time of redis om lambda function
- Prisma AVG: Avg response time of prisma lambda function
- Redis OM AVG: Avg response time of redis om lambda function
VU Count | Duration | Function Type | Prisma p95 | Redis OM P95 | Prisma p90 | Redis OM p90 | Prisma AVG | Redis OM AVG | Prisma Failed Reqs | Redis OM Failed Reqs |
---|---|---|---|---|---|---|---|---|---|---|
10 | 30s | Handler - Create Data | 91.99 ms | 67.56 ms | 74.57 ms | 60.4 ms | 71.56 ms | 57.16 ms | 0 | 0 |
20 | 30s | Handler - Create Data | 89.35ms | 65.7ms | 74ms | 59.19ms | 70.56ms | 54.73ms | 26.60% | 24.66% |
20 | 30s | Handler - Retrieve Data | 698.56ms | 516.26ms | 600.07ms | 394.06ms | 547.38ms | 344.9ms | 0 | 0 |
20 | 30s | Migration Runner | 10.87s | 3.16s | 8.95s | 2.93s | 7.8s | 2.67s | 0 | 0 |
From these stats we can infer that there's a signifact performance boost when migrating schemas using Redis OM instead of Prisma. Reads from Redis are also faster than Mysql & while not much but writes are faster as well.
- NodeJs - V16
- Docker
- AWS SDK
- Setup aws CDK by following this guide
- copy .env.example & add the REDIS_URI to it
cp .env.example .env
- Install dependencies using npm
npm ci
- Deploy the Lambda function using cdk
npx cdk deploy --require-approval never
- Take note of the output generated by the above function so that we can use them to test the lambda functions
RedisOmLambdaCdkStack.ApplicationHandlerLambdaArn*
RedisOmLambdaCdkStack.ApplicationMigrationRunnerLambdaArn*
- Create the search index
aws lambda invoke --function-name ApplicationMigrationRunnerLambdaArn response.json
# replace the `ApplicationMigrationRunnerLambdaArn` with actual ARN you wrote down in the previous step
- Creating Records
aws lambda invoke --function-name ApplicationHandlerLambdaArn response.json
# Replace the `ApplicationHandlerLambdaArn` with actual ARN you wrote down in the previous step
- Fetching Records
aws lambda invoke --function-name ApplicationHandlerLambdaArn --payload eyJjb21tYW5kIjoiZ2V0In0= response.json
# Replace the `ApplicationHandlerLambdaArn` with actual ARN you wrote down in the previous step
# Check response.json to see the records you created