An Introduction to Serverless Computing

Since a couple of years, we have been hearing about ‘Serverless Architecture’ through various conferences and also on social media. By doing some elemental research of this amazing concept, I came up with this write-up and will try to manifest different nuances of it. Also, you will get to see how to start hands-on with serverless.

When I heard this word ‘Serverless’ for the first time what struck my mind was, how can there be any applications without servers. And if there are servers (which is a must for any web application) then what does serverless exactly mean? It’s actually a bit of a misnomer. Just as cloud computing didn’t mean that the servers disappeared into the cloud, it just means the servers are no longer physical but remote, managed by someone else.

Similarly ‘Serverless’ isn’t exactly serverless at all. There always have to be servers somewhere. You just don’t need to manage them. The cloud provider (AWS, GCP etc) delivers exactly the right amount of compute capability and the developer doesn’t even have to think about it or code for it. In short, it is Function as a Service or ‘FaaS’ and the code runs on short-lived containers.

“Serverless” usually refers to an architectural pattern where the server side logic is run in stateless compute containers that are event-triggered and ephemeral
— Mike Roberts

Evolution: Building layers of Abstraction

In the beginning, we had physical servers which had major drawbacks of upscaling and wastage of resources. This led to the development of virtual machines, where a single physical server is divided into multiple virtual ones dynamically. While that was a huge breakthrough for its time and paved the way for cloud computing. Software as a Service (SaaS) concept was introduced by Salesforce in the early 2000s, Infra as a Service (IaaS)was started by AWS in 2006.

Then came running the production code on containers, which kicked off with the development of open source platforms like Docker in 2013 and K8s in 2014. Containers enable the developer to break down a large monolithic program into discrete pieces, which helps it run more efficiently.

More recently, there has been a rise of serverless or event-driven computing so as to replicate the successes of big players like Netflix and create a system that is fault-tolerant and resilient to the nth-degree. AWS started the serverless movement in 2014 with a “serverless compute” platform called AWS Lambda/Lambda Functions.

A decade ago, cloud servers abstracted away physical servers. And now, “Serverless” is abstracting away cloud servers.

There always have to be servers somewhere. Broadly, serverless means that you aren’t responsible for all of the configuration and management of those servers. A good definition of serverless is pay-per-use computing where uptime is out of the developer’s control. With zero usage, there is zero cost. And if the service goes down, you are not responsible for getting it back up. AWS started the serverless movement in 2014 with a “serverless compute” platform called AWS Lambda.

AWS Lambda is a compute service that lets you run code without provisioning or managing servers.

— AWS Documentation

Tim Wagner, general manager for AWS Lambda, says the primary advantage of serverless computing is that it allows developers to strip away all of the challenges associated with managing servers. “So there is no provisioning, deploying, patching or monitoring — all those details at the server and operating system level go away”. He says this allows developers to reduce the entire coding process to the function level. The programmer defines the event or function and the AWS figures out the exact amount of underlying infrastructure required to run it.

Use Cases

Consider an E-Commerce site with functionalities like Authorization, productSearch and payment. In traditional architecture, all these requests are served by a single server. However, in the Serverless approach, all these different functionalities will be different ‘Lambda functions’ which runs on short-lived containers. They are triggered by events like API calls. These Lambda functions can be integrated with external services, S3, DB etc and can be used accordingly.

Serverless provides the ability to make the system into more granular services and scale each functionality. Consider, in the above example ‘Product Search’ is used comparatively more than the ‘Payment’ service, in the traditional way of hosting this doesn’t matter as all the services are served by a single server (or set of servers). But in serverless mode, as the servers are abstracted out you will be charged only for the number of calls for each service. Also, no need to worry about maintaining the servers. Later in this article, we will also see how we can get rid of non-functional code like Authorization from our codebase.

Another use case would be running the Automated functional regression suits which usually takes hours to run when executed serially, with chrome headless browser, functional suites can be triggered parallel, when not in use, you are not charged. Serverless also helps in performance testing to generate higher loads which mock real environment structure.

Key characteristics of a serverless application

  1. No server management
  2. Flexible scaling
  3. High availability
  4. Pay-per-use policy

Driving the startup ecosystem

A proper serverless Architecture is quite different a method that has been termed a serverless, service-full approach. Whereas Lambda-like compute mechanisms provide functions as a service, managed services provide functionality as a service.

You write and maintain the code (e.g., the functions) for serverless compute, whereas the provider writes and maintains the code for managed services.

With managed services, the platform is providing both the functionality and managing the operational complexity behind it.

By adopting managed services, the vast majority of an application’s as usual functionalities like authentication, file storage, API gateway, DB, monitoring, notification services, logging and more is handled by the cloud provider’s various off-the-shelf platforms, can be integrated together without writing any code. These ready-made services along with the remaining business logic that makes the application unique and also runs on ultra-cheap, infinitely-scalable Lambda (or equivalent) infrastructure, thereby eliminating the need for servers altogether.

Serverless architecture has become a luxury for the startups who can achieve a great development velocity as the conventional services are already provided by the provider

Also, the price factor. Lambda is astonishingly cheap: $0.0000002 per request plus $0.00001667 per gigabyte-second of compute. Similar prices with API gateway and other managed services as well. This enables the startups to be prod ready swiftly with low maintenance.

Hands-on with AWS Lambda

Here is the official documentation for a Hello World! on AWS. You need to have an account with AWS to start with for which a Credit/Debit card is needed. You will be charged with Rs 2 as to validate the payment details. Once validated you will be able to access the AWS free tier. This has a 12 Month free subscription with 1Million free lambda requests per month. This is good enough to get your hands dirty and start exploring the Serverless on AWS.

My early experimentation with Lambda was a difficult affair with coding directly into the AWS console or uploading a .zip file every time. This was frustrating when it took a lot of time and effort for trivial things, to go through the AWS documentation and figuring it out on the AWS console. The bridge that connects my IDE to a production environment was missing.

Well, its’ missing until I discovered serverless framework which is honestly the most exciting thing I’ve found in recent past.

With the Serverless Framework, you can define your entire Serverless application, utilizing popular Serverless technologies like AWS Lambda, with a simple yml configuration file and simple CLI to deploy. A great thing about this is Provider agnostic. I will share more details on this in the next post. Watch this space for further details.