Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS lambda and web application with sessions and databases

We are in the very initial phase of developing a new web application and want to make use of Amazon cloud platform. However, we have a choice of developing web applications using Java spring framework or any other programming language/framework. The multipage application is not stateless i.e. application need to keep track of user session for transactions once logged in. Once all the required information is captured from the user through multiple pages, transaction is committed to the database.

However, there is a possibility of we using AWS lambda services for this application. Would it be a right decision to develop a web application with the above requirements using AWS Lambda (knowing that Lambda service is sessionless)?

like image 964
kk. Avatar asked Dec 22 '17 13:12

kk.


People also ask

Can Lambda connect to database?

Yes. AWS Lambda can connect to an AWS hosted databases such as RDS or DynamoDB. AWS Lambda can also connect to external databases which are public or grant network access.

How do you maintain a session in AWS Lambda?

AWS Lambda has nothing to do with session management unless you want to re-invent the wheel and write Lambda functions that store/retrieve session variables from the database, in which case I'd recommend that you use Amazon Cognito for session management. See Amazon Cognito Identity SDK for JavaScript.

Can Lambda store session state?

2 Answers. Show activity on this post. First of all Lambda is used as a stateless compute service. Therefore keeping session state in Lambda is not practical.


Video Answer


1 Answers

Your question is not specific to Lambda. The only case where session management is easy (e.g. can be solved by in memory or file based storage) is on a single server environment. That's probably where you're coming from.

The moment you start scaling horizontally (put more servers into the mix opposed to just putting more RAM/CPU into the server) you need a central storage for sessions (and also cache). This is the same if you choose docker, or EC2 or even metal servers behind a load balancer.

This is because you never can tell if the next request from the same user will land on the same environment. That's of course also true for lambda. There's one workaround though: that is, if you use a loadbalancer to use "sticky sessions": this would route every request of the same user to the same machine, see this AWS doc on session management. But sticky session is always suboptimal (e.g. scaling down would mean to destroy sessions) plus for lambda, sticky sessions are not possible afaik.

The real solution, as the other answers here suggest, includes central session storage via Elasticache. A quote from the above link:

In order to address scalability and to provide a shared data storage for sessions that can be accessible from any individual web server, you can abstract the HTTP sessions from the web servers themselves. A common solution to for this is to leverage an In-Memory Key/Value store such as Redis and Memcached.

like image 161
hansaplast Avatar answered Oct 13 '22 01:10

hansaplast