2

I have the problem that I will have multiple servers that perform jobs. They all request a joblist from the same API and need to identify them with a unique id. So that I know which server is running which jobs and have no distribution collisions etc.

I am now trying to figure out a way to generate a unique server id. It is not critical that it stays the same forever. But it should be unique.

I noticed the PHP build time in phpinfo() but couldn't find any documentation about it. What if php is installed as a binary package? It will probably not change if two servers use the same package.

I would prefer some sort of mother board serial number or something.

A yeah, and it should be portable. So I would rather like to avoid something like calling a shell command using shell_exec or so.

Going for the external IP could also be an option. But how to reliably determine the "external" ip adress? what if there are more? What if its natted? Then i would need a combination of external and internal adress.

I could request the API one time and ask it to give me the external ip adress and combine. But this is going way to complicated and extensive...

Do you have any recommendations on this?

5
  • Is there something here? php.net/manual/en/reserved.variables.server.php
    – Rimian
    Commented Feb 23, 2011 at 11:40
  • possible duplicate of How to generate unique ids for my scaled Servers with PHP?
    – Mark Baker
    Commented Feb 23, 2011 at 11:49
  • no, its not a duplicate of that @Mark Baker. because i don't need to generate just a uniqu id, i have the requirement that its unique per system and stays the same when applied the same algorithm again. Commented Feb 23, 2011 at 12:08
  • You wrote "It is not critical that it stays the same forever" - how does it match the 'stays the same' statement? Commented Feb 23, 2011 at 19:13
  • "not forever" does not mean every time. it needs to stay the same for the process to work. but if it changes, some jobs get "lost". just like if a server breaks down. then a cleanup job fetches them. the application will work just fine. but it will not work or only with a huge overhead if it changes too much. Commented Feb 23, 2011 at 20:21

3 Answers 3

1

I would say give them an Id.

Your server is the master, build a protocol, if the server is not giving you is unique Id in the sent query then build a new record for this new server and give him the record id. Then Your protocol on the joblist query state that the client server should now reuse the given Id. if he does not he will get a new id each time (which is unique).

You'll face the problem, maybe, of a growing number of Id, then add a TTL to each Id, and perform some cleanup (or simply wipe out this table sometimes). In your protocol you should now at a way to tell your client server his given Id has been invalidated and that you give him a new one.

If you really want to delegate this 'unique Id' management to the clients, then ask them to generate a random uid, and prey that it do not collide (very low %, but it can happen, but maybe not before you die). You'll have to store it on your sied, and manage the growing number of Id as well. But there you will simply take the server given Id as a new one after wipeout, you don't need to ask client to rebuild a new Id. Simplier.

1
  • thats what i am doing right now :) i cache them in apc in the application. thanks for the answer and covering more eventualities. it helped me. i just wonder whether there is a nice way to generate something from the system environment... Commented Feb 23, 2011 at 12:06
0

uniqid() gives you a unique ID for which chances are pretty rare that IDs collide.

2
  • i think you mis understood the question. i need a unique id that identifies the system. the chances of uniqid colliding in the same server a zero. therefore it has a prefix parameter. Commented Feb 23, 2011 at 12:04
  • Since uniqid() relies on microseconds, chances are also rare that it collides between multiple servers. Generating the unique ID on a server, when it asks for jobs for the first time, should be sufficiently unique. Or am I wrong? You can maybe also use the hostname as a prefix, if that is set properly.
    – tobyS
    Commented Feb 23, 2011 at 12:08
0

You can use Mac Address as unique id for your servers: https://stackoverflow.com/a/1420402/318765

1
  • <?php /*Getting MAC Address using PHP * Md. Nazmul Basher * nazmulb.wordpress.com/2008/07/04/getting-mac-address-using-php */ ob_start(); // Turn on output buffering system(‘ipconfig /all’); //Execute extrnl prog to display output $mycom=ob_get_contents(); // Capture the output into a variable ob_clean(); // Clean(erase)the output buffer $findme = “Physical”; $pmac = strpos($mycom, $findme); // Find the position of Physical text $mac=substr($mycom,($pmac+36),17); // Get Physical Address echo $mac; ?> I havent tested it. Just searched on internet and found this code. Commented Feb 23, 2011 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.