Source code Releases Stability

Global mutable state in serverless

Part of the Scalability antipatterns check · fix arrives as a guide

What it is

Module-level mutable state — an in-memory cache, a counter, an accumulating array — is used in a serverless or multi-instance deployment.

Why it matters

Each instance has its own copy, so reads are inconsistent depending on which one answers, and everything is lost when the container recycles. An array that only grows is also a memory leak on any instance that lives long enough.

How to fix it

Move shared state to Redis or the database, where every instance sees the same value. Keep module scope for genuinely immutable things — configuration, clients, compiled regexes — which is what it is good for.

Run them all on your app

Connect your repo and your live services with read-only scopes. The first scan is free, and nothing changes without your approval.