Is serverless the end of servers?

What is serverless? and when to use it?

Many people think that serverless computing is a good concept because the code doesn't run on a server, I agree that the term "serverless" is misleading but it's wrong, so where does the code run in such an architecture? What are the advantages of serverless? When to use it instead of a classic stack?

Serverless is a computing paradigm where the final user (the developer) focuses just on writing the code, this code is in most cases a function (“Functions as a Service), then the cloud service provider ( Google Cloud, AWS, etc) takes care of allocating the necessary resources to execute it, the provider also takes care of scaling, security and other “server” technicalities.

In return the developer pays the provider a tiny amount of money each time its function gets executed. (Pay-per-execution)

In case it’s still not clear to you dear reader, the code still runs on a “Server”, specifically inside stateless containers, it’s usually triggered by a http request or a Cron job.

Example:

POST  http://localhost/cloud/function/send-sms

{
  number: "01 01 01 01 01",
  message: "Hello world!"
}
sendSms(params) {
  if (isValidNumber(params.number) && params.message.length > 0) {
    smsService.send(params.number, params.message)
  }
}

In this example our function is called “sendSms”, it gets triggered each time someone makes a POST request to “http://localhost/cloud/function/send-sms”, it sends an sms to the provided phone number with the provided message.

We can also trigger this same function with a Cron job if we want.

Advantages of serverless:

1- focus on application logic

The service provider allocates and maintains the resources (servers) so the developer can focus on application logic.

2- No single point of failure

The code runs on multiple servers so if one of the servers fails it gets automatically replaced with another one by the server provider.

3- Auto scaling

In case the number of concurrent calls to your function increases consequently the provider will increase the number of servers to cover the demand, similarly if no one calls your function the provider will scale down the number of servers running your function to 0.

4- Pay-per-execution

As I stated earlier, the developer pays a tiny amount of money each time its function gets executed, this is ideal because you only pay what you use, contrary to the “classic” cloud where you should pay for the total running time of your provisioned servers. 

DISADVANTAGES OF SERVERLESS:

1- Cold starts (high latency):

The service provider automatically scale up or down the containers running your code, so in case your function hasn’t been triggered during a while, the containers will be brought down, then when your function gets triggered again the provider will provision a new container to run your code and that will take some time, the technical term for this is “Cold start”.

The same issue will happen in case you experience a surge in calls to your function, it will take your provider some time to provision the containers and install your code inside them, during that time your clients would be waiting and might think your app is down.

2- Short life-span:

You can’t run relatively long tasks as most serverless providers set a maximum running time. (5 minutes in case of AWS Lambda function)

Classic use cases:

Serverles is best suited for time consuming background tasks, here are a few examples:

  • Data extraction and transformation.
  • PDF file processing
  • Video transcoding
  • Chat bots

Basic example:

I choose AWS lambda to show you how to create a simple serverless function, we’re gonna create create a function that responds to HTTP GET requests with a dimple “Hello world”

I won't go into details so I invite you to read the official aws documentation https://docs.aws.amazon.com/lambda/

1- I created a lambda function in my aws dashboard, I called it “myFunction” and chose Node.js 14.

Creating a lambda function in AWS

2- When you create the function, aws shows you an editor with your function’s code (it’s just a simple “hello world”), I didn’t change much.

Testing a lambda function in AWS

3- You can click on “Test” to test your function, but that’s not very interesting, so we’ll create a “Http Trigger”, that will allow us to execute our function each time we make a http GET request to it.

AWS lambda function trigger

After the creation of the trigger, aws generates a unique link to call our function. In our case it’s https://xxxxxxxxxx.execute-api.us-east-2.amazonaws.com/default/myFunction

AWS lambda function REST trigger

4- Let’s call our function!

Testing a lambda function in postman

Conclusion

In conclusion, serverless doesn’t mark the end of servers, but it's an additional resource for architects and developers to meet the challenges they face to solve complex computing problems, and an opportunity for developers to save time while developing their applications.