Node-RED Context Storage: Memory, File, or Database
Decide where to persist Node-RED flow and global state across restarts using memory, file, or database context stores.
12 Jul 2025, 04:25 UTC

Decision: where should Node-RED keep flow and global state?
Node-RED loses flow and global context every time the Node.js process restarts unless you configure context storage. If a flow stores a counter, session flag, or device state in flow or global context, that data disappears on restart, redeploy, or container update. The choice is between the built-in memory store, the file-based store, and an external database backend.
Useful takeaway: keep ephemeral working state in memory, use the file store for single-instance durability across restarts, and choose an external database only when multiple Node-RED instances must share the same context.
Constraints to check first
- Scope: only flow and global context are persisted by the file store. Node-local context is never durable across restarts.
- Secrets: context values are not encrypted. Do not store credentials, tokens, or API keys in flow or global context without separate protection.
- Restart requirement: changing contextStorage in settings.js requires restarting Node-RED.
- Write load: high-frequency telemetry written to context can saturate disk I/O or memory.
Supported context storage options
| Backend | Survives restart | Shared across instances | Best for |
|---|---|---|---|
| Memory (default) | No | No | Temporary working state, dashboards, prototypes |
| File system | Yes | No | Single-instance counters, flags, device state |
| External database | Yes | Yes | Multi-instance flows, clustered Node-RED |
The context API—flow.get, flow.set, global.get, global.set—stays the same regardless of backend, so flow logic does not need rewriting when you switch stores.
Trade-offs
Memory
Memory context lives in RAM and is cleared when the process exits. It is the fastest option and requires no configuration, but it is volatile. Use it for values that can be recalculated or repopulated after a restart.
File system
File-based context writes JSON files under the Node-RED user data directory. It survives restarts on a single instance, but abrupt termination can lose the most recent writes because Node.js may not flush buffers before exit. Heavy write loads create disk I/O bottlenecks.
External database
Database backends let multiple Node-RED instances read and write the same context. They add infrastructure, connection management, and operational complexity, and are usually unnecessary for a single standalone instance.
Implementation: enable file-based context storage
Edit settings.js in your Node-RED user directory. On Linux or macOS this is typically ~/.node-red/settings.js; in Docker it is often /data/node-red/settings.js. You need write permission to the user data directory and to any custom context path you choose.
contextStorage: {
default: { module: "localfilesystem" },
flow: {
module: "localfilesystem",
config: {
dir: "/data/node-red/context/flow"
}
},
global: {
module: "localfilesystem",
config: {
dir: "/data/node-red/context/global"
}
}
}
Replace /data/node-red/context with a path your Node-RED process can write to. If you omit config.dir, Node-RED writes to a default context folder inside the user data directory.
Restart Node-RED after saving settings.js. On a systemd-managed host:
sudo systemctl restart nodered
In Docker:
docker restart <container-name>
Validation: confirm state survives a restart
- Deploy a flow with an inject node connected to a function node that sets flow context:
flow.set("lastSeen", Date.now());
return null;
- Add a debug node wired to read the value:
msg.payload = flow.get("lastSeen");
return msg;
- Trigger the inject and note the timestamp in the debug sidebar.
- Restart Node-RED.
- Trigger the debug read again. If the timestamp matches the pre-restart value, file-based persistence is working.
Expected check: the debug output after restart shows the same value written before restart. If it is undefined or null, persistence is not active—verify the context path, file permissions, and that Node-RED was fully restarted, not just redeployed.
Limitations and operational checks
- Abrupt process termination can lose the last file-based writes. Do not rely on context for exactly-once financial ledgers or safety-critical state.
- Large objects or high-frequency telemetry in context increase memory and disk usage. Measure write latency at your expected message rate before production use.
- Context is not encrypted. Restrict file permissions on the context directory and avoid secrets.
- Switching backends or paths may make previously persisted data inaccessible. Back up the context directory before changing settings.js.
Practical check
List the context directory after a write to confirm JSON files are being created:
ls -la /data/node-red/context/flow
If the directory is empty after setting flow context, the store is not writing to the expected path.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.