Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uuid v7 collisions #518

Closed
vudaltsov opened this issue Oct 13, 2023 · 9 comments · Fixed by #522
Closed

Uuid v7 collisions #518

vudaltsov opened this issue Oct 13, 2023 · 9 comments · Fixed by #522
Assignees
Labels

Comments

@vudaltsov
Copy link
Contributor

vudaltsov commented Oct 13, 2023

Description

From time to time I get v7 collisions. I also noticed that since 0ba1ffb uuids of this version do not differ much in the random part, maybe, that's the reason?

Steps to reproduce

<?php

// generate_uuids_v7.php

use function Ramsey\Uuid\v7;

require_once __DIR__.'/vendor/autoload.php';

for ($j = 0; $j < 100; ++$j) {
    $uuids = '';

    for ($i = 0; $i < 1_000_000; ++$i) {
        $uuids .= v7()."\n";
    }

    file_put_contents('uuids_v7.txt', $uuids, $j === 0 ? 0 : FILE_APPEND);
}
php generate_uuids_v7.php && sort uuids_v7.txt | uniq -cd

Expected behavior

Empty output

Screenshots or output

   2 018b2847-0e47-7148-8dec-df216b0d76c7
   2 018b2847-a950-7367-9fa7-e3344774c890
   2 018b2847-c3b4-7203-be93-5976ad423647
   2 018b2847-fbbd-703e-b616-d256da1f0ec0
   2 018b2848-1f15-7200-b831-ddc7c0e6f86f
   2 018b2848-dcbf-72eb-b6ea-256c9e1f5d38

Environment details

  • version of this package: 4.7.4
  • PHP version: 8.2.10
  • OS: macOS Ventura (13.3.1)
@vudaltsov vudaltsov added the bug label Oct 13, 2023
@vudaltsov
Copy link
Contributor Author

Running same code with "ramsey/uuid": "4.5.1" does not give any collisions. So the bug is in new UnixTimeGenerator.

@ramsey ramsey self-assigned this Oct 27, 2023
@ramsey
Copy link
Owner

ramsey commented Oct 27, 2023

The new code in UnixTimeGenerator was mostly borrowed from symfony/uid. Can you reproduce the issue using their method for generating v7 UUIDs?

@vudaltsov
Copy link
Contributor Author

Can you reproduce the issue using their method for generating v7 UUIDs?

Yes, done.

@nicolas-grekas
Copy link

See symfony/symfony#52367 for the fix

@ramsey
Copy link
Owner

ramsey commented Oct 31, 2023

Thanks, @nicolas-grekas!

@vudaltsov
Copy link
Contributor Author

I will do a PR.

@fannyfan414
Copy link

@ramsey @nicolas-grekas Hi! Have a question about uuid v7 collisions.

I need to replace 4-5 chars after generation uuid v7 with my specific characters (servers id or smth else).
At what positions would you advise me to change them so as not to greatly increase the chance of collisions?

@vudaltsov
Copy link
Contributor Author

@fannyfan414 , you can use UUID v8, it allows to put arbitrary content.

@ramsey
Copy link
Owner

ramsey commented Oct 31, 2023

I would recommend using UUIDv8 for your use-case, by the way. It's intended for custom layouts like the one you're using.

To minimize chance of collision, I would probably place the server ID in the bytes to the far right of the UUID layout.

vudaltsov added a commit to vudaltsov/ramsey-uuid that referenced this issue Nov 1, 2023
ramsey pushed a commit that referenced this issue Nov 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants