Go back

Design Patterns for Serverless and FaaS

The patterns are divided into categories, and each one includes the title, the problem (or to put it another way, the symptom), the solution and some of the possible disadvantages. So, without further ado, here is the complete list:

Composition Patterns

How to compose and orchestrate serverless functions in sequences or workflows.

Routing Function

  • Problem: How can different branches or flows be executed based on a message?
  • Solution: Use a serverless function that receives the message and invokes the following function based on a decision. API Gateway could be used, but sometimes we need to keep it private and avoid the expense of such a service.
  • Disadvantages: Double billing, since the first function will have to remain blocked until it receives the response from the flow. Another disadvantage is that the flowchart is hidden in the code. Possible bottleneck. Point of failure.

Function Chain

  • Problem: The execution of the function exceeds the time limit, generating a timeout.
  • Solution: Divide the task into separate functions that are chained sequentially.
  • Disadvantages: Coupling, number of deploys, amount of data transferred from link to link, difficulty in maintaining states.

Fan-out / Fan-in

  • Issue: Feature resource limitations lead to reduced performance.
  • Solution: Split the function into multiple parallel invocations. The master function receives the message and divides the tasks into smaller functions, then calls a worker for each segment. As each segment finishes, it stores the partial result in a database and finally an aggregation service compiles the results (this can be avoided if the partial results are sufficient).
  • Disadvantages: Costs, difficulty, some limitation in the division of tasks.

Externalized State

  • Problem: How can a state be shared between parallel or sequential functions? (because serverless functions are stateless by design)
  • Solution: Store the state in External Storage.
  • Disadvantages: Latency, costs, extra programming to store or consume the state.

Externalized State

  • Problem: How should complex state-dependent processes with branched steps be coordinated?
  • Solution: Divide a task into a number of functions and coordinate their performances with an orchestration tool.
  • Disadvantages: Costs, the work to maintain the workflow.

Thick Client

  • Problem: Using the classic three-tier model with services as a mediator between the client and the database creates extra costs and performance issues.
  • Solution: Create a larger and more powerful client which has business logic and triggers workflows.
  • Disadvantages: Difficulty in deciding what should go to the FrontEnd or in a workflow; security; performance.

Event Patterns

How to manage asynchronous workflows executed by external events. 

Event Processor

  • Problem: How can a task be executed on-demand when an event is executed?
  • Solution: Subscribe a serverless function to a provider event, for example a change in a database.
  • Disadvantages: Difficulty in designing large processes.

Periodic Invoker

  • Problem: How can a serverless function be executed periodically at predefined intervals?
  • Solution: Subscribe the serverless function to a “scheduler”.
  • Disadvantages: none foreseeable.

Polling Event Processor

  • Problem: How to react to a state change in an external service that does not publish events?
  • Solution: Periodically review state changes and execute the necessary events.
  • Disadvantages: Latency between the change of state and the execution of the function. Excessive expenditure of resources in verifying a change of state.

Event Broadcast

  • Problem: How can multiple functions be invoked in parallel in light of a single event?
  • Solution: Subscribe multiple functions to a notification service, and post a notification to those events.
  • Disadvantages: none foreseeable.

Integration Patterns

How to integrate with external systems, including legacy?

Aggregator

  • Problem: An operation includes multiple calls to an API, generating extra jumps between the client and the services.
  • Solution: Bundle multiple API calls into a single serverless function.
  • Disadvantages: none foreseeable.

Proxy

  • Problem: How can legacy services be simplified for consumption by modern customers?
  • Solution: Implement a serverless proxy function that translates calls between the client and the legacy services.
  • Disadvantages: none foreseeable.

Strangler

  • Problem: How can an existing service be migrated to a serverless feature architecture in a controlled manner?
  • Solution: Create a facade to serve as middleware before calling the legacy API and incrementally replace each service with a serverless function.
  • Disadvantages: Excess work due to having to maintain two versions of the same system.

Valet Key

  • Problem: How can access to resources be authorized, without needing to send all traffic through a gatekeeper?
  • Solution: Get an access token by calling the authorization services, then use that token to access the desired resource. For example, if we want to upload a file to S3, we should request permission to directly access S3 from the authorization service, which would save us the cost of uploading the file to a serverless function and then from there to the S3 service.
  • Disadvantages: none foreseeable.

Availability Patterns

How can availability in serverless architecture be guaranteed and performance limitations solved?

Function Warmer

  • Problem: The Cold Start phenomenon creates unnecessary latency for frequent calls to the same function.
  • Solution: Periodically ping to the function so that the infrastructure remains active (“warm”)
  • Disadvantages: Costs of keeping the infrastructure running, added to the cost of the scheduler, overhead to manage the ping.

Strangler

  • Problem: External dependencies such as database connection are destroyed and re-generated by each invocation of the serverless function, leading to a decrease in performance.
  • Solution: Use the Global Scope of the serverless functions to gain an advantage in the reuse of containers.
  • Disadvantages: none foreseeable.

Bulkhead

  • Problem: Problem: Multiple workflows in a single function generate high latency in the execution flow, negatively impacting the performance of the rest of the flows.
  • Solution: Isolate the execution flow that has high latency in a separate serverless function, taking care of the resources for the rest of the flows.
  • Disadvantages: Overhead in the development of the different partitions.

Throttler

  • Problem: An FAAS function that scales quickly consumes a function or service that does not scale at the same speed generating a timeout response.
  • Solution: Add a message queue that enables us to queue many messages but consume them at the speed of the service that scales slowly.
  • Disadvantages: Costs of the message queue, need for extra work to get the result of the final operation.

Circuit breaker

  • Problem: A third-party service that is blocked or down generates timeouts in the serverless functions that consume it, generating an unnecessary cost during the timeout period.
  • Solution: Restrict the use of said service while it is down to avoid the inconvenience.
  • Disadvantages: Overhead in the development of said validation. Cost of maintaining the status of these services in some type of storage.
Mauricio Bergallo

About the Author

Mauricio Bergallo is a Systems Engineer with extensive knowledge in a variety of programming languages. Great experience and understanding with all aspects of the software development life cycle.