Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Redisはデータの大きさに関係なくget/setの回数で遅さが際立ってくるぽい

More than 1 year has passed since last update.

追記: get,setに限らずexpire, delなど、とにかくredisにアクセスすると同様の結果になる。

Redisに短時間で大量のget/setをするとデータのサイズに関係なくむちゃくちゃ時間がかかってしまうようだ。
(変化自体は線形に増えているのだけど、体感時間としてはっきりわかってくるのが10000回あたりから)

以下、検証コード。

require 'benchmark'
require 'redis'

redis = Redis.new

repeats = [100, 1000, 10_000, 50_000, 100_000, 200_000, 500_000, 1_000_000]

repeats.each do |repeat|
  exec_time = Benchmark.realtime do
    (1..repeat).each do |_i|
      redis.set('record', 'input')
      record = redis.get('record')
    end
  end
  puts "#{repeat}: Redis working in #{exec_time.round(3)} sec"
end

puts "--------------"

repeats.each do |repeat|
    exec_time = Benchmark.realtime do
    (1..repeat).each do |_i|
      record = 'input'
      output = record.dup
    end
  end
  puts "#{repeat}: Variable working in #{exec_time.round(3)} sec"
end

実行結果 (Win10, Ruby 2.5.0, redis-server 3.2.1)

100: Redis working in 0.036 sec
1000: Redis working in 0.233 sec
10000: Redis working in 2.614 sec
50000: Redis working in 12.352 sec
100000: Redis working in 25.814 sec
200000: Redis working in 51.055 sec
500000: Redis working in 132.253 sec
1000000: Redis working in 279.764 sec
--------------
100: Variable working in 0.0 sec
1000: Variable working in 0.0 sec
10000: Variable working in 0.003 sec
50000: Variable working in 0.011 sec
100000: Variable working in 0.022 sec
200000: Variable working in 0.046 sec
500000: Variable working in 0.109 sec
1000000: Variable working in 0.216 sec

インスタンス間で共用しなければいけないデータで、かつメモリに乗せる必要があるという状況以外ではRedisの使用は避けたほうがよいのかも。
ElastiCache使うのとEC2のメモリ量増やすのとで、料金の差がそんなに大きな違いにならない気もするんだけど、どうなんでしょ。

ymstshinichiro
色々あってバックエンド専業っぽくなってきました。最近はRailsにどっぷり。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away