Search code examples
javawebsocketload-balancingscalabilitydistributed-system

Distributed system - How to assign a node ID to each node in distributed system?


How can we assign a unique ID to each node/instance of an application in a distributed environment?

  • We have deployed our app on multiple hosts and requests are distributed across them through a load balancer.
  • We have a requirement where we want to store some data specific to these nodes, and if one of the servers crashes then upon restart we want to delete data specific to only that node, not for others.
  • We can use IP addresses, but that doesn't seem to be a perfect solution. How can we use node ID which is non-changeable so that once the server/node restarts it remains the same?
  • Our application is basically a Java web application which is a kind of web socket server running on tomcat containers.

Solution

  • You could potentially use any of the following:

    • The MAC address of the server when you created the node. MAC addresses are not supposed to be reused / re-assigned to a different server.
    • A random (type 4) UUID. The probability of a type 4 UUID being regenerated is too small to worry about.
    • Any large enough random number generated by a crypto quality PRNG seeded with a decent source of entropy.
    • Anything you want ... in combination with a registry mechanism that ensures that identifiers are not going to be reused.

    Presumably, you store each node's id in the persistent state of the node itself so that it can look it up when it restarts. (If not, you could make it the responsibility of the the node launch / relaunch mechanism to tell it.)