This tutorial shows a basic serverless TypeScript REST API using DynamoDB (AWS) or Firestore (GCP) supercharged with Momento Serverless Cache. It contains a serverless application that can be built and deployed with CDK in AWS or Pulumi in GCP. The API is supposed to represent a basic users
api. It contains the following endpoints:
# Generate base users in authoritative store to use for test
POST /bootstrap-users
# Get single user from authoritative store
GET /users/1
# Get single user from cache
GET /cached-users/1
# Get passed user's follower names from authoritative store
GET /followers/1
# Get passed user's follower names from cache
GET /cached-followers/1
Bootstraps all the test user data needed. For this demo it generates 100 test users each following 5 random other test users.
Makes 1 call to DB (/users
) or Momento (/cached-users
)
$ curl $API_URL/cached-users\?id\=1 -s | jq .
{
"id": "1",
"followers": [
"63",
"60",
"81",
"18",
"60"
],
"name": "Happy Sloth"
}
In case you don't have it already, jq is a great tool for working with JSON.
Will make a call to either DB (/followers
) or Momento (/cached-followers
) for the passed
user id and then five additional calls (in series) to either DB or Momento to look up each follower name.
$ curl https://x949ucadkh.execute-api.us-east-1.amazonaws.com/Prod/cached-followers/1 -s
["Dumb Rabbit","Excited Wombat","Lazy Squirrel","Lazy Sloth","Strange Rabbit","Lazy Squirrel","Mystical Dog","Strange Cat","Dumb Dog","Excited Dog","Clingy Lion","Strange Frog","Strange Rabbit","Lazy Frog","Happy Sloth","Happy Sloth","Sad Cat","Clingy Cat","Happy Sloth","Obnoxious Fish","Excited Lion","Spacey Frog","Goofy Dog","Goofy Dog","Happy Hamster","Obnoxious Dog","Sad Cat","Obnoxious Lion","Happy Sloth","Obnoxious Otter","Angry Dog","Sad Rabbit","Excited Fish","Dumb Hamster","Clingy Otter","Angry Dog","Happy Hamster","Happy Hamster","Clingy Hamster","Happy Sloth","Happy Dog","Spacey Wombat","Clingy Lion","Clingy Sloth","Clingy Hamster","Rare Lion","Spacey Wombat","Angry Rabbit","Mystical Zebra","Excited Frog","Happy Dog","Angry Dog","Spacey Wombat"]%