I'm developing a NATS based solution with deterministic subject partitioning, I use this type of mapping:
service.* --> service.*.<number of partition>
Now I need a way to subscribe only one of my replicas per partition, what's the right way to do that?
I was thinking about K8s ordinal index, but all the replicas should be stateless.
One way to ensure that only one replica per partition subscribes to messages is to use a queue group subscription in NATS. When multiple subscribers are part of the queue group. Only one of them will receive each message. This allows you to ensure that only one replica per partition processes messages at a time.
Example:
Assign a unique identifier to each replica such as pod name or other unique identifier.
If there are multiple subscribers in the queue group. NATS will distribute messages to them in a round-robin fashion.
If there is only one replica subscriber in the queue group; then it will receive all the messages for the partition.
By above methods only one replica per partition will receive messages and even if it goes down, NATS will automatically reassign remaining users to the group.
For more information please check this official page.