Skip to content

Instantly share code, notes, and snippets.

@twolfson
Created February 2, 2017 03:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twolfson/6751d2a1a529eceec604a5c1c7f62668 to your computer and use it in GitHub Desktop.
Save twolfson/6751d2a1a529eceec604a5c1c7f62668 to your computer and use it in GitHub Desktop.
Proof of concept for splitting UUID ranges

We are processing items in bulk and want to have multiple jobs running for them without double sending emails or the like. We can't use offset without potentially having that problem so we are considering splitting UUID ranges.

It should work like:

  • Count total amount of items present
  • Calculate how many batches we'll need for items present
  • Divide UUID range (i.e. 0000... to FFFF...) into potential values
    • For 2 batches, this would be (0000... - 7777...F and 8888... - FFFF...)
  • Kick off jobs with these given ranges so they can process in parallel without potential collision

Proof of concept code for splitting UUID ranges:

// Load in our dependencies
var BigNumber = require('bignumber.js');

// Define the upper bound of UUIDs
// FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF (without -'s)
var x = new BigNumber('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF', 16);

// Split our UUID into its parts and rejoin it
var num = 2;
console.log(x.div(num).ceil().mul(num).toString(16));

// Use `BigNumber.max` to prevent overflowing UUIDs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment