What is serverless technology? Should you use it to host your microservices?
Serverless is a way to run your code without managing your own server. Recently serverless gains a lot of popularity in startups and in this newsletter I am going to share my opinion about it.
In the past, when you want to deploy your code, you have to build your own server. You have to account for power outages, natural disasters, component failures, etc. With the rise of cloud services, you can rent virtual machines and don’t have to worry about all those problems I have mentioned. Now you only have to configure a Virtual machine and run your code there.
The process of deploying your code is still very lengthy and somewhat tedious. You also have to make provisions for larger VMs than you need for a sudden rise in traffic. It actually a lot of work to manage a server, and solo developers and small startups can't afford to do this kind of thing.
To solve all these issues, AWS (Amazon Web Services) launched AWS lambda in 2014. AWS Lambda is a serverless technology that runs your code in their server and manages everything for you. Now you just need to focus on writing quality code, and AWS lambda handles the rest of it.
Let's understand the process of how AWS lambda actually works. When a request comes in, AWS boots a lightweight container and runs your code there. Thus, your code doesn't consume any resource at idle, and you don't have to pay for unused resources. A win for solo and small startups, right?
The pricing model of serverless technology is also interesting. In this model, you are billed for the number of invocations. Traditional VMs are billed on the amount of resources you are using. Therefore, if your application doesn’t have continuous usage, serverless may be the cheapest option for you.
But serverless is not all sunshine and rainbows. As you have already learned, your code runs only when a request comes in. Therefore, when your code doesn’t have continuous invocation, it goes to sleep. The process of starting the code from sleep is called cold-start, and sometimes the time needed to cold-start an application is very long.
If you have a very small number of people visiting your site, your visitors may see a huge lag in your application.
Surely these drawbacks can be overcome. Edge functions are the latest technology that can handle the cold-start much more efficiently than AWS lambda. We will talk about edge function in the next issue of this newsletter. Please share this newsletter and keep learning.