Skip to content

Instantly share code, notes, and snippets.

@anton-yurchenko
Created November 4, 2024 19:03
Show Gist options
  • Save anton-yurchenko/fb9378c1703df2913828e2948476db68 to your computer and use it in GitHub Desktop.
Save anton-yurchenko/fb9378c1703df2913828e2948476db68 to your computer and use it in GitHub Desktop.
Redis Shards Distribution
import binascii
# Define nodes with precise slot ranges based on Redis cluster configuration
nodes = [
{"name": "node-0001", "ranges": [range(2730, 8192)]},
{"name": "node-0002", "ranges": [range(8192, 9767), range(12498, 16384)]},
{"name": "node-0003", "ranges": [range(0, 2730), range(9767, 12498)]}
]
# Define the keys to calculate hash slots
keys = [
"{trend:products}:monthly",
"{trend:products}:weekly",
"{trend:products}:biweekly",
"{trend:products}:daily",
"{trend:products}:yearly",
"{trend:offices}:monthly",
"{trend:offices}:weekly",
"{trend:cities}:monthly",
"{trend:cities}:weekly"
]
def calculate_slot(key):
# Extract hash tag within {} or use the entire key if no hash tag
if '{' in key and '}' in key:
hash_tag = key[key.index('{') + 1:key.index('}')]
else:
hash_tag = key
# Calculate CRC16 and map to one of the 16384 slots
crc = binascii.crc_hqx(hash_tag.encode('utf-8'), 0)
slot = crc % 16384
return slot
def find_node_for_slot(slot):
for node in nodes:
for slot_range in node["ranges"]:
if slot in slot_range:
return node["name"]
return "Unknown Node"
# Calculate and display the slot and node for each key
for key in keys:
slot = calculate_slot(key)
node = find_node_for_slot(slot)
print(f"Key: {key}, Slot: {slot}, Node: {node}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment