Developers that want to bring serverless computing benefits to stateful applications have several options to maintain state for real-time and data-intensive workloads. Choose the best setup -- and nail the execution -- to meet application needs.
Functions are essentially independent blocks of code that perform a finitely specified task. They execute a script or transfer log data from one application to another, for example. Serverless functions, just like those in a Java program, have dedicated inputs and outputs, which means they can connect and form a chain where the output of one function is the input for another. This helps manage the application architecture in a distributed manner.
Although function as a service (FaaS) serverless design suits event-based applications, the ephemeral nature of functions makes it limited for serverless development in the context of real-time and data-centric applications. Function instances are volatile and have a lifespan of five to 15 minutes. Real-time, cloud-native workloads would need a way to recover the application's state every few minutes if deployed in a pure FaaS model. Although an application can store the states of these stateless functions in the back end, serverless developers should avoid scenarios where the app must frequently load state from storage. It is costly, leads to latency and affects the throughput of network requests.
Functions and latency
Functions should be directly accessible to each other. Without immediate connections, functions depend on a slow storage medium to transport data from one function to another, building up latency. In real-time application scenarios -- such as 24/7 monitoring systems -- latency is unacceptable.
Serverless functions predominantly underpin short-term workloads, which means that resources are allocated to them when requested and taken away once the request ends. Stateful applications developed on serverless functions can't use traditional mechanisms to work, such as global variables that can hold data throughout the application's lifetime. It's impossible for stateless functions to read from and write to disk, and the application can't maintain a constant connection to the database.
To create stateful applications, serverless developers can manage application state with database connections, an event payload or backend as a service (BaaS) to integrate with the application. Developers should use these approaches to make full use of the serverless architecture. Done correctly, they manage application state without degrading performance.
Direct connection to the database
Serverless developers can store application state in a database and then connect the functions to the database to access it.
In this setup, developers must worry about scalability. To avoid stress on the database and ensure frequent and continuous access to application state, follow the serverless vendor's database connection guidelines specific to its cloud platform, such as AWS Lambda to Amazon Relational Database Service. Create and store the state at the outset to avoid the cost incurred by constant calls to the database.
Avoid object-relational mapping (ORM), the process of converting data between two incompatible systems. ORM requires individual models for each function used in an application, which just creates more non-feature code to write for serverless development.
Database transactions are costly and don't scale well. Rather than handle transaction concurrency along with application development, use an external connection pooling agent, such as a Java database connectivity (JDBC) driver. Cloud vendors offer these drivers, such as the Microsoft JDBC Driver for a SQL Server to connect to an Azure SQL Database. Third-party software products, such as Progress DataDirect, can also function as pooling agents.
Backend as a service is the other major mechanism for stateful serverless development. Serverless functions use stateless HTTP APIs, and BaaS deals with issues like concurrency and performance by abstracting the entire serverless architecture and managing the state irrespective of the workload. BaaS APIs can be invoked inside a serverless function through one line of code, which keeps the app codebase clean and enables functions to act stateless. BaaS options include Progress Kinvey and Google Firebase, among others.