docs/OBSERVE.md
OBSERVE <cmd> <key> [args...] subscribes the client to real-time updates for the observed key:
observe-debounce-period-ms config to combine output of multiple updates and emit just once for debounce periodOnly two top-level commands exist: OBSERVE and UNOBSERVE. No per-command variants like GET.OBSERVE are added.
All observe messages use a 5-element array:
["observe", "fingerprint", "<hex-fingerprint>", "result", <command-result>]
The fingerprint is a CRC64 hash of the command name + arguments (e.g., GET key1). Clients use it to match notifications to subscriptions.
signalModifiedKey(c->db, key) in src/db.cobserveNotifyKeyChange(key, dbid) in src/observe.cobserve_debounce_period > 0, changes are buffered and flushed by a timer; otherwise fired immediatelyexecuteObserveCommand() finds all fingerprints watching the key and pushes updates to subscribed clientsFile: src/observe.c, function findHandlerForCommand() (near the top of the file):
static observeCommandHandler findHandlerForCommand(const char *cmd_name) {
if (!strcasecmp(cmd_name, "GET")) return getCommand;
if (!strcasecmp(cmd_name, "ZRANGE")) return zrangeCommand;
if (!strcasecmp(cmd_name, "HGET")) return hgetCommand; /* ADD HERE */
return NULL;
}
Rule: The handler must be the existing read-only command's proc function. It receives a client whose argv[0] is the command name and argv[1] is the key (same layout as a direct invocation).
Because OBSERVE is a single generic command, no new command functions, server.h declarations, or commands.def entries are required for new supported commands.
| File | What to change |
|---|---|
src/observe.c | Add else if branch in findHandlerForCommand() |
OBSERVE GET key: handled by getCommand, registered at src/observe.c in findHandlerForCommand()OBSERVE ZRANGE key start stop [opts]: handled by zrangeCommandcd ~/workspace/dicedb/dicedb
make
./src/dicedb-server
# Terminal 1 — subscribe
./src/dicedb-cli
> OBSERVE GET user:name
# receives 5-element array immediately, then waits
# Terminal 2 — trigger update
./src/dicedb-cli
> SET user:name Alice
# Terminal 1 receives a new 5-element push message
# Run all observe tests
./runtest --single unit/observe
# Run a specific test
./runtest --single unit/observe --only "OBSERVE GET"
cd src && make test
OBSERVEOBSERVE, UNOBSERVE, PING, QUIT, RESETUNOBSERVE <fingerprint> [fingerprint ...] removes subscriptions; returns count of removed subscriptions