Show the dangers of assuming local memory, local disk, or execution reuse as durable state. This anti-pattern is subtle and common.
Stateful assumptions in stateless compute are one of the most subtle and common serverless failure modes. A function may appear to keep data in memory, reuse a temporary file, or rely on a warm runtime instance across several invocations. Sometimes that works in light testing, which makes the anti-pattern even more dangerous. The problem is that none of those behaviors are a safe source of business truth.
Serverless runtimes may reuse execution environments, but they are not a durability guarantee. Memory can disappear, instances can scale out, local disk can be isolated to one runtime, and retries may land on a completely different worker. Treating those optimizations as durable state leads to data loss, duplicate processing, and unpredictable behavior under load.
flowchart LR
A["Invocation 1"] --> B["Runtime instance A"]
C["Invocation 2"] --> D["Runtime instance B"]
B --> E["Local memory or local disk"]
D --> F["No access to prior local state"]
What to notice:
Common examples include:
These often appear because the optimization works during development or at low concurrency.
1const processedOrders = new Set<string>();
2
3export async function handler(event: { orderId: string }) {
4 if (processedOrders.has(event.orderId)) {
5 return { skipped: true };
6 }
7
8 await billingService.charge(event.orderId);
9 processedOrders.add(event.orderId);
10 return { skipped: false };
11}
What this demonstrates:
There is nothing wrong with warm caches or temporary local files when used as optimization aids. The problem is using them as the only source of truth. A healthy rule is:
That usually means:
A queue consumer keeps a local in-memory set of processed event IDs to avoid duplicates. It works well in low-volume tests but creates duplicate invoices during retries and scale-out. What is the real design flaw?
The stronger answer is that deduplication state was implemented as a runtime optimization instead of a durable contract. The fix is a durable idempotency or deduplication store, not “hope for warmer instances.”