Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Can redis be used as a message queue?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces whether redis can be used as a message queue, which can be used for reference by friends who need it. I hope you will learn a lot after reading this article. Next, let the editor take you to learn about it.

Application scenarios:

Like second kill. Instantly write a large number of orders to the database, resulting in the database can not respond in a timely manner. At this time, Redis can be used as the message queue, all the data that needs to be written is first written to the Redis message queue, and then at the same time, the php-cli process is opened in the server to read the data in the queue and write to the database asynchronously. Using redis for message queuing may cause message loss because there is no confirmation mechanism for message reception. For large programs, you should use something like RabitMQ to do professional message queues.

1. Use publish/subscribe as the message queue

Features: a message publisher (producer) can correspond to multiple message subscribers (consumers). When a message is published to a message queue, all message subscribers can receive the message. Suitable for distributed message distribution. Client waits for messages from the publisher side in a blocked manner. Multiple consumers cannot speed up the consumption of messages.

Message production:

$params = json_encode (['xroomuid' = > $x_uid, 'phone' = > $phone]); $redis- > publish (' test',$params); / / test indicates the name of the published channel

Message consumption (php-cli mode runs):

$redis = new Redis (); $redis- > pconnect ('127.0.0.1'); / / must use pconnect persistent connection / / set the redis connection never times out. The default 60s timeout disconnects $redis- > setOption (Redis::OPT_READ_TIMEOUT,-1); $redis- > subscribe (array ('test'),' callback'); / / test represents the channel name, and the callback callback function name functioncallback ($redis, $chan, $msg) {/ / a function to handle received messages $params = json_decode ($msg,true);....}

The difference between pconnect and connect:

Connect: the connection is released after the script ends.

Pconnect: the connection is not released after the end of the script, and the connection remains in the php-fpm process.

Therefore, using pconnect instead of connect can reduce the consumption of frequently establishing redis connections.

2. Use list as the redis message queue

Features: a message producer, corresponding to a message consumer. Multiple consumers can speed up the consumption of messages.

Message production:

$redis = newRedis (); $redis- > connect ('127.0.0.1'); / / push all the data that needs to be written to the database to the queue (complex data can be encoded into strings by json first) $list = json_encode (['xroomuid' = > $x_uid, 'phone' = > $phone,' goods_id' = > $goodsId, 'add_time' = > time (),' num_field' = > $num_field]); $redis- > lpush ('winer',$list)

Note: if brpop consumption data is not successfully written to the database, it will result in data loss. It is strongly required that the production data be backed up to redis or file for the second time.

Message consumption (php-cli mode runs):

Note: if MySQL does not actively close the connection, the connection will be automatically disconnected after up to eight hours at a time.

Other:

The second kill scene prevents goods from being oversold:

1. Set the quantity of goods in the database to be unsigned, that is, negative numbers are not allowed. False is returned when the number of items is updated to a negative number.

2. The quantity of goods is stored in the list queue of Redis. Pop deletes an element out of the queue every time it is snapped up.

/ / queue for for storing the quantity of goods ($j = 1; $j

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report