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

The method of preventing data loss by message queue in redis

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Preface:

Redis is a key-value storage system. Similar to Memcached, it supports relatively more value types, including string (string), list (linked list), set (collection), zset (sorted set-ordered collection), and hash (hash type). These data types support push/pop, add/remove, and take intersection union and difference sets, and richer operations, and these operations are atomic. On this basis, redis supports a variety of different sorting methods.

Message queuing is the message processing technology of Microsoft. It provides message processing and message queuing functions for any application in any combination of computers with Microsoft Windows installed, regardless of whether the computers are on the same network or online at the same time.

A message queuing network is any group of computers that can send messages back and forth to each other. Different computers in the network play different roles in ensuring that messages are processed smoothly. Some of them provide routing information to determine how to send messages, some keep important information about the entire network, and some simply send and receive messages.

Redis implements message queues in two forms:

Broadcast subscription mode: Redis-based Pub/Sub mechanism. Once a client publish a message into a key, all subscribe clients will trigger the event cluster subscription mode: based on Redis List bidirectional + atomicity + BRPOP

In the case of Redis message queuing, messages may be lost when Redis goes down (also depending on the persistence policy). If the recipient does not have a retransmission and authentication mechanism, the data in the Redis will be lost. Therefore, using Redis as a message queue is usually a scenario where the accuracy of the message is not particularly high.

If you absolutely guarantee the final consistency of the data and ensure that the message is not lost 100%, then you need to:

1. When writing, transaction processing is required to be enabled to ensure the success of the write.

2. Redis is configured so that any changes must be persisted in real time. For example, if the storage end is a disk, each change is synchronously written to the disk. Redis supports configuration in this way, but doing so will completely disappear its in-memory database features and make its performance very poor.

3. The consumer side should also implement the transaction mode, and then come back to delete the message after the completion of the processing.

4. Multithreading or concurrent processing of multiple terminals at the same time can be avoided by locking.

The requirements of 3 / 4 need to be implemented on their own, can be considered together, or implemented in another queue, but it is better to implement a counter inside the queue. Hash format plus a field plus numerical value, list first push a numerical value to the bottom, string head plus a numerical value plus a separator, you can do a simple counter, although the soil, the better is practical enough.

With the exception of specific systems, such a strong consistency is generally not required, and it is not difficult to implement, but the performance can be very poor.

Bank payment business requires strict transaction consistency, while Internet business generally uses a tricky way, that is, it can tolerate a small amount of data loss in a very short period of time in exchange for higher performance.

For example, the above redis processing can be changed to 1000 pieces of data and then the real disk, that is, write to disk. In extreme cases, if there is a sudden power outage, there is a risk that these 1000 pieces of data may be lost. Of course, the probability of this happening is also very low (away from Lanxiang excavator? So it is acceptable in most scenarios

These are the details of how redis message queuing prevents data loss. Please follow other related articles for more information.

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