DEV Community

Cover image for Serverless 101
Wasif
Wasif

Posted on • Originally published at blog.wasif.tech

Serverless 101

Overview of Serverless

Why you should learn and adopt Serverless in 2022

Consider these statistics on the state of Serverless as of May 2021:

  • Lambda functions invocation is 3.5 times more often than two years ago
  • At least 1 in 5 organizations is using FaaS on cloud
  • 48% developers use FaaS as a deployment target, higher than bare-metal servers and PaaS
  • 1 in 4 CloudFront users has embraced Serverless edge computing

History of Web-App Deployment

Screenshot 2022-01-17 at 22.10.34.pngTraditionally web-apps would be deployed on a bare-metal server on-premise or provided by a hosting service. This server would be a physical machine running a web server like Apache, NGINX, etc. on which the web-app would be deployed.

Then came Cloud 1.0, which allowed us to move our workloads to cloud providers’ data-centers. This meant deploying our web-app on VMs provided by the cloud service.

Cloud 2.0, introduced deployment of containerized web-apps on the cloud. Containers and container orchestrators made this possible.

What is Serverless?

Serverless refers to the concept of building and running applications that do not need server management.
There’s no need for a traditional always-on server component.

It's categorized into BaaS and FaaS.

  • Backend-as-a-Service (BaaS) is a cloud service model in which developers outsource all the behind-the-scenes aspects of a web or mobile application so that they only have to write and maintain the frontend. One of the most popular examples of BaaS is AWS S3 service - simple storage service.
  • Function-as-a-Service (FaaS) is about running backend code without managing your own server systems or your own long-lived server applications.

image.png

CNCF Definition: Serverless computing does not mean that we no longer use servers to host and run code; nor does it mean that operations engineers are no longer required. Rather, it refers to the idea that consumers of serverless computing no longer need to spend time and resources on server provisioning, maintenance, updates, scaling, and capacity planning. Instead, these tasks and capabilities are handled by a serverless platform and are completely abstracted away from the developers and IT/operations teams. As a result, developers focus on writing their applications’ business logic.

Basics of Functions-as-a-Services (FaaS)

FaaS is a cloud-native platform for short-running, stateless computation and event-driven applications which scales up and down instantly & automatically and charges for actual usage at a millisecond granularity.

Cloud-native technologies empower organizations to build and run scalable applications in modern, dynamic environments such as public, private, and hybrid clouds. Containers, service meshes, microservices, immutable infrastructure, and declarative APIs exemplify this approach.

image.png

Azure Functions consumption plan is billed based on per-second resource consumption and executions. Consumption plan pricing includes a monthly free grant of 1 million requests and 400,000 GB-s of resource consumption per month per subscription in pay-as-you-go pricing across all function apps in that subscription. It charges $0.20 per million executions.

AWS Lambda charges $0.0000206667 for every GB-second and $0.25 per 1M requests

What triggers FaaS execution?

Code execution is in response to events. FaaS relies on event-driven programming model.

image.pngConsider the example of an API Gateway shown in the above diagram.

AWS API Gateway provides tools for creating and documenting web APIs. These APIs route HTTP requests to Lambda functions. When the API Gateway service receives an API request, it triggers an event. This event is an API Call, to execute the mapped AWS Lambda function. The Lambda functions create a response based on the request and returns it back. Your code and business logic is written in the Lambda function.

Platforms

Cloud Computing Services

The common categories of cloud computing services are as follows:

1. IaaS

Infrastructure-as-a-Service provides the base infrastructure. For e.g. a Virtual Machine provides by AWS EC2 or Azure VM. Server setup, provisioning, deployment and management is the user's responsibility.

2. CaaS

Container-as-a-Service provides container based virtualization. It allow users to organize, deploy, scale and manage containers on the cloud. Azure Kubernetes Service(AKS) is a fully-managed Kubernetes service.

3. PaaS

Platform-as-a-Service is an integrated application development and deployment solution. It allows application developers to focus on their application. It takes away the complexity of provisioning, configuring, and managing resources. Azure App Services is a PaaS solution for building Web-Apps and APIs.

4. FaaS

Function-as-a-Service allows developers to execute functions in response to events. These functions have code encapsulating business logic. Developers don't have to worry about building complex infrastructure. Azure Functions is a FaaS service .

5. SaaS

Software-as-a-Service is an on-demand software which is completely managed by the vendor. Office 365 is an example of SaaS.

image.png

Azure Services

In the image below we can see how the different services from Azure map to different categories of cloud computing services. As we move from IaaS to FaaS, the concern of provisioning and management decreases and focus on business logic increases.image.png

FaaS Platforms

Shown below are some of the popular public cloud and on-premise FaaS platforms.image.png

Deployment Target

As per the DORA State of DevOps Report 2021 from Google, 48% of the respondents were using FaaS as their deployment target. Which is higher than bare-metal servers and PaaS environments.image.png

Pros and Cons of FaaS

Pros

  • Save time and money on managing servers
  • Pay for what you use
  • Increased scalability, security and reliability
  • Improved developer productivity
  • Decreased management responsibilities
  • Convenient integrations

Cons

  • Cold starts
  • Compute time
  • VPC/Network issues
  • Application size
  • Vendor lock-in
  • Complex debugging

Cold Starts

One or many micro-containers are responsible for serving a function. When a request comes in, the function checks whether there is a container already running to serve. When an idle container is already available, we call it a “warm” container. If there isn’t a container available, the function will spin up a new one and this is what we call a “cold start”. Applications that haven’t been invoked for a while take longer to startup and handle the first request.

The image below shows a comparison of typical cold start durations across cloud providers for common languages. The darker ranges are the most common 67% of durations, and lighter ranges include 95%. AWS leads with all languages being well below 1 second. GCP start-up usually takes between 0.5 and 2 seconds. Azure has high startup times often up to 5 seconds. Factors like cloud provider, language, base OS, package size, memory, etc. affect cold starts.image.png

When to use FaaS

✅ FaaS is good for:

  • Stateless event-drive tasks
  • Tasks that can be broken into small independent units of work
  • Tasks that have infrequent or unpredictable usage patterns
  • Background work or system to system communication.

❌ FaaS is not good for:

  • Stateful tasks
  • Long running tasks
  • Computationally intensive
  • Streaming

Real-world usecases

1. BBC website

Around half of the BBC’s website is rendered with AWS Lambda. They perform over 100 million serverless function invocations per day.
Traffic management – NGINX on EC2 instances.
Web rendering - the rendering happens on AWS Lambda. Every second About 2,000 lambdas run every second to create the BBC website Business layer. REST APIs powered by Lambda Platform & Content production is on EC2.image.png

2. LEGO transformation

Lego uses 200+ AWS Lambda functions to transform its legacy website.
Architecture for Email notification with signed URL for vouchers.image.png

References

Top comments (0)