This repo shows how you can use Momento as a cache layer with AWS AppSync resolvers, without using any Lambda function.
In this demo, we want to query the wikipedia API through AWS AppSync. In order to avoid overwhelming the wiki and return results faster for recurrent requests, we use Momento as a caching layer.
Under the hood, Momento works with an HTTP API which allows you to read and write cache items. This solution uses AppSync pipeline resolvers with an HTTP data source in order to get an item from cache. When there is a cache hit, the data is returned immediately. Otherwise, the data is resolved from Wikipedia and then saved into the cache.
graph TD;
start([Start]) --> get[GetCacheItem]
get --> hit_or_miss{Cache Hit?}
hit_or_miss -- miss --> resolver[ResolveData] --> put[PutCacheItem] --> ret
hit_or_miss -- hit --> ret
ret([Return])
This is a proof of concept for learning purpose only. Before using this for production workloads, there are some considerations and improvements that could be taken:
- compare with "native" AppSync caching feature.
- use hooks/scripts to create a cache before deploying.
- store tokens securely somewhere (e.g. on Secret Manager).
- use short-lived momento tokens and auto-renew them.
- etc.
- Generate a momento token
- Create a cache called
wikipedia
- Create a (secured) SSM parameter named
/momento/cache/token
and store your momento token in it. npm i
npx sls deploy