• lemmyvore@feddit.nl
    link
    fedilink
    English
    arrow-up
    27
    arrow-down
    1
    ·
    3 months ago

    These days I follow a hard heuristic: Always use synthetic keys for database tables.

    And the way to follow this rule is fairly simple, but it has a few twists.

    For internal use, the best and most common key (in a relational database) is an auto-generated incremental sequence. But it it ok to use it externally? – across databases, across types of data storage, across APIs / services etc.

    It’s tempting to refer to the sequence number in API calls, after all they are going to that particular database and are only going to be used with it, right? Well not necessarily; the database and the code powering the API are different systems, who says there won’t be other apps accessing the database for example.

    The current OpSec school of thought is that sequence keys are an internal database mechanism and sequence numbers should only be used for internal consistency, never used as external references (even for the “local” API).

    Sequence keys also don’t offer any way to deal with creating duplicate data entries. If you’ve been around for a while you’ve seen this, the client sends the same “create” request twice for whatever reason (UI lets user multiple-click a button, client assumes timeout when in fact it had gone through etc.) Some programmers attempt to run heuristics on the data and ignore successive create attempts that look “too similar” but it can backfire in many ways.

    An UUID is pretty much universally supported nowadays, its designed to be unique across a vast amount of systems, doesn’t give anything away about your internal mechanisms, and if you ask the client to generate the UUID for create requests you can neatly solve the duplicate issue.

    Do keep in mind that this doesn’t solve the problem of bijection across many years and many systems and many databases. An entity may still acquire multiple UUID’s, even if they’re each individually perfectly fine.

    There can also be circumstances where you have to offer people a natural-looking key for general consumption. You can’t put UUID’s on car plates for example.

    • atzanteol@sh.itjust.works
      link
      fedilink
      English
      arrow-up
      7
      ·
      3 months ago

      I love UUID keys. Only drawback is they can be a bit difficult to share, but they solve so many other problems. Being able to generate them client-side makes batch inserting with dependencies easier, being globally unique means you can copy data between environments, never needing to “fix” a sequence again… So many up-sides.

    • CasualTee@beehaw.org
      link
      fedilink
      arrow-up
      4
      ·
      3 months ago

      On the topic of exposing sequence number in APIs, this has been a security issue in the past. Here is one I remember: https://www.reuters.com/article/us-cyber-travel-idUSKBN14G1I6/

      From the article:

      Two of the three big booking systems - Amadeus and Travelport - assign booking codes sequentially, making brute-force computer guesswork easier. Of the three, Amadeus, through its web portal CheckMyTrip, is especially vulnerable, Nohl said.

      The PNRs (flight booking code) have many more security issues, but at least nowadays, their sequential aspect should no longer be exposed.

      So that’s one more reason to be careful when exposing DB id in APIs, even if converted to a natural looking key or at least something easier to remember.

    • Ephera@lemmy.ml
      link
      fedilink
      arrow-up
      3
      ·
      3 months ago

      There can also be circumstances where you have to offer people a natural-looking key for general consumption. You can’t put UUID’s on car plates for example.

      Often times, the first section of the UUID is unique enough. With certain UI design choices, one can encourage users to normally work with that, while having the full UUID available in a detail view or from a copy-button.

      Another strategy I quite like, is to have the UUID as the definitely-always-unique identifier, and then have a separate name, which either the users can enter or we generate something like random adjective+animal.

      But yes, neither of those strategies would work for car plates.

      • lemmyvore@feddit.nl
        link
        fedilink
        English
        arrow-up
        6
        ·
        3 months ago

        Speaking of car plates, the Wikipedia pages for “Vehicle license plates of [insert country here]” are a rabbit hole.

        I was just reading the page for Romania the other day, speaking of uniqueness, and they had this issue apparently where the combinations overall were enough for the whole country but not enough for their capital city, so they had to hack an extra digit into the plates for the capital.

        • Reddfugee42@lemmy.world
          link
          fedilink
          arrow-up
          1
          ·
          3 months ago

          Seeing as just five alphanumeric symbols gets you over 60 million variations, exactly how the hell did they fuck up that hard

          • lemmyvore@feddit.nl
            link
            fedilink
            English
            arrow-up
            2
            ·
            3 months ago

            60M total but divided among 40 counties makes 1.5M variations per county and the capital city (which is its own county, like Berlin) went over that.

            I looked it up and Bucharest actually has only a 1.7M population so… I think it’s understandable that nobody expected an almost 1:1 person-to-car ratio. Exactly why and how they reached that crazy ratio I have no idea. 😆

            Told you it’s a crazy rabbit hole.

  • kevincox@lemmy.ml
    link
    fedilink
    arrow-up
    15
    ·
    3 months ago

    It is true, don’t do it.

    Even at huge companies like Google, lots of stuff was keyed on your email address. This was a huge problem so Google employees were not allowed to change their email for the longest time. Eventually they opened it up by request but they made it very clear that you would run into problems. So many systems and services would break. Over time I think most external services are pretty robust now, but lots of internal systems still use emails (or the username part of it) and have issues.

    IIUC Google accounts now use a random number as the key. But there are still places where the email is in use, slowly being fixed at massive cost.

      • kevincox@lemmy.ml
        link
        fedilink
        arrow-up
        1
        ·
        3 months ago

        I don’t think that is true. Not much at Google really bought into the UUID hype. At least not for internal interfaces. But really there is no difference between a UUID v4 and a large random number. UUID just specifies a standard formatting.

  • BrianTheeBiscuiteer@lemmy.world
    link
    fedilink
    arrow-up
    13
    ·
    3 months ago

    Got hands on experience with this. Wasn’t my design choice but I inherited an app with a database where one of the keys was tied to a completely separate database. I mean at the time it probably made sense but the most unlikely of scenarios actually happened: that other database, the one I had zero control over, was migrated to a new platform. All of those keys were synthetic so of course they were like, “Meh, why we gotta keep the old keys?” So post-migration my app becomes basically useless and I spent 6 hours writing migration code, some of it on off hours, to fix my data.

    So it’s questionable whether a foreign key of a completely different system is a natural key, but at the very least never use a key YOU don’t control.

    • state_electrician@discuss.tchncs.de
      link
      fedilink
      arrow-up
      1
      arrow-down
      6
      ·
      3 months ago

      I don’t think that holds true in all scenarios. You need to use a key that has some guarantees. In many systems you will use data you don’t control, like email addresses, IBANs, ISBNs, passport IDs and many more. You have zero control over those keys, but because each comes with certain guarantees, they might be suitable as a foreign key in your context.

      • Kissaki@programming.dev
        link
        fedilink
        English
        arrow-up
        10
        ·
        3 months ago

        People regularly change email addresses. Listing that as an example is a particularly bad example in my opinion.

      • BrianTheeBiscuiteer@lemmy.world
        link
        fedilink
        arrow-up
        5
        ·
        3 months ago

        Even in this scenario it’s feasible for standards to change. ISBN-15 becomes a thing and suddenly you have books that never get an ISBN-13 so your primary key constraints cause an error for trying to insert a null. Granted, you can see a lot of these changes coming but again, they come on a schedule you don’t control.

      • Eager Eagle@lemmy.world
        link
        fedilink
        English
        arrow-up
        4
        ·
        edit-2
        3 months ago

        These things can just be unique fields. I think the takeaway here is exactly to not use these unique fields as database keys if you have the option / if it’s up to you.

  • Aux@lemmy.world
    link
    fedilink
    arrow-up
    6
    arrow-down
    2
    ·
    3 months ago

    I didn’t even know that natural keys exist. Who is using them and why?