nodejs-snowflake icon indicating copy to clipboard operation
nodejs-snowflake copied to clipboard

Concurrent bulk generation of Snowflake IDs from same machine is failing

Open anubhav-pandey1 opened this issue 11 months ago • 9 comments

If we try to bulk generate multiple snowflake IDs concurrently from the same machine, all the IDs are same:

const generateSnowflakeId = (machineId = null) => {
  const config = {
    instance_id: machineId,
    custom_epoch: new Date("2001-01-01").getTime(),
  };
  const uid = new Snowflake(config);
  const snowflakeId = uid.getUniqueID();
  return snowflakeId;
};

const COUNT = 10;
const CURRENT_MACHINE_ID = 1; // Assumed
const snowflakeIds = [];
for (let i = 0; i < COUNT; i++) {
  const snowflakeId = generateSnowflakeId(CURRENT_MACHINE_ID);
  snowflakeIds.push(objectId);
}

All the IDs are the same. Am I doing something incorrectly?

Node.js version: 16.17 and 18.17 Package version: 2.0.1

anubhav-pandey1 avatar Feb 26 '24 15:02 anubhav-pandey1