Redis实战(12)订阅和发布消息
发布订阅(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:
Client2:
Client3:
下面看Client1和Client2的接收情况:
Client1和Client2都收到了tv1的消息,然后我们再使用Client3发布一条消息:
看看Clien1,Client2的接收情况
下面将详细的解释一下上面的例子
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。基本的通信过程如下:
基本上四个命令需要8 个tcp 报文才能完成。由于通信会有网络延迟,假如从client 和server
之间的包传输时间需要0.125 秒。那么上面的四个命令8 个报文至少会需要1 秒才能完成。
这样即使redis 每秒能处理100 个命令,,而我们的client 也只能一秒钟发出四个命令。这显
示没有充分利用redis 的处理能力,怎么样解决这个问题呢? 我们可以利用pipeline 的方式
从client 打包多条命令一起发出,不需要等待单条命令的响应返回,而redis 服务端会处理
完多条命令后会将多条命令的处理结果打包到一起返回给客户端。通信过程如下
假设不会因为tcp 报文过长而被拆分。可能两个tcp 报文就能完成四条命令,client 可以将四
个incr 命令放到一个tcp 报文一起发送,server 则可以将四条命令的处理结果放到一个tcp
报文返回。通过pipeline 方式当有大批量的操作时候,我们可以节省很多原来浪费在网络延
迟的时间,需要注意到是用pipeline 方式打包命令发送,redis 必须在处理完所有命令前先缓
存起所有命令的处理结果。打包的命令越多,缓存消耗内存也越多。所以并不是打包的命令
越多越好。具体多少合适需要根据具体情况测试。
具体是否使用pipeline 必须要基于大家手中的
网络情况来决定,不能一切都按最新最好的技术来实施,因为它有可能不是最适合你的。
本文出自 “phper-每天一点点~” 博客,请务必保留此出处

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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 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.

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.

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).

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

On CentOS systems, you can limit the execution time of Lua scripts by modifying Redis configuration files or using Redis commands to prevent malicious scripts from consuming too much resources. Method 1: Modify the Redis configuration file and locate the Redis configuration file: The Redis configuration file is usually located in /etc/redis/redis.conf. Edit configuration file: Open the configuration file using a text editor (such as vi or nano): sudovi/etc/redis/redis.conf Set the Lua script execution time limit: Add or modify the following lines in the configuration file to set the maximum execution time of the Lua script (unit: milliseconds)

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information
