Serverless functions have been quite a new and hot topic in the tech community. “Serverless” probably means that its free right? Or does it mean that it lives somewhere “in the cloud?” Does it have to do with a restaurant where you interact with a kiosk? In this post, I will try to demystify what serverless functions are, and why you would want to consider them in your next project.
I tend to use the definition by ThoughtWorks as the two sentence summary:
Serverless architecture replaces long-running virtual machines with ephemeral compute power that comes into existence on request and disappears immediately after use.
The Car Analogy
When I’m explaining to people what serverless functions are, I like to use an analogy to the types of car ownership that I learned from Dana Engebretson last year. I’ll cover that analogy in this post and explain why it is such a good one to understand what a serverless function is.
Owning a Car
Owning and managing your own physical server is like owning the old family car. Its reliable, can be available whenever you need it, and is exactly what you need when you buy it. However, you are also now responsible for this car. You’re responsible for maintaining the car, paying for its fuel, and paying insurance. The costs are entirely on you.
What happens if your family grows beyond what you expected? Now you must get a second car or possibly a minivan. There is also a hidden additional cost: You are also paying for the car when it is not even being used. The car also depreciates in value, no matter how little you use it.
Family size may not change as quickly or as large as a userbase, but the same concepts apply for servers: physical servers are costly to maintain, require expenses to host, and cannot adapt to changing demand.
Leasing a Car
Moving your application to a Virtual Private Cloud (VPC) or Virtual Private Server (VPS) is like leasing a car. This still incurs the regular monthly payments, but you now don’t have to pay for the maintenance and upkeep, and there’s a low-to-no upfront fee. Once you’re done using it, you can return it to the dealer.
Renting a Car
Furthermore, there are containers and PaaS services like Heroku; these are like renting a car. You only have to pay for the amount of computing power you use, but it may be more than you need. What if you only need the car to commute to work and back home? What if you only need a car for a trip to the grocery? Containers and PaaS services are very lightweight and are only spun up on demand, but they still require resources to host the platform (Docker image, OS), and also require that the entire application be ready to run. What if only small parts of your application are used? And what if the usage varies by the time of day? You still need to spin up more resources as the demand increases.
Car Sharing Services
Finally, we come to Serverless Functions, the leanest of the leans. They are like ride-sharing services: think Car2Go or Zipcar. You grab the car that you need, when you need it, and use it only if you need it. You are then charged with only how much you use, and you can dramatically save money if you only infrequently require a car.
This highlights the power and flexibility of Serverless functions. They are a small (must be executed quickly) piece of functionality that can be implemented directly within the cloud! They can scale based on demand (to a limit that you set) and you are charged only the exact amount that is used. This can result in huge savings especially if usage is erratic over the course of a day. Most services cost about 20 cents per 10 million executions of code! That can really add up if your app only requires a few intensive processes that run only a few times in a day (or month).
So why should you switch to Serverless? Well, you probably shouldn’t switch over entirely (that’ll be covered in my next blog post). However, you should definitely look into incorporating Functions into your architecture in the future, because it will ultimately save you money if you don’t require a server to be constantly processing (like most web apps). For now, I recommend trying it out with a small project. Most Cloud providers like Microsoft Azure and Amazon AWS offer free tiers that you can use until a usage threshold is met. If you’d like to talk serverless, feel free to contact me on twitter @thepatleong.