惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
J
Java Code Geeks
I
InfoQ
腾讯CDC
Vercel News
Vercel News
IT之家
IT之家
V
Visual Studio Blog
P
Proofpoint News Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
G
Google Developers Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 叶小钗
有赞技术团队
有赞技术团队
月光博客
月光博客
Martin Fowler
Martin Fowler
量子位
L
LangChain Blog
B
Blog
Last Week in AI
Last Week in AI
博客园 - 司徒正美
Microsoft Security Blog
Microsoft Security Blog
博客园 - 聂微东
Microsoft Azure Blog
Microsoft Azure Blog
A
About on SuperTechFans

Jiajun的技术笔记

你好,2026! TiDB 源码阅读(六):TiDB Coprocessor 源码解析 性能优化的核心思想 TiDB 源码阅读(五):索引 TiDB 源码阅读(四):AST、逻辑计划、物理计划 CockroachDB Serverless Architecture podman 无故退出 Cursor Control-L (CTRL-L) Keyboard Shortcuts in Terminal Replace docker with podman Using xmonad with xfce4 A RC script for freebsd frpc 自己动手写一个k8s controller AI 会取代你的(编程)岗位吗? 自建DERP服务器提升Tailscale连接速度(使用Nginx转发) 自动升级Docker容器 再读《程序员修炼之道-从小工到专家》 让浏览器下载文件 再读《软件随想录》/《黑客与画家》/《软技能》 HTTP 压力测试中的 Coordinated Omission 2的补码 编程语言中的 context 是什么? flutter macOS 构建出错 Flatpak 使用小记 Golang CAS 操作是怎么实现的 PostgreSQL 当MQ来使用 Clash 结合 工作VPN 的网络设计 使用 PostgreSQL 搭建 JuiceFS PostgreSQL 配置优化和日志分析 有GitHub Copilot?那就可以搭建你的ChatGPT4服务 窗口函数的使用(以PG为例)
为啥Redis使用pipelining会更快?
Jiajun Huang · 2020-11-02 · via Jiajun的技术笔记

这是一个很考究细节的问题,大部分人都会说:因为减少了网络开销,那么,看如下例子:

import time

import redis

client = redis.Redis(decode_responses=True)
count = 10000


def no_pipelining():
    for i in range(count):
        client.set("test:nopp:{}".format(i), i, ex=100)


def with_pipelining():
    pp = client.pipeline()

    for i in range(count):
        pp.set("test:withpp:{}".format(i), i, ex=100)

    pp.execute()


if __name__ == "__main__":
    start = time.time()
    no_pipelining()
    mid = time.time()
    with_pipelining()
    end = time.time()

    print("no_pipelining: {} seconds; with_pipelining: {} seconds".format(mid - start, end - mid))

为什么执行结果相差如此之大呢?

$ python test.py
no_pipelining: 2.3809118270874023 seconds; with_pipelining: 0.4370129108428955 seconds

因为这是连接本地的redis,所以网络开销非常小,当然,这里仍然有一部分是网络开销影响,可是除此之外是否还有其它影响因素呢? 答案是有,比如OS进程调度,当不使用管道时,Redis处理每个命令之间是有时间空隙的,因此OS很有可能会将Redis进程转换为sleep状态, 然后运行其它程序,而使用pipelining时,可以提高CPU利用率,Redis空闲的时间没有那么多,因此,这也是pipelining速度会更快的 重要原因之一。


ref: