RocketMQ message loss scenario analysis and how to solve it!
Since MQ is used in the project, it is inevitable to consider the problem of message loss. In some scenarios involving monetary transactions, message loss is still fatal. So what kinds of message loss scenarios exist in RocketMQ?
The producer generates a message and sends it to RocketMQ After RocketMQ receives the message, it must be stored in the disk, otherwise the data will be lost after power failure or downtime The consumer obtains the message consumption from RocketMQ. After the consumption is successful, the whole process ends.
In order to reduce the IO of the disk, RocketMQ will first write the message to the os cache, instead of writing it directly to the disk. The consumer gets the message from the os cache, which is similar to getting the message directly from the memory. The speed is faster, and after a while The time will be asynchronously flushed to the disk by the os thread, and the persistence of the message is truly completed at this time. During this process, if the message has not been flushed asynchronously and the Broker in RocketMQ goes down, the message will be lost. If the message has been flushed to the disk, but the data has not been backed up, once the disk is damaged, the message will also be lost
First, the producer sends a half message to RocketMQ. At this time, the consumer cannot consume the half message. If the half message fails to be sent, the corresponding rollback logic is executed. After the half message is sent successfully, and RocketMQ returns a successful response, the core link of the producer is executed. If the producer's own core link fails, roll back and notify RocketMQ to delete the half message If the core link of the producer is successfully executed, the RocketMQ commit half message is notified so that the consumer can consume this data
https://blog.csdn.net/LO_YUN/article/details/101673893
//Register a message listener to process messages consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context){ //Open the child thread to process the message asynchronously new Thread() { public void run() { // process the message } }.start(); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } });
Using the transaction mechanism to transmit messages will take many more steps than ordinary message transmission and consume performance Compared with asynchronous brushing, one is stored in disk and the other is stored in memory, and the speed is not an order of magnitude at all. For master-slave institutions, the leader needs to synchronize data to the follower You cannot consume asynchronously when consuming, you can only wait for the consumption to complete and then notify RocketMQ that the consumption is complete
0 Comments