PFCOUNT.WATCH
The PFCOUNT.WATCH
command is a novel feature designed to provide real-time updates to clients whenever the cardinality of a HyperLogLog changes. This enables clients to track approximate set cardinality in real-time as new data is added or merged.
Protocol Support
Protocol | Supported |
---|---|
TCP-RESP | ✅ |
HTTP | ❌ |
WebSocket | ❌ |
Syntax
Parameters
Parameter | Description | Type | Required |
---|---|---|---|
key | The key of the HyperLogLog the client wants to monitor | String | Yes |
Return Value
Condition | Return Value |
---|---|
Command is successful | Returns a message indicating subscription, similar to subscribe |
Behavior
- The client establishes a subscription to the specified HyperLogLog key.
- The current cardinality of the HyperLogLog is sent to the client.
- DiceDB continuously monitors the specified key for changes in its cardinality.
- Updates are triggered by operations such as
PFADD
andPFMERGE
that affect the cardinality. - Whenever the cardinality of the HyperLogLog changes, the updated value is sent to the client.
Errors
Missing Key
- Error Message:
(error) ERROR wrong number of arguments for 'pfcount.watch' command
- Occurs if no key is provided.
- Error Message:
Example Usage
Basic Usage
Here’s an example of using the PFCOUNT.WATCH
command to monitor the approximate cardinality of a HyperLogLog in real-time.
This query does the following:
- Monitors the HyperLogLog key
users:hll
.
When the HyperLogLog is updated using the following commands from another client:
The subscribing client will receive messages similar to the following:
- The
PFADD
commands add elements to theusers:hll
key, incrementally increasing the cardinality. - The
PFMERGE
command mergesusers:hll
withother:hll
, updating the cardinality ofusers:hll
based on the union of both HyperLogLogs.
Notes
Use the PFCOUNT.UNWATCH
command to unsubscribe from a HyperLogLog key. This will stop the client from receiving updates on the cardinality. Refer to the PFCOUNT.UNWATCH command documentation for more details.
Related Commands
The following commands are related to PFCOUNT.WATCH
: