Skip to content

My take on idempotency for Durable Functions

  • by

Idempotency is one of the least pleast things one has to implement when creating Durable Functions, but I have found a solution that works well for me, is very cheap and seems to be trustworthy.

What is idempotency? I hear you ask…

Long story short, idempotency means that you have to make sure that your activity function is only executed once. The orchestrator function will call each activity function at least once, because of reasons, and you have to make sure that only the first execution “goes through”.

Ok… So how do I do it?

TLDR: Redis

Long version: To tackle the problem you have to understand it first. What you need to build proof against, is the possibility that your Activity Function is called twice within a second or so. If it happens, it happens really fast and of course you want to stop it as early as possible.

Needless to say that in order to identify an execution, you need to have an identifier, some short of Id. If you don’t have it, then it’s time to get creative and somehow else identify your input? Hash it maybe?

The logic you want to build is simple:

My tool of choice for this little thing, is Redis. Redis is fast, light, cheap and as simple as it can be. Wrap it in a nice Singleton and make it accessible everywhere via Dependency Injection, peace of cake (but I will give you -later- a github repo just in case). So, the actions get this form:

Is this the first run?

To tell if this is the first run, I just ask Redis for the value of the key "ExecutionWithID-" + ID

Did I get anything? Did it even exist? Then this is not the first run and you need to GTFO.

No?? Then congrats, you are the first! Now it’s time to…

Mark your territory

How do you do it? Simple! Create a KeyValuePair in Redis, with the key being called (you guessed it)

"ExecutionWithID-" + ID

By doing so, you make sure that the next execution will find your “trace” and gtfo. After that you are just free to do your thing. Simple, right?

Is that all?

Kind of, yes! But! Reality might slap you in the face and force you to allow a second (and a third..) run sometimes. You might want the “trace” to disappear after an hour or so, to allow a re-run. Just let your business logic bloom.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies. By continuing to use this site, you accept our use of cookies.