Hey everyone! I need some advice on how to cache data in AWS Lambda. My use case involves retrieving a few config values (mostly booleans and integers) from a DynamoDB table, and I want to keep them stored for multiple invocations of the Lambda. I'm currently migrating a service to a Kotlin-based Lambda from EC2, so I'm missing the long-running process that I used to rely on for caching. I'm looking at several options for caching and would love some input on the best path moving forward considering ease of implementation and cost. Here are the caching options I've considered:
- DAX: caching on the DynamoDB side
- No cache: just querying the DynamoDB table with every invocation, but I'm worried about throttling issues caused by hot partitions
- Elasticache: using an external caching service
- A global variable to use the Lambda's ephemeral storage (but this might need a way to refresh from DynamoDB). Any thoughts?
6 Answers
Another tip would be to pull those config values outside of your handler. That way, they remain accessible for each invocation. Check out this link for more details on how to manage that!
If you're focused on retrieving from a DynamoDB table, DAX seems like a no-brainer choice. You could also use a global variable, but as you mentioned, managing cache invalidation could get tricky.
Also, think about how often the data changes and how fast you need access to the updated values for your functions. Depending on that, you might lean towards one solution or another.
Just so you know, the smallest AWS Redis instance should be enough to handle your caching needs if you go that route.
It really depends on how often your config values change. If they're only updated when you deploy new code, a global variable could work since the Lambdas restart during deployment. If that's not the case and you're considering ongoing caching, think about what other services you're using. For example, if you only need to cache DynamoDB data, DAX makes a lot of sense. If you might need to cache other stuff later, Elasticache might be a better fit. If you're not invoking the Lambdas often enough to cause bottlenecks, leaving it uncached could be fine too—just monitor how things perform.
For your needs, where it's just config variables, have you thought about using Parameter Store? It can handle caching with the AWS Parameters and Secrets Lambda Extension. This extension caches the parameter values from Parameter Store locally and uses them in subsequent invocations until they expire. Check out the AWS documentation for more info!
Yeah, that sounds like a solid plan! I’ll look into Parameter Store for sure.