HeadlinesBriefing favicon HeadlinesBriefing.com

AWS Lambda Durable Functions Code Determinism

DEV Community •
×

A developer posted a code snippet asking if it was deterministic, sparking a debate on LinkedIn. The code, using AWS's durable execution SDK, sorts orders by priority and processes them in a loop. Most respondents correctly identified it as non-deterministic, but the reasons were more nuanced than initially apparent.

The primary issue lies in sorting orders with equal priority. JavaScript's sort is stable, but it preserves an arbitrary input order that may be incidental. This hidden dependency can cause inconsistent step execution during replays. Using `Date.now()` for a timestamp is also non-deterministic, which can cause replay issues if the value influences workflow state later.

A third concern is retry safety. If a step contains multiple side effects, a partial failure could cause duplicate operations on retry. The fix involves breaking steps into smaller, idempotent units and using explicit tie-breakers in the sort, like order IDs, to ensure predictable behavior under all durable execution scenarios.