RabbitMQ lazy queue usage

Explanation: The lazy queue is to solve the problem of message accumulation. When the speed of producing messages by producers is much higher than that of consumers consuming messages, a large number of messages will accumulate in the queue, and the number of messages stored in the queue is limited. When the number is exceeded, messages will be lost. It is actually not realistic to expand the queue, because the messages of the queue are stored in the memory, and the memory also has an upper limit.

Starting from RabbitMQ version 3.6.0, the concept of lazy queues (Lazy Queues) has been added. After receiving messages, lazy queues are stored in disk instead of memory. Only when consumers consume messages, messages are read from disk and loaded into memory.

use

@RabbitListener way

    @RabbitListener(queuesToDeclare = @Queue(
            name = "lazy.queue",
            durable = "true",
            arguments = @Argument(name="x-queue-mode", value="lazy")
    ))
    public void listenerQueue(String message){
    
    
        System.out.println("message = " + message);
    }

@Bean way

    @Bean
    public Queue lazyQueue(){
    
    
        return QueueBuilder
                .durable("lazy.queue")
                .lazy()
                .build();
    }

The normal queue and the lazy queue each send 100,000 messages, and observe the difference

(normal queue)

insert image description here


(lazy queue)

insert image description here

the difference:

  • Lazy queues use a larger amount of memory than normal queues because of the increased writing/reading to disk

  • The lazy queue consumes messages, and the speed is more stable than the normal queue, of course, the speed is slower (the above is only an accidental situation, most of the cases are slower than the normal queue)

Summarize

Lazy queues are used to solve the problem of message accumulation

Guess you like

Origin blog.csdn.net/qq_42108331/article/details/131842773