Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is TTL implemented in Pika?

Tags:

pika

I'd like my queue to drop messages not processed within a certain time.

I already do this in the consumer by recording the publish time. However, in the case that no-one is subcribing, it would be better for the queue to simply drop stale messages.

Can I set a expiry time (TTL) in messages in Pika. The RabbitMQ docs talk about it but i don't see TTL references in the Pika docs.

like image 406
user48956 Avatar asked Sep 21 '17 23:09

user48956


People also ask

What is TTL in RabbitMQ?

RabbitMQ allows you to set TTL (time to live) for both messages and queues. This is controlled by optional queue arguments and best done using a policy. Message TTL can be applied to a single queue, a group of queues or applied on the message-by-message basis. TTL settings also can be enforced by operator policies.

Is RabbitMQ round robin?

By default, RabbitMQ will send each message to the next consumer, in sequence. On average every consumer will get the same number of messages. This way of distributing messages is called round-robin.

How does RabbitMQ works?

RabbitMQ is a messaging broker - an intermediary for messaging. It gives your applications a common platform to send and receive messages, and your messages a safe place to live until received.


1 Answers

You can set the per message TTL using the expiration flag on the BasicProperties object, as seen in the pika documentation here.

Using it would look something like this.

channel.basic_publish(
    exchange='',
    routing_key='hello_world',
    properties=pika.BasicProperties(
        expiration='60000',
    ),
    body='my message'
)

Keep in mind that the expiration policy is expressed using milliseconds as string, so 60000 would translate to 60 seconds.

You can read more about message based TTL and it's caveats here.

like image 110
eandersson Avatar answered Sep 27 '22 20:09

eandersson