Jay Taylor's notes

back to listing index

Sharding & IDs at Instagram – Instagram Engineering

[web search]
Original source (instagram-engineering.com)
Tags: database id scalability sharding instagram twitter snowflake instagram-engineering.com
Clipped on: 2019-03-13

Sharding & IDs at Instagram

Image (Asset 1/5) alt=DECLARE
our_epoch bigint := 1314220021721;
seq_id bigint;
now_millis bigint;
shard_id int := 5;
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);

And when creating the table, we do:

CREATE TABLE insta5.our_table (
"id" bigint NOT NULL DEFAULT insta5.next_id(),
...rest of table schema...

And that’s it! Primary keys that are unique across our application (and as a bonus, contain the shard ID in them for easier mapping). We’ve been rolling this approach into production and are happy with the results so far. Interested in helping us figure out these problems at scale? We’re hiring!

Discuss this post on Hacker News.