this post was submitted on 20 Jun 2024
97 points (94.5% liked)

Programming

17344 readers
410 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 29 points 4 months ago (7 children)

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.

[–] [email protected] 4 points 4 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.

load more comments (6 replies)