<p>I am using springboot microservices and Kubernetes for our application development and deployments. We are using stateful sets for deployments. We have a need to generate unique identifiers that can be used in the services across pods deployed on nodes across clusters.</p>
<p>I am using twitter snowflake format for the generation of unique Id with K8s stateful sets.
Format:
64 bit java long: 1 bit reserved + 41 bit timestamp + 10 bits nodeId + 12 bits counter</p>
<p>Its a simple problem to solve if we are talking about say 5 replicas deployed on the same cluster (across different nodes). I could easily generate unique orderId that will serve as nodeId in the format above. But the moment I deploy the pods on multiple clusters, it duplicates the nodeIds since it generated similar orders across the clusters.</p>
<p>example: 5 replicas in a single cluster create pods like:
service-0,
service-1,
service-2,
service-3
service-4</p>
<p>I could get the unique pod numbers (0,1,2,3..) that will serve as nodeId in my uniqueId generator and hence I could derive a unique identifier. But if I deploy the application (via stateful set) to 2 clusters, it will generate similar services in the second cluster too:</p>
<p>service-0,
service-1,
service-2,
service-3
service-4</p>
<p>now if my GLB routes my calls randomly across the 10 pods in the 2 cluster, the probability of generating duplicate Unique Id (for calls made during the same milisecond) is pretty high and hence my solution might not work.</p>
<p>Need inputs on how to solve the problem. Any help will be highly appreciated</p>

whizKid
<p>Hi <code>WhizKid</code> I can give a work around for this try to include your cluster name included in the naming convention (e.g: cluster1-service-0) instead of just going with nodeID. Since we have generated unique IDs for each cluster now even if they lie on the same nodes or same servers it won’t become a problem. Hope this helps you.</p>
