Lambda Functions Are NOT Fault-Tolerant by Default
Tags: awslambda, serverless, failure, faulttolerance
Published: 2019-09-30 08:31:00

---
title: Lambda Functions Are NOT Fault-Tolerant by Default
published: true
description: Learn why AWS Lambda does NOT make your functions fault-tolerant by default and you need to do about it.
tags: aws-lambda, serverless, failure, fault-tolerance
cover_image: https://thepracticaldev.s3.amazonaws.com/i/etvadp77dov6lyx6mm8o.jpg
---

When an entire system crashes, we call it a _failure_. If a component of a system fails, that’s a _fault_. _Fault-tolerance_ is about modeling a system to withhold one or more _faults_ and still meet its purpose without major hurdles.

AWS Lambda infrastructure is highly unlikely to suffer from a widespread _failure_. But the software we deploy becomes a component of the _system_, and it can experience _faults_.

When a _software fault_ happens, the AWS Lambda platform may automatically invoke the function again with the same event payload: called _retry-behavior_.

### And that can be a big problem. Much bigger than the fault itself.

Bear with me along the next lines, as this advice can save you a ton. Lambda won't magically make your functions _fault-tolerant_, but you can accomplish that by applying a single concept.

![jellyfish](https://thepracticaldev.s3.amazonaws.com/i/ah1eb01tyyycufo9ds6z.jpg)

# Why is that a big issue?

Let's say you're operating an e-commerce site and AWS Lambda is being used to process customer orders. A person purchases an item and you have a function taking care of the following steps, all in a single run:

1. Making sure the item is available in stock
2. Processing credit card
3. Removing item from stock
4. Sending confirmation email

Now consider the first three steps completed successfully, but there was a momentaneous issue in sending the email and your application raised an error. Lambda platform automatically invokes the function again, with the same parameters, and the email is sent successfully. Awesome, isn’t it?

Well, not so fast. Our system just registered a second, unintended purchase for the same customer… and charged his credit card twice!

_Seldom this process would be implemented like this, but it serves as an illustrative example._

![Worried man](https://thepracticaldev.s3.amazonaws.com/i/oiykkjk66pmjvl04d8w5.jpeg)

# Why AWS Lambda does that?

Lambda retry behavior is actually a very cool feature, don’t get it wrong. In a distributed system, many things can go wrong. In fact, when things _can_ go wrong, rest assured they _will_ go wrong at some point. AWS takes care of making sure these errors aren’t left buried and the operation has a few more chances to succeed. We surely don’t want to miss the revenue of a sale due to a technical issue.

# Deploying fault-tolerant code on Lambda

All right, we see value in the retry behavior, but how can we avoid the headaches such as the double charge example?

There’s a concept called [idempotence](https://en.wikipedia.org/wiki/Idempotence) that comes to our rescue. Wikipedia defines it as a “_property of certain operations in mathematics and computer science whereby they can be applied multiple times without changing the result beyond the initial application_”.

A good practice to combine with idempotency is the [separation of concerns](https://en.wikipedia.org/wiki/Separation_of_concerns). In the previous example, we had several different operations bundled together. If possible, it would be good to have different functions taking care of each operation. One of the reasons is that idempotency needs to be analyzed and implemented from the perspective of the operation.

Read operations usually do not produce any side effects, they’re idempotent by nature. In our example, operation #1 (check if an item is available in stock) would be an example of that. In most cases, you won’t need to worry about these, so having them implemented separately will make it easier to manage the rest of your stack.

Storing and deleting a value aren’t idempotent operations by nature, but they can be _if we have a unique identifier (UID)_ for that resource. In our e-commerce scenario, if the customer order has a UID, the storing operation can be performed multiple times without creating multiple different order placements. 

The order UID could be, for instance, a hash of the customer email or username, the purchase timestamp, and a list of items purchased. These variables would be sent as a parameter to our API when the site receives the order request. If the function fails at some point and the invocation is retried, the same order UID would be generated again, meeting the idempotency requirement. Again, _this is just for illustration purposes_ - each circumstance will require proper analysis to find a stable and resilient idempotent implementation.

For the credit card charging part, most platforms will support idempotent requests. [Stripe](https://stripe.com/), for example, will provide an _[idempotency key](https://stripe.com/docs/api/idempotent_requests)_, so that you can safely retry a request if something goes wrong in transit.

Usually, if the operation takes place in the realm of your stack, it will be fully on your hands to meet idempotency requirements. The unique identifier principle explained above will usually be enough. But if you’re relying on third-party APIs, it might be tricky to ensure idempotency and you might need help from the other party to accomplish this goal, in case this kind of operation isn’t supported out of the box. If you can’t get the third party to work with you, there’s always the possibility to run all operations on your end first, create a separate process to check whether everything ran successfully, then interact with the external API. This wouldn’t be the ideal implementation but could be as good as one can get in some circumstances.


# Managing Lambda retries like a PRO

[Dashbird](https://dashbird.io/) automatically identifies when an invocation is actually a retry from a previous execution. It links the first execution to subsequent retries and you can easily navigate them, as well as browse individual logs, all in one place. This makes it a lot easier to understand why your functions are failing and whether your idempotency strategy is working properly. You can try Dashbird for free by [signing up here](https://dashbird.io/signup/) (no credit card required).

_Full disclosure: I work as serverless advocate for Dashbird_.

### Photo Credits

* Cover image - skater: [Bradley Dunn](https://unsplash.com/@bradleycdunn?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)
* Jellyfish: [Joel Filipe](https://unsplash.com/photos/_AjqGGafofE?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)
* Man worried: [Sebastian Herrmann](https://unsplash.com/photos/jzTQVxCyKYs?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)