An engineer responsible for matching couriers to deliveries needed to cache massive numbers of routing estimates to meet tight latency targets, but found repeated, nearly identical route calculations were overloading a shared cache layer. Coordinates were deduplicated with H3 hex IDs, and those IDs became Redis keys. Running on a Redis cluster revealed a crucial constraint: Redis computes 16,384 hash slots via CRC16(key) mod 16384 and only allows multi-key commands like MGET/MSET when all keys land in the same slot. Unique H3-derived keys scattered across slots, so a single logical MGET was being split into many per-key requests, exploding round trips and latency.
The fix combined Redis hash tags, slot-aware key grouping, an atomic write-with-expiry technique, and data-format changes. Wrapping a common tag in braces forces Redis to hash only the tag, so the engineer generated a small set of tagged integers (routing:v1:%d) by brute force to ensure tags mapped to slots spread across primaries. Keys are grouped by computed slot before fan-out so each node gets one large multi-key command; chunking must respect destination nodes. Because MSET has no TTL, a Lua EVAL script performs multi-key writes with expiry atomically. Switching cached values from JSON to CSV cut CPU, and thorough benchmarking informed these trade-offs.
Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.