Idempotence is an important but often overlooked part of developing for AWS Lambda. In this article I'll explain what it ˜is, why you should care about it and when you need to use it.
What is idempotence?
Idempotence is a quality in computing that repetitions of an action has no further effects on the outcome.
Let's start with a simple example. Supposed that I have a function which retrieves a record from DynamoDB. I can run that as many times as I want and it will return the same value. The only way the value would change is if another function updated the record. ˜ While that's obvious for reading data what about writing it? Here it become more complicated because you may end up creating duplicate records or subsquent updates may alter fields like version numbers and updated timestamp.
In it's
Why does idempotence matter?
AWS Lambda guarantees a function will be run at least once. While this typically means that a function will be run only once it is well documented that functions can run multiple times with the exact same input. In some cases this happens minutes apart.
You might not think that it matters if the odd additional record is created in Lambda. But what if that Lambda is responsible for charging a customers credit card? Is it really acceptable to charge their card twice?
Solving idempotence in Lambda
Before we start solving the idempotence problem it's worth asking if you There are a number of ways of dealing with idempotence in Lambda but first it's important to understand when you need it. Here are a couple of examples where you might not need to deal with idempotency.
Lambda functions that retrieve data with no side effects will almost never need to deal with idempotency. Providing you are just fetching data then re-fetching the data should be acceptable.
Update or create functions that always result in the same data being written to DynamoDB.
Functions where external systems can deal with idempotency for you. The Stripe API has an
Idempotency-Key
header. Providing your Lambda function always creates and passes the same key and your code deals
If you need to deal with idempotence yourself then librar