One of the biggest misconceptions about Serverless is that it cannot support stateful applications. We have already pointed out that the new breed of Functions as a Service (FaaS) offerings support stateful applications. In this post, we will go a bit deeper on the topic and break the myth that Serverless is fit only for stateless applications. While we should not have any confusion about the constraints imposed by the FaaS abstraction, they are also good for most of the stateful applications. We will first look at the conventional wisdom about Serverless platforms and then talk about why it is wrong.
Serverless is for stateless applications myth
FaaS was originally built as a platform that deploys functions that takes the input and transforms into an output. This required an architectural rethink that componentizes a large application into smaller chunks. The constraints imposed by FaaS was a result of a newer business model that could offer compute capabilities for pennies. While the virtual machines in the cloud had ephemeral storage that required developers to rethink how they architect their applications, FaaS has made even compute ephemeral. This put severe constraints on the architecture and limited the kind of applications that can be deployed using FaaS. These constraints made people think that FaaS is only suitable for stateless applications.
Let us not delude ourselves here by thinking that deploying stateful applications with FaaS is easy. Even with virtual machines and containers on the cloud, there is operational overhead for developers to deploy stateful applications. FaaS abstraction is at a higher level where the developers need not worry about the underlying infrastructure, including both containers and virtual machines. This makes handling state a more difficult task for developers unless the FaaS providers offer a more seamless way to persist state.
Beyond the storage volume, FaaS has other constraints that make it difficult to handle stateful applications.
- Many FaaS providers offer severe constraints on the execution time. This is how FaaS providers can offer services for cheap. Persisting states with short-lived code execution are not easy
- Early day FaaS offerings had the cold start problem that made it difficult to handle state persistence. Today, we have FaaS providers managing the cold start issues much better and some FaaS offerings don’t even have a cold start problem
- Early day FaaS offerings didn’t make it easy to ensure that the state from one function execution is available to another execution of the same function. There is no way for two executions to share the state stored in the associated ephemeral storage. The only way to handle state is by storing in externalized storage which came with even higher operational overhead than handling external storage for virtual machines and containers, defeating the very advantage of FaaS
Such constraints forced developers to focus mainly on stateless applications and, hence, had lead to the myth that FaaS is suitable only for stateless applications.
Deploying stateful applications with FaaS
In spite of the constraints imposed by FaaS providers, it is possible to deploy stateful applications and persist state across multiple invocations. There are a few ways in which stateful applications can be deployed with FaaS:
- Connecting to the database directly. Except in the case of some serverless databases that scale seamlessly, developers need to ensure that the database can handle the load and scale properly. If done wrong, the costs could run high
- Use a Backend as a Service (BaaS) offering which can abstract away the complexity in managing the data store and remove some of the operational overhead that comes with connecting directly to the databases
As mentioned earlier, connecting to a database in a service like AWS Lambda adds some operational overhead to the developers. Also, If the database or BaaS is in a different Zone or Region, there is a latency that will impact the applications. However, a new breed of FaaS offerings like Catalyst, Nimbella, etc. offer an integrated BaaS or data store, which usually is available in the same zone as the compute, and these services reduce the latency impact and remove any additional operational overhead for the developer. If you are looking for deploying stateful applications on FaaS, be sure to pick a provider like Catalyst that can seamlessly support these applications.
Also, Azure Functions now offers Durable Functions that allow chaining of functions, opening up another opportunity for long-running applications that can handle the state better. Even though early day FaaS offerings made it easy to deploy stateless applications, newer offerings are explicitly supporting stateful applications in their platform. It is time for the developer community to look beyond stateless applications when it comes to use cases supported by FaaS. With the right platform, many stateful applications can be deployed but you may have to re-architect the application into a more modular architecture.