Serverless computing is a new coding paradigm that allows you to run code without configuring servers. It is offered as a service by many cloud providers, including Amazon and Google. What is it about?
What is serverless?
“Server-free” is a slightly incorrect term – your code will still be running on a server somewhere. You just do not have to worry about it, because the metal it runs on will all be handled for you “in the cloud”;, which means you can only focus on software. This is the lid on serverless solutions; By reducing the cost of managing servers, you save money, both the labor cost of sewing admin and the cost of running a VPS, especially one that may be more than you need.
Serverless is a broad category, but most services will include three main things:
- No servers to deal with (obviously).
- Automatic scaling without additional configuration. Because code is executed in small containers, it is easy to launch several containers in parallel.
- Payment per use invoicing. This can make serverless code cheaper than a traditional VPS-based solution, as you probably do not use that VPS at maximum load all the time.
This makes serverless a competitive model based on cost alone. For a site like Bustle, they moved to AWS Lambda 84% cost savings compared to their old architecture. Scaling up serverless solutions is much more linear because you do not have to pay for individual server instances.
PaaS vs. FaaS
It is important to clarify the difference between PaaS and FaaS “serverless” models, as the marketing for each type is largely the same.
Platform as a Service (PaaS) cuts out servers, but does not affect your underlying code or implement any new coding methods. This is usually done through containers like Docker, where snapshots of your application can be distributed very quickly across multiple servers. With some services, such as AWS Fargate and Azure Container Instances, you do not have to worry about servers at all, which makes them “serverless”. With other services, such as Heroku, you are still charged per server, but you do not have to worry about configuring it yourself, and it is easy to scale across multiple instances.
Function as a Service (FaaS) completely cuts out the idea of servers. Instead, your code is triggered, runs a quick function and charges for the memory and estimated time you used. FaaS models force the idea of ”micro-services”, break down your app into its components and develop each one individually. This does not work for everyone, but when it does, it can accelerate development and maintenance compared to traditional “monolithic” application designs. AWS Lambda, Google Cloud features and Azure features are all FaaS models.
FaaS is generally considered a “serverless” calculation, but it may not be the most descriptive term for it. “Microservices” is much more representative of the ideas behind FaaS and “serverless” models. You can still build microservices with PaaS services, but FaaS forces you to do so.
Where microservices shine
Imagine that your app is a large monolithic application with many different moving parts.
All these parts of your app talk to each other and depend on each other to make everything work. But it’s messy, everything is intertwined and addiction management becomes a pain. If you want multiple teams working on different parts of the app, you will come across each other’s shoulders quite often.
A micro-service-based application model divides everything into its own service. These can still be run on the same machine, but you may want to package them with Docker to run on separate servers, or scale them across a cluster of Kubernetes. This is much easier to do when you can scale a piece separately from the others.
This can be broken down further with FaaS suppliers, where these building blocks can now be single functions instead of entire services. For example, you can have a feature to automatically resize images for different devices, which the Seattle Times uses Lambda for. You can create separate functions for each REST API endpoint and refrain from running an API server completely.
Of course, this model does not work for all applications. Some things are not designed to split up, and doing this retroactively can be a huge pain. But when it works, you can end up with a very structured application that is easier to fix when it starts to break.
Micro-services are not always easier
Micro services are cleaner, and when you lay it all out, they look much nicer than the messy monolithic design that many apps use. But someone has to design all that structure.
Building a micro-service-based app puts a lot of effort into your DevOps cycle, especially when you are planning everything. While you may be able to start a monolithic application and build everything as you go, microservices require careful planning from the start. This requires good architects and good communication with the rest of the team about the structure of the app.
And with more services, more things will monitor, troubleshoot and maintain. If you only have a small development team, it can be overwhelming to try to build and fix many services at once.
In addition, FaaS services are not usually designed to run 24/7. If you use Lambda features to build an app that constantly triggers Lambda features, you will run your bill higher than just paying for a VPS.
When and when not to use serverless
PaaS services like AWS Fargate simply run container apps without bugging you about the metal they run on. You can switch your entire application infrastructure to a service like this without any problems. With PaaS also comes easy clustering and adopting some ideals from the microservice model can help you improve the scalability of your apps without much restructuring.
FaaS services are more complicated. They are not right for everything but are very useful when used correctly. They are great for simple lightweight backend services that do not require much configuration or a full VPS to run them. You can use them as a complement to your existing app, and while you can build an entire microservice-based application with only serverless cloud features, the decision is much more complex.
But money is also an important factor, and although micro-services and serverless application models can be more difficult to plan, you can save a lot of money if you succeed.
AWS Lambda is also very useful in combination with other AWS services. Lambda functions can be triggered based on actions in your AWS infrastructure, such as automatically running a function when a file is released in an S3 bucket. If you already use AWS services, Lambda can be a good automation tool.