Home Database Mysql Tutorial Redis实战(12)订阅和发布消息

Redis实战(12)订阅和发布消息

Jun 07, 2016 pm 05:37 PM
redis

发布订阅(pub/sub)是一种消息通信模式,主要的目的是解耦消息发布者和消息订阅者之间的耦合,这点和设计模式中的观察者模式比较相似。pub/sub不仅仅解决发布者和

发布订阅(pub/sub)是一种消息通信模式,主要的目的是解耦消息发布者和消息订阅者之间的
耦合,这点和设计模式中的观察者模式比较相似。pub/sub 不仅仅解决发布者和订阅者直接
代码级别耦合也解决两者在物理部署上的耦合。redis 作为一个pub/sub 的server,在订阅者
和发布者之间起到了消息路由的功能。订阅者可以通过subscribe 和psubscribe 命令向redis
server 订阅自己感兴趣的消息类型,redis 将消息类型称为通道(channel)。当发布者通过
publish 命令向redis server 发送特定类型的消息时。订阅该消息类型的全部client 都会收到
此消息。这里消息的传递是多对多的。一个client 可以订阅多个channel,也可以向多个channel
发送消息。
下面做个实验。这里使用3 不同的client, client1 用于订阅tv1 这个channel 的消息,client2
用于订阅tv1 和tv2 这2 个chanel 的消息,client3 用于发布tv1 和tv2 的消息。

Client1:

212155349.png

Client2:

212226969.png

Client3:

212314958.png

下面看Client1和Client2的接收情况:

212435865.png

212504435.png

Client1和Client2都收到了tv1的消息,然后我们再使用Client3发布一条消息:

212623854.png

看看Clien1,Client2的接收情况

212728278.png

212801287.png

下面将详细的解释一下上面的例子
1、client1 订阅了tv1 这个channel 这个频道的消息,client2 订阅了tv1 和tv2 这2 个频道的
消息
2、client3 是用于发布tv1 和tv2 这2 个频道的消息发布者
3、接下来我们在client3 发布了一条消息”publish tv1 program1”,大家可以看到这条消息是
发往tv1 这个频道的
4、理所当然的client1 和client2 都接收到了这个频道的消息
5、 然后client3 又发布了一条消息”publish tv2 program2”,这条消息是发往tv2 的,由于
client1 并没有订阅tv1,所以client1 的结果中并没有显示出任何结果,但client2 订阅了这个
频道,所以client2 是会有返回结果的。
我们也可以用psubscribe tv*的方式批量订阅以tv 开头的频道的内容。
看完这个小例子后应该对pub/sub 功能有了一个感性的认识。需要注意的是当一个连接通过
subscribe 或者psubscribe 订阅通道后就进入订阅模式。在这种模式除了再订阅额外的通道或
者用unsubscribe 或者punsubscribe 命令退出订阅模式,就不能再发送其他命令。另外使用
psubscribe 命令订阅多个通配符通道,如果一个消息匹配上了多个通道模式的话,会多次收
到同一个消息。

 

Pipeline 批量发送请求

redis 是一个cs 模式的tcp server,使用和http 类似的请求响应协议。一个client 可以通过一
个socket 连接发起多个请求命令。每个请求命令发出后client 通常会阻塞并等待redis 服务
处理,redis 处理完后请求命令后会将结果通过响应报文返回给client。基本的通信过程如下:

Client: INCR X Server: 1 Client: INCR X Server: 2 Client: INCR X Server: 3 Client: INCR X Server: 4

 

基本上四个命令需要8 个tcp 报文才能完成。由于通信会有网络延迟,假如从client 和server
之间的包传输时间需要0.125 秒。那么上面的四个命令8 个报文至少会需要1 秒才能完成。
这样即使redis 每秒能处理100 个命令,,而我们的client 也只能一秒钟发出四个命令。这显
示没有充分利用redis 的处理能力,怎么样解决这个问题呢? 我们可以利用pipeline 的方式
从client 打包多条命令一起发出,不需要等待单条命令的响应返回,而redis 服务端会处理
完多条命令后会将多条命令的处理结果打包到一起返回给客户端。通信过程如下

Client: INCR X Client: INCR X Client: INCR X Client: INCR X Server: 1 Server: 2 Server: 3 Server: 4

 

假设不会因为tcp 报文过长而被拆分。可能两个tcp 报文就能完成四条命令,client 可以将四
个incr 命令放到一个tcp 报文一起发送,server 则可以将四条命令的处理结果放到一个tcp
报文返回。通过pipeline 方式当有大批量的操作时候,我们可以节省很多原来浪费在网络延
迟的时间,需要注意到是用pipeline 方式打包命令发送,redis 必须在处理完所有命令前先缓
存起所有命令的处理结果。打包的命令越多,缓存消耗内存也越多。所以并不是打包的命令
越多越好。具体多少合适需要根据具体情况测试。

具体是否使用pipeline 必须要基于大家手中的
网络情况来决定,不能一切都按最新最好的技术来实施,因为它有可能不是最适合你的。

 

 

本文出自 “phper-每天一点点~” 博客,请务必保留此出处

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to use the redis command How to use the redis command Apr 10, 2025 pm 08:45 PM

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

How to read the source code of redis How to read the source code of redis Apr 10, 2025 pm 08:27 PM

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

How to clear redis data How to clear redis data Apr 10, 2025 pm 10:06 PM

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

How to view all keys in redis How to view all keys in redis Apr 10, 2025 pm 07:15 PM

To view all keys in Redis, there are three ways: use the KEYS command to return all keys that match the specified pattern; use the SCAN command to iterate over the keys and return a set of keys; use the INFO command to get the total number of keys.

How to start the server with redis How to start the server with redis Apr 10, 2025 pm 08:12 PM

The steps to start a Redis server include: Install Redis according to the operating system. Start the Redis service via redis-server (Linux/macOS) or redis-server.exe (Windows). Use the redis-cli ping (Linux/macOS) or redis-cli.exe ping (Windows) command to check the service status. Use a Redis client, such as redis-cli, Python, or Node.js, to access the server.

See all articles