2

Do server-less functions install modules every time they are called?

I am trying to understand how serverless functions really work. I understand that serverfull is basically a computer that executes code and that the server code runs on it 24/7 unless it is stopped for some reason. On the other hand I understand serverless code just runs when it is called. Where is this code stored? When I call a function in a serverless application does it install the modules (for example from npm) everytime I call the function? Is this what causes cold-start delays?

I understand that serverfull is like my computer running code. How can I describe serverless using the same analogy?

My Questions:

  1. Do server-less functions install modules every time they are called?
  2. If there is no server, where is this code stored in serverless?
  3. I understand that serverfull is like my computer running code. How can I describe serverless using the same analogy?
YulePale
  • 6,688
  • 16
  • 46
  • 95

1 Answers1

2

No, the dependencies are a part of the deployment artifact (e.g. a ZIP file or container image in the case of AWS Lambda), so they do not have to be installed on each invocation.

I understand that serverfull is like my computer running code. How can I describe serverless using the same analogy?

That's not going to be a perfect explanation, but hopefully, it fits your analogy. Imagine that your computer is sleeping, but there's another computer that can receive requests and wake up your computer whenever it receives a new one, so it can be run on your computer. After it finishes running, it goes back to sleep. But instead of a single computer, there are many of them that can be brought from sleep in a matter of milliseconds. Hope that makes sense.

pgrzesik
  • 1,869
  • 13
  • 14
  • Thanks. I think I understand now. Correct me if I am wrong. So each function is kind of like a small server virtual that runs on command? – YulePale Nov 29 '22 at 16:06
  • So every time a function is called in the case of AWS Lambda a new container is created? – YulePale Nov 29 '22 at 16:15
  • I have just read "A deployment artifact (or a build ) is the application code as it runs on production: compiled, built, bundled, minified, optimized, and so on. Most often, it's a single binary, or a bunch of files compressed in an archive. In such a state, you can store and version an artifact." So a function is build into binary and so anytime its execution is needed it can be called and if need be more instances of it can be deployed? – YulePale Nov 29 '22 at 16:19
  • @YulePale, yes, you can think of each instance = micro virtual machine. For example, AWS Lambda uses Firecracker (https://firecracker-microvm.github.io/) as the underlying technology for that. Not every time a new container is created. AWS Lambda "keeps" the containers available for a while so they can be reused for additional invocations (warm invocations). When a new container needs to be created, we deal with cold starts. – pgrzesik Nov 29 '22 at 16:34
  • so the containers have a set time they will live until they die? Also for example in nextjs is each micro virtual machine a whole instance of the app or just one function? – YulePale Nov 29 '22 at 16:44
  • 1
    Yes, but the time it takes for them to fully turn off is not documented anywhere as far as I'm concerned. As for NextJS, that depends how the app is structured. I'm not an expert here so I won't be able to give a definitive answer. – pgrzesik Nov 30 '22 at 14:21