Flickr 的 Unique ID 是一個非常實際的解決問題方式,稱作 Ticket Servers 。
使用 MySQL 來達成 master -master 與 sharding 。
在使用一個table
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 |
+-------------------+------+
先插入一個ID值使用
REPLACE INTO Tickets64 (stub) VALUES ('a');
SELECT LAST_INSERT_ID();
就會得到下一個ID然後透過兩臺不同的設定奇偶數分開避免SPOF
TicketServer1:
auto-increment-increment = 2
auto-increment-offset = 1
TicketServer2:
auto-increment-increment = 2
auto-increment-offset = 2
Ticket Servers: Distributed Unique Primary Keys on the Cheap | code.flickr.com http://code.flickr.net/2010/02/08/ticket-servers-distributed-unique-primary-keys-on-the-cheap/
沒有留言:
張貼留言