Serverless Computing

Nov 25, 2021by, Shawn

Technology

Serverless Computing is a term that refers to a computing model where the execution of code is done on the cloud. The term may give rise to the misconception that we are getting rid of servers altogether, but this isn’t true. It’s just that our involvements with it are minimal. All the hassle of setting up the servers and maintaining them is handled by the cloud providers.

Advantages of going Serverless

  • No server Management required: There is no need to provision or maintain any servers. It is done by the Cloud providers.
  • Flexible Scaling: Your application can be scaled automatically or by adjusting its capacity through toggling the units of consumption (e.g. throughput, memory) rather than units of individual servers. On automatic scaling, the resources are scaled up automatically based on the usage of these resources.
  • Pay for value: In serverless computing, we pay for exactly the resources we use. This is unlike the traditional system where we invest in resources first and wait on them till the resources are insufficient and purchase more. This is perfectly illustrated in the graph below. The prices rise in a linear fashion as it should be in an ideal world.

Serverless Cloud Providers

The main companies that offer computational capabilities with their cloud services are Google, Microsoft, and Amazon. Let’s talk about Amazon’s service in detail.

Amazon Lambda

Amazon was the first to offer serverless computing capabilities with its infrastructure. AWS Lambda was introduced in 2014. It is a part of AWS and integrates with its other services including DynamoDB, a NoSQL database, and CloudWatch used to monitor the usage of the services offered by AWS. lambda function supports multiple programming languages including Java, JavaScript, Go, and C#.

How lambda functions work

Lambda functions can be made to work pretty much however we want them to. Generally, we make use of triggers to start the execution of the lambda functions to not having to start it manually. Various AWS services provide the capability to trigger a lambda function. An API request can be used to trigger the lambda function with AWS’s API gateway service. Other services that provide this feature include S3(AWS Simple Storage Service), DynamoDB,  and so on. A rather simple use case of lambda functions is illustrated below. It’s a simple web application created using AWS services. The S3 is used to serve the front end of the website. The API requests made by the frontend code are handled by the API Gateway. This triggers specific lambda functions that can fetch data from the database and return it to the user.

Lambda functions are stateless and this allows multiple copies of the same function to be run at the same time without interrupting the other invocations.

Creating a lambda function

Getting started with a lambda function can be really easy. There are two ways to work with lambda functions.

  • Using the AWS console.

  The AWS console is easy to use and it even has an online IDE to create and edit your code online. When creating a function using the console, it is as easy as clicking on a button called ‘create a function’ and setting up the configurations as they show them out to you. Apart from creating and editing, adding triggers and linking the function with other AWS services are also easily possible using the AWS console.

  • Using the AWS Command Line Interface(CLI)

  The AWS CLI can be used to deploy and invoke Lambda functions. Since it’s a command-line interface it’s not as user-friendly as the console. The keyword create-function can be used to create a function. For eg:

aws lambda create-function --function-name HelloWorld --zip-file fileb://function.zip --handler index.handler --runtime nodejs8.10 --role  rn:aws:iam::123456789012:role/lambda-cli-role

The code, once ready to be deployed is uploaded a zip file and the index file is mentioned as in the command above. IAM roles are profiles on aws that are given access to particular services and functions. A role with the permission to create functions is required to do this.

Invoking a function can be done by the invoke keyword.

For eg: aws lambda invoke –function-name helloworld

Challenges with serverless computing

  • Debugging and monitoring of resources tend to get more difficult on serverless computing. Since the code is not executed on the server we have complete access to, we are limited to the capabilities provided to us by the vendors. Apart from this, since these functions are timed it’s impossible to use debuggers or such that could slow down or pause the execution. Amazon offers the capability to run Lambda functions locally which is a great way to test the code on your machine before deploying. All other solutions to the problem involve getting the data from specific points during execution without actually interrupting the process.
  • Serverless code that is not used often can suffer from latency issues. ‘Cold start’ is a word used to define this phenomenon, where the first execution of the function after a long break can have a latency period greater than the consequent executions. This latency period can increase drastically with concurrent function calls.
  • The resources available are limited by the vendors. Due to this high computational workloads may not suit serverless computing.