Re:Going Serverless: Migrating an Express App to AWS API Gat...
by pmontra on Tuesday October 11, 2016 @05:20AM
(#12682741)
DynamoDB is not immune from that problem. There is no way out: if the container is terminated the connection goes down. The key is not making Lambda stop and rm the container (in docker terms). If there are enough requests the container is reused and the connection stays up, but you must initialize it outside the function. An example with DynamoDB
const AWS = require("aws-sdk");
const docClient = new AWS.DynamoDB.DocumentClient();
module.exports.theFunction = (event, context, callback) => {
var params = {
TableName: "table",
Item: {
"attribute": "value"
}
};
docClient.put(params, function (err, data) {
...
});
callback(null, {...});
};
I didn't try but it should work with any other db.However, is Lambda cost effective for services with the amount of requests required to have their containers almost never terminated?
Re:Going Serverless: Migrating an Express App to AWS API Gat...
by brianwawok on Tuesday October 11, 2016 @09:01AM
(#12683928)
If there are enough requests to keep your container up all the time, why are you using lambda??
Re:Going Serverless: Migrating an Express App to AWS API Gat...
by pmontra on Tuesday October 11, 2016 @02:16PM
(#12687027)
Exactly what I was asking but I didn't do the math.
Maybe it's OK for burst shaped traffic. The first request pays a toll, the others are quick.
Re:Going Serverless: Migrating an Express App to AWS API Gat...
by andrew_k on Tuesday October 11, 2016 @02:28AM
(#12682222)
If you are creating connections outside of handler, it gets cached by Lambda between invocations. However you'll need to tune your database settings or you'll run out of connections on traffic spikes. Depends on your pattern of usage. Also cold start of lambda in VPC could take around 15 seconds
https://www.reddit.com/r/aws/comments/49l91l/lambda_function...
https://forums.aws.amazon.com/thread.jspa?messageID=735318&t...
My experience with DynamoDb was much smoother than with Postgres RDS in VPC
Re:Going Serverless: Migrating an Express App to AWS API Gat...
by Mizza on Tuesday October 11, 2016 @01:41AM
(#12682100)
If you design your correctly (at least in Python), you can cache and reuse your open database connection.
Re:Going Serverless: Migrating an Express App to AWS API Gat...
by ranman on Tuesday October 11, 2016 @01:43AM
(#12682108)
How exactly have you been doing that? It would be a cool blog post if you had the time. I've been thinking about really high volume applications that connect to haproxy instances that do DB load balancing but even that has it's own set of initial latency issues. I haven't fully figured out that architecture. In python in particular I'd really love to know how to setup the connection reuse.
Re:Going Serverless: Migrating an Express App to AWS API Gat...
by Mizza on Tuesday October 11, 2016 @01:48AM
(#12682127)
Check out the handler source code in Zappa for an example of a pattern like that. Similarly, if you use Zappa to deploy your application, if you create your database connection when the application loads, it'll just work.
Stop by the Zappa slack if you want to explore this in more detail! https://slack.zappa.io It could always use more investigation, but there are quite a few Zappa users at extremely high loads now without any issues.
Re:Going Serverless: Migrating an Express App to AWS API Gat...
by joeyspn on Tuesday October 11, 2016 @06:39AM
(#12683046)
You've got a problem with the naked domain redirects http://zappa.io https://zappa.io/
Re:Going Serverless: Migrating an Express App to AWS API Gat...
by avyfain on Tuesday October 11, 2016 @02:50AM
(#12682282)
This project looks super interesting. Thanks for sharing it!
Now I want to come up with a use case to test it out over the next few days, maybe something for the Echo? I joined your Slack, we'll see where this takes us :)
Re:Going Serverless: Migrating an Express App to AWS API Gat...
by Mizza on Tuesday October 11, 2016 @11:47AM
(#12685469)
That's a great use case, many people are using flask-ask and Zappa together to great success: https://github.com/johnwheeler/flask-ask
Re:Going Serverless: Migrating an Express App to AWS API Gat...
by bni on Tuesday October 11, 2016 @07:27AM
(#12683310)
Is application side connection pooling always necessary to have good response time?
MySQL has a really fast server side connection pool.
Re:Going Serverless: Migrating an Express App to AWS API Gat...
by brianwawok on Tuesday October 11, 2016 @09:02AM
(#12683938)
Usually 15 or 20 ms hit to make a new connection even in MySQL.
Also adds a fairly big server load. I suspect if a given MySQL instance supports 1000 long lived connections.. it would only support 100-200 connections that are closing every request. (Have not benchmarked this side, would be curious to see )
Re:Going Serverless: Migrating an Express App to AWS API Gat...
by breandr on Tuesday October 11, 2016 @12:04PM
(#12685661)
Hi Everhusk, I don't specifically call it out in the article, but the code is actually reusing database connections. As mentioned in other comments, the trick is to establish the connection outside of the handler so that the connection only happens once per container.
Re:Going Serverless: Migrating an Express App to AWS API Gat...
by collyw on Tuesday October 11, 2016 @04:55AM
(#12682663)
I am curious what sort of stuff is being built that need microservices (presumably so they scale) yet still has a database back end (usually the IO is the bottleneck in most applications).
Re:Going Serverless: Migrating an Express App to AWS API Gat...
by ende on Tuesday October 11, 2016 @01:47AM
(#12682122)
One way to do it is to stand an http rest API middle layer in between with something like http://postgrest.com.
It's a pretty big deal that every single API call requires a new database connection. The only solutions I've seen so far are to run a separate app to interface with the database, or moving the connection outside of the handler (which still has issues).
Am I missing something or is everyone just really happy to use DynamoDB?