16. 1.1 x 1019
UUIDs we could generate per second
since the Universe began
17. 2.1 x 1027
Olympic swimming pools filled if each
possible value contributed a millilitre
18. Type 1 – MAC address
51063800-dc76-11e1-9fae-001c42000009
• Time component is based on 100
nanosecond intervals since
October 15, 1582
• Most significant bits of timestamp
shifted to least significant bits of
UUID
19. Type 1 – MAC address
• The address (MAC) of the
computer that generated the ID is
encoded into it
• Lexical ordering essentially
meaningless
• Deterministically unique
29. References
Flickr distributed ticket server
http://code.flickr.com/blog/2010/02/08/ticket-servers-distributed-unique-primary-keys-on-
the-cheap/
UUIDs
http://tools.ietf.org/html/rfc4122
How random are random UUIDs?
http://stackoverflow.com/a/2514722/15318
Twitter Snowflake
https://github.com/twitter/snowflake
Boundary Flake
https://github.com/boundary/flake
PHP Cruftflake
https://github.com/davegardnerisme/cruftflake
30. private function mintId64($timestamp, $machine, $sequence)
{
$timestamp = (int)$timestamp;
$value = ($timestamp << 22) | ($machine << 12) | $sequence;
return (string)$value;
}
private function mintId32($timestamp, $machine, $sequence)
{
$hi = (int)($timestamp / pow(2,10));
$lo = (int)($timestamp * pow(2, 22));
// stick in the machine + sequence to the low bit
$lo = $lo | ($machine << 12) | $sequence;
// reconstruct into a string of numbers
$hex = pack('N2', $hi, $lo);
$unpacked = unpack('H*', $hex);
$value = $this->hexdec($unpacked[1]);
return (string)$value;
}
31. public function generate()
{
$t = floor($this->timer->getUnixTimestamp()
- $this->epoch);
if ($t !== $this->lastTime) {
$this->sequence = 0;
$this->lastTime = $t;
} else {
$this->sequence++;
if ($this->sequence > 4095) {
throw new OverflowException('Sequence overflow');
}
}
if (PHP_INT_SIZE === 4) {
return $this->mintId32($t, $this->machine,
$this->sequence);
} else {
return $this->mintId64($t, $this->machine,
$this->sequence);
}
}