Skip to content

Instantly share code, notes, and snippets.

@koorchik
Created September 7, 2023 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koorchik/80cab629555650ab8111f1d3a775f0e4 to your computer and use it in GitHub Desktop.
Save koorchik/80cab629555650ab8111f1d3a775f0e4 to your computer and use it in GitHub Desktop.
Testing UUID distribution if used as sharding key
import crypto from "crypto";
const NUMBER_OF_SHARDS = 100;
const NUMBER_OF_ENTRIES = 100_000;
const shards = [];
for (let i = 0; i < NUMBER_OF_ENTRIES; i++) {
const numericUUID = BigInt("0x" + crypto.randomUUID().replace(/-/g, ""));
const shardId = Number(numericUUID % BigInt(NUMBER_OF_SHARDS));
if (shards[shardId]) {
shards[shardId]++;
} else {
shards[shardId] = 1;
}
}
console.log(shards);
// Results:
// [
// 1048, 988, 1002, 1003, 977, 1023, 1009, 979, 1068, 1026,
// 1001, 1005, 975, 980, 1005, 994, 953, 1021, 1032, 986,
// 988, 1031, 1004, 1051, 1004, 1017, 975, 1044, 1011, 936,
// 1055, 1006, 940, 1052, 1009, 1021, 986, 980, 1026, 1018,
// 1035, 974, 1002, 994, 958, 959, 1004, 1020, 1022, 938,
// 1064, 984, 985, 999, 1007, 1001, 1029, 979, 1062, 935,
// 953, 1054, 1030, 992, 1007, 1006, 999, 986, 988, 1002,
// 997, 997, 958, 928, 1049, 983, 995, 972, 970, 986,
// 1029, 1003, 968, 1036, 1040, 1043, 1019, 1001, 966, 932,
// 1021, 942, 1038, 936, 977, 977, 1011, 1021, 1013, 995
// ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment