Giter VIP home page Giter VIP logo

Comments (9)

broofa avatar broofa commented on July 21, 2024 12

tl;dr: No guarantees, especially in the legal sense (see all-caps text).

Expecting IDs to be unique "at any point of space and time", is an unrealistic standard. It's not practical to build such a solution. It requires a source of uniqueness that, as you expand the context in which you need your IDs to be unique, becomes ever more illusory. _Thus, the goal of node-uuid is not to provide universally unique ids but, rather, to faithfully implement the industry accepted methods for generating UUIDs, as defined in RFC 4122_. Those methods are known to be imperfect, but they are the best we have to work with.

Having said that, to your question about uuids in the context of node.js clusters ...

Where v4 UUIDs are concerned, these are just randomly generated values. Thus, they're only probabilistically unique. For any two ids there is a theoretical 0.00000000000000000000000000000000002% chance of collision. But the actual probability depends on the quality of the random number source. On node.js, this is crypto.randomBytes(), which is the best we have available, but you'll have to decide if that's "good enough".

v1 IDs are timestamp based. What makes them interesting in the context of a node cluster is their "node" field. The node is a value unique to the system generating the IDs. RFC4122 specifies that this should be the hosts MAC address... but in thinking about this, I'm not sure that's a good thing. Cluster processes would (I assume) all share the same MAC address, which would actually increase, not decrease, the risk of collision. Fortunately the spec goes on to state that if the MAC address isn't available (and on node.js it isn't... at least, not easily) then the node field should be randomly generated, which is what node-uuid does. So we avoid that little pitfall. But it also means there's a 0.0000000000004% chance that any two cluster processes will have the same node value. Should that happen, then it just comes down to whether or not they're generating IDs at during the same system clock tick. :-/

If you want to insure UUID uniqueness across your cluster processes, then I would suggest using v1({node:aUniqueVal}), where aUniqueVal is some value you know to be unique to each cluster process.

from uuid.

nodesocket avatar nodesocket commented on July 21, 2024 3

In v1 Could we perhaps replace the MAC address with the node.js pid? Wouldn't that help with clusters?

For example replace MAC address with process.pid and pad the rest with random.

from uuid.

enrichz avatar enrichz commented on July 21, 2024

+1 for the awesome explanation, thanks you.

from uuid.

thosil avatar thosil commented on July 21, 2024

@nodesocket not sure it's a good idea, if you run node.js inside a docker its pid will always be 1.

from uuid.

tmorehouse avatar tmorehouse commented on July 21, 2024

Onecould grab the first non 'internal' interface's IPV4 address from os.networkInterfaces(), use that as the first 4 butes of the node ID, and then use the process.pid to generate the last two bytes:

var addr = '1.2.3.4.'; // say from os.networkInterfaces().eth0[0].address
var pid = process.pid;
var nodeid = [
    ...addr.split('.').map(function(n){return parseInt(n,10)}),
     (pid >>> 8) & 0xff,
     pid & 0xff
];
// nodeid is now something like [ 0x01, 0x02, 0x03, 0x04, 0x56, 0x78]
var u = uuid.v1({node:nodeid});

from uuid.

broofa avatar broofa commented on July 21, 2024

@tmorehouse IP-based UUIDs introduces the requirement that you shouldn't have systems on LANs that assign similar IP ranges.

from uuid.

tmorehouse avatar tmorehouse commented on July 21, 2024

Yeah, this would only work for systems that have unique IP's across instances, and across fork()ed processes on those instances

But the nice thing about node-uuid is that you can assign your own 6 byte node ID value any way you like, assuming your are only using these UUIDs (V1) within your application (i.e. no other UUID generation is used).

from uuid.

murphman300 avatar murphman300 commented on July 21, 2024

Hello,

Simple question, using the uuid.v1 method, is it possible to re-create a uuid if you supply the options fields that were used to create it in the first place?

from uuid.

broofa avatar broofa commented on July 21, 2024

@murphman300 https://github.com/kelektiv/node-uuid/blob/master/test/test.js#L77

from uuid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.