Serverless computing is a cloud computing paradigm where we do not need to set up servers to execute tasks. The cloud service provider manages and provisions the servers to execute tasks.

Monolith applications vs. serverless architecture

Traditionally, applications were designed on request/response models. The client would send requests to the backend, consisting of multiple classes to process the request and return responses. Since such applications are all bundled in one package, they are also called monolith applications.

With the arrival of cloud computing, developers deployed their monolith software applications on virtual machines on the cloud, such as an EC2 instance. They would install an operating system and the application code on these machines and configure a firewall to secure the application. If the demand spiked, they would spin up new EC2 instances, perform installations, and set up proxies to balance the traffic. In simple terms, they manually upscaled and downscaled the compute resources.

Press + to interact

However, concerns about simultaneous maintenance and development can be draining for the developer. This is where serverless computing comes in as the savior. It takes on the responsibility of managing the servers and spares the developer the hassle of managing servers. Simply put, you can upload your code to the cloud, and it will manage the rest.

The architectural approach where components, such as authentication, database, and business logic, are provided by serverless services is called serverless architecture. The entire application is designed to execute without the need to deploy or configure any servers on the developer’s end. The illustration given below compares serverless architectures with monolith architectures.

Press + to interact
Monolith architecture vs. serverless architecture
Monolith architecture vs. serverless architecture

What makes an application serverless?

Here are some of the core features of a serverless architecture.

Multiple microservices

A serverless architecture comprises many individual serverless microservices where each service does a specialized job. For example, an authentication service that gets credentials, verifies the credentials and returns a validation token that can be used by other services to validate requests.

Press + to interact
Decopuled Infrastructure of an application
Decopuled Infrastructure of an application

An efficient serverless architecture leverages the existing managed services to decrease the overhead of writing code for that service. For example, Simple Notification Service (SNS) is a fully managed, serverless notification service that enables the creation and publication of messages or notifications to a distributed set of recipients or subscribers. Developers can conveniently add the functionality of notifications to an application using this service.

Execution cost

Since serverless services do not have idle sitting servers, they only cost us when they are executing. Therefore, the serverless architecture helps optimize cost by cutting idle capacity. For example, the EC2 instance costs per minute without traffic; hence, it is not serverless. On the other hand, the Lambda functions only cost for the invocations and follow a pay-as-you-go model.

Press + to interact

Scalability

Serverless architectures, due to it's decoupled nature, are easier to scale as compared to monolithic architectures. Furthermore, serverless services scale independently and instantly. This allows serverless architectures to scale up and down even to zero automatically.

Use cases of serverless architecture

Some serverless architectural patterns are frequently employed to replicate a similar use case among multiple applications. AWS also offers an official repository for the most commonly used serverless patterns available as a SAM template and Cloud Development Kit (CDK). These templates define the IAM roles and policies required to provide the desired access for communication between the services.

Let’s discuss some patterns to learn how serverless architectures are practically implemented to solve real-world problems.

Image processing application

Consider an application where we want to store images in an S3 bucket. However, all the images shall be of a specific dimension. Thus, we need to resize the image every time it is uploaded to the S3 bucket. This is one of the simplest examples of serverless applications.

We can use a Lambda function to execute code to resize an image. Here, adding images to the bucket can act as the event to trigger the function, which will upload the resulting image to the S3 bucket. The architecture diagram will help us understand the infrastructure better.

Press + to interact
Architecture diagram for image processing
Architecture diagram for image processing

CRUD application

Another example of serverless architecture is to break the traditional monolith architecture of a CRUD application into multiple services. Consider an education website that faces a spike in demand during exam season.

The application architecture in the figure below shows an RDS Aurora serverless to store data. Aurora serverless adjusts well to the seasonal increase in demand and thus is ideal for our use case. To route client requests to the backend, we have used API Gateway. The Lambda function acts as the middleman between requests and the database. Four Lambda functions connect to the RDS instance, each performing a CRUD operation.

Note: It is a good archirectural approach to have separate Lambda functions for each CRUD operation as it improved the modularity and scalability of the application.

Press + to interact
Architecture diagram for CRUD application
Architecture diagram for CRUD application

To handle business logic and connect the frontend to the backend, many developers use EC2 instances. However, in our use case, since the application's demand peaks during the exam season, EC2 would not be a cost-effective solution.

Serverless technologies are not only used to transform monolith architectures. They can also complement traditional architectures by connecting, integrating, or augmenting them.

Create a free account to view this lesson.

Continue your learning journey with a 14-day free trial.

By signing up, you agree to Educative's Terms of Service and Privacy Policy