Database ID Strategies

Discussions center on best practices for database identifiers, debating sequential integers versus UUIDs, obfuscation techniques like Hashids, and using separate public IDs to avoid exposing internal primary keys in APIs or URLs for security and performance reasons.

➡️ Stable 0.5x Databases
2,711
Comments
20
Years Active
5
Top Authors
#3538
Topic ID

Activity Over Time

2007
1
2008
7
2009
46
2010
61
2011
58
2012
62
2013
69
2014
82
2015
47
2016
114
2017
131
2018
126
2019
174
2020
166
2021
267
2022
202
2023
373
2024
412
2025
294
2026
19

Keywords

GUID hashids.org e.g DB SQL ID MD5 PID ISO URL id ids keys integer identifier key identifiers numeric primary database

Sample Comments

Areading314 Oct 8, 2019 View on HN

Database IDs should be considered an implementation detail. It's better to assign a randomly-generated identifier for public-facing APIs for exactly this reason.

ponytech May 6, 2024 View on HN

My preference: using traditional incremental numeric IDs and obfuscate them with Hashids (https://hashids.org) when exposed publicly

tekne Oct 18, 2025 View on HN

Question: why not use UUIDv7 but encrypt the user-facing ID you hand out? Then it's just a quick decrypt-on-lookup, and you have the added bonus of e.g. being able to give different users different IDs

That sounds really overkill for simply wanting an identifier that doesn't change, isn't guessable, and doesn't reveal how many rows there are

mherrmann Feb 19, 2021 View on HN

What's wrong with integer IDs?

dimitrisnl Dec 15, 2025 View on HN

Noob question, but why no use ints for PK, and UUIDs for a public_id field?

s4i Dec 29, 2022 View on HN

I assume you mean internal IDs == primary keys in your database. If that's the case, and if your primary keys are UUIDv4's, ULIDs, KSUIDs, Cuids or similar... then why not? There are a lot of benefits, such as simpler SQL queries in your backend. You can also merge data from different databases into one as collissions are highly unlikely, which is not possible with numeric IDs.

throwaway74432 Apr 12, 2024 View on HN

It's a security vs convenience tradeoff. For most people, making the ID practically unguessable, while also preserving index locality in databases, is the right balance.

nostrademons Feb 19, 2021 View on HN

One of the best pieces of programming advice I got at Google was consider IDs carefully. This is not a solved problem, and there isn't a one-sized-fits-all solution. Rather, there are a bunch of guide rules, and then you need to understand your problem domain very well to choose good identifiers.An ID should be:1.) Actually an identifier, i.e. it needs to identify objects uniquely and must be immutable throughout the lifetime of the object.2.) Assigned at object-cre

onnnon Oct 6, 2025 View on HN

Don't expose primary keys to the public. Create separate external ids instead. I personally use BIGINT primary keys, with UUIDv4 external ids, but any random string will do.