XGROUP CREATECONSUMER
Introduction and Use Case(s)
The XGROUP CREATECONSUMER command in Redis is used to create a new consumer in an existing consumer group. This command is particularly useful when dealing with Redis Streams, where consumers are part of a consumer group that processes messages from the stream. Typical use cases include scaling out message processing by adding new consumers to distribute the workload.
Syntax
1. XGROUP CREATECONSUMER <key> <groupname> <consumername>
Parameter Explanations
<key>: The name of the stream where the consumer group exists.<groupname>: The name of the consumer group to which the new consumer will be added.<consumername>: The unique name of the consumer to be created within the consumer group.
Return Values
The command returns an integer:
(integer) 1if the consumer was successfully created.(integer) 0if the consumer already exists in the specified group.
Code Examples
Creating a new consumer in an existing consumer group using the CLI:
1. dragonfly> XGROUP CREATE mystream mygroup $ MKSTREAM
2. OK
3. dragonfly> XGROUP CREATECONSUMER mystream mygroup consumer1
4. (integer) 1
5. dragonfly> XGROUP CREATECONSUMER mystream mygroup consumer1
6. (integer) 0
7. dragonfly> XINFO GROUPS mystream
8. 1) 1) "name"
9. 2) "mygroup"
10. 3) "consumers"
11. 4) (integer) 1
12. 5) "pending"
13. 6) (integer) 0
14. 7) "last-delivered-id"
15. 8) "0-0"
Best Practices
- Ensure the stream key and consumer group exist before creating a consumer.
- Use meaningful consumer names for easier management and debugging.
Common Mistakes
- Attempting to create a consumer in a non-existent consumer group or stream.
- Using duplicate consumer names within the same consumer group.
FAQs
What happens if I try to create a consumer in a non-existent consumer group?
The command will return an error since the consumer group must exist prior to adding consumers.
Can I create multiple consumers with the same name in different consumer groups?
Yes, consumer names need to be unique only within the same consumer group.
