Skip to content

Node Red Context Store

DodaTech 2 min read

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 default store 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 maxSize in the context store config.

Common Mistakes with red context store

  1. Mixing let bindings with <- bindings in do notation, producing type errors
  2. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  3. 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

What context stores are available?

Node-RED includes built-in memory and localfilesystem stores. Community modules provide Redis, MongoDB, PostgreSQL, and MySQL stores. Redis is recommended for clustered Node-RED setups.

What is the difference between flow, global, and node context?
  • flow: Per-flow tab, cleared on deploy. Good for temporary flow state.
  • global: Shared across all flows. Can be persistent with a file store.
  • node: Private to a single node instance. Useful for per-node counters.
Does context persist across Node-RED version upgrades?

Yes — file-based context is stored outside the Node-RED installation directory in .node-red/context/. Upgrades do not touch it. Always back up this directory before major upgrades.


DodaTech — context that remembers between restarts.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro