Ranking database record identifiers

In our web application, we want to randomize record identifiers. The reason is that we want to hide how many records are already in the database, and we have unregistered things. In case the identifiers would be prime incremental numbers, it would be easy to guess the identifiers of unregistered things.

As I can see, there are three ways to do this:

Prime random numbers

Algorithm:

  • Create a random number when pasting.
  • Make sure the identifier is already in use. If yes goto 1.
  • Use this identifier.

Pro

  • Easy
  • works with any size or type of identifier (32 bit, 64 bit, variable length, string)

Contra

  • a transaction is required for possible race conditions (the algorithm is not atomic)

UUID

Pro

  • ,

Contra

  • URL- URL ("#{id}--#{page_title}), UUID
  • , UUID ?

:

  • nextval (atomic!)
  • , , ID

Pro

  • ( )

Contra

  • ID .
  • - / ,

-

@emboss

Pro

  • ID

Contra

  • ( , )

/

@viktor tron ​​

, URL-, . ( ..).

Pro

  • / URL ()

Contra

  • , .
  • , URL- , URL-

, . ? ? Ruby on Rails 3.x PostgreSQL 9.x.

: ! , YouTube , . , . , ( ID), , URL, . , , - - , , (URL- ).

. . ( , , , "", .)

+5
6

.

, , "" .

: .

, , , .

DB

, , , . , 100000 N :)

+13

: . . URL.

, URL-, -/, https://github.com/norman/friendly_id

friendly_id slug- slug , , , .

, ID:)

+3

- " ". , SHA-256 X H ; , () X H.

Ruby :

@hashed_id = Digest::SHA2.new << SHA_SALT << @foo.id

, , , . , .

+1

, .

, , : , , , . , , . : -, . , 0 , .

, :

SecureRandom . rand , , "" , . , "" , . / , , , , .

UUID,

, , . , .

/

. , , , , . , , , , , . , . -, . , : . , ID 1-100 , , , .

, , . , , , .

, ? " " , URL-, .

+1
+1

, , , . , . , , . , , -: . - , , , , .

To achieve this, you must create a new column called "number" and implement one of these strategies on it. Then, if you need to switch to a new strategy, it will be much easier for you: instead of doing it Model.find(id), you just do it Model.find_by_number(number).

0
source

All Articles