English 中文(简体)
Assign 10-digit char user ids to 1 of 1000 servers
原标题:

Looking to shard a database and to assign different users to different home servers based on their user id. User IDs are 10 character strings, e.g., "f4gKUKkj91" ... each server has an ID of 1 - 1000. How can I create a hash function in php to uniquely and consistently assign each user id to a specific shard ? If the user id were an integer I could do userid % 1000 ... but since they are alphanumeric I m not sure how to do this with even distribution in php.

Thank you!

最佳回答

You could use crc32() which gives you a numerical hash of the alphanumeric userids.

问题回答

This is not a perfect algorithm, as there will be a slight preference for smaller ID numbers. It assumes the user IDs are spread out fairly evenly, so to speak; if they re not, the distribution may not be good.

Figure out what your alphabet is and put it in a string like $str = 0123456789abcdefghijklmnopqrstuvwxxyzABCDEFGHIJKLMNOPQRSTUVXYZ ; This string has n characters. Now, we will essentially treat the user ID as a base n integer.

For each character, find its index in the string (0-based). Take this index and multiply it with nx, where x is the character position in your original string, starting with 0. Add all of these together, and take the modulo of the sum.

You probably only want to do this for a few characters - once you ve read a few characters, the sum becomes quite big, and PHP can t handle it properly unless you resort to using functions suitable for large integer math (you can certainly use GMP and such, but it may not be ideal for your case). If you are using native integers, stop before the maximum possible sum goes beyond 2^31 (nx+nx+1+...+n).

You can use either start from the beginning or going backwards (going backwards corresponds to usual integer notation). One of them may be more suitable, depending on how the ID generation works.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签