What does serverless mean?
Serverless computing means that the machine resources required for running code are managed by the cloud provider. To be more precise, serverless doesn’t mean we don’t use servers at all. It means that we don’t have to worry about infrastructure, and management tasks are automated.
From developers perspective the key benefit is saving time on provisioning and maintenance tasks, and focusing on writing code and delivering new features.
Serverless architecture is closely related to cloud computing. The very first serverless computing service, Google Cloud Engine, was introduced by Google in 2008. It is a platform supporting multiple programming languages for hosting web applications.
Currently, there are more serverless computing providers on the market. The best known are Amazon Web Services (AWS), Microsoft Azure and Google Cloud Platform (GCP), but there are other options like installing a serverless framework on your own cluster, i.e., Kubless, OpenFaaS, Serverless Framework or Apache OpenWhisk.
The main focus of this article is the Function as a Service model (FaaS). FaaS is a variation of serverless computing which makes it possible to execute code in response to events like an HTTP request or a new element in the queue. It is a good choice for single purpose, short tasks.
Advantages and Disadvantages od serverless applications
- Pay only for what you use – in many situations, a serverless solution can help save money, i.e., when your application is used only during business hours or you need very high capacity only during a specific period. In these situations you can schedule when your application is to be turned on and how it should scale.
- Less management work – this point follows on the previous one. Instead of hiring people to perform administrative and operational tasks, you can use serverless and focus on design and development. Of course, you may have some low level limits you can’t change, but thanks to this you gain better security and scalability.
- Better productivity – using Functions as a Service developers can take advantage of event driven programming, which can simplify code.
- Performance – if there is no running instance of a given serverless function, wait time is longer. This is known as a Cold start.
- Timeout – usually, there is a maximum duration for a single execution. Below the timeout limits are listed for the most popular cloud providers:
- AWS Lambda – 15 minutes
- Azure Functions – 10 minutes (in Premium Plan – unlimited)
- GCP Cloud Functions – 9 minutes
- Harder testing and debugging than traditional server code.
- Dependance on a specific provider – it is harder to move a solution to a different provider.
- More expensive in situations of stable resource usage.
When should you use serverless architecture? (Scenarios)
You can create any serverless application, but it does not always make sense to do so(see the disadvantages section above). Regardless of the technical possibility to create a specific solution using the serverless approach, there are typical situations when serverless is most beneficial.
- HTTP triggered requests – you can create serverless microservices, e.g., API for signing up to a newsletter.
- Event-driven architecture – serverless is great for processing incoming files like images or JSONs, e.g., automatically renaming and resizing files put in the bucket.
- Serverless may be useful in a situation of unpredictable usage patterns, e.g., if you are not able to estimate the amount of resources you need and don’t want to run out of resources.
- FaaS is meant for small, independent and non-compute intensive tasks like sending an email.
Building serverless applications – best practices
Design & Scaling
- Serverless applications are usually parts of distributed systems and should be stateless.
- Try to keep FaaS applications as simple as possible.
- Estimate the right number of active instances. Remember not only about ensuring the availability of your service, but also about costs. If you want to avoid unpredictable expenses, limit the number of max instances.
- Reckon the amount of memory required for function execution and configure the memory limit. Remember, you pay for what you use.
Debugging & Testing
Taking into account that testing distributed and serverless systems is much harder than centralized ones, you should:
- Prepare reliable and detailed logging.
- Monitor the status of executions in order to identify failure and set alerts if it happens.
- Don’t fail silently – make sure that each unexpected behavior will be noticed.
Configuration & Security
- Always separate configuration from application code and store it in a different file, e.g., JSON or YAML.
- Remember about timeouts in FaaS – functions should be used for short tasks with predictable execution times.
- Keep one codebase tracked in the version control system for all environments (development, staging, production). This helps you maintain more consistent code and facilitates the deployment process.
1. Serverless API
Description: An HTTP request invokes a function, and the function executes a query on a database, e.g., insert a record in a table.
Use case: CRUD operations.
2. Processing files
Description: Delivering a new file to storage invokes a function. Files have to be resized and compressed to save space and need to be saved using a defined file name pattern.
Use case: handling delivered files.
3. Adding tasks to the queue
Description: HTTP requests invoke the first function which just adds a new task to the queue. The second function consumes queue elements and executes a longer task.
Use case: sending a confirmation e-mail.
Serverless technologies are becoming more and more popular. At the same time, they face dynamic changes and development. Using these kinds of solutions requires new skills and different approaches in many areas, such as designing, developing, testing and maintaining applications, but it brings new opportunities and could make developers lives’ easier.
- Jason Katzer, Learning Serverless: Design, Develop, and Deploy with Confidence, O’Reilly
- Boris Scholl, Trent Swanson, and Peter Jausovec, Cloud Native, O’Reilly
- AWS Lambda Documentation: https://docs.aws.amazon.com/lambda/
- Azure Functions Documentation: https://docs.microsoft.com/pl-pl/azure/azure-functions/
- Cloud Functions Documentation: https://cloud.google.com/functions