XGROUP command in Redis is used to manage consumer groups within a stream. It's ideal for use cases where you need to distribute the processing of data across different consumers. This is common in distributed systems where load balancing and failure tolerance are critical.
We'll be using the Go
go-redis/redis package for our examples. Ensure it's installed by running
go get github.com/go-redis/redis.
In this example, we connect to our Redis instance and use the
XGroupCreateMkStream function to create a new consumer group named
mygroup on the
mystream stream. The
$ guarantees that only new messages will be delivered to consumers of this group.
XGROUP CREATEcommand. If the consumer group already exists, an error will be returned.
0-0) when creating a consumer group - whether you want to process only new messages or all existing and future messages.
MKSTREAMoption will result in an error. If you're not sure whether the stream exists, use
What does the
$ mean in
XGROUP CREATE command?
$ is an ID which represents the latest ID in the stream. It means that the consumer group will only receive new messages that are added to the stream after the creation of the group.
Can I add a consumer to a group directly?
No, consumers are not added directly to a group. When a consumer reads from a group using
XREADGROUP, it gets associated with that group.