Simple session state storage with Lambda?

Hi! I’m working on some services with Lambda and I’d like to be able to store simple key/value session data that would persist over multiple event calls. My needs are so basic it seems silly to use a whole database to store such a tiny amount of data temporarily. Could someone recommend the best way/aws service to handle something like this?

Dynamodb?

Elasticache/Redis?

Do you think that Dynamodb is best even for just a few bytes of data per request? I just don’t want to go overboard if there is a simpler way to do this.

Sure, why not. Millions of requests cost you under a dollar. On demand scaling for those tiny requests.

I believe AWS AppConfig in AWS Systems Manager may be what you are looking for.

https://docs.aws.amazon.com/appconfig/latest/userguide/what-is-appconfig.html

How much time will be between the event calls?

It is possible to store data in memory across lambda invocations - as long as your lambda environment isn’t terminated, your data will still be there. This article explains it in more detail: https://aws.amazon.com/blogs/compute/container-reuse-in-lambda/

You can’t assume that the data you are storing in memory will still be there, so I would store that data in dynamo as well/instead of if it’s data that you can’t recreate easily.

My experience is that your lambda environment will persist for minutes - the exact length of time depends on a lot of other factors.

Yeah it seems like a good service with a lot of flexibility - thank you for your input! Really appreciated!

Even though your storage needs are small, you’re not going to know when an existing Lambda will stop, or a new Lambda will start and your session data on the new Lambda will be gone.

DynamoDB is great for lightweight storage needs like this.

Others have mentioned it, but I agree that DynamoDB is probably the “default” answer for this. It’s fast, cheap, and relatively easy to set up and use (at least compared to something like RDS). Bonus points that you can use a VPC endpoint to access DynamoDB without reaching out to the internet.

I also agree that Elasticache would also be suitable, but frankly I’d think DynamoDB would be easier all around if the extra features of something like Redis isn’t necessary.

Yes I noticed that I can’t count on the Lambda runtime to persist and it could be waiting on user input between 1 second and 5 minutes. It looks like dynamo is probably the best option. Thank you!

I could be waiting on the user input between about a second until a 5 minute session timeout. There is no way of knowing how much time will pass between requests. It is looking like dynamodb might be the best option. Thanks for your perspective and comment!

Second for this. Exact use case these were designed for.

You can either use ElastiCache or DynamoDB for data session storage but depending on your use case, you can use the Step Functions as a state manager and persist random session data as you go along the downstream

To use these, you have to manage the cluster and for the lambda to access then you either:

  1. Make then public (no no) or
  2. Deploy lambda in vpc mode which takes much longer to start

Better to use dynamodb

But at what cost.

Interesting. I haven’t actually used redis from lambda do wasn’t aware this would be an issue. Having done a bit of a search it seems they may have resolved the slow start issue. Although I couldn’t really find info one way or the other.

Dynamo may still be the right tool for the job though.

Thanks for the tip! I think dynamodb it is!

Deploy lambda in vpc mode which takes much longer to start

This hasn’t been true for a while. VPC Lambda functions are just as fast as non-VPC functions now. Lambda no longer provisions ENIs at invocation time so there’s no init penalty anymore

Redis