Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate unique ID's on a cluster of web servers

In the following scenario:

1 Database 4 Web servers

How do the Web servers generate unique ID's for the database so that they are unique? Yes it's possible to use Auto-increment, but this is too easily crawled/guessed/etc. So auto-increment is currently not an option.

like image 357
Stephane Grenier Avatar asked Nov 29 '22 19:11

Stephane Grenier


1 Answers

Use a UUID (http://www.ietf.org/rfc/rfc4122.txt). Collisions are unlikely, and could be dealt with when they occur by regenerating a new UUID, or they could be prevented by concatenating a unique id for each server (like the mac address): -

StringBuilder sb = new StringBuilder(UUID.randomUUID());
InetAddress address = InetAddress.getLocalHost();
String uid = sb.append(NetworkInterface.getByInetAddress(address).getHardwareAddress());
like image 113
Jim Downing Avatar answered Dec 06 '22 18:12

Jim Downing