以下內(nèi)容來源官網(wǎng):
https://redis.io/topics/cluster-spec
Redis Cluster implements a concept called hash tags that can be used in order to force certain keys to be stored in the same hash slot. However during manual resharding, multi-key operations may become unavailable for some time while single key operations are always available.
Redis分片就是一個hash的過程:根據(jù)hash值分配到不同的slot.
slot = CRC16(key)%16384
但是有時候我們又希望能夠?qū)㈥P(guān)聯(lián)的數(shù)據(jù)放到同一個slot上去,處于查詢的效率也好或者業(yè)務(wù)本身的需要也好,那么我們就需要用到hash tags了。
具體用法如下:
key----> {user:login}:id
這樣redis 只會取{}中的數(shù)據(jù)進(jìn)行hash操作,相同的前綴自然就映射到同一個slot 上去了。
需要注意的地方:
我們要合理利用HASH TAGS 以防止數(shù)據(jù)傾斜,建議在對數(shù)據(jù)有特殊要求的情況使用,否則數(shù)據(jù)的離散性變得很差。