DEV Community

Jay Sitapara
Jay Sitapara

Posted on

Serverless or Containers?

I've read about people switching over from Docker (container) to AWS Lambda (serverless) but what scratches my head is, are there any performance (latency and cold start especially) difference? I mean, both are quite similar, so there shouldn't be any difference at all?

Let's say you create a single function that returns a certain calculated number, and you deploy this as Lambda function and to docker. My guess is that both should perform that function equally fast, am I wrong?

My question: Why should you prefer one over the other?

My understanding is that AWS Lambda functions are just like Docker except they're "smaller" and stateless and only runs when the function is being called.

Top comments (2)

Collapse
 
michaelgv profile image
Mike

It’s a matter of preference, for example where I work we do this:

  • each developer runs Xubuntu
  • all development happens on LXD based containers, with the files injected
  • all testing is done via Gitlab CI (self hosted)
  • all non-global but project specific tests are run on docker inside the development container, and gitlab is interacting with them directly when applicable
  • Jenkins builds the releases, containers, and runs tests when necessary

In our situation a serverless approach would be unfathomable. I’m all in favour of serverless when it makes sense.

In my opinion, when it makes sense:

  • simple application
  • simple deployment pipeline
  • not hard pressed if files override customer specific
  • no complex or time consuming functionality needs to be run continuously

When it doesn’t make sense:

  • development on local machine (LXD or docker here!)
  • complex or multi-Layer deployment cycles
  • remote vs local
  • cost efficiency versus developer time versus reward (pick two, if you want cost low, and high reward, don’t expect to have lots of free developer time, etc)
Collapse
 
kspeakman profile image
Kasey Speakman

AWS Lambda does have cold start every 4 hrs. The first request to hit after cold start will have increased latency. (Apparently they removed the 5 minute idle cold start.)

Here is an interesting article on cold start perf.

Aside from that, lambda nodes are basically just containers (on Amazon Linux distro) under the covers... running python, node, jvm, dotnet core, etc. In between function executions, the container is frozen (background processes will freeze too, if you started any).

Whereas Docker containers are still running between executions. This allows you to run stateful workflows (i.e. when entities are too large to load/save on every request). And also keep background tasks running between API executions. Those two things are not supported in Lambda. You can actually keep some data in memory between executions, but the container may be thrown away at any time. So it could be useful for caching things (caches can be rebuilt), but for stateful workflows a thrown-away container would lose data.

When you get into auto-scale docker containers, you are probably going to run into some of the same issues as lambda (ephemeral containers). Plus you have to manage a cluster. So it starts to make sense to just use Lambda instead.