Realtime
Lux supports realtime data updates at the database layer. Use KSUB when you want raw key mutation events with pattern matching and zero overhead when unused.
KSUB
Subscribe to mutation events for keys matching one or more patterns. Patterns use glob-style matching: * matches any
sequence of characters and ? matches a single character.
KSUB pattern [pattern ...]127.0.0.1:6379> KSUB user:*
1) "ksubscribe"
2) "user:*"
3) (integer) 1
127.0.0.1:6379> KSUB session:* cache:config
1) "ksubscribe"
2) "session:*"
3) (integer) 2
1) "ksubscribe"
2) "cache:config"
3) (integer) 3KUNSUB
Unsubscribe from one or more patterns. If called with no arguments, unsubscribes from all active patterns.
KUNSUB [pattern ...]Event Format
When a key matching a subscribed pattern is mutated, Lux pushes a message to the connection with the following structure:
["kmessage", pattern, key, operation]For example, if you are subscribed to user:* and another client runs SET user:42 "online",
you receive:
1) "kmessage"
2) "user:*"
3) "user:42"
4) "set"Supported Operations
KSUB fires on any command that mutates a key. The operation field is the lowercase command name:
set, del, hset, hdel, lpush, rpush, lpop, rpop,
sadd, srem, zadd, zrem, tsadd, vset, vdel,
expire, rename, and moreHow KSUB Differs from Redis Keyspace Notifications
Redis has a feature called keyspace notifications that serves a similar purpose, but there are fundamental architectural differences that make KSUB more practical for production use.
| KSUB (Lux) | Keyspace Notifications (Redis) | |
|---|---|---|
| Targeting | Surgical: subscribe to specific key patterns | Global: enable notification classes for all keys |
| Dispatch | Async: events dispatched off the hot path | Synchronous: fires inline with the command |
| Idle cost | Zero overhead when no subscriptions are active | Must be enabled globally, adds overhead to every write |
| Configuration | No config needed, works out of the box | Requires notify-keyspace-events config flag |
How KSUB Differs from Pub/Sub
Lux supports both KSUB and traditional Pub/Sub, but they solve different problems. KSUB is implicit: it fires automatically when keys are mutated. Pub/Sub is explicit: messages are only delivered when a client calls PUBLISH. Use KSUB when you want to react to data changes. Use Pub/Sub when you want to send arbitrary messages between services.
SDK Example
The direct SDK client exposes KSUB for raw keyspace patterns.
import Lux from "@luxdb/sdk";
const lux = new Lux("lux://127.0.0.1:6379");
const keySub = lux.ksub(["user:*"], (event) => {"'{'"}
console.log(event.pattern); // "user:*"
console.log(event.key); // "user:42"
console.log(event.operation); // "set"
{"'}'"});
const orderSub = lux.ksub(["orders:*"], (event) => {"'{'"}
if (event.operation === "hset") {"'{'"}
refreshOrderDashboard(event.key);
{"'}'"}
{"'}'"});
keySub.unsubscribe();
orderSub.unsubscribe();Use Cases
- Live dashboards - Subscribe to metric keys and push updates to the frontend as soon as new data arrives, without polling.
- Cache invalidation - Watch keys that back a cache layer. When the source of truth changes, invalidate immediately instead of waiting for TTL expiry.
- Collaborative features - Subscribe to document or session keys so multiple users see changes in realtime.
- Event-driven workflows - Trigger downstream processing when a key is created or updated. Use KSUB as a lightweight event bus without adding a separate message broker.