How to design a million digital user ID
Level1-public – Exposes the permission document
The foreword 0.
When we were designing the platform account system, we met such a requirement: the user ID registered on the platform was presented as n digits. At first glance, the requirement is simple, in one sentence. But on closer reflection, there’s a lot to think about:
- Is the user ID generated randomly or incremented sequentially?
- If it is randomly generated:
- How do I ensure that the ID generated is unique each time?
- If it is sequential increment:
- Will it reveal the scale of platform users and increase security risks?
- How to ensure the sequential increment of ids in distributed high concurrency?
- If the number of users reaches the upper n-digit limit, how to automatically extend the ID?
1. The train of thought
First of all, determine whether the ID is self-increasing or random. From the above thinking, it can be found that the self-increasing of user ID will bring some security risks: for example, when there is a bug in the system, when the user information can be queried by the user ID, it will face the privacy disclosure problem of obtaining the full amount of user information on the platform by traversing the user ID. In addition, the increment will also expose the scale of users on the platform, which is not good for business behavior. Therefore, the user ID must be generated randomly.
So if the user ID is randomly generated, how do you ensure that it’s a unique value every time? Here are two ideas:
- Design an algorithm, through mathematical operation, with a certain input parameter, get a one – to – one mapping of the out parameter, the range of the out parameter n is a number in the size range. The mathematical expression is:
Where, m= number of input parameters, uniqueness determined by m input parameters, n= range size of y.
- With the help of engineering ideas, this requirement is realized by using various middleware. Such as with MySQL+Redis.
2. Plan
For the first idea, we can refer to the unique random number mapping algorithm
Here the main development of the use of engineering ideas, with the help of MySQL+Redis+ timer implementation.
Create a table t_user_ID_pool in MySQL. This table contains all user ids that meet the requirements, for example, 000000-999999.
The timer logic is as follows: every hour, read the key whose key is ID_pool from redis. If id_pool does not exist, read 1000 user ids randomly from the database and store them into the ID_pool of Redis in the form of queue.
When generating a user ID on the service side, pop a value from the ID_pool of redis as the user ID. Because redis has a natural single-threaded structure, you don’t have to worry about getting the same ID in high concurrency.
Above, is about how to design millions of digital user ID some immature scheme, throw a brick to lead to jade, welcome to put forward deficiencies or the industry more mature and perfect scheme.