Title: Database Ticket Servers
Slug: database-ticket-servers
Date: 2013-03-24 20:23:00
Author: Kartones
Lang: en
Tags: Development, MySQL, Databases, Patterns & Practices
Description: Using 'ticket servers' to manage auto-incremented unique IDs across multiple databases, a common issue when sharding databases.

 <p>When starting to shard databases, one problem that sooner or later will arise is: <b>how to manage auto-incremented unique IDs between multiple databases?</b>.</p> <p>The simplest way that some giants like Etsy or Flickr employ is to use "ticket servers", dedicated database servers with single instances (no replication) that generate the unique ids. </p> <p>It uses a not so well known MySQL statement, "<a href="http://dev.mysql.com/doc/refman/5.0/en/replace.html">REPLACE INTO</a>", which since SQL Server 2008 is also present at MSSQL as "<a href="http://stackoverflow.com/questions/10385933/replace-into-in-sql-server-2008">MERGE INTO</a>". It simply does an INSERT if no row with same value is present, else does a DELETE + INSERT.</p> <p><a href="http://code.flickr.net/2010/02/08/ticket-servers-distributed-unique-primary-keys-on-the-cheap/">Flickr</a> explains everything in great detail, including how to use more than one database for the same "tickets table", so I recommend reading it first.</p> <p><a href="http://www.slideshare.net/jgoulah/the-etsy-shard-architecture-starts-with-s-and-ends-with-hard">Etsy</a> have a more visual and broader sharding talk that summarizes quite simply the process (it's interesting that they have exactly the same code example ;) :</p> <p><a href="https://images.kartones.net/posts/kartonesblog/etsy_ticket_servers.jpg" title="Click to see full screen image"><img src="https://images.kartones.net/posts/kartonesblog/thumbs/t_etsy_ticket_servers.jpg" alt="Etsy ticket server architecture diagram" mce_src="https://images.kartones.net/posts/kartonesblog/thumbs/t_etsy_ticket_servers.jpg"></a></p> <p>Not trivial to think about at first, but really simple concept and quite easy to implement.</p><p><b>UPDATE</b>:</p><p>Thanks to <a href="https://twitter.com/dahernan">@dahernan</a>, a non-DB alternative: </p><ul><li><a href="https://github.com/twitter/snowflake">Snowflake</a> </li></ul>
