Node Red Context Store
In this tutorial, you'll learn about Node. We cover key concepts, practical examples, and best practices.
Hook
You store a counter in Node-RED context: context.global.counter = 42. It works during the current session. You restart Node-RED, and context.global.counter is undefined. The context data you rely on for long-running logic keeps resetting.
The Wrong Way
Using flow context for data that should survive restarts — flow context is cleared every time the flow is deployed or restarted.
// BAD: Using flow context for persistent data
flow.set("counter", 42);
Counter: 42 (after set)
Node-RED restarted
Counter: undefined (flow context cleared on restart)
Flow context is in-memory only. It survives a single runtime session, not a restart.
The Right Way
Use context.global with a persistent context store (file-based or Redis).
# 1. Configure a persistent context store in settings.js
cat ~/.node-red/settings.js | grep -A 10 "contextStorage"
contextStorage: {
default: {
module: "memory"
},
file: {
module: "localfilesystem"
}
}
# 2. Restart Node-RED
node-red-restart
// 3. Use the file-based context store
var counter = context.global.get("counter", "file") || 0;
counter++;
context.global.set("counter", counter, "file");
node.warn("Counter: " + counter);
# 4. Verify persistence
# Run the flow, restart Node-RED, run again
First run: Counter: 1
Restart...
Second run: Counter: 2 ✓ (persisted)
# 5. Check the context file
ls -la ~/.node-red/context/
-rw-r--r-- 1 node-red node-red 1024 Jun 24 12:00 global-counters.json
Prevention
- Use
defaultstore for temporary data and a named store ("file") for persistent data. - Always specify the store name when calling context functions:
get(key, "file"). - For multi-instance Node-RED, use Redis context store instead of local filesystem.
- Do not store large binary data in context — keep it under 1 MB.
- Monitor the context file size — it grows with every change. Set
maxSizein the context store config.
Common Mistakes with red context store
- Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
These mistakes appear frequently in real-world NODE code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
DodaTech — context that remembers between restarts.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro