<p>I'm developing a NATS based solution with deterministic subject partitioning, I use this type of mapping:</p>
<blockquote>
<p>service.* --&gt; service.*.&lt;number of partition&gt;</p>
</blockquote>
<p>Now I need a way to subscribe only one of my replicas per partition, what's the right way to do that?</p>
<p>I was thinking about K8s ordinal index, but all the replicas should be stateless.</p>

alexHX12
<p>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.</p>
<p>Example:</p>
<ol>
<li><p>Assign a unique identifier to each replica such as pod name or other unique identifier.</p>
</li>
<li><p>If there are multiple subscribers in the queue group. NATS will distribute messages to them in a round-robin fashion.</p>
</li>
<li><p>If there is only one replica subscriber in the queue group; then it will receive all the messages for the partition.</p>
</li>
</ol>
<p>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.</p>
<p>For more information please check this <a href="https://docs.nats.io/using-nats/developer/receiving/queues" rel="nofollow noreferrer">official page</a>.</p>
