Which Shard Made This
Instagram generates 64 bit ids inside Postgres rather than from a separate id service. The layout is 41 bits of milliseconds since a custom epoch, then 13 bits of logical shard id, then 10 bits of per-table sequence taken modulo 1024. Every logical shard is a schema carrying its own copy of this function:
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;
A photo row carries id 8388608001263623. Which logical shard produced it?
Sign in to answer questions and track your progress
Sign In