存档

文章标签 ‘rename’

RedisCluster数据覆盖问题

2017年6月28日 没有评论

碰到一个典型的问题:
redis的set数据,有大量的读redis和少量的写源(同时删除redis中对应的key),在读的时候做miss回源。

碰到的问题:
少量写后删除key,会同时有大量的读去回源,源压力暴涨,且导致了多次写。回源越慢,源的压力就越大。

计划由读miss回源改为写时覆盖,问题是:set好像没办法覆盖,只能先删除key后再加,删除key时,还是会引发读回源。

看到redis支持rename,就想到一个办法:
少量写的时候,先把数据写到一个新key里,然后rename成老key,从命令上看是支持覆盖的:https://redis.io/commands/rename

RENAME key newkey

Available since 1.0.0.
Time complexity: O(1)
Renames key to newkey. It returns an error when key does not exist. If newkey already exists it is overwritten, when this happens RENAME executes an implicit DEL operation, so if the deleted key contains a very big value it may cause high latency even if RENAME itself is usually a constant-time operation.
Note: Before Redis 3.2.0, an error is returned if source and destination names are the same.
Return value
Simple string reply

Examples
redis> SET mykey "Hello"
"OK"
redis> RENAME mykey myotherkey
"OK"
redis> GET myotherkey
"Hello"
redis> 

结果是出错:

>rename newkey oldkey
(error) CROSSSLOT Keys in request don't hash to the same slot

更多内容…

分类: 工作 标签: , , , ,