<p>I am working on a service (written in Go), which is expected to receive a huge number of requests. As per the architecture, each pod of the service is supposed to serve specific clients. Lets say, if there are 3 pods of this service, the split will be like -> <code>A-H</code>, <code>I-P</code>, <code>Q-Z</code>, where each letter is client's name's first letter.</p>

<p>But if there are 4 pods of this service, then split can be -> <code>A-F</code>, <code>G-N</code>, <code>O-U</code>, <code>V-Z</code>.</p>

<p>Is there a way I can know in Go code how many other replicas are there?</p>

<p>PS: AFAIK, one possibility is to have an <code>environment variable</code> in <code>deployment.yaml</code>. But there are ways where scaling can be done without changing the <code>yaml</code>.</p>

<p>As per the title, the solution is to use <code>StatefulSet</code> where each service is aware of each other and apps can be written in a way that they handle this scenario.</p>

<p>However, for this question, as per the details mentioned, one good solution without using <code>StatefulSet</code> is to create a <code>Service</code> with a <code>sessionAffinity: ClientIP</code>. The requirement as per the details is that subsequent requests must go to a specific pod which served the previous request. This can be configured using <code>sessionAffinity</code> field. <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.15/#servicespec-v1-core" rel="nofollow noreferrer">Check documentation for it here</a> With this, when a new client connects, <code>service</code> will select a <code>pod</code> after doing load-balancing. Post that, all subsequent requests will go to that <code>pod</code> only. This can be configured further using <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.15/#sessionaffinityconfig-v1-core" rel="nofollow noreferrer"><code>SessionAffinityConfig</code></a>.</p>
