adamhalasz / uniqid

Unique ID Generator
https://adamhalasz.com
617 stars 49 forks source link

Safety when node clusters are used #1

Closed khalilTN closed 8 years ago

khalilTN commented 8 years ago

Hi,

The uniqId is generated from Date.getTime() function, the uniquness is guaranteed when running only one node process, but what about running a node app on multiple clusters, probably, a collision may occure.

Thanks

adamhalasz commented 8 years ago

Hi @khalilTN

You are right. However the purpose of this module is speed and simplicity for single process applications. If you need to make sure the ID's are unique on clusters you can do 2 things:

1) Just use UUID with something like the uuid module. You might still run into generating 2 identical ID's but the chances are astronomical.

2) Centralize. Generate your Unique IDs on a single machine (MACHINE A). Every other machine (MACHINE X, Y, Z) requests unique id's from MACHINE A. Since MACHINE A is running on a single process the uniqueness of the ID's are guaranteed again. The cost is speed and network overhead, but hey clustering is not easy :)

                  ╔════════════════════════════╗
                  ║         MACHINE A          ║
                  ║   (Unique ID Generator)    ║
                  ╚════════════════════════════╝
                                 ▲
                                 │
                                 │
                                 │
                                 │
                                 │
          ┌──────────────────────┼─────────────────────┐
          │                      │                     │
┌─────────┴─────────┐  ┌─────────┴─────────┐  ┌────────┴──────────┐
│ Return "io1yia7t" │  │ Return "io1yia7u" │  │ Return "io1yia7v" │
└─────────┬─────────┘  └─────────┬─────────┘  └────────┬──────────┘
      2:21:31pm              2:21:32pm              2:21:33pm
          │                      │                     │
          ▽                      ▽                     ▽
   ╔═════════════╗        ╔═════════════╗       ╔═════════════╗
   ║  MACHINE X  ║        ║  MACHINE Y  ║       ║  MACHINE Z  ║
   ╠═════════════╣        ╠═════════════╣       ╠═════════════╣
   │  Get ID at  │        │  Get ID at  │       │  Get ID at  │
   │  2:21:30pm  │        │  2:21:30pm  │       │  2:21:30pm  │
   └─────────────┘        └─────────────┘       └─────────────┘

Let me know if you have any other suggestions or questions.

Kind Regards, Adam

adamhalasz commented 8 years ago

Well... I just created v2 which uses the Process ID and the OS's Name. The PID will be always unique to every process within a Single Machine. That solves the multiple process issue.

Now for making sure the ID's are unique even in multi cluster / multi machine environments I use the first 3 letters of the Machine's Name. With v2 you can safely generate unique id's even with many concurrent machines if the name of the machines differ.

Hope this solves your issue :)

khalilTN commented 8 years ago

Hi, I'm glad i contributed to the evolution of this Simple/Perfect Module. Those who seek to use mutli-machine should guarantee that machine names differ. What about using mac addresses (of one of the interfaces, if multiple interfaces), i think the problem will be solved permanently. Thanks !

adamhalasz commented 8 years ago

@khalilTN thanks a lot! As I was mentioning to @Offirmo, you're right the Mac address would solve the uniqueness of the id's permanently however, I also want to make sure the length of the ID's remain as small as possible. I will think more about this over the weekend. :)

Since v2 solves your initial problem, I will close this issue but let me know if you have any other questions/ideas.

Thanks, Adam

adamhalasz commented 8 years ago

@khalilTN v3 now uses mac addresses :)

khalilTN commented 8 years ago

Thanks a lot !