Lux Docs

Build on Lux without guessing.

Lux Cloud + OSS core

Product docs for the Lux runtime: tables, cache, vectors, realtime, time series, auth, HTTP, SDK, CLI, and self-hosting.

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.

Syntax
KSUB pattern [pattern ...]
Example
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) 3

KUNSUB

Unsubscribe from one or more patterns. If called with no arguments, unsubscribes from all active patterns.

Syntax
KUNSUB [pattern ...]

Event Format

When a key matching a subscribed pattern is mutated, Lux pushes a message to the connection with the following structure:

Event format
["kmessage", pattern, key, operation]

For example, if you are subscribed to user:* and another client runs SET user:42 "online", you receive:

Delivered event
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:

Mutations
set, del, hset, hdel, lpush, rpush, lpop, rpop,
sadd, srem, zadd, zrem, tsadd, vset, vdel,
expire, rename, and more

How 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)
TargetingSurgical: subscribe to specific key patternsGlobal: enable notification classes for all keys
DispatchAsync: events dispatched off the hot pathSynchronous: fires inline with the command
Idle costZero overhead when no subscriptions are activeMust be enabled globally, adds overhead to every write
ConfigurationNo config needed, works out of the boxRequires 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.

SDK example
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.