Redis Swiss Army Knife: Slow query, Pipeline and publish subscription

Source: Internet
Author: User
Tags redis server

1. Slow query 1.1 life cycle of slow query
步骤一:client通过网络向Redis发送一条命令步骤二:由于Redis是单线程应用,可以把Redis想像成一个队列,client执行的所有命令都在排队等着server端执行步骤三:Redis服务端按顺序执行命令步骤四:server端把命令结果通过网络返回给client

Description

慢查询发生在命令执行过程中,不包含网络延迟时间及排除等待执行的时间客户端超时不一定慢查询,但慢查询是客户端超时的一个可能因素    
1.2 Configuration items for slow queries
slowlog-max-len             慢查询队列的长度slowlog-log-slower-than     慢查询阈值(单位:微秒),执行时间超过阀值的命令会被加入慢查询命令    如果设置为0,则会记录所有命令,通常在需要记录每条命令的执行时间时使用    如果设置为小于0,则不记录任何命令slowlog list                慢查询记录

Description

慢查询是一个先进先出的队列,如果一条命令在执行过程中被列入慢查询范围内,就会被放入一个队列,这个队列是基于Redis的列表来实现,而且这个队列是固定长度的,当队列的长度达到固定长度时,最先被放入队列就会被pop出去慢查询队列保存在内存之中,不会做持久化,当Redis重启之后就会消失    
1.3 Slow Query Configuration method 1.3.1 Modify configuration file restart
修改/etc/redis.conf配置文件,配置慢查询修改配置方式应该在第一次配置Redis中时配置完成,生产后不建议修改配置文件
1.3.2 Dynamic Configuration
127.0.0.1:6379> config get slowlog-max-len1) "slowlog-max-len"2) "128"127.0.0.1:6379> config get slowlog-log-slower-than1) "slowlog-log-slower-than"2) "10000"127.0.0.1:6379> config set slowlog-max-len 1000OK127.0.0.1:6379> config get slowlog-max-len1) "slowlog-max-len"2) "1000"127.0.0.1:6379> config set slowlog-log-slower-than 1000OK127.0.0.1:6379> config get slowlog-log-slower-than1) "slowlog-log-slower-than"2) "1000"
1.4 Slow Query command
slowlog get [n]         获取慢查询队列slowlog len             获取慢查询队列长度slowlog reset           清空慢查询队列
1.5 Redis Slow query operation and maintenance experience
slowlog-max-len不要设置过小,通常设置1000左右slowlog-log-slower-than不要设置过大,默认10ms,通常设置1ms理解命令生命周期可以通过slowlog get等命令定期将慢查询命令持久化到其他数据源,这样就可以查到很多历史的慢查询操作命令在生产环境中,不管slowlog-max-len设置多大,当慢查询命令逐步增多时,最开始的慢查询命令会被丢掉当需要查询历史数据时,这些慢查询命令都是非常关键的可以使用开源软件来实现这些功能,对于分析解决Redis问题是非常有帮助的
The concept of 2.pipeline2.1 pipeline

One network command communication model:

client通过网络传输命令到server端server端通过计算得到命令执行结果server端把命令执行结果给client    

At this time

一次网络命令通信时间=1次网络时间 + 1次命令时间

At this point, if there are multiple commands, then only one entry command is executed.

n次时间 = n次网络时间 + n次命令时间

Redis executes commands very quickly, but network transmissions can be very delayed,

Pipeline is to package a batch of commands, then transfer them to the server for batch calculations, and then return the execution results to the client in order

Time required for N-time network communication using the pipeline model

1次pipeline(n条命令) = 1次网络时间 + n次命令时间
2.2 Examples
import redisimport timeclient = redis.StrictRedis(host=‘192.168.81.100‘,port=6379)start_time = time.time()for i in range(10000):    client.hset(‘hashkey‘,‘field%d‘ % i,‘value%d‘ % i)ctime = time.time()print(client.hlen(‘hashkey‘))print(ctime - start_time)

Program execution Results:

100002.0011684894561768

In the above example, writing 10,000 hash records directly to Redis takes 2 seconds

Write 10,000 hash records to Redis using the pipeline method

import redisimport timeclient = redis.StrictRedis(host=‘192.168.81.100‘,port=6379)start_time = time.time()for i in range(100):    pipeline = client.pipeline()    j = i * 100    while j < (i+ 1) * 100:        pipeline.hset(‘hashkey1‘,‘field%d‘ % j * 100,‘value%d‘ % i)        j += 1    pipeline.execute()ctime = time.time()print(client.hlen(‘hashkey1‘))print(ctime - start_time)

Program execution Results:

100000.3175079822540283

You can see that using the pipeline method sends 100 commands to the Redis server each time, 100 times takes only 0.31 seconds, you can see the use of pipeline can save network transmission time

2.3 Pipeline Usage Recommendations
首先要注意每次pipeline携带数据量不能太大pipeline可以提高Redis批量处理的并发的能力,但是并不能无节制的使用如果批量执行的命令数量过大,则很容易对网络及客户端造成很大影响,此时可以把命令分割,每次发送少量的命令到服务端执行pipeline每次只能作用在一个Redis节点上
3. Publishing a role in a subscription 3.1 release subscription
发布者(publisher)订阅者(subscriber)频道(channel)
3.2 Model for publishing subscriptions
Redis server就相当于频道发布者是一个redis-cli,通过redis server发布消息订阅者也是于一个redis-cli,如果订阅了这个频道,就可以通过redis server获取消息

Description

发布订阅就是一个生产者消费者模型每个订阅者可以订阅多个频道发布者发布消息后,订阅者就可以收到不同频道的消息订阅者不可以接收未订阅频道的消息订阅者订阅某个频道后,Redis无法做消息的堆积,不能接收频道被订阅之前发布的消息
3.3 Commands for publishing subscriptions
publish channel message         发布消息subscribe [channel]             订阅频道unsubscribe [channel]           取消订阅psubscribe [pattern...]         订阅指定模式的频道punsubscribe [pattern...]       退订指定模式的频道pubsub channels                 列出至少有一个订阅者的频道pubsub numsub [channel...]      列表给定频道的订阅者数量pubsub numpat                   

Example:

Open a Terminal 1

127.0.0.1:6379> subscribe sohu_tv               # 订阅sohu_tv频道Reading messages... (press Ctrl-C to quit)1) "subscribe"2) "sohu_tv"3) (integer) 1

Open a Terminal 2 again

127.0.0.1:6379> publish sohu_tv ‘hello python‘      # sohu_tv频道发布消息(integer) 1127.0.0.1:6379> publish sohu_tv ‘hello world‘       # sohu_tv频道发布消息(integer) 3

You can see messages that have been received by SOHU_TC in Terminal 1

127.0.0.1:6379> subscribe sohu_tvReading messages... (press Ctrl-C to quit)1) "subscribe"2) "sohu_tv"3) (integer) 11) "message"2) "sohu_tv"3) "hello python"1) "message"2) "sohu_tv"3) "hello world"

Open Terminal 3, unsubscribe from SOHU_TC Channel

127.0.0.1:6379> unsubscribe sohu_tv1) "unsubscribe"2) "sohu_tv"3) (integer) 0
3.4 Publishing subscriptions and Message Queuing
redis server维护一个队列消息发布者,相当于一个redis-cli,通过redis server发布消息消息订阅者就相当于一个redis-cli,所有的消息订阅者通过redis server抢消息发布者发布的消息

Redis Swiss Army Knife: Slow query, Pipeline and publish subscription

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.