Unique ID generation in distributed systems

Dave Gardner
Dave GardnerSoftware Developer at HailoCab
ID generation



PHP London 2012-08-02
@davegardnerisme
@davegardnerisme




hailoapp.com/dave
(for a £5 discount)
MySQL auto increment



                                    DC 1



               1,2,3,4…
       MySQL              Web App
MySQL auto increment


   • Numeric IDs
   • Go up with time
   • Not resilient
MySQL multi-master replication



                                     DC 1

                1,3,5,7…
        MySQL
                           Web App
                2,4,6,8…
        MySQL
MySQL multi-master replication


   • Numeric IDs
   • Do not go up with time
   • Some resilience
DC 1

DC 2   DC 4
                       DC 5




                              DC 6
         DC 3




                 Going global…
MySQL in multi DC setup

                            DC 1

           1,2,3…   Web
   MySQL
                    App
                                   WAN LINK


                     DC 2




                            ?                 Web
                                              App
Flickr MySQL ticket server

                             DC 1


   Ticket   1,3,5…   Web                 WAN link not required to
   Server            App                          generate an ID

                                      WAN LINK


                      DC 2


                             Ticket   4,6,8…     Web
                             Server              App
Flickr MySQL ticket server


    • Numeric IDs
    • Do not go up with time
    • Resilient and distributed
    • ID generation separated from
      data store
The anatomy of a ticket server

                                        DC


    Web      Web            Web   Web
    App      App            App   App




                   Ticket
                   Server
Making things simpler



                                          DC


    Web      Web        Web      Web
    App      App        App      App

   ID gen    ID gen     ID gen   ID gen
UUIDs


   • 128 bits
   • Could use type 4 (Random) or
     type 1 (MAC address with time
     component)
   • Can generate on each machine
     with no co-ordination
Type 4 – random


                             version

 xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
        variant (8, 9, A or B)



 f47ac10b-58cc-4372-a567-0e02b2c3d479
5.3 x          1036


possible values for a type 4 UUID
1.1 x        1019


UUIDs we could generate per second
     since the Universe began
2.1 x          1027


Olympic swimming pools filled if each
possible value contributed a millilitre
Type 1 – MAC address


 51063800-dc76-11e1-9fae-001c42000009


   • Time component is based on 100
     nanosecond intervals since
     October 15, 1582
   • Most significant bits of timestamp
     shifted to least significant bits of
     UUID
Type 1 – MAC address


   • The address (MAC) of the
     computer that generated the ID is
     encoded into it
   • Lexical ordering essentially
     meaningless
   • Deterministically unique
There are some other options…
No co-ordination needed


Deterministically unique


K-ordered (time-ordered
       lexically)
Twitter Snowflake


   • Under 64 bits
   • No co-ordination (after startup)
   • K-ordered
   • Scala service, Thrift interface,
     uses Zookeeper for configuration
Twitter Snowflake


   41 bits   Timestamp
             millisecond precision, bespoke epoch

   10 bits   Configured machine ID
   12 bits   Sequence number
Twitter Snowflake


   77669839702851584


   = (timestamp << 22)
    | (machine << 12)
    | sequence
Boundary Flake


   • 128 bits
   • No co-ordination at all
   • K-ordered
   • Erlang service
Boundary Flake


   64 bits   Timestamp
             millisecond precision, 1970 epoch

   48 bits   MAC address
   16 bits   Sequence number
PHP Cruftflake


   • Based on Twitter Snowflake
   • No co-ordination (after startup)
   • K-ordered
   • PHP, ZeroMQ interface, uses
     Zookeeper for configuration
Questions?
References
Flickr distributed ticket server
http://code.flickr.com/blog/2010/02/08/ticket-servers-distributed-unique-primary-keys-on-
the-cheap/

UUIDs
http://tools.ietf.org/html/rfc4122

How random are random UUIDs?
http://stackoverflow.com/a/2514722/15318

Twitter Snowflake
https://github.com/twitter/snowflake

Boundary Flake
https://github.com/boundary/flake

PHP Cruftflake
https://github.com/davegardnerisme/cruftflake
private function mintId64($timestamp, $machine, $sequence)
{
    $timestamp = (int)$timestamp;
    $value = ($timestamp << 22) | ($machine << 12) | $sequence;
    return (string)$value;
}

private function mintId32($timestamp, $machine, $sequence)
{
    $hi = (int)($timestamp / pow(2,10));
    $lo = (int)($timestamp * pow(2, 22));

    // stick in the machine + sequence to the low bit
    $lo = $lo | ($machine << 12) | $sequence;

    // reconstruct into a string of numbers
    $hex = pack('N2', $hi, $lo);
    $unpacked = unpack('H*', $hex);
    $value = $this->hexdec($unpacked[1]);
    return (string)$value;
}
public function generate()
{
    $t = floor($this->timer->getUnixTimestamp()
             - $this->epoch);
    if ($t !== $this->lastTime) {
        $this->sequence = 0;
        $this->lastTime = $t;
    } else {
        $this->sequence++;
        if ($this->sequence > 4095) {
             throw new OverflowException('Sequence overflow');
        }
    }

    if (PHP_INT_SIZE === 4) {
        return $this->mintId32($t, $this->machine,
             $this->sequence);
    } else {
        return $this->mintId64($t, $this->machine,
             $this->sequence);
    }
}
1 of 31

Recommended

Event-driven microservices by
Event-driven microservicesEvent-driven microservices
Event-driven microservicesAndrew Schofield
512 views30 slides
Rainbird: Realtime Analytics at Twitter (Strata 2011) by
Rainbird: Realtime Analytics at Twitter (Strata 2011)Rainbird: Realtime Analytics at Twitter (Strata 2011)
Rainbird: Realtime Analytics at Twitter (Strata 2011)Kevin Weil
77K views60 slides
Netflix Global Cloud Architecture by
Netflix Global Cloud ArchitectureNetflix Global Cloud Architecture
Netflix Global Cloud ArchitectureAdrian Cockcroft
71.2K views59 slides
Stream Processing with Apache Kafka and .NET by
Stream Processing with Apache Kafka and .NETStream Processing with Apache Kafka and .NET
Stream Processing with Apache Kafka and .NETconfluent
13.4K views23 slides
Introduction to microservices by
Introduction to microservicesIntroduction to microservices
Introduction to microservicesAnil Allewar
1.1K views46 slides
Reliability of the Cloud: How AWS Achieves High Availability (ARC317-R1) - AW... by
Reliability of the Cloud: How AWS Achieves High Availability (ARC317-R1) - AW...Reliability of the Cloud: How AWS Achieves High Availability (ARC317-R1) - AW...
Reliability of the Cloud: How AWS Achieves High Availability (ARC317-R1) - AW...Amazon Web Services
2.8K views27 slides

More Related Content

What's hot

Kafka error handling patterns and best practices | Hemant Desale and Aruna Ka... by
Kafka error handling patterns and best practices | Hemant Desale and Aruna Ka...Kafka error handling patterns and best practices | Hemant Desale and Aruna Ka...
Kafka error handling patterns and best practices | Hemant Desale and Aruna Ka...HostedbyConfluent
3.2K views18 slides
Scalability, Availability & Stability Patterns by
Scalability, Availability & Stability PatternsScalability, Availability & Stability Patterns
Scalability, Availability & Stability PatternsJonas Bonér
515.7K views196 slides
Flink Forward Berlin 2017: Aris Kyriakos Koliopoulos - Drivetribe's Kappa Arc... by
Flink Forward Berlin 2017: Aris Kyriakos Koliopoulos - Drivetribe's Kappa Arc...Flink Forward Berlin 2017: Aris Kyriakos Koliopoulos - Drivetribe's Kappa Arc...
Flink Forward Berlin 2017: Aris Kyriakos Koliopoulos - Drivetribe's Kappa Arc...Flink Forward
2.6K views67 slides
Exactly-Once Financial Data Processing at Scale with Flink and Pinot by
Exactly-Once Financial Data Processing at Scale with Flink and PinotExactly-Once Financial Data Processing at Scale with Flink and Pinot
Exactly-Once Financial Data Processing at Scale with Flink and PinotFlink Forward
683 views41 slides
Deep Dive into Apache Kafka by
Deep Dive into Apache KafkaDeep Dive into Apache Kafka
Deep Dive into Apache Kafkaconfluent
8K views35 slides
Cassandra Introduction & Features by
Cassandra Introduction & FeaturesCassandra Introduction & Features
Cassandra Introduction & FeaturesDataStax Academy
31.9K views21 slides

What's hot(20)

Kafka error handling patterns and best practices | Hemant Desale and Aruna Ka... by HostedbyConfluent
Kafka error handling patterns and best practices | Hemant Desale and Aruna Ka...Kafka error handling patterns and best practices | Hemant Desale and Aruna Ka...
Kafka error handling patterns and best practices | Hemant Desale and Aruna Ka...
HostedbyConfluent3.2K views
Scalability, Availability & Stability Patterns by Jonas Bonér
Scalability, Availability & Stability PatternsScalability, Availability & Stability Patterns
Scalability, Availability & Stability Patterns
Jonas Bonér515.7K views
Flink Forward Berlin 2017: Aris Kyriakos Koliopoulos - Drivetribe's Kappa Arc... by Flink Forward
Flink Forward Berlin 2017: Aris Kyriakos Koliopoulos - Drivetribe's Kappa Arc...Flink Forward Berlin 2017: Aris Kyriakos Koliopoulos - Drivetribe's Kappa Arc...
Flink Forward Berlin 2017: Aris Kyriakos Koliopoulos - Drivetribe's Kappa Arc...
Flink Forward2.6K views
Exactly-Once Financial Data Processing at Scale with Flink and Pinot by Flink Forward
Exactly-Once Financial Data Processing at Scale with Flink and PinotExactly-Once Financial Data Processing at Scale with Flink and Pinot
Exactly-Once Financial Data Processing at Scale with Flink and Pinot
Flink Forward683 views
Deep Dive into Apache Kafka by confluent
Deep Dive into Apache KafkaDeep Dive into Apache Kafka
Deep Dive into Apache Kafka
confluent8K views
Cassandra Introduction & Features by DataStax Academy
Cassandra Introduction & FeaturesCassandra Introduction & Features
Cassandra Introduction & Features
DataStax Academy31.9K views
Introduction to Apache ZooKeeper by Saurav Haloi
Introduction to Apache ZooKeeperIntroduction to Apache ZooKeeper
Introduction to Apache ZooKeeper
Saurav Haloi128.4K views
eBay Architecture by Tony Ng
eBay Architecture eBay Architecture
eBay Architecture
Tony Ng26.8K views
Apache Flink and what it is used for by Aljoscha Krettek
Apache Flink and what it is used forApache Flink and what it is used for
Apache Flink and what it is used for
Aljoscha Krettek1.4K views
Kafka Tutorial - Introduction to Apache Kafka (Part 1) by Jean-Paul Azar
Kafka Tutorial - Introduction to Apache Kafka (Part 1)Kafka Tutorial - Introduction to Apache Kafka (Part 1)
Kafka Tutorial - Introduction to Apache Kafka (Part 1)
Jean-Paul Azar8.9K views
[215] Druid로 쉽고 빠르게 데이터 분석하기 by NAVER D2
[215] Druid로 쉽고 빠르게 데이터 분석하기[215] Druid로 쉽고 빠르게 데이터 분석하기
[215] Druid로 쉽고 빠르게 데이터 분석하기
NAVER D25.4K views
The eBay Architecture: Striking a Balance between Site Stability, Feature Ve... by Randy Shoup
The eBay Architecture:  Striking a Balance between Site Stability, Feature Ve...The eBay Architecture:  Striking a Balance between Site Stability, Feature Ve...
The eBay Architecture: Striking a Balance between Site Stability, Feature Ve...
Randy Shoup30.2K views
Event-Driven Architecture (EDA) by WSO2
Event-Driven Architecture (EDA)Event-Driven Architecture (EDA)
Event-Driven Architecture (EDA)
WSO210.9K views
Microservice Architecture | Microservices Tutorial for Beginners | Microservi... by Edureka!
Microservice Architecture | Microservices Tutorial for Beginners | Microservi...Microservice Architecture | Microservices Tutorial for Beginners | Microservi...
Microservice Architecture | Microservices Tutorial for Beginners | Microservi...
Edureka!1.4K views
Developing event-driven microservices with event sourcing and CQRS (phillyete) by Chris Richardson
Developing event-driven microservices with event sourcing and CQRS (phillyete)Developing event-driven microservices with event sourcing and CQRS (phillyete)
Developing event-driven microservices with event sourcing and CQRS (phillyete)
Chris Richardson9K views

Viewers also liked

Velocity 2017 Performance analysis superpowers with Linux eBPF by
Velocity 2017 Performance analysis superpowers with Linux eBPFVelocity 2017 Performance analysis superpowers with Linux eBPF
Velocity 2017 Performance analysis superpowers with Linux eBPFBrendan Gregg
735.3K views54 slides
Linux Performance Analysis: New Tools and Old Secrets by
Linux Performance Analysis: New Tools and Old SecretsLinux Performance Analysis: New Tools and Old Secrets
Linux Performance Analysis: New Tools and Old SecretsBrendan Gregg
603.9K views75 slides
Linux Systems Performance 2016 by
Linux Systems Performance 2016Linux Systems Performance 2016
Linux Systems Performance 2016Brendan Gregg
504.5K views72 slides
Velocity 2015 linux perf tools by
Velocity 2015 linux perf toolsVelocity 2015 linux perf tools
Velocity 2015 linux perf toolsBrendan Gregg
1.1M views142 slides
Kernel Recipes 2017: Using Linux perf at Netflix by
Kernel Recipes 2017: Using Linux perf at NetflixKernel Recipes 2017: Using Linux perf at Netflix
Kernel Recipes 2017: Using Linux perf at NetflixBrendan Gregg
1.5M views79 slides
Broken Linux Performance Tools 2016 by
Broken Linux Performance Tools 2016Broken Linux Performance Tools 2016
Broken Linux Performance Tools 2016Brendan Gregg
822.5K views95 slides

Viewers also liked(7)

Velocity 2017 Performance analysis superpowers with Linux eBPF by Brendan Gregg
Velocity 2017 Performance analysis superpowers with Linux eBPFVelocity 2017 Performance analysis superpowers with Linux eBPF
Velocity 2017 Performance analysis superpowers with Linux eBPF
Brendan Gregg735.3K views
Linux Performance Analysis: New Tools and Old Secrets by Brendan Gregg
Linux Performance Analysis: New Tools and Old SecretsLinux Performance Analysis: New Tools and Old Secrets
Linux Performance Analysis: New Tools and Old Secrets
Brendan Gregg603.9K views
Linux Systems Performance 2016 by Brendan Gregg
Linux Systems Performance 2016Linux Systems Performance 2016
Linux Systems Performance 2016
Brendan Gregg504.5K views
Velocity 2015 linux perf tools by Brendan Gregg
Velocity 2015 linux perf toolsVelocity 2015 linux perf tools
Velocity 2015 linux perf tools
Brendan Gregg1.1M views
Kernel Recipes 2017: Using Linux perf at Netflix by Brendan Gregg
Kernel Recipes 2017: Using Linux perf at NetflixKernel Recipes 2017: Using Linux perf at Netflix
Kernel Recipes 2017: Using Linux perf at Netflix
Brendan Gregg1.5M views
Broken Linux Performance Tools 2016 by Brendan Gregg
Broken Linux Performance Tools 2016Broken Linux Performance Tools 2016
Broken Linux Performance Tools 2016
Brendan Gregg822.5K views
Container Performance Analysis by Brendan Gregg
Container Performance AnalysisContainer Performance Analysis
Container Performance Analysis
Brendan Gregg448.6K views

Similar to Unique ID generation in distributed systems

MySQL Cluster Scaling to a Billion Queries by
MySQL Cluster Scaling to a Billion QueriesMySQL Cluster Scaling to a Billion Queries
MySQL Cluster Scaling to a Billion QueriesBernd Ocklin
10.6K views107 slides
Why Kubernetes as a container orchestrator is a right choice for running spar... by
Why Kubernetes as a container orchestrator is a right choice for running spar...Why Kubernetes as a container orchestrator is a right choice for running spar...
Why Kubernetes as a container orchestrator is a right choice for running spar...DataWorks Summit
1.4K views28 slides
Introduction to web security @ confess 2012 by
Introduction to web security @ confess 2012Introduction to web security @ confess 2012
Introduction to web security @ confess 2012jakobkorherr
2.4K views70 slides
.NET Cloud-Native Bootcamp Minneapolis by
.NET Cloud-Native Bootcamp Minneapolis.NET Cloud-Native Bootcamp Minneapolis
.NET Cloud-Native Bootcamp MinneapolisVMware Tanzu
424 views80 slides
Fixing twitter by
Fixing twitterFixing twitter
Fixing twitterRoger Xia
546 views49 slides
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ... by
Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...xlight
444 views49 slides

Similar to Unique ID generation in distributed systems(20)

