blogging

Why Serverless Is The Future Of Hosting Websites ?

I strongly believe that serverless is the future of web hosting because of the cost savings and infinite scalability it offers

RahulVk May 12 2020 12 min read
Share this

Serverless is the new trend on the market. Everyone is talking about it and its wonders. FaaS(Function as a Service) is what we mean when we use the term Serverless. So in this article we are going to explain what is serverless , how is it different from VPS and how much you can potentially save by going Serverless.

Serverless or Function as a Service is now offered by all the major players on the market like Amazon Web Services, Google Cloud Platform, Microsoft Azure, IBM Cloud, Cloudflare Workers and many others. In this article we are going to be focusing on AWS Lambda which is the most popular FaaS out there.

What is FaaS and How is it different from traditional VPS ?

FaaS or Function as a Service is what we commonly mean when we are referring to Serverless. FaaS are indeed run on physical servers, we call them Serverless because we do not have to configure and setup these servers on our own.

Traditionally when you are hosting a app on cloud, you will have to probably deploy a VPS and configure it. Since here we are mainly talking about AWS Lambda, we will make our point by comparing Lambda and AWS EC2. We know that AWS EC2 instances are billed by seconds or hours. And now lets say you are going to host a website on it. What makes VPS like AWS EC2 (AWS EC2 is much more than a VPS, but for simplicity lets call them VPS) so inefficient at this task is they have to run all the time . Whether or not somebody is accessing the website you have to pay for the server. Because if you shut it down when not in use, then when somebody access your website it will take a lot of time to start up and serve the request. That means you have to run it all the times whether or not anybody is accessing it and it ain't cheap either.

Now imagine that your website runs on AWS Lambda. Each Lambda function at the time of its creation is packed into a container which is then executed by AWS whenever the function is called or invoked and You only pay for the compute time it consumes. That is you only pay for what you use. You will pay nothing if no one accesses your website. AWS Lambda is very cheap when compared to AWS EC2. Besides the cost savings other main advantage is infinite scalability. If you are hosting your website on a VPS , then you will have to upgrade to a higher configuration plan as your site grows. The problem is finding the right balance. If your VPS is too powerful for your needs, then you are paying extra for nothing. In order to avoid that if you went with a lower configuration then your app may not perform well.

Now consider another scenario, imagine that you are hosting a news blog on a VPS. Whenever there is a major event or something like that then your traffic will spike exponentially. In the case of planned events, you might be able to allocate some extra resources but still you cannot be sure if it is going to be enough or not. But in the case of something unexpected then such a traffic spike can easily bring down your website. Now imagine the same scenario but it runs on a function as a service like AWS Lambda. You don't need to worry about any traffic spikes or anything like that as Lambda automatically scales. You don't need to preallocate extra server resources or worry about the site going down.

Challenges you might face on moving to serverless

These serverless functions and their configurations have some learning curve. If you are a big enterprise then you may have to train your existing workforce on the Serverless platform which could be expensive.

Serverless functions by themselves are very cheap. But there are other services you would need to use along with it. For example hosting a website on AWS Lambda could require AWS API Gateway, AWS Lambda, AWS S3 and AWS Cloudfront. Among this AWS S3 and AWS Cloudfront are probably likely to be used if you are hosting a large website on AWS EC2 also. So you will require AWS API Gateway. AWS API Gateway actually costs more than AWS Lambda. But it is still less than what a AWS EC2 will cost in most cases.

We told you that its billed on the compute time you consume. So it is not suitable for running high performance lengthy tasks, in such cases it could become very expensive.

  • Learning Curve
  • The other costs
  • Long High Performance Applications

AWS Lambda Pricing

I am going to give you an overview about AWS Lambda Pricing

AWS states this " The price for Duration depends on the amount of memory you allocate to your function. You can allocate any amount of memory to your function between 128MB and 3008MB, in 64MB increments. The table below contains a few examples of the price per 100ms associated with different memory sizes."

I am giving you a small table below that contains basic pricing information about AWS Lambda

 

Memory(MB) Price per 100 ms Price per 1 million requests
128 $0.0000002083 $0.20

per 1M requests
512 $0.0000008333 $0.20

per 1M requests
1024 $0.0000016667 $0.20

per 1M requests
1536 $0.0000025000 $0.20

per 1M requests
2048 $0.0000033333 $0.20

per 1M requests
3008 $0.0000048958 $0.20

per 1M requests

 

AWS API Gateway Pricing

Since most of the use cases we talked about will also require AWS API Gateway, I am including the pricing for AWS API Gateway.

Number of Requests (per month) Price (per million)
First 300 million $1.00
300+ million $0.90

 

What if you find all these configurations very confusing but wants to use serverless functions ?

Well in that case you can use other third party services that are built on top of these Serverless functions . Popular examples are Vercel (formerly zeit now) and Netlify functions. This website is made using Flask framework and hosted on Vercel. So far we have faced no issues. Vercel serverless functions are built on top of AWS Lambda giving you all the advantages of using AWS Lambda. The good thing with Vercel is you can write your website as you usually do and all you have to do is include a now.json file with your project which has the configurations. It is very easy to make a now.json file as all required information can be found on their website itself. Vercel supports Node , Python , Ruby and Go for the serverless functions.

Netlify functions is also a good alternative which also runs on top of lambda. But they only support Javascript and Go.

AWS EC2 vs AWS Lambda Pricing Comparison Based on a Real Life Example of Hosting Website

About our imaginary test website

Now let's calculate the price for one month based on the above conditions

  • It uses Python Flask as backend
  • Has a lot of static files like images and videos
  • Average page size is 5 MB
  • Has about 100,000 page views daily
  • Since the site is very large all the static content is stored and served from AWS S3 using AWS Cloudfront
  • One version of the site is hosted on AWS EC2 t2.small instance
  • Another version is hosted on AWS Lambda with 256 MB memory

 

EC2 Version costs

Since AWS S3 and AWS Cloudfront are common on both sides, we can avoid that in our comparison

  • t2.small instance running for 730 hours a month: 0.0208*730 = 15.184 USD

So price for the EC2 version is 15.184 USD

Serverless Lambda Version

  • AWS Lambda with 300 MB function memory and a execution time of 200 ms per request for 3 million requests a month : 3.53 USD
  • AWS API Gateway Pricing : 3 USD ( According to AWS , 1 USD per 1 million http api calls )

So price for the Lambda version is 6.53 USD

 

See the price difference.

Disclaimer

Depending on your app and many other factors the actual price may vary. This article just presents my opinion about these services. You should do proper research before using such services to avoid any unexpected costs. I am not in any way responsible for the costs you may incur. All articles on this website just present my opinion. It should not be taken as professional advice.

Let us know your thoughts on the comments below

Comments

Great Article , well written and very informative

by Kannan

Hey can you do another speed test article like the "Netlify vs Github pages one". This time make it Vercel vs Netlify

by Kannan

@Kannan , Thank you for your suggestion. I will make that and post it as soon as possible.

by RahulVk

Add your Comment

Read next

Netlify vs Github Pages Speedtest

Github pages and Netlify are among the most popular choices for hosting static sites. Here we compare them on page load times.

RahulVk April 2 2020 · 10 min read

Mi Home Security Camera 360 review

by RahulVk
April 10 · 10 min read

Why you should use a static site generator ?

by RahulVk
March 27 2020 · 15 min read