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 in doubt #102

Open
hujun8610 opened this issue May 28, 2021 · 4 comments
Open

uuid in doubt #102

hujun8610 opened this issue May 28, 2021 · 4 comments

Comments

@hujun8610
Copy link

As mentioned in the gitbook, Returning response with uuid is a better practice. But backend developer usually use database to store data.As we know,It is not recommended to use uuid in the database,So how to solve the problem or haveing good suggestion.

@brandur
Copy link
Member

brandur commented May 28, 2021

It may indeed be worthwhile to amend this one slightly, even if only to make it clear that it's certainly not a slam dunk to use UUIDs.

The way I see it, UUID advantages:

  • Operational errors, even across different tables/resources, are incredibly difficult to do by accident. Two different resources in two different tables might share the ID 23, but when it comes to UUIDs, no entity shares an ID with anything else in the database. This has legitimately saved me a couple times as I've run an UPDATE against the wrong table (again, accidentally).
  • Scales well beyond a single master database.

Sequence/serial advantages:

  • Much improved data locality in that new data will land in the same nodes in the B-tree/pages on disk. If you have a table with a lot of INSERTs happening, sequences will be far more performant I/O-wise.
  • More human friendly in that it's easy to copy or remember a comparatively short integer ID compared to a UUID.

@geemus
Copy link
Member

geemus commented May 28, 2021

Good callouts. Do you think we should move from a recommendation to an explanation of the tradeoffs then?

@holly-hacker
Copy link

holly-hacker commented Jul 24, 2024

It's worth mentioning that UUIDs also bring security benefits. An online store may leak information about their sales numbers if they expose integer IDs for their order numbers, allowing competitors to monitor their activity. Malicious actors may also use them to enumerate/scrape resources which may not be desirable. I'd personally recommend UUIDs over integers unless you expect to never expose these ids or have a good reason to use them.

EDIT 2024/09/16: UUIDs also match integers' data locality improvements if you use v7 UUIDs, which sort by the date when they were created.

@geemus
Copy link
Member

geemus commented Jul 30, 2024

I'd welcome a PR if you'd like to draft some updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants