The Serverless Trilemma
Tags: serverless, architecture, designpatterns
Published: 2020-02-26 13:51:20

---
title: The Serverless Trilemma
published: true
description: A simple framework to evaluate and make better architectural decisions for serverless backends
tags: serverless, architecture, designpatterns
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/yazzvkz90618yrom0d5i.PNG
---

# Quick Intro

The **Serverless Trilemma** is a framework developed by researchers in IBM a couple of years ago ([full academic paper](https://drive.google.com/file/d/1-M9_oK4kWMcsx38DFRhxrdSWE958O7bh/view?usp=sharing)). It helps us evaluate the quality of architectural designs and make better decisions by understanding the trade-offs involved.

> This is an introductory article, we'll publish more hands-on guidance on how to apply these principles in next articles on this series. To stay tuned, please [subscribe here](https://dashbird.io/subscribe-knowledge-base/?utm_source=dev.to&utm_medium=article&utm_campaign=knowledge-base&utm_content=serverless-trilemma).


# Basic Constraints

There are three desired properties we should look for in a serverless architecture:

1. Black-box
2. Double-billing
3. Substitution

## Black-box

The Black Box principle indicates that each function should work
independently from the rest of the system and no implementation details should be leaked.

Consider two functions running on [AWS Lambda](https://dashbird.io/knowledge-base/aws-lambda/introduction-to-aws-lambda/?utm_source=dev.to&utm_medium=article&utm_campaign=knowledge-base&utm_content=serverless-trilemma): `A` and `B`.

`Function A` uses the AWS SDK to invoke `Function B`. 

![Black-box Functions A and B in AWS Lambda](https://dev-to-uploads.s3.amazonaws.com/i/ywde4ghmlu0gfvy1uxrr.PNG)

What happens if we need to move the `Function B` code to another infrastructure? Perhaps the [AWS Lambda](https://dashbird.io/knowledge-base/aws-lambda/introduction-to-aws-lambda/?utm_source=dev.to&utm_medium=article&utm_campaign=knowledge-base&utm_content=serverless-trilemma) memory limits aren't enough anymore.

The AWS SDK invocation process will not work anymore. In order to migrate `Function B`, a concomitant modification is required in `Function A`.

![Black-box AWS SDK fails](https://dev-to-uploads.s3.amazonaws.com/i/i92mi0gj2mtskcp28b52.PNG)

This type of architecture makes it difficult to introduce changes and increases the likelihood of unintended consequences.

The problem here is that `Function B` is leaking its implementation details, thus not working as a Black-box. The leakage is having `Function A` aware that `Function B` is deployed in [AWS Lambda](https://dashbird.io/knowledge-base/aws-lambda/introduction-to-aws-lambda/?utm_source=dev.to&utm_medium=article&utm_campaign=knowledge-base&utm_content=serverless-trilemma).

To satisfy the Black Box principle, we could create an HTTP API. By routing requests through the API, we can decouple the functions. Later we can just update the API routing to the new infrastructure when migrating Function B.

This simplifies change, which can be extremely valuable as the system and development team grows.

![Black-box - decoupling with an HTTP API](https://dev-to-uploads.s3.amazonaws.com/i/0u0wes6k6k2lmj5ln9e8.gif)


## Double-billing

This principle is relatively simple: two functions should not run at the same time while one is in idle state waiting for the other.

In Serverless, we pay for the function execution time. Consider `Function A` starts processing and being billed. At some point, it invokes `Function B` and waits for a response.

Problem is: While `B` is processing, `A` continues to be charged, despite being idle. `Function B` processing time is effectively being charged twice, which is arguably undesirable.

![Double Billing Animated Gif](https://dev-to-uploads.s3.amazonaws.com/i/xbtoya5vxo75zxkwdf6j.gif)


## Substitution

Substitution is about having a composition of functions behaving just like any normal function.

> But what is composition?

Consider a user requests an invoice from a billing system, which is served by a serverless function. This function may rely on a second one to apply any discounts available to this user. Yet another function is responsible for calculating Sales Tax, for instance.

All three functions are composed to work as a group, hence the name "composition".

![Functions Composition](https://dev-to-uploads.s3.amazonaws.com/i/jt5myub4cyfak3298v0y.PNG)

The substitution principle indicates that all three functions
together should behave like a function. From invocation methods to timeout to dead-letter-queue, everything behaves as if they were a single function.

The requester doesn't actually know this is a composition of functions. Any developer can rely on this composition without worrying about how it behaves, they just apply everything they already know.

This property improves productivity and overall quality by reducing errors and unintended consequences.

![Functions Composition behaves as a single function](https://dev-to-uploads.s3.amazonaws.com/i/t0cabsxqwg8jduq7kfs7.PNG)


# Trade-offs involved

Most of the time, it is not possible to have all the properties together. Applying the Black box principle is usually easy. When it comes to the other two, there is usually a trade-off.

When we prioritize one, such as **Substitution** it becomes difficult to apply **Double-billing**.

![Trade-off animated gif](https://dev-to-uploads.s3.amazonaws.com/i/royi2uqy79nmqlw1cysw.gif)

The Serverless Trilemma framework helps to make architectural decisions
and identify what needs to be prioritized:

* **Black Box** improves maintainability, scalability, and resilience by decoupling components
* **Double-billing** enables cost-efficiency and reduces the overall concurrency level of the system
* **Substitution** enables code reusability and improves the quality

What should be prioritized will really depend on each case. We'll dive into more details about this decision-making process in the next articles coming on this series.

To stay tuned, [subscribe here for free](https://dashbird.io/subscribe-knowledge-base/?utm_source=dev.to&utm_medium=article&utm_campaign=knowledge-base&utm_content=serverless-trilemma) to receive announcements when we publish again.