Redis 批量插入:协议与内联命令
Redis mass insertion: protocol vs inline commands
对于我的任务,我需要尽快将大量数据加载到 Redis 中。看起来这篇文章对我的情况是正确的:https://redis.io/topics/mass-insert
本文从给出一个使用 redis-cli 的多个内联 SET
命令的示例开始。然后他们继续生成 Redis 协议并再次将其与 redis-cli 一起使用。他们没有解释使用 Redis 协议的原因或好处。
Redis 协议的使用有点困难,它会产生更多的流量。我想知道,使用 Redis 协议而不是简单的单行命令的原因是什么?可能尽管数据更大,但 Redis 解析它更容易(也更快)?
说得好。
Only a small percentage of clients support non-blocking I/O, and not
all the clients are able to parse the replies in an efficient way in
order to maximize throughput. For all this reasons the preferred way
to mass import data into Redis is to generate a text file containing
the Redis protocol, in raw format, in order to call the commands
needed to insert the required data.
我的理解是直接使用Redis协议时模拟了一个客户端,这将受益于突出显示的点。
根据您提供的文档,我尝试了这些脚本:
test.rb
def gen_redis_proto(*cmd)
proto = ""
proto << "*"+cmd.length.to_s+"\r\n"
cmd.each{|arg|
proto << "$"+arg.to_s.bytesize.to_s+"\r\n"
proto << arg.to_s+"\r\n"
}
proto
end
(0...100000).each{|n|
STDOUT.write(gen_redis_proto("SET","Key#{n}","Value#{n}"))
}
test_no_protocol.rb
(0...100000).each{|n|
STDOUT.write("SET Key#{n} Value#{n}\r\n")
}
ruby test.rb > 100k_prot.txt
ruby test_no_protocol.rb > 100k_no_prot.txt
time cat 100k.txt | redis-cli --pipe
time cat 100k_no_prot.txt | redis-cli --pipe
我得到了这些结果:
teixeira: ~/Whosebug $ time cat 100k.txt | redis-cli --pipe
All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 100000
real 0m0.168s
user 0m0.025s
sys 0m0.015s
(5 arquivo(s), 6,6Mb)
teixeira: ~/Whosebug $ time cat 100k_no_prot.txt | redis-cli --pipe
All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 100000
real 0m0.433s
user 0m0.026s
sys 0m0.012s
对于我的任务,我需要尽快将大量数据加载到 Redis 中。看起来这篇文章对我的情况是正确的:https://redis.io/topics/mass-insert
本文从给出一个使用 redis-cli 的多个内联 SET
命令的示例开始。然后他们继续生成 Redis 协议并再次将其与 redis-cli 一起使用。他们没有解释使用 Redis 协议的原因或好处。
Redis 协议的使用有点困难,它会产生更多的流量。我想知道,使用 Redis 协议而不是简单的单行命令的原因是什么?可能尽管数据更大,但 Redis 解析它更容易(也更快)?
说得好。
Only a small percentage of clients support non-blocking I/O, and not all the clients are able to parse the replies in an efficient way in order to maximize throughput. For all this reasons the preferred way to mass import data into Redis is to generate a text file containing the Redis protocol, in raw format, in order to call the commands needed to insert the required data.
我的理解是直接使用Redis协议时模拟了一个客户端,这将受益于突出显示的点。
根据您提供的文档,我尝试了这些脚本:
test.rb
def gen_redis_proto(*cmd)
proto = ""
proto << "*"+cmd.length.to_s+"\r\n"
cmd.each{|arg|
proto << "$"+arg.to_s.bytesize.to_s+"\r\n"
proto << arg.to_s+"\r\n"
}
proto
end
(0...100000).each{|n|
STDOUT.write(gen_redis_proto("SET","Key#{n}","Value#{n}"))
}
test_no_protocol.rb
(0...100000).each{|n|
STDOUT.write("SET Key#{n} Value#{n}\r\n")
}
ruby test.rb > 100k_prot.txt
ruby test_no_protocol.rb > 100k_no_prot.txt
time cat 100k.txt | redis-cli --pipe
time cat 100k_no_prot.txt | redis-cli --pipe
我得到了这些结果:
teixeira: ~/Whosebug $ time cat 100k.txt | redis-cli --pipe
All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 100000
real 0m0.168s
user 0m0.025s
sys 0m0.015s
(5 arquivo(s), 6,6Mb)
teixeira: ~/Whosebug $ time cat 100k_no_prot.txt | redis-cli --pipe
All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 100000
real 0m0.433s
user 0m0.026s
sys 0m0.012s