1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Redisの KEYS コマンドの利用は避けよう

Last updated at Posted at 2024-08-21

What is KEYS command

Redisには KEYS というコマンドでキーを確認することができます。
引数に pattern を渡すことで、文字列部分一致でキーの登録の確認ができるので使い勝手が良いかと思います。

You can check registered keys by calling KEYS to Redis.
When you pass pattern to KEYS, you can get by partial string matching.

KEYS command makes performance trouble

image.png

Redisのパフォーマンスに影響を与えます。
他のRequestが Timeout します。

KEYS command makes bad performance effect to Redis and at the time other requests may timeout.

image.png

What shoud we do

  1. 代用のコマンド : alternative command
  2. 使用する場合(非推奨): call it to reader node.(not reccomended)

alternative command

SCAN コマンドを使用する
Use SCAN command

3.x系だと以下
https://github.com/redis/redis-rb/blob/v3.3.5/lib/redis.rb#L2471-L2488

4.xからは type(data type を指定) が使える
From 4.x library we can specify data type
https://github.com/redis/redis-rb/blob/v4.8.1/lib/redis/commands/keys.rb#L6-L27

irb(main):040:0> redis.scan(0, match: 'hoge*', count: 10)
=> ["4352", ["hoge_fuga:81141b0d9496d36e84eb8b6b2f319282", "hoge_fuga:5e49a2fe8396bd1f0bfa794aa6d3843d", "hoge_fuga:f4f0b518955b495584b6842de41bf8ac", "hoge_fuga:d0a0f8a87963df7f09f4d2dd42ff7046", "hoge_fuga:f43edc65a4e3c3057c4e901fee431315", "hoge_fuga:2eafa75ce47218635d94a198de4aa772", "hoge_fuga:59f716434011531905aae7535180ab94", "hoge_fuga:b956e1345782920854460232759e91f4", "hoge_fuga:a924c207c2de2182e380dc0162784285", "hoge_fuga:4c395e91dd05b5165c6109cd47d93e1f"]]

xxx.0001.apne1.cache.amazonaws.com:6379> scan 0 MATCH hoge_fuga:* TYPE string
1) "2304"
2)  1) "hoge_fuga:81141b0d9496d36e84eb8b6b2f319282"
    2) "hoge_fuga:5e49a2fe8396bd1f0bfa794aa6d3843d"
    3) "hoge_fuga:f4f0b518955b495584b6842de41bf8ac"
    4) "hoge_fuga:d0a0f8a87963df7f09f4d2dd42ff7046"
    5) "hoge_fuga:f43edc65a4e3c3057c4e901fee431315"
    6) "hoge_fuga:59f716434011531905aae7535180ab94"
    7) "hoge_fuga:b956e1345782920854460232759e91f4"
    8) "hoge_fuga:a924c207c2de2182e380dc0162784285"
    9) "hoge_fuga:4c395e91dd05b5165c6109cd47d93e1f"
   10) "hoge_fuga:7f3a16157431eba3fdbdb1dd0a680c6a"

call it to reader node.(not reccomended)

どうしても使用したい場合は サービスで使用されている writer 側ではなくて、 reader 側で使用する
If you want to use KEYS command, you should call it to reader node.

irb(main):032:0> redis=Redis.new({host: 'xxx.ng.0001.apne1.cache.amazonaws.com', port: 1234, db: 0, driver: :ruby, timeout: 
1})
=> #<Redis client v3.3.5 for redis://xxx.i3gcdw.ng.0001.apne1.cache.amazonaws.com:1234/0>
$ redis-cli -h xxx.ng.0001.apne1.cache.amazonaws.com
xxx.ng.0001.apne1.cache.amazonaws.com:1234>

ちなみに、ElastiCache Redisは設定で禁止コマンドを登録することができるので、
KEYSコマンドを禁止にしてしまうのは良い戦略かと思います。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?