Instagram: http://instagram-engineering.tumblr.com/post/10853187575/sharding-ids-at-instagram

Flickr: http://code.flickr.net/2010/02/08/ticket-servers-distributed-unique-primary-keys-on-the-cheap/

Ticket Server based on database (auto-increment in DB, ACID guarantee)

Instagram:

id should be chronologically sorted;

Sharding: several thousands "logical" shard, and hosted in far fewer physical shards => makes it much easier to move logical shards out to new physical shards.

Id genration : PL/PGSQL stored procedure to generate next_id() in database (transactionally), which contains : 41bits for current time in mililliseconds, 13 bits for logical shards, 10 bits for auto-incrementing sequence % 1024 => at most 1024 IDs per shard per millisecond;

Given the ID also encodes the shard, it's easy to find out shard for a primary key id

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; CREATE TABLE insta5.our_table (
"id" bigint NOT NULL DEFAULT insta5.next_id(),
...rest of table schema...
)

Flickr : http://code.flickr.net/2010/02/08/ticket-servers-distributed-unique-primary-keys-on-the-cheap/

/* The Tickets64 schema looks like: */

CREATE TABLE `Tickets64` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`stub` char(1) NOT NULL default '',
PRIMARY KEY (`id`),
UNIQUE KEY `stub` (`stub`)
) ENGINE=MyISAM
SELECT * from Tickets64 returns a single row that looks something like:
+-------------------+------+
| id | stub |
+-------------------+------+
| 72157623227190423 | a |
+-------------------+------+

/* When I need a new globally unique 64-bit ID I issue the following SQL: */ REPLACE INTO Tickets64 (stub) VALUES ('a');
SELECT LAST_INSERT_ID();

In order not to make it a single-point-of-failure:

TicketServer1:
auto-increment-increment = 2
auto-increment-offset = 1 TicketServer2:
auto-increment-increment = 2
auto-increment-offset = 2

presentation on this topic:

http://media.postgresql.org/sfpug/instagram_sfpug.pdf 

Sharding & IDs at Instagram, Flickr ID generation的更多相关文章

  1. Sharding & IDs at Instagram(转)

    英文原文:http://instagram-engineering.tumblr.com/post/10853187575/sharding-ids-at-instagram 译文:http://ww ...

  2. [解决]Linux Tomcat启动慢--Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [236,325] milliseconds

    一.背景 今天部署项目到tomcat,执行./startup.sh命令之后,访问项目迟迟加载不出来,查看日志又没报错(其实是我粗心了,当时tomcat日志还没打印完),一开始怀疑是阿里云主机出现问题, ...

  3. Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [33,755] milliseconds.

    刚部署好程序,第一次登录时,加载非常得慢,查看log日志发现:Creation of SecureRandom instance for session ID generation using [SH ...

  4. WARNING [main] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [] milliseconds.

    编译安装tomcat-native和tomcat-deamon以后,发现toomcat启动很慢,好久才有响应.以下日志供参考: 11-Sep-2017 12:19:28.102 INFO [main] ...

  5. 【解决】Linux Tomcat启动慢--Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [236,325] milliseconds

    一.背景 今天部署项目到tomcat,执行./startup.sh命令之后,访问项目迟迟加载不出来,查看日志又没报错(其实是我粗心了,当时tomcat日志还没打印完),一开始怀疑是阿里云主机出现问题, ...

  6. Mismatched locale IDs. The component locale ID (2052) does not match the connection manager locale ID (2057)

    Snapshot: When using the 'Flat File Source' and 'OLE DB Destination' or something else components to ...

  7. tomcat启动时间5分钟左右org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [342,445] milliseconds.

    org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance ...

  8. 根据flickr id 下载图片

    #coding=utf-8 import flickrapi import requests import os n=1 flickr=flickrapi.FlickrAPI('*********** ...

  9. 创建App IDs时选择App ID Prefix才能勾选push notifications

随机推荐

  1. Python—发邮件总结

    来自: http://my.oschina.net/jhao104/blog/613774 1.登录SMTP服务器 首先使用网上的方法(这里使用163邮箱,smtp.163.com是smtp服务器地址 ...

  2. 点击textbox弹出对话框,返回弹出对话框的值

    主要是在父页面使用 function PopupWindow() {            window.open(url, "", "status=no,resizab ...

  3. 开源监控系统Prometheus介绍

    前言 Prometheus是CNCF的一个开源项目,Google BorgMon监控系统的开源版本,是一个系统和服务的监控系统.周期性采集metrics指标,匹配规则和展示结果,以及触发某些条件的告警 ...

  4. mongo-connector导入数据到Es

    要求 基于mongo-connector同步数据,必须要求mongodb为复制集架构,原因是此插件是基于oplog操作记录进行数据同步的:而oplog可以说是Mongodb Replication的纽 ...

  5. 九度OJ 1013:开门人和关门人 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5052 解决:2563 题目描述:     每天第一个到机房的人要把门打开,最后一个离开的人要把门关好.现有一堆杂乱的机房签到.签离记录,请 ...

  6. mooc课程mit6.00.1x--problem set1解决方法

    counting vowels: 计算字符串中含有元音字母aeiou的数量 char = 'azcbobobegghakl' num = 0 #利用in方法直接查找字符串char中含有的元音字母数量 ...

  7. cordova 插件创建

    peng@PENG-PC /E/_My_File_____/_work/MyCode/myCode/cordova-workspace/plugman-test/ABCD $ npm install ...

  8. c语言高速推断一个数是偶数还是奇数

    #include <stdio.h> int main() { int a; while(1) { printf("please input the number:\n" ...

  9. Android 进程增加存活率

    引用 : http://geek.csdn.net/news/detail/68515

  10. 【Leetcode-easy】Remove Nth Node From End of List

    思路1:设置两个指针p1,p2指向表头,p1先走n步.再两个指针同时走.当p1指针指到链表尾部时,P2指针已经在需要删除节点的前一位.一定要注意一些细节. class ListNode { int v ...