❔ Best way to create unique order reference numbers?
First of all, do I do it at the DB level (identity field?) or the application level? GUID? hashes?
What is the most common approach to ensure that they are unique and don't give out too much information?
38 Replies
Database level, as it is also responsible for ensuring the integrity of unique primary keys
I recommend using a guid
DB if possible, and most DBs can do both guids or sequences
like this?
depends on your database, but with for example SqlServer you don't need that configuration (assuming your reference is your primary key)
you jsut set it to be a
Guid
type primary key and ya donesqlserver yes, and the primary key is a separate id in this case. but maybe it shouldnt be
there are things like this to obfuscate integer IDs, haven't tried it myself yet https://hashids.org/
Hashids
Hashids
Generate short obfuscated strings from integers. Use in url shorteners or as unique ids.
@Pobiegaso you think the PK should be the reference number?
Can be useful, if you want to avoid enumerable IDs
I dont know enough about your domain to say either way
but its unusual to need more than one unique identifier 🙂
you're right, with a resrevation it makes no sense for the order number not to be the PK 😛
A reservation either exists or it doesnt, so yeah, seems about right.
Now, it seems reasonable that you will have a url somewhere that contains this value
api/reservations/{id}
or similaryep
if its just a plain int, people will try to enumerate it, 100%
so a guid, hashid or snowflake might be better
do i want to let SQL or ef core generate the GUID?
doesnt really matter, but usually you let the DB handle it if you can
@Pobiegagotcha, thx
you want to avoid doing it in application code, but EF can be trusted, as can the DB
but i've never seen a guid reference number before
is that really common?
or order number
guids aren't particularly user friendly imo
exactly
lol
imagine if they were on the phone with support and had to read out a GUID
that's why i wanted to know what the most common approach is
i know guids basically ensure uniqueness but they aren't user friendly like you say
You could do a youtube-style id hash
I might have to do some unique convention for my own application i guess, maybe there needs to be actual identifiers in hte order/reference number for staff to quickly identify the type of reservation
instead of just random characters
Sure, if you need it. You might want to look at something like snowflake ids, they do something similar but with time
random googled example: https://github.com/RobThree/IdGen
@Pobiegainteresting, ill check it out
and if its just an ID you want, but wiht parts of it being human readable, thats doable too
volvo uses a fixed position ID system for their cars, for example 🙂
where the first 3 indicate the model, the first 12 contain engine and salesversion etc
@Pobiegathat'll generate the id on teh applicaiton level though?
yep, it would
since now ID generation is non-trivial
the idgen above that is
will it ensure uniqueness?
yep
discord and twitter both use something similar to that
I'll use that for now and try to create my own convention later if needed. Not sure if there needs to be actual information in the reference number
just seems like a "good to have" but not necessary by any means
yeah well if it's good enough for them it's good enough for me lol
haha
this sounds like something a BA or product owner should have decided before it ended up on your desk 🙂
probably lol, i have to make a lot of decisions myself since im the only backend developer lol
relatable
(am full stack + random devops/IT things)
like, if they wanted partially human readable IDs, thats not something you randomly come up with 😄
thats decided ahead of time
if its "just make some unique ids" you can do whatever you feel like
I like both the snowflake and the hashid ways tbh
yeah so I'll go with that since that saves me time
and he didnt specify otherwise
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.