How Flickr generate globally unique IDs in a distributed master-master setup
Flickr used Ticket Servers
Ticket servers were MySQL-backed Unique ID generation services.
Ticket Tables were either generating 32-bit IDs or 64-bit IDs.
Ticket servers provide globally unique integers for primary keys in Flickr's distributed setup.
They were crucial for activities like sharding and master-master replication.
Avoiding GUIDs in Favor of Ticket Servers
Flickr opted for ticket servers over GUIDs due to size and indexing concerns in MySQL.
Ticket servers provide sequentiality for efficient querying and database performance.
Role of Consistent Hashing in Shard Management
Consistent hashing rings like Amazon's Dynamo aid in handling GUID/sharding complexities.
MySQL's design favors fast random reads and a small Index size helps a lot by allowing it to keep the index in memory.
Technology
MySQL auto-incrementing columns cannot guarantee uniqueness across databases.
Flickr used MYSQL's `REPLACE INTO` command initially and then switched to MYSQL's `INSERT ON DUPLICATE KEY UPDATE`.
The idea is simple, use a small character as a unique key and try to insert a new row with it, a new auto-incremented ID will be generated based on provided auto-increment-increment and auto-increment-offset values.
Create a Ticket Table like below
CREATE TABLE `Ticket64` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`stub` char(1) NOT NULL default '',
PRIMARY KEY (`id`),
UNIQUE KEY `stub` (`stub`)
) ENGINE=InnoDB
The "stub" being unique ensures a fast `Auto Increment Operation`. In `Ticket64`, there will be only one row (small table).
Generate a new ID
REPLACE INTO Ticket64 (stub) VALUES ('a');
SELECT LAST_INSERT_ID();
Strategies for High Availability with Ticket Servers
High availability in ticket servers is ensured by running two servers with divided ID spaces.
Load balancing and round-robin between servers help manage downtime scenarios.
Dividing ID space
-- TicketServer1 - Odd Number Server
auto-increment-increment = 2
auto-increment-offset = 1
-- TicketServer2 - Even Number Server
auto-increment-increment = 2
auto-increment-offset = 2
Usage of Sequences
Ticket servers manage sequences for Photos, Accounts, OfflineTasks, and Groups.
Different sequences are allocated based on the usage frequency and importance of each entity.
Reference
https://code.flickr.net/2010/02/08/ticket-servers-distributed-unique-primary-keys-on-the-cheap/