The XGROUP
command in Redis is used when working with stream data structures. It's particularly useful while building consumer groups, which are a powerful way to distribute stream processing among multiple consumers:
$redis = new Redis(); $redis->connect('127.0.0.1', 6379); // Create a stream if it doesn't already exist $redis->xAdd('mystream', '*', ['field' => 'Hello']); // Create a new consumer group $redis->rawCommand('XGROUP', 'CREATE', 'mystream', 'mygroup', '$', 'MKSTREAM');
In the above example, we first connect to Redis using PHP's Redis extension. Then we ensure a stream exists by adding an item to it. Finally, we create a new consumer group called 'mygroup' for the 'mystream' stream, starting from the earliest item ($
).
$redis = new Redis(); $redis->connect('127.0.0.1', 6379); // Set the last delivered ID for a consumer group $redis->rawCommand('XGROUP', 'SETID', 'mystream', 'mygroup', '0-0');
This example sets the last delivered ID of the 'mygroup' consumer group on 'mystream' to '0-0', which means the next read will start from the beginning of the stream.
SETID
subcommand.Q: Can I add consumers to a consumer group in PHP Redis?
A: Consumers are not explicitly added to a consumer group. Rather, when a consumer reads from a group using XREADGROUP
, it is implicitly added if it doesn't already exist.
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.