MySQL Cluster Scaling to a Billion Queries by Bernd Ocklin
MySQL Cluster Scaling to a Billion QueriesMySQL Cluster Scaling to a Billion Queries
MySQL Cluster Scaling to a Billion Queries
Bernd Ocklin10.6K views
Why Kubernetes as a container orchestrator is a right choice for running spar... by DataWorks Summit
Why Kubernetes as a container orchestrator is a right choice for running spar...Why Kubernetes as a container orchestrator is a right choice for running spar...
Why Kubernetes as a container orchestrator is a right choice for running spar...
DataWorks Summit1.4K views
Introduction to web security @ confess 2012 by jakobkorherr
Introduction to web security @ confess 2012Introduction to web security @ confess 2012
Introduction to web security @ confess 2012
jakobkorherr2.4K views
.NET Cloud-Native Bootcamp Minneapolis by VMware Tanzu
.NET Cloud-Native Bootcamp Minneapolis.NET Cloud-Native Bootcamp Minneapolis
.NET Cloud-Native Bootcamp Minneapolis
VMware Tanzu424 views
Fixing twitter by Roger Xia
Fixing twitterFixing twitter
Fixing twitter
Roger Xia546 views
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ... by xlight
Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...
xlight444 views
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ... by smallerror
Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...
smallerror1.4K views
Chirp 2010: Scaling Twitter by John Adams
Chirp 2010: Scaling TwitterChirp 2010: Scaling Twitter
Chirp 2010: Scaling Twitter
John Adams38K views
Using Riak for Events storage and analysis at Booking.com by Damien Krotkine
Using Riak for Events storage and analysis at Booking.comUsing Riak for Events storage and analysis at Booking.com
Using Riak for Events storage and analysis at Booking.com
Damien Krotkine3.9K views
Game On! Exploring Microservices with a Text-Based Adventure Game by Erin Schnabel
Game On! Exploring Microservices with a Text-Based Adventure GameGame On! Exploring Microservices with a Text-Based Adventure Game
Game On! Exploring Microservices with a Text-Based Adventure Game
Erin Schnabel701 views
SPS Ozarks 2012: Kerberos Survival Guide by J.D. Wade
SPS Ozarks 2012: Kerberos Survival GuideSPS Ozarks 2012: Kerberos Survival Guide
SPS Ozarks 2012: Kerberos Survival Guide
J.D. Wade699 views
Infrastructure API Lightning Talk by Jeremy Pollard of box.com by DevOps4Networks
Infrastructure API Lightning Talk by Jeremy Pollard of box.comInfrastructure API Lightning Talk by Jeremy Pollard of box.com
Infrastructure API Lightning Talk by Jeremy Pollard of box.com
DevOps4Networks1.4K views
How to over-engineer things and have fun? | Oto Brglez, OPALAB by HostedbyConfluent
How to over-engineer things and have fun? | Oto Brglez, OPALABHow to over-engineer things and have fun? | Oto Brglez, OPALAB
How to over-engineer things and have fun? | Oto Brglez, OPALAB
HostedbyConfluent339 views
John adams talk cloudy by John Adams
John adams   talk cloudyJohn adams   talk cloudy
John adams talk cloudy
John Adams3.3K views
Getting started with Spark & Cassandra by Jon Haddad of Datastax by Data Con LA
Getting started with Spark & Cassandra by Jon Haddad of DatastaxGetting started with Spark & Cassandra by Jon Haddad of Datastax
Getting started with Spark & Cassandra by Jon Haddad of Datastax
Data Con LA3.4K views
iland Internet Solutions: Leveraging Cassandra for real-time multi-datacenter... by DataStax Academy
iland Internet Solutions: Leveraging Cassandra for real-time multi-datacenter...iland Internet Solutions: Leveraging Cassandra for real-time multi-datacenter...
iland Internet Solutions: Leveraging Cassandra for real-time multi-datacenter...
DataStax Academy1.6K views
Leveraging Cassandra for real-time multi-datacenter public cloud analytics by Julien Anguenot
Leveraging Cassandra for real-time multi-datacenter public cloud analyticsLeveraging Cassandra for real-time multi-datacenter public cloud analytics
Leveraging Cassandra for real-time multi-datacenter public cloud analytics
Julien Anguenot2.3K views
Asynchronous design with Spring and RTI: 1M events per second by Stuart (Pid) Williams
Asynchronous design with Spring and RTI: 1M events per secondAsynchronous design with Spring and RTI: 1M events per second
Asynchronous design with Spring and RTI: 1M events per second
Realtime Messaging und verteilte Systeme mit SharePoint und Windows Azure Ser... by Damir Dobric
Realtime Messaging und verteilte Systeme mit SharePoint und Windows Azure Ser...Realtime Messaging und verteilte Systeme mit SharePoint und Windows Azure Ser...
Realtime Messaging und verteilte Systeme mit SharePoint und Windows Azure Ser...
Damir Dobric740 views

More from Dave Gardner

Cabs, Cassandra, and Hailo (at Cassandra EU) by
Cabs, Cassandra, and Hailo (at Cassandra EU)Cabs, Cassandra, and Hailo (at Cassandra EU)
Cabs, Cassandra, and Hailo (at Cassandra EU)Dave Gardner
4.5K views68 slides
Cabs, Cassandra, and Hailo by
Cabs, Cassandra, and HailoCabs, Cassandra, and Hailo
Cabs, Cassandra, and HailoDave Gardner
4.4K views65 slides
Planning to Fail #phpne13 by
Planning to Fail #phpne13Planning to Fail #phpne13
Planning to Fail #phpne13Dave Gardner
2.4K views88 slides
Planning to Fail #phpuk13 by
Planning to Fail #phpuk13Planning to Fail #phpuk13
Planning to Fail #phpuk13Dave Gardner
2.4K views68 slides
Cassandra concepts, patterns and anti-patterns by
Cassandra concepts, patterns and anti-patternsCassandra concepts, patterns and anti-patterns
Cassandra concepts, patterns and anti-patternsDave Gardner
17K views63 slides
Learning Cassandra by
Learning CassandraLearning Cassandra
Learning CassandraDave Gardner
12.4K views58 slides

