This project is no longer maintained.
It has been moved to a new repository 👉
➡️ Siphion/node-red-cluster
A Node-RED context store implementation using Valkey/Redis for clustering support.
node-red-context-valkey provides a context storage backend for Node-RED that stores all context data in Valkey/Redis. This enables true clustering of Node-RED instances, where multiple instances can share the same context data in real-time.
- Always stores in Valkey/Redis - No optional parameters, ensuring cluster consistency
- Atomic operations - Uses Lua scripts for nested property updates to prevent race conditions
- Shared configuration - Uses the same
valkeyconfig object asnode-red-storage-valkey - High availability - Supports Redis Sentinel for automatic failover
- Optional compression - Automatically compresses large values (>1KB) to save memory
- Full context API - Supports global, flow, and node-scoped contexts
- TypeScript - Written in TypeScript with full type definitions
npm install node-red-context-valkeyAdd to your Node-RED settings.js:
module.exports = {
// Optional: Storage API using Valkey
storageModule: require('node-red-storage-valkey'),
// Context API configuration
contextStorage: {
default: {
module: require('node-red-context-valkey')
}
},
// Shared Valkey configuration (used by both storage and context modules)
valkey: {
host: 'localhost',
port: 6379,
password: 'your-password', // Optional
db: 0, // Optional, default: 0
keyPrefix: 'nodered:', // Optional, default: 'nodered:'
enableCompression: true, // Optional, default: false
}
}Note: Both the storage module and context module automatically use the valkey configuration object from settings.js. There's no need to specify config: "valkey" explicitly.
module.exports = {
contextStorage: {
default: {
module: require('node-red-context-valkey')
}
},
valkey: {
sentinels: [
{ host: 'sentinel1.example.com', port: 26379 },
{ host: 'sentinel2.example.com', port: 26379 },
{ host: 'sentinel3.example.com', port: 26379 }
],
name: 'mymaster', // Sentinel master name
password: 'your-password',
keyPrefix: 'nodered:',
enableCompression: true,
}
}module.exports = {
contextStorage: {
default: "memory", // Use memory for default
valkey: {
module: require('node-red-context-valkey'),
config: {
host: '127.0.0.1',
port: 6379,
password: 'your-password',
db: 0,
keyPrefix: 'nodered:',
enableCompression: true,
// ioredis connection options
retryStrategy: (times) => {
const delay = Math.min(times * 50, 2000);
return delay;
},
connectTimeout: 10000,
maxRetriesPerRequest: 3,
}
}
}
}Once configured, use Node-RED's context API normally. All operations automatically go to Valkey/Redis:
// Global context (shared across all flows and instances)
global.set("userCount", 42);
const count = global.get("userCount");
// Flow context (shared across nodes in the same flow)
flow.set("temperature", 72.5);
const temp = flow.get("temperature");
// Node context (private to this node instance)
context.set("counter", 1);
const counter = context.get("counter");
// Nested properties
global.set("user.profile.name", "Alice");
const name = global.get("user.profile.name");
// Get multiple keys at once
const [name, age] = global.get(["user.name", "user.age"]);- Global - Shared across all flows and all Node-RED instances in the cluster
- Flow - Shared across all nodes in a flow tab, and across all instances in the cluster
- Node - Private to a specific node, but shared across all instances in the cluster
This module works seamlessly with node-red-storage-valkey to provide a complete clustering solution for Node-RED:
- Storage Module (node-red-storage-valkey) - Manages flows, credentials, and settings with automatic synchronization across instances
- Context Module (this package) - Handles shared context data across all instances
Together, these modules enable:
- Shared Flows and Credentials - Storage module keeps all instances in sync
- Shared Context Data - This module ensures consistent state across instances
- Auto-reload on Flow Updates - Workers automatically reload when admin saves flows
- True Horizontal Scaling - Scale to any number of instances seamlessly
For optimal clustering, configure different Node-RED instances based on their role:
Admin Node (with editor access):
module.exports = {
storageModule: require('node-red-storage-valkey'),
contextStorage: {
default: {
module: require('node-red-context-valkey')
}
},
valkey: {
host: 'localhost',
port: 6379,
keyPrefix: 'nodered:',
publishOnSave: true, // Broadcast flow changes
enableCompression: true
}
}Worker Nodes (load-balanced instances):
module.exports = {
storageModule: require('node-red-storage-valkey'),
contextStorage: {
default: {
module: require('node-red-context-valkey')
}
},
httpAdminRoot: false, // Disable editor interface
valkey: {
host: 'localhost',
port: 6379,
keyPrefix: 'nodered:',
subscribeToUpdates: true, // Listen for flow updates
enableCompression: true
}
}Key Points:
- Admin nodes use
publishOnSave: trueto broadcast changes via pub/sub - Worker nodes use
subscribeToUpdates: trueto receive and auto-reload flows - Worker nodes should have
httpAdminRoot: falseto disable the editor - Both modules automatically use the shared
valkeyconfiguration object
When running multiple Node-RED instances connected to the same Valkey/Redis:
- Shared State - All instances see the same context data in real-time
- Load Balancing - Distribute work across instances while maintaining state
- High Availability - If one instance fails, others continue with the same data
- Horizontal Scaling - Add more instances without data inconsistency
- Automatic Synchronization - Workers auto-reload when admin updates flows
// In a function node receiving API requests
// All instances share the same counter in Redis
let count = global.get("api_requests") || 0;
count++;
global.set("api_requests", count);
msg.payload = {
instance: process.env.INSTANCE_ID,
totalRequests: count
};
return msg;| Option | Type | Default | Description |
|---|---|---|---|
host |
string | '127.0.0.1' |
Redis/Valkey host |
port |
number | 6379 |
Redis/Valkey port |
password |
string | undefined |
Redis/Valkey password |
db |
number | 0 |
Redis database number |
keyPrefix |
string | 'nodered:' |
Prefix for all Redis keys |
enableCompression |
boolean | false |
Enable gzip compression for values >1KB |
sentinels |
array | undefined |
Redis Sentinel configuration |
name |
string | undefined |
Sentinel master name |
timeout |
number | 5000 |
Redis operation timeout (ms) |
All ioredis options are also supported.
Context data is stored in Redis with the following key pattern:
{keyPrefix}context:{scope}:{key}
Examples:
nodered:context:global:userCountnodered:context:flow:a1b2c3d4:temperaturenodered:context:node:x5y6z7w8:counter
Values are stored as JSON. Nested properties are stored as complete JSON objects with atomic updates using Lua scripts.
Enable compression for applications with large context values:
valkey: {
enableCompression: true // Compress values > 1KB
}Trade-offs:
- Pros: Reduces Redis memory usage by 60-90% for large objects
- Cons: Additional CPU overhead for gzip compression/decompression
ioredis automatically manages connection pooling. For high-traffic deployments, consider:
valkey: {
maxRetriesPerRequest: 3,
enableReadyCheck: true,
enableOfflineQueue: true,
}If Node-RED fails to connect to Redis:
- Check Redis is running:
redis-cli ping - Verify connection details in
settings.js - Check Node-RED logs for error messages
- Ensure firewall allows connection to Redis port
- Verify
contextStorageconfiguration insettings.js - Check that
defaultis set to"valkey"or flows use explicit store name - Confirm Redis is not configured with volatile eviction policy
If Redis memory grows too large:
- Enable compression:
enableCompression: true - Implement periodic cleanup using
clean()method - Use appropriate Redis
maxmemoryand eviction policies - Monitor with
redis-cli --bigkeys
npm install
npm run buildnode-red-context-valkey/
├── src/
│ ├── index.ts # Entry point
│ ├── context.ts # ValkeyContext implementation
│ └── types.ts # TypeScript interfaces
├── dist/ # Compiled output (ESM + CommonJS)
├── package.json
└── README.md
Apache-2.0
- node-red-storage-valkey - Storage API implementation for Valkey/Redis
- Node-RED - Low-code programming for event-driven applications
Contributions are welcome! Please feel free to submit a Pull Request.
For issues and questions:
- GitHub Issues: https://github.com/Siphion/node-red-context-valkey/issues
- Node-RED Forum: https://discourse.nodered.org/