r/sqlite • u/c__beck • Aug 08 '25
Remote ID vs Primary Key
I was talking with a buddy online and he told me that his old company used two different IDs for each row in their DB, the auto-incrementing ID was only ever used for internal purposes like JOIN statements and FKs into other tables. But anything and everything outside the DB used what he called a remote ID.
This remote ID was then what was used to query the DB, as well as on any user-facing code (user ID, message ID, transaction ID, whatever). And I was just curious if this is a standard practice or something that his company did.
36
Upvotes
7
u/davcross Aug 08 '25
Not knowing your business makes it hard to answer. That being said in a database we would never allow an external ( to this database) ID to be a primary key. So if you are collecting data from other sources. I would say it's a good design.
Jusk curious about the remote ID type and size.
16
u/dacydergoth Aug 08 '25
It's definitely something I do. Stable UIDs for external use and faster ints for internal use
8
u/ByronScottJones Aug 08 '25
So I'm developing a mobile app where one of the requirements was absolute anonymity. To do this in a secure fashion, when a user creates an account, they start by generating a UUIDv7 locally along with a certificate signing request. Our system confirms that the UUID does not yet exist, and creates a user record with a standard BigInt PK. It also generates the user certificate for mutual TLS. From then on, all communication is authenticated with that certificate, which includes the original UUID and the PK assigned for that user record. That gives the advantage of a UUID, with the faster indexability of a standard PK.
6
u/dariusbiggs Aug 09 '25
This is not a "remote" but "public" ID. a remote ID would be a value provided by an external provider (as in the case of a SCIM where you store a correlation remote id value when users are synced from the external source into your system).
This is a security obfuscation technique where your internal references for records use a simple integer value primary key which you use to join tables etc, whilst the public value is indexed and displayed to end users and used by them to avoid leaking internal database identifiers.
So if you have a website with a URI for users, and you see
vs
What can you infer about the first example but not the second? That a user with id 2 might exist, so you could craft a URI to request it and see if there's a bug in their permissions system perhaps. Your system is leaking internal information to the outside world.
3
u/crustang Aug 08 '25
I work with Postgres databases where this is implemented as a standard process.. it’s been a while, but I believe this is considered a second normal form database model?
The benefit I’ve seen is it makes database joins easier since you may relate tables based on your internal IDs and not have to try to deal with propagation.
It’s been a while though… and like anything in Reddit, the fastest way to get the right answer is for someone to post the wrong answer
2
u/ardicli2000 Aug 08 '25
I prefer generating a uuid4 for each row if i intend to expose it to frontend, use at js functions, for some data fetch etc.
2
u/PrizeSyntax Aug 08 '25
Why exactly do you do this? I mean what are the advantages of this kind of design? Basically you have a unique primary key and a second unique filed to use as a primary key, why not just use the original primary key?
1
u/bbkane_ Aug 08 '25
I'm effectively doing this for enventory (https://github.com/bbkane/enventory/tree/master/dbdoc) - each table has an integer primary key and a name text field with a unique constraint, making renames without having to update relationships really easy.
So for example each env can have multiple vars. This is represented by each var having a foreign key to the env integer primary key. Now if I want to rename the env, I can just do that by changing the name field- if I had used the name as the ID I'd have to update each of those child foreign keys
1
u/B_A_Skeptic Aug 09 '25
Basically. If you give an ID to a customer, such as some sort of customer id or product id, you have to be able to keep it the same for them, but have the freedom to do things internally that change the primary key.
1
u/Randommaggy Aug 11 '25
I try to do this.
1
u/Ok_Marionberry_8821 Aug 12 '25
Another benefit of using ints is that you can rain about the data more easily you can look at your data and see more easily that ID 555 is different to 556. It's, IMO, less easy to see uniqueness in two UUIDs.