How to generate a database id that is secure and not easily duplicated?

How to generate a database id that is secure and not easily duplicated?

Using auto-incrementing primary keys may expose database information, using random numbers to generate ids, and using random numbers as IDs has the risk of duplication. Here are some suggestions.

1. UUID (Universal Unique Identification Code): You can consider using UUID as the primary key. UUID is a 128-bit number whose generation algorithm is designed to ensure that IDs generated on different computers and times have an extremely low probability of duplication. So using a UUID provides a good guarantee of uniqueness while not leaking the state of the database.

2. Ordered snowflake algorithm (such as Twitter's Snowflake algorithm): The IDs generated by this algorithm are basically ordered and incremental, but they are composed of multiple parts, including timestamps, worker node IDs, and serial numbers. This makes each ID highly unique, while making it difficult to guess the IDs of other records in the database.

3. Add checksum redundancy to random numbers: If you insist on using random numbers, consider adding additional checksums or redundant data to random IDs. This way, in the case of duplicates, the checksum can help detect the problem and act accordingly.

Of course, the rule is that dead people are alive. The specific choice of a plan needs to be decided in combination with business and trade-offs.

Guess you like

Origin blog.csdn.net/youngerxsd/article/details/131001528