get_shard_num_by_key_id returns the Redis shard number (zero-indexed)
        given a key id.
        Keyword arguments:
        key_id -- the key id (e.g. '12345' or 'anythingcangohere')

        This method is critical in how the Redis cluster sharding works. We
        emulate twemproxy's md5 distribution algorithm.

def get_shard_num_by_key_id(self, key_id):
        """
        get_shard_num_by_key_id returns the Redis shard number (zero-indexed)
        given a key id.
        Keyword arguments:
        key_id -- the key id (e.g. '12345' or 'anythingcangohere')

        This method is critical in how the Redis cluster sharding works. We
        emulate twemproxy's md5 distribution algorithm.
        """
        # TODO: support other hash functions?
        m = hashlib.md5(str(key_id).encode('ascii')).hexdigest()
        # Below is borrowed from
        # https://github.com/twitter/twemproxy/blob/master/src/hashkit/nc_md5.c
        val = (int(m[0:2], 16) |
               int(m[2:4], 16) << 8 |
               int(m[4:6], 16) << 16 |
               int(m[6:8], 16) << 24)

        return val % self.num_shards()