开发者

Unique ids in database

开发者 https://www.devze.com 2023-03-24 08:20 出处:网络
I\'m often seeing sites there IDs in urls looks like this: http://battl开发者_C百科elog.battlefield.com/bf3/forum/view/2811510711334022807/

I'm often seeing sites there IDs in urls looks like this: http://battl开发者_C百科elog.battlefield.com/bf3/forum/view/2811510711334022807/ How do they achieve this? This isn't regular autoincrement field. All ids seems to be random, but at fixed width. What are the benefits?


One approach is to hide the real id (which is auto_increment) in a big random-looking number. For example:

  • the first digit shows where the real Id starts
  • the second digit shows the length
  • the real id is located as determined by the previous two points
  • everything else is random numbers up to a predefined length

Other options are:

  • UUID - your example doesn't seem to use one, but it is universally unique identifier and is sometimes used.
  • snowflake - twitter's new id generation method (or something like it)

Note that there are two important aspects here:

  • don't let users easily guess IDs - this is handled by my example. But if there are protected resources, always protect them with a security check in addition to the unguessable ID
  • provide cluster-wide unique ids - UUIDs, snowflake, and the likes, guarantee that an ID is unique across a cluster of machines.


GUIDs/UUIDs such as this make it much harder to guess what will and won't be a valid ID number. Unless you have 10^18 users, the valid IDs will be very sparse compared to invalid ones.

0

精彩评论

暂无评论...
验证码 换一张
取 消