systemdebt
systemdebt

Reputation: 4941

Caching results of a lambda function

We are developing a serverless application. The application has "users" that get added, "groups" that have "permissions" on different "resources". To check if a user has permission to do an action on a resource, there would be some calculations we will need to do. (We are using DynamoDB)

Basically, before every action, we will need to check if the user has permission to do that particular action on the given resource. I was thinking we could have a lambda function that checks that from a cache and If not in the cache, hits the DB, does the calculation, writes in the cache, and returns.

What kind of cache would be best to use here? We are going to be calling this internally from the backend itself.

Is API gateway the way to go still?

How about elastic cache for this purpose? Can we use it without having to configure a VPC? We are trying not to have to use a VPC in our application.

Any better ways?

Upvotes: 1

Views: 2254

Answers (2)

Vinícius OA
Vinícius OA

Reputation: 357

Where are those permissions map (user <-> resource) is stored?

This aws's blog post might be interesting (it's about caching in lambda execution environment's memory.), because you could use dynamodb's table for that.

Upvotes: 1

John Rotenstein
John Rotenstein

Reputation: 269284

They are all good options!

Elasticache is designed for caching data. API Gateway can also cache results.

An alternative is to keep the data "inside" the AWS Lambda function by using global variables. The values will remain present the next time the Lambda function is invoked, so you could cache results and an expiry time. Note, however, that Lambda might launch multiple containers if the function is frequently run (even in parallel), or not run for some time. Therefore, you might end up with multiple caches.

I'd say the simplest option would be API Gateway's cache.

Upvotes: 3

Related Questions