Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Identify All the Things With UUIDs! (Dutch PHP 2016)

Identify All the Things With UUIDs! (Dutch PHP 2016)

Universally unique identifiers (UUIDs) are a fun and exciting way to identify things. We can issue UUIDs forever and never run out; they're practically unique! Join this whirlwind adventure in search of the perfect identifier to find out why UUIDs might be good for your projects. Along the way, you'll learn what is a UUID, the various types of UUIDs, pros and cons of using UUIDs, and how to use the ramsey/uuid library. Advanced and little-known features of ramsey/uuid will be covered.

Ben Ramsey
PRO

June 24, 2016
Tweet

More Decks by Ben Ramsey

Other Decks in Programming

Transcript

  1. Identify All the
    Things With
    UUIDs
    Ben Ramsey

    Dutch PHP Conference

    24 June 2016

    View Slide

  2. View Slide

  3. universally unique identifier
    U U ID

    View Slide

  4. universally unique
    There are 1632 possible UUIDs

    “only after generating 1 billion UUIDs every
    second for the next 100 years, the probability
    of creating just one duplicate would be about
    50%”
    — Wikipedia, UUID

    View Slide

  5. 379dae82-5a2b-4c4b-8193-b8e7749a3495
    Unsigned 128-bit integer

    View Slide

  6. 73 926 269 841 798 498 179 266 328 839 241 479 317
    Unsigned 128-bit integer

    View Slide

  7. b"7Ø«éZ+LKüô©þtÜ4ò"
    Unsigned 128-bit integer

    View Slide

  8. View Slide

  9. HI, I’M BEN.
    I’m a web craftsman, author, and
    speaker. I build a platform for
    professional photographers at
    ShootProof. I enjoy APIs, open
    source software, organizing user
    groups, good beer, and
    spending time with my family.
    Nashville, TN is my home.
    ▸ Zend PHP Certification
    Study Guide
    ▸ Nashville PHP & Atlanta
    PHP user groups
    ▸ array_column()
    ▸ league/oauth2-client
    ▸ ramsey/uuid

    View Slide

  10. View Slide

  11. ramsey/uuid

    View Slide

  12. composer require ramsey/uuid

    View Slide

  13. use Ramsey\Uuid\Uuid;

    View Slide

  14. RFC 4122
    Defines a specific variant of UUID with five
    versions.

    View Slide

  15. Anatomy of
    a UUID

    View Slide

  16. 379dae82-5a2b-4c4b-8193-b8e7749a3495

    View Slide

  17. 379dae82-5a2b-4c4b-8193-b8e7749a3495
    time low

    View Slide

  18. 379dae82-5a2b-4c4b-8193-b8e7749a3495
    time mid

    View Slide

  19. 379dae82-5a2b-4c4b-8193-b8e7749a3495
    time high & version

    View Slide

  20. 379dae82-5a2b-4c4b-8193-b8e7749a3495
    time high & version
    UUID version

    View Slide

  21. 379dae82-5a2b-4c4b-8193-b8e7749a3495
    clock sequence high & reserved

    View Slide

  22. 379dae82-5a2b-4c4b-8193-b8e7749a3495
    clock sequence high & reserved
    UUID variant

    View Slide

  23. 379dae82-5a2b-4c4b-8193-b8e7749a3495
    clock sequence low

    View Slide

  24. 379dae82-5a2b-4c4b-8193-b8e7749a3495
    node

    View Slide

  25. Version 1:
    Time Based

    View Slide

  26. $uuid = Uuid::uuid1();
    echo $uuid->toString();

    View Slide

  27. for ($i = 0; $i < 5; $i++) {
    echo Uuid::uuid1()->toString();
    echo "\n";
    }

    View Slide

  28. 68845efc-1303-11e6-8d40-3c15c2cafa76
    68846d0c-1303-11e6-8088-3c15c2cafa76
    68847216-1303-11e6-830b-3c15c2cafa76
    68847590-1303-11e6-892a-3c15c2cafa76
    68847806-1303-11e6-913b-3c15c2cafa76

    View Slide

  29. Version 4:
    Random

    View Slide

  30. for ($i = 0; $i < 5; $i++) {
    echo Uuid::uuid4()->toString();
    echo "\n";
    }

    View Slide

  31. 28b7883f-3df0-4e8a-9994-57b28f01fab2
    dd78278c-03d7-4ca4-8411-06380c4cfd57
    ae8de9f1-6ffc-454e-8522-b2c260e4c66b
    a5e9ac6b-0680-4fb7-a019-f305c7d2d5da
    09fd4b15-2b2d-49be-af51-30899169ff99

    View Slide

  32. Name Based:
    Version 3 & 5

    View Slide

  33. $uuid = Uuid::uuid3(
    Uuid::NAMESPACE_DNS,
    'www.phpconference.nl/about-dpc'
    );

    View Slide

  34. 2f75e334-aa61-3af8-b570-49dd823d7248

    View Slide

  35. $uuid = Uuid::uuid5(
    Uuid::NAMESPACE_DNS,
    'www.phpconference.nl/about-dpc'
    );

    View Slide

  36. f2a7c7f9-358c-51b7-ab30-cc456c5b2256

    View Slide

  37. $dpcNS = Uuid::uuid5(
    Uuid::NAMESPACE_DNS,
    'www.phpconference.nl'
    );
    define(
    'NAMESPACE_DPC',
    $dpcNS->toString()
    );
    $pageId = Uuid::uuid5(
    NAMESPACE_DPC,
    '/about-dpc'
    );

    View Slide

  38. Version 2?

    View Slide

  39. Version 2:
    DCE Security

    View Slide

  40. Advanced

    Topics

    View Slide

  41. Database Considerations
    • Some databases have UUID as native type

    • MySQL does not

    • InnoDB uses PK in all secondary keys; if PK
    is big (CHAR(36)), then all keys will be huge

    • InnoDB stores data in PK order; if UUID is
    PK and not sequential, then inserts are
    scattered

    View Slide

  42. Database Solutions
    • Use binary instead of string UUID

    • CHAR(16)
    • $uuid->getBytes()
    • Pull request #118: Codec to store UUID in
    an optimized way for InnoDB

    • Read Percona blog post “Store UUID in an
    optimized way”

    View Slide

  43. COMB
    • Combined random UUID/timestamp

    • Replaces the least significant bytes of the
    node field with the current timestamp

    • Tries to compensate for the reduced
    clustering in database indexes

    View Slide

  44. $factory = new UuidFactory();
    $generator = new CombGenerator(
    $factory->getRandomGenerator(),
    $factory->getNumberConverter()
    );
    $factory->setRandomGenerator($generator);
    $combUuid = $factory->uuid4();

    View Slide

  45. Override Time Generator
    Want to use pecl-uuid as your time
    generator?

    We can do that.

    View Slide

  46. $factory = new UuidFactory();
    $factory->setTimeGenerator(
    new PeclUuidTimeGenerator()
    );
    $uuid = $factory->uuid1();

    View Slide

  47. Override Random Generator
    Want to use libsodium as your random
    generator?

    We can do that.

    View Slide

  48. $factory = new UuidFactory();
    $factory->setRandomGenerator(
    new SodiumRandomGenerator()
    );
    $uuid = $factory->uuid4();

    View Slide

  49. More Customization
    In fact, you’re able to customize ramsey/uuid
    in any way you need by using interfaces and
    injecting dependencies in the factory:
    - Generators

    - Codecs

    - Converters

    - Providers

    View Slide

  50. UUID all the
    things?

    View Slide

  51. Cons
    • Takes up much more DB space than integer

    • By default (and spec) not sequential

    • Decreases database performance

    • Can’t use ORDER BY

    • Ugly in URLs

    View Slide

  52. Pros
    • Unique everywhere

    • Easily able to merge recordsets from
    multiple sources

    • Easy distribution of databases across
    multiple servers

    • May be generated anywhere, independently

    View Slide

  53. THANK YOU.
    ANY QUESTIONS?
    If you want to talk more, feel
    free to contact me.
    benramsey.com
    @ramsey
    github.com/ramsey
    [email protected]
    Identify All the Things With UUIDs

    Copyright © 2016 Ben Ramsey

    This work is licensed under Creative Commons
    Attribution-ShareAlike 4.0 International. For uses not
    covered under this license, please contact the author.
    Ramsey, Ben. “Identify All the Things With UUIDs.” Dutch PHP
    Conference. RAI Amsterdam Convention Centre, Amsterdam. 24
    Jun. 2016. Conference presentation.
    This presentation was created using Keynote. The text
    is set in Chunk Five and Helvetica Neue. The source
    code is set in Menlo. The iconography is provided by
    Font Awesome.

    Unless otherwise noted, all photographs are used by
    permission under a Creative Commons license. Please
    refer to the Photo Credits slide for more information.
    Ŏ joind.in/talk/11781

    View Slide

  54. Photo Credits
    1. “38-365 Fingerprint” by Bram Cymet
    2. “Baby Feet” by Katelyn Kenderdine
    3. “Random” by Vladimer Shioshvili
    4. “Hello My Name Is.... 221/365” by Robert Occhialini
    5. “Curvy Road Ahead” by Kit Ng
    6. “Security door” by reynermedia
    7. “Hello! My Name Is JEDi.” by Tyrone J Moore
    8. “Mr Anatomini” by clement127
    9. “Everything” by Dan Dvorscak
    10. “thinker” by Fredrik Rubensson
    1 2
    3 4
    5 6
    7 8
    9 10

    View Slide