Skip to content

Instantly share code, notes, and snippets.

@eMaringolo
Created May 24, 2014 03:39
Show Gist options
  • Save eMaringolo/be973dcf03b0783711f1 to your computer and use it in GitHub Desktop.
Save eMaringolo/be973dcf03b0783711f1 to your computer and use it in GitHub Desktop.
8 byte pretty much unique ID generator
"http://instagram-engineering.tumblr.com/post/10853187575/sharding-ids-at-instagram"
| epoch generator ids runLength shardId seqId|
epoch := '01-01-2014' asDateAndTime asUnixTime. "1388545200"
generator := [:seed1 :seed2 |
| id |
id := (DateAndTime now asUnixTime - epoch) bitShift: 64-41.
id := id bitOr: (seed1 bitShift: (64-41-13)).
id := id bitOr: seed2 ].
shardId := 1.
seqId := 1.
ids := IdentitySet new.
runLength := 1000000.
Transcript
show: 'Consecutive sample of ';
show: runLength printString;
cr;
show: (
Time millisecondsToRun: [
runLength timesRepeat: [
ids add: (generator value: shardId value: (seqId \\ 1024) ) ]]) printString,'ms';
tab;
show: 'Unique IDs: ', ids size printString .
CREATE OR REPLACE FUNCTION insta5.next_id(OUT result bigint) AS $$
DECLARE
our_epoch bigint := 1314220021721;
seq_id bigint;
now_millis bigint;
shard_id int := 5;
BEGIN
SELECT nextval('insta5.table_id_seq') %% 1024 INTO seq_id;
SELECT FLOOR(EXTRACT(EPOCH FROM clock_timestamp()) * 1000) INTO now_millis;
result := (now_millis - our_epoch) << 23;
result := result | (shard_id << 10);
result := result | (seq_id);
END;
$$ LANGUAGE PLPGSQL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment