Hey everyone! I'm working on a project that involves an API Gateway, a Lambda function, and an Aurora PostgreSQL database. I've set up some triggers in my database to modify specific data, and now I'm looking to enhance my system by adding a Redis cache to store information for specific devices. The goal is to retrieve device data directly from the cache instead of hitting the database every time the Lambda is called. My main question is: how can I effectively write values to the Redis cache from the database? Would using an AWS Lambda extension be a proper solution? The idea would be to update the cache whenever data is modified by a trigger in the database. Or is there a more streamlined or elegant approach to achieve this? Thanks in advance!
5 Answers
Honestly, I'm not sure why you would want to write to Redis directly from the database. It adds complexity, and there might be cleaner alternatives.
It sounds like you’re aiming for a standard Level 2 cache setup, which should be decoupled from your primary data store. Your database engine already handles a lot of caching, so tightly coupling your level 2 cache to the database isn’t necessary. Consider incorporating the caching layer into your data access layer instead. Also, if you haven't looked into it yet, DynamoDB might be a better fit than Redis/ElastiCache for your use case since it can offer similar performance with less operational overhead.
You should be focusing on cache management at the application level rather than directly at the database level. But keep in mind that invalidating the cache can be quite tricky when updates happen.
Check out this resource on caching strategies with Redis! It’s a great starting point for understanding how to implement your cache strategy effectively: https://d1.awsstatic.com/whitepapers/Database/database-caching-strategies-using-redis.73adbc8708febc9f3e5efc88382ab86f092bda82.pdf
I've never attempted to write directly to the cache from the database. Instead, I’d suggest setting up a 'wrapper' Lambda function that calls your main Lambda service, gets the response, and then stores the data in the cache. This keeps things flexible, so if your response changes, you won’t have to adjust your caching approach. Plus, it makes adding new endpoints easier.
This really is a solid reference! Lazy loading or write-through caching can be beneficial for managing updates.