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?

Comments

Is there something here? php.net/manual/en/reserved.variables.server.php

Written by Rimian

possible duplicate of How to generate unique ids for my scaled Servers with PHP?

Written by Mark Baker

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.

Written by Joe Hopfgartner

You wrote "It is not critical that it stays the same forever" - how does it match the 'stays the same' statement?

Written by Grzegorz Oledzki

"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.

Written by Joe Hopfgartner

Accepted Answer

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.

Written by regilero
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki