News for hackers. Stuff that matters.
Re:Serverless Best Practices by tomcam on Sunday September 02, 2018 @02:31PM (#17898002)
I still don’t understand how you do a PostgreSQL insert on a serverless system—please help! Also, this statement is patently silly:

> The biggest point to make here is that serverless architecture may well require you to rethink your data layer. That’s not the fault of serverless.

Well, it is the fault of serverless. It’s a shortcoming. Own it. The trade off may we be worthwhile, I just can’t tell yet. I’m trying to wrap my mind around serverless as regards database-backed apps, but obviously it would be preferable to be able to use the RDBMS of my choice.

Re:Serverless Best Practices by deeg on Sunday September 02, 2018 @04:04PM (#17898371)
Agreed. Perhaps the first thing a "best practices" post should start off with is what kinds of problems serverless is best suited for.
Re:Serverless Best Practices by tachang on Sunday September 02, 2018 @02:48PM (#17898066)
I agree with this. Having a traditionally relational store can be a huge help: lots of people know it well, the disadvantages are well known, and everything is insanely mature. I dislike how the article tries to say that isn’t serveless’s fault when it is just a trade off.
Re:Serverless Best Practices by actuator on Sunday September 02, 2018 @03:47PM (#17898279)
Yeah, RDBMSs do have some big advantages; one of them being the plethora of query patterns possible.

If there are other parts of your application which are consuming the same data but in different patterns, it might not be necessary that systems like DynamoDB work in those cases. You might need to make several secondary indexes in those cases and all of them require provisioning of their own. The other alternative being setting up some ETL layer which transforms the data and sends to appropriate sinks, from where they can be consumed; but for small services that adds more moving parts and cost to the system.

Re:Serverless Best Practices by k__ on Sunday September 02, 2018 @05:38PM (#17898811)
> It’s a shortcoming. Own it.

Well, I don't know much about Oracle and the other players, but AWS at least tries to own it with Aurora Serverless.

Re:Serverless Best Practices by matchagaucho on Sunday September 02, 2018 @02:42PM (#17898046)
Moving away from RDBMS just wasn't an option for us. But AWS serverless provided a couple benefits:

  1. Aurora Serverless RDBMS (In pilot)
  2. Lambda function property caching (for which we cache connections)
Re:Serverless Best Practices by staticassertion on Sunday September 02, 2018 @05:53PM (#17898887)
How does Aurora Serverless help with the issue of having to reconnect for every lambda? (I am unfamiliar with the Aurora Serverless offering)

Also, can you elaborate/ point me to info on 2?

Re:Serverless Best Practices by lostcolony on Sunday September 02, 2018 @07:49PM (#17899304)
Really quickly then -

Background info: A lambda that spins up stays live for a while, and can continue to handle requests. The function handler is called again, so anything scoped to it, will disappear with the function, but anything scoped outside of the function will persist.

Ergo, move your connection creation outside of your handler.

Realistically you may need some abstraction to properly close and reopen the connection in the event of failure (so you can't have a 'bad' lambda, where something has happened to the connection and you can't fix it without deploying code again, to trigger a lambda refresh), but for the most part it just works.

Re:Serverless Best Practices by philliphaydon on Sunday September 02, 2018 @09:21PM (#17899626)
Keeping connections open on a lambda isn’t a good thing. If you have a lot of Lambdas and a lot of load you can end up hitting max connection limits. Better to open late and close early and take a hit rather than risk taking your db offline.
Re:Serverless Best Practices by lostcolony on Sunday September 02, 2018 @11:48PM (#17900177)
Oh, agreed; it doesn't bottleneck on the connection but instead keeps creating them which can take performance down when you spike. Hence why the original article speaks of using something other than a RDBMS, such as Dynamo, since there really is no way of addressing that issue (since you can't set a concurrency limit on one kind of function; else you could have one function that has high concurrency invoke one that is far lower, that exists just to keep connections pooled).

If you absolutely must hit an RDBMS, be quick about it to free up the connection, and definitely consider if the latency matters that much; a few dozen ms more to initialize and close the connection is going to be a LOT cleaner.

But that said, how badly it penalizes you for scaling connections up depends on the DB.

Re:Serverless Best Practices by staticassertion on Sunday September 02, 2018 @08:04PM (#17899349)
Interesting. I'd considered this, but was worried about failure cases. Seems tricky.
Re:Serverless Best Practices by ersiees on Sunday September 02, 2018 @06:38PM (#17899074)
You don’t need a reconnect for every invocation actually, only per instance. I know you don’t want to think about instances with lambda, but actually they just run on some VM and you can just open one connection per VM.
Re:Serverless Best Practices by ActVen on Tuesday September 04, 2018 @10:46AM (#17909394)
We use lambda functions(node with pg) to process incoming data. We just connect them to Aurora(Postgres) and make the inserts. We have never had any problem with the process whether we are pulling from a queue or other data source.
Re:Serverless Best Practices by travbrack on Sunday September 02, 2018 @04:08PM (#17898393)
So why is rdbms and serverless seemingly mutually exclusive? Are rdbms queries really that slow compared to something like dynamodb?
Re:Serverless Best Practices by actuator on Sunday September 02, 2018 @04:23PM (#17898452)
Actually RDBMS queries are quite fast. But those systems weren't designed for use patterns which serverless applications typically have. Like MySQL will limit you on connection count and each new connection puts extra overhead on the server. So if every function negotiates its own connection you might eat away DB resources just on negotiating connections even though the DB queries were fast.
Re:Serverless Best Practices by AmericanChopper on Sunday September 02, 2018 @06:26PM (#17899026)
A couple of concepts core to the serverless model is that components communicate via API, and that resources are consumed on demand. Traditional RDBMS don’t really fit into that model. Google Spanner is the only thing I’ve seen that seems to come close. AWS doesn’t have much aside from Dynamo, especially considering how poorly lambdas perform when you put them inside a VPC.
Re:Serverless Best Practices by k__ on Sunday September 02, 2018 @05:36PM (#17898805)
As far as I know, most RDBMS are connection based, which brings an overhead. When you got your connection they're quite fast.
Re:Serverless Best Practices by whopa on Sunday September 02, 2018 @05:44PM (#17898847)
I wonder why all the predominant RDBMSes are heavyweight connection based, and why this is presumably so hard to change. There's nothing in SQL that requires this to be the case, at least conceptually.
Re:Serverless Best Practices by wgjordan on Sunday September 02, 2018 @06:53PM (#17899132)
TCP/IP performs a handshake to establish connections so there will always be some per-connection overhead required for this, no matter the service on the other end.

Beyond that, MySQL for example pre-allocates a fixed amount of memory for read, join and sort buffers per connection to efficiently handle different types of queries, though the buffer sizes are all tunable to optimize performance. There is also some OS-scheduler overhead involved in creating and running a separate thread per connection. A thread-cache optimizes the case where lots of short-lived connections are being created. (I dont have direct experience with PostgreSQL but I read that it forks a separate process per connection, which would presumably incur a significantly higher overhead than threads.)

Beyond this, any more specific discussion requires quantifying connection 'weight' to dispel/confirm your superstition that 'all the predominant RDBMSes are heavyweight'. In an apples-to-apples benchmark, it's quite possible that the connection-weight of some RDBMSes might actually be lighter than other database engines. I dont know myself, but would be interested if anyone has any such data to share on this.

Re:Serverless Best Practices by twblalock on Sunday September 02, 2018 @08:44PM (#17899482)
It seems like pretty much everything was connection-based a few decades ago, when those systems were written. This can lead to some behavior in legacy software that causes big problems in cloud environments, or really any environments where connections are transitory.

The problem really boils down to the tight coupling between the HTTP layer and the rest of the stuff all the way down the stack. If you assume connections are always long-lived and stateful, you can make performance and memory optimizations by reserving memory buffers and threads for the sole use of a single connection, and using threadlocals for storing session data.

But if each connection gets its own thread (or thread pool), idle connections lock up resources and can prevent new connections from being opened, because all of the threads are currently allocated to existing connections. And setting up every new connection is a bit expensive and wasteful if it's only going to be used for one request.

I think the industry trend toward transitory connections, and even transitory application instances a la Kubernetes pods, is a good thing for a lot of reasons. If nothing else, the knowledge that connections can be short-lived leads to more fault-tolerant software because the business logic layer cannot depend on assumptions about the HTTP layer. The big downside is that a lot of older stuff isn't really suitable for that kind of world, serverless or not, and it can be really hard to refactor.

Re:Serverless Best Practices by pvg on Sunday September 02, 2018 @06:22PM (#17899006)
Part of it is legacy but also, interactions with an RDBMS are often stateful so it's useful to have a session (in which such things as transactions can live, etc). I'd guess there isn't much reason to change this because there are standard and effective workarounds that just haven't yet made their way to things like lambda.
Re:Serverless Best Practices by lostapathy on Sunday September 02, 2018 @07:05PM (#17899175)
Can you elaborate on what “standard and effective workarounds” you are referring to here?
Re:Serverless Best Practices by pvg on Monday September 03, 2018 @12:01AM (#17900225)
Connection pooling is the most obvious one which is so common it tends to be transparent and built into many DB access libraries. Then there are all sorts of proxies/load balancers/multiplexors. If you think about it, the bits of code that handle web requests in your typical web app/framework/whatnot are very much like lambdas and when you write those, you generally don't have to worry about the cost of DB connections because it's a well-solved problem.
Re:Serverless Best Practices by k__ on Sunday September 02, 2018 @05:52PM (#17898883)
Saves overhead.

Crappy application-servers pump queries like nobodies business, so you need to shave every byte of them you can.

Re:Serverless Best Practices by etaioinshrdlu on Sunday September 02, 2018 @05:51PM (#17898873)
I don't get the difference particularly... You have to open a tcp connection to talk to dynamodb, right?
Re:Serverless Best Practices by k__ on Sunday September 02, 2018 @05:54PM (#17898890)
From the docs: DynamoDB is a web service, and interactions with it are stateless. Applications do not need to maintain persistent network connections. Instead, interaction with DynamoDB occurs using HTTP(S) requests and responses.
Re:Serverless Best Practices by etaioinshrdlu on Sunday September 02, 2018 @05:56PM (#17898897)
One could, if they wanted, open a new MySQL connection for every statement, which seems similar. One could do this asynchronously in the background too, simulating the eventually consistent nature as well.
Re:Serverless Best Practices by twblalock on Sunday September 02, 2018 @08:31PM (#17899441)
I think the problem with that is the performance would be bad, because the database is not designed to be used thay way and those assumptions go as deep as the way it allocates memory.
Re:Serverless Best Practices by k__ on Sunday September 02, 2018 @05:59PM (#17898912)
True.

Maybe DynamoDB works better with this model, because it is build up without the idea of maintaining connections?

Re:Serverless Best Practices by idunno246 on Sunday September 02, 2018 @08:39PM (#17899474)
There’s many frontend servers to Dynamodb. Effectively the http endpoints are a connection pool to the actual backing datastore
Re:Serverless Best Practices by gbrits on Monday September 03, 2018 @02:49AM (#17900869)
To solve connect to a connection pool service instead of a RDBMS directly. E.g. Pgbouncer for Postgres
Re:Serverless Best Practices by bni on Monday September 03, 2018 @08:28AM (#17902196)
I think that a big part of Aurora Serverless being different from regular RDS, is something like this has been implemented in front of RDS. Havent used Aurora Serverless yet, but I think you can only talk to it by HTTPS, right?