More from Dave Gardner(13)

Cabs, Cassandra, and Hailo (at Cassandra EU) by Dave Gardner
Cabs, Cassandra, and Hailo (at Cassandra EU)Cabs, Cassandra, and Hailo (at Cassandra EU)
Cabs, Cassandra, and Hailo (at Cassandra EU)
Dave Gardner4.5K views
Cabs, Cassandra, and Hailo by Dave Gardner
Cabs, Cassandra, and HailoCabs, Cassandra, and Hailo
Cabs, Cassandra, and Hailo
Dave Gardner4.4K views
Planning to Fail #phpne13 by Dave Gardner
Planning to Fail #phpne13Planning to Fail #phpne13
Planning to Fail #phpne13
Dave Gardner2.4K views
Planning to Fail #phpuk13 by Dave Gardner
Planning to Fail #phpuk13Planning to Fail #phpuk13
Planning to Fail #phpuk13
Dave Gardner2.4K views
Cassandra concepts, patterns and anti-patterns by Dave Gardner
Cassandra concepts, patterns and anti-patternsCassandra concepts, patterns and anti-patterns
Cassandra concepts, patterns and anti-patterns
Dave Gardner17K views
Learning Cassandra by Dave Gardner
Learning CassandraLearning Cassandra
Learning Cassandra
Dave Gardner12.4K views
Cassandra's Sweet Spot - an introduction to Apache Cassandra by Dave Gardner
Cassandra's Sweet Spot - an introduction to Apache CassandraCassandra's Sweet Spot - an introduction to Apache Cassandra
Cassandra's Sweet Spot - an introduction to Apache Cassandra
Dave Gardner13.9K views
Intro slides from Cassandra London July 2011 by Dave Gardner
Intro slides from Cassandra London July 2011Intro slides from Cassandra London July 2011
Intro slides from Cassandra London July 2011
Dave Gardner728 views
2011.07.18 cassandrameetup by Dave Gardner
2011.07.18 cassandrameetup2011.07.18 cassandrameetup
2011.07.18 cassandrameetup
Dave Gardner740 views
Cassandra + Hadoop = Brisk by Dave Gardner
Cassandra + Hadoop = BriskCassandra + Hadoop = Brisk
Cassandra + Hadoop = Brisk
Dave Gardner11.4K views
Introduction to Cassandra at London Web Meetup by Dave Gardner
Introduction to Cassandra at London Web MeetupIntroduction to Cassandra at London Web Meetup
Introduction to Cassandra at London Web Meetup
Dave Gardner2K views
Running Cassandra on Amazon EC2 by Dave Gardner
Running Cassandra on Amazon EC2Running Cassandra on Amazon EC2
Running Cassandra on Amazon EC2
Dave Gardner13.8K views
PHP and Cassandra by Dave Gardner
PHP and CassandraPHP and Cassandra
PHP and Cassandra
Dave Gardner15.7K views

Recently uploaded

The details of description: Techniques, tips, and tangents on alternative tex... by
The details of description: Techniques, tips, and tangents on alternative tex...The details of description: Techniques, tips, and tangents on alternative tex...
The details of description: Techniques, tips, and tangents on alternative tex...BookNet Canada
121 views24 slides
How the World's Leading Independent Automotive Distributor is Reinventing Its... by
How the World's Leading Independent Automotive Distributor is Reinventing Its...How the World's Leading Independent Automotive Distributor is Reinventing Its...
How the World's Leading Independent Automotive Distributor is Reinventing Its...NUS-ISS
15 views25 slides
Digital Product-Centric Enterprise and Enterprise Architecture - Tan Eng Tsze by
Digital Product-Centric Enterprise and Enterprise Architecture - Tan Eng TszeDigital Product-Centric Enterprise and Enterprise Architecture - Tan Eng Tsze
Digital Product-Centric Enterprise and Enterprise Architecture - Tan Eng TszeNUS-ISS
19 views47 slides
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor... by
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...Vadym Kazulkin
75 views64 slides
20231123_Camunda Meetup Vienna.pdf by
20231123_Camunda Meetup Vienna.pdf20231123_Camunda Meetup Vienna.pdf
20231123_Camunda Meetup Vienna.pdfPhactum Softwareentwicklung GmbH
28 views73 slides
Uni Systems for Power Platform.pptx by
Uni Systems for Power Platform.pptxUni Systems for Power Platform.pptx
Uni Systems for Power Platform.pptxUni Systems S.M.S.A.
50 views21 slides

Recently uploaded(20)

The details of description: Techniques, tips, and tangents on alternative tex... by BookNet Canada
The details of description: Techniques, tips, and tangents on alternative tex...The details of description: Techniques, tips, and tangents on alternative tex...
The details of description: Techniques, tips, and tangents on alternative tex...
BookNet Canada121 views
How the World's Leading Independent Automotive Distributor is Reinventing Its... by NUS-ISS
How the World's Leading Independent Automotive Distributor is Reinventing Its...How the World's Leading Independent Automotive Distributor is Reinventing Its...
How the World's Leading Independent Automotive Distributor is Reinventing Its...
NUS-ISS15 views
Digital Product-Centric Enterprise and Enterprise Architecture - Tan Eng Tsze by NUS-ISS
Digital Product-Centric Enterprise and Enterprise Architecture - Tan Eng TszeDigital Product-Centric Enterprise and Enterprise Architecture - Tan Eng Tsze
Digital Product-Centric Enterprise and Enterprise Architecture - Tan Eng Tsze
NUS-ISS19 views
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor... by Vadym Kazulkin
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...
Vadym Kazulkin75 views
Igniting Next Level Productivity with AI-Infused Data Integration Workflows by Safe Software
Igniting Next Level Productivity with AI-Infused Data Integration Workflows Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Safe Software225 views
AI: mind, matter, meaning, metaphors, being, becoming, life values by Twain Liu 刘秋艳
AI: mind, matter, meaning, metaphors, being, becoming, life valuesAI: mind, matter, meaning, metaphors, being, becoming, life values
AI: mind, matter, meaning, metaphors, being, becoming, life values
Web Dev - 1 PPT.pdf by gdsczhcet
Web Dev - 1 PPT.pdfWeb Dev - 1 PPT.pdf
Web Dev - 1 PPT.pdf
gdsczhcet55 views
Combining Orchestration and Choreography for a Clean Architecture by ThomasHeinrichs1
Combining Orchestration and Choreography for a Clean ArchitectureCombining Orchestration and Choreography for a Clean Architecture
Combining Orchestration and Choreography for a Clean Architecture
ThomasHeinrichs169 views
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors by sugiuralab
TouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective SensorsTouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective Sensors
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors
sugiuralab15 views
Business Analyst Series 2023 - Week 3 Session 5 by DianaGray10
Business Analyst Series 2023 -  Week 3 Session 5Business Analyst Series 2023 -  Week 3 Session 5
Business Analyst Series 2023 - Week 3 Session 5
DianaGray10209 views
Beyond the Hype: What Generative AI Means for the Future of Work - Damien Cum... by NUS-ISS
Beyond the Hype: What Generative AI Means for the Future of Work - Damien Cum...Beyond the Hype: What Generative AI Means for the Future of Work - Damien Cum...
Beyond the Hype: What Generative AI Means for the Future of Work - Damien Cum...
NUS-ISS34 views
Future of Learning - Yap Aye Wee.pdf by NUS-ISS
Future of Learning - Yap Aye Wee.pdfFuture of Learning - Yap Aye Wee.pdf
Future of Learning - Yap Aye Wee.pdf
NUS-ISS41 views
Perth MeetUp November 2023 by Michael Price
Perth MeetUp November 2023 Perth MeetUp November 2023
Perth MeetUp November 2023
Michael Price15 views
.conf Go 2023 - Data analysis as a routine by Splunk
.conf Go 2023 - Data analysis as a routine.conf Go 2023 - Data analysis as a routine
.conf Go 2023 - Data analysis as a routine
Splunk93 views
Transcript: The Details of Description Techniques tips and tangents on altern... by BookNet Canada
Transcript: The Details of Description Techniques tips and tangents on altern...Transcript: The Details of Description Techniques tips and tangents on altern...
Transcript: The Details of Description Techniques tips and tangents on altern...
BookNet Canada130 views
DALI Basics Course 2023 by Ivory Egg
DALI Basics Course  2023DALI Basics Course  2023
DALI Basics Course 2023
Ivory Egg14 views

Unique ID generation in distributed systems

  • 1. ID generation PHP London 2012-08-02 @davegardnerisme
  • 3. MySQL auto increment DC 1 1,2,3,4… MySQL Web App
  • 4. MySQL auto increment • Numeric IDs • Go up with time • Not resilient
  • 5. MySQL multi-master replication DC 1 1,3,5,7… MySQL Web App 2,4,6,8… MySQL
  • 6. MySQL multi-master replication • Numeric IDs • Do not go up with time • Some resilience
  • 7. DC 1 DC 2 DC 4 DC 5 DC 6 DC 3 Going global…
  • 8. MySQL in multi DC setup DC 1 1,2,3… Web MySQL App WAN LINK DC 2 ? Web App
  • 9. Flickr MySQL ticket server DC 1 Ticket 1,3,5… Web WAN link not required to Server App generate an ID WAN LINK DC 2 Ticket 4,6,8… Web Server App
  • 10. Flickr MySQL ticket server • Numeric IDs • Do not go up with time • Resilient and distributed • ID generation separated from data store
  • 11. The anatomy of a ticket server DC Web Web Web Web App App App App Ticket Server
  • 12. Making things simpler DC Web Web Web Web App App App App ID gen ID gen ID gen ID gen
  • 13. UUIDs • 128 bits • Could use type 4 (Random) or type 1 (MAC address with time component) • Can generate on each machine with no co-ordination
  • 14. Type 4 – random version xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx variant (8, 9, A or B) f47ac10b-58cc-4372-a567-0e02b2c3d479
  • 15. 5.3 x 1036 possible values for a type 4 UUID
  • 16. 1.1 x 1019 UUIDs we could generate per second since the Universe began
  • 17. 2.1 x 1027 Olympic swimming pools filled if each possible value contributed a millilitre
  • 18. Type 1 – MAC address 51063800-dc76-11e1-9fae-001c42000009 • Time component is based on 100 nanosecond intervals since October 15, 1582 • Most significant bits of timestamp shifted to least significant bits of UUID
  • 19. Type 1 – MAC address • The address (MAC) of the computer that generated the ID is encoded into it • Lexical ordering essentially meaningless • Deterministically unique
  • 20. There are some other options…
  • 21. No co-ordination needed Deterministically unique K-ordered (time-ordered lexically)
  • 22. Twitter Snowflake • Under 64 bits • No co-ordination (after startup) • K-ordered • Scala service, Thrift interface, uses Zookeeper for configuration
  • 23. Twitter Snowflake 41 bits Timestamp millisecond precision, bespoke epoch 10 bits Configured machine ID 12 bits Sequence number
  • 24. Twitter Snowflake 77669839702851584 = (timestamp << 22) | (machine << 12) | sequence
  • 25. Boundary Flake • 128 bits • No co-ordination at all • K-ordered • Erlang service
  • 26. Boundary Flake 64 bits Timestamp millisecond precision, 1970 epoch 48 bits MAC address 16 bits Sequence number
  • 27. PHP Cruftflake • Based on Twitter Snowflake • No co-ordination (after startup) • K-ordered • PHP, ZeroMQ interface, uses Zookeeper for configuration
  • 29. References Flickr distributed ticket server http://code.flickr.com/blog/2010/02/08/ticket-servers-distributed-unique-primary-keys-on- the-cheap/ UUIDs http://tools.ietf.org/html/rfc4122 How random are random UUIDs? http://stackoverflow.com/a/2514722/15318 Twitter Snowflake https://github.com/twitter/snowflake Boundary Flake https://github.com/boundary/flake PHP Cruftflake https://github.com/davegardnerisme/cruftflake
  • 30. private function mintId64($timestamp, $machine, $sequence) { $timestamp = (int)$timestamp; $value = ($timestamp << 22) | ($machine << 12) | $sequence; return (string)$value; } private function mintId32($timestamp, $machine, $sequence) { $hi = (int)($timestamp / pow(2,10)); $lo = (int)($timestamp * pow(2, 22)); // stick in the machine + sequence to the low bit $lo = $lo | ($machine << 12) | $sequence; // reconstruct into a string of numbers $hex = pack('N2', $hi, $lo); $unpacked = unpack('H*', $hex); $value = $this->hexdec($unpacked[1]); return (string)$value; }
  • 31. public function generate() { $t = floor($this->timer->getUnixTimestamp() - $this->epoch); if ($t !== $this->lastTime) { $this->sequence = 0; $this->lastTime = $t; } else { $this->sequence++; if ($this->sequence > 4095) { throw new OverflowException('Sequence overflow'); } } if (PHP_INT_SIZE === 4) { return $this->mintId32($t, $this->machine, $this->sequence); } else { return $this->mintId64($t, $this->machine, $this->sequence); } }