Redis 似乎根据 get/set 操作的次数而更加明显地受到影响,而与数据的大小无关
追注:不管是使用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的内存量在费用方面并没有太大的差异,但不确定是怎样的情况。