Reputation: 1019
In this docs, https://docs.pingcap.com/tidb/stable/dev-guide-unique-serial-number-generation It says "Finally, note that the IDs generated by the above two solutions are not random enough to be directly used as primary keys for TiDB tables. In practice, you can perform bit-reverse on the generated IDs to get more random new IDs." However, does anyone know what this bit-reverse refers to? What specific modifications shall I do to make snowflake style ID more random?
Upvotes: 1
Views: 104
Reputation: 1400
In general, UUID generators ensure uniqueness, but most UUID algorithms produce IDs that are somewhat ordered. This orderliness can lead to hotspots in TiDB, and the purpose of bit-reverse is to transform ordered IDs into more random ones.
for example:
import uuid
# Generate a UUID
original_uuid = uuid.uuid4()
# Convert the UUID to an integer and reverse the bits
original_integer = int(original_uuid)
reversed_integer = int(bin(original_integer)[2:][::-1], 2)
# Convert the reversed integer back to a UUID
reversed_uuid = uuid.UUID(int=reversed_integer)
print("Original UUID:", original_uuid)
print("Reversed UUID:", reversed_uuid)
Upvotes: 2