{"id":12973,"library":"cluster-key-slot","title":"Redis Cluster Key Slot Calculator","description":"The `cluster-key-slot` package provides a high-performance utility for calculating CRC16 hashes, specifically designed for determining key slots in Redis Cluster environments. This functionality is crucial for Node.js Redis clients such as `node_redis`, `ioredis`, and `redis-clustr` to correctly route commands to the appropriate Redis instance. The current stable version is 1.1.2. The package appears to have a stable, low-cadence release schedule, with updates primarily focusing on performance enhancements or supporting new key types like binary keys (introduced in v1.1.0). Its key differentiator lies in its optimized performance compared to older `Buffer`-based CRC calculations, offering significantly faster slot computations for both keys with and without tags, and supporting Redis Cluster key tags (e.g., `somekey{actualTag}`). It ships with TypeScript types, facilitating its use in modern TypeScript projects.","status":"active","version":"1.1.2","language":"javascript","source_language":"en","source_url":"https://github.com/Salakar/cluster-key-slot","tags":["javascript","redis","hash","crc","slot","calc","node","node_redis","typescript"],"install":[{"cmd":"npm install cluster-key-slot","lang":"bash","label":"npm"},{"cmd":"yarn add cluster-key-slot","lang":"bash","label":"yarn"},{"cmd":"pnpm add cluster-key-slot","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"`calculateSlot` is the default export in ESM and the `module.exports` in CommonJS. Using named import syntax in ESM is also generally supported by TypeScript's `esModuleInterop` for dual CJS/ESM packages, but the explicit default import is preferred for clarity.","wrong":"import { calculateSlot } from 'cluster-key-slot';\n// Incorrect for CJS:\nconst { calculateSlot } = require('cluster-key-slot');","symbol":"calculateSlot","correct":"import calculateSlot from 'cluster-key-slot';\n// Or for CJS:\nconst calculateSlot = require('cluster-key-slot');"},{"note":"`generateMulti` is a named export for both ESM and CommonJS. Attempting to import it as a default export will result in `undefined`.","wrong":"import generateMulti from 'cluster-key-slot';\n// Incorrect CJS:\nconst generateMulti = require('cluster-key-slot').generateMulti;","symbol":"generateMulti","correct":"import { generateMulti } from 'cluster-key-slot';\n// Or for CJS:\nconst { generateMulti } = require('cluster-key-slot');"},{"note":"The package ships with TypeScript definitions. Specific types for functions or return values can be imported as needed for stronger type checking.","symbol":"Types","correct":"import type { ClusterKeySlotCalculator } from 'cluster-key-slot';"}],"quickstart":{"code":"import calculateSlot, { generateMulti } from 'cluster-key-slot';\nimport { Buffer } from 'node:buffer'; // Explicit import for Buffer\n\nasync function runRedisSlotCalculations() {\n  console.log('--- Redis Key Slot Calculations ---');\n\n  // Calculate a single slot for a string key\n  const slotString = calculateSlot('test:key:{butOnlyThis}redis');\n  console.log(`Slot for 'test:key:{butOnlyThis}redis': ${slotString}`);\n\n  // Calculate a single slot for a Buffer key\n  const bufferKey = Buffer.from([0x7b, 0x7d, 0x2a, 0x68, 0x65, 0x6c, 0x6c, 0x6f]); // Example buffer \"{}\"*hello\n  const slotBuffer = calculateSlot(bufferKey);\n  console.log(`Slot for Buffer key ${bufferKey.toString('hex')}: ${slotBuffer}`);\n\n  // Calculate a single slot for multiple keys (requires key tags to match or returns -1)\n  const keysForMultiOp = [\n    'test:key:{butOnlyThis}redis',\n    'something:key45:{butOnlyThis}hello',\n    'example:key46:{butOnlyThis}foobar'\n  ];\n  const slotForRedisMulti = generateMulti(keysForMultiOp);\n  if (slotForRedisMulti === -1) {\n    console.log(`Keys for multi-operation (${keysForMultiOp.join(', ')}) do not share a common slot tag.`);\n  } else {\n    console.log(`Common slot for multiple keys: ${slotForRedisMulti}`);\n  }\n\n  // Example of keys not sharing a common tag\n  const differentTagKeys = [\n    'key1{tagA}',\n    'key2{tagB}'\n  ];\n  const slotDifferentTags = generateMulti(differentTagKeys);\n  console.log(`Common slot for different tags (${differentTagKeys.join(', ')}): ${slotDifferentTags} (expected -1)`);\n\n  console.log('\\nCalculations complete.');\n}\n\nrunRedisSlotCalculations().catch(console.error);","lang":"typescript","description":"Demonstrates calculating Redis cluster key slots for single string and buffer keys, and determining a common slot for multiple keys with matching tags, highlighting the `-1` return for non-matching tags."},"warnings":[{"fix":"Always check the return value of `generateMulti` for `-1` before proceeding with multi-key Redis commands.","message":"The `generateMulti` function returns `-1` if the provided array of keys does not share a common hash tag or if any key's slot does not match the base slot (derived from the first key). Users should explicitly check for `-1` to handle cases where multi-key operations are not possible.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Upgrade to `cluster-key-slot@^1.1.0` or newer to ensure full support for binary keys (Buffers).","message":"Older versions of `cluster-key-slot` (prior to v1.1.0) did not explicitly support binary keys (Buffers). Passing Buffer objects to `calculateSlot` on older versions might lead to incorrect results or errors. Ensure you are on v1.1.0 or newer for reliable Buffer support.","severity":"gotcha","affected_versions":"<1.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"For ESM: `import calculateSlot from 'cluster-key-slot';`. For CJS: `const calculateSlot = require('cluster-key-slot');`","cause":"Attempting to import `calculateSlot` as a named export in ESM when it's primarily a default export, or destructured in CommonJS.","error":"TypeError: calculateSlot is not a function"},{"fix":"For ESM: `import { generateMulti } from 'cluster-key-slot';`. For CJS: `const { generateMulti } = require('cluster-key-slot');`","cause":"Attempting to import `generateMulti` as a default export in ESM or accessing it incorrectly in CommonJS.","error":"TypeError: generateMulti is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}