Skip to content

Instantly share code, notes, and snippets.

@emzeq
Created July 9, 2012 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emzeq/3079571 to your computer and use it in GitHub Desktop.
Save emzeq/3079571 to your computer and use it in GitHub Desktop.
Consistent Shard
# Append shard ranges as new shard groups are brought online
# Format: [Cutoff User ID, Min Shard, Max Shard]
USER_ID_SHARD_MAPPING = [
[0, 1, 10],
[30000, 11, 20],
[500000, 21, 50],
]
# Consistent shard that falls within a shard range by the user’s ID
def consistent_shard(user)
*, min, max = USER_ID_SHARD_MAPPING.
select { |s| s[0] < user.id }.last
(user.id % max - min) + min
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment