如何高效地向Redis插入亿级数据? [英] How to insert Billion of data to Redis efficiently?

查看:34
本文介绍了如何高效地向Redis插入亿级数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约 20 亿个键值对,我想有效地将​​它们加载到 Redis 中.我目前正在使用 Python 并使用了 redis-py 中记录的 Pipe.如何加快以下方法的速度?

I have around 2 billion key-value pairs and I want to load them into Redis efficiently. I am currently using Python and used Pipe as documented by the redis-py. How can I speed the following approach up?

import redis

def load(pdt_dict):
    """
    Load data into redis.

    Parameters
    ----------
    pdt_dict : Dict[str, str]
        To be stored in Redis
    """
    redIs = redis.Redis()
    pipe = redIs.pipeline()
    for key in pdt_dict.keys():
        pipe.hmset(self.seller + ":" + str(key), pdt_dict[key])
    pipe.execute()

推荐答案

关于问题和示例代码的几点说明.

A few points regarding the question and sample code.

  1. 流水线不是灵丹妙药 - 在使用它之前,您需要了解它的作用.流水线的作用是批处理多个作为批量发送的操作,以及来自服务器的响应.您获得的是,每个操作的网络往返时间被批次的网络往返时间替换.但是无限大的批次是对资源的真正消耗——你需要保持它们的尺寸足够小才能有效.根据经验,我通常尝试将每个管道的目标设置为 60KB,并且由于每个数据都不同,因此管道中的实际操作数量也不同.假设您的密钥及其值约为 1KB,您需要每 60 次左右的操作调用 pipeline.execute().

除非我严重误解,否则不应运行此代码.您使用 HMSET 就好像它是 SET 一样,因此您基本上缺少哈希的字段->值映射.哈希 (HMSET) 和字符串 (SET) 是不同的数据类型,因此应相应地使用.

Unless I grossly misunderstand, this code shouldn't run. You're using HMSET as if it is SET, so you're basically missing the field->value mapping of Hashes. Hashs (HMSET) and Strings (SET) are different data types and should therefore be used accordingly.

似乎这个小循环负责整个十亿数据"——如果是这样的话,那么运行代码的服务器不仅会像疯了一样交换,除非它有很多RAM 来保存字典,它也会非常低效(无论 Python 的速度如何).您需要通过运行此过程的多个实例来并行化数据插入.

It appears as if this one little loop is in charge of the entire "Billion of data" - if that is the case, not only would your server running the code be swapping like crazy unless it has a lot of RAM to hold the dictionary, it would also be very ineffective (regardless Python's speed). You need to parallelize the data insertion by running multiple instances of this process.

您是否远程连接到 Redis?如果是这样,网络可能会限制您的性能.

Are you connecting to Redis remotely? If so, the network may be limiting your performance.

考虑您的 Redis 设置 - 也许可以调整/调整这些设置以获得更好的性能,假设它确实是一个瓶颈.

Consider your Redis' settings - perhaps these can be tweaked/tuned for better performance for this task, assuming that it is indeed a bottleneck.

这篇关于如何高效地向Redis插入亿级数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