Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Fency: an idempotency barrier for RabbitMQ cons...

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

Fency: an idempotency barrier for RabbitMQ consumers

Even when a sender application sends a message only once, the receiver application may receive the message more than once.

The term idempotent is used in mathematics to describe a function that produces the same result if it is applied to itself: f(x) = f(f(x)). In Messaging this concepts translates into a message that has the same effect whether it is received once or multiple times. This means that a message can safely be resent without causing any problems even if the receiver receives duplicates of the same message.

The recipient can explicitly de-dupe messages by keeping track of messages that it already received. A unique message identifier simplifies this task and helps detect those cases where two legitimate messages with the same message content arrive.

In order to detect and eliminate duplicate messages based on the message identifier, the message recipient has to keep a list of already received message identifiers.

https://github.com/fencyio

Avatar for Gilles Robert

Gilles Robert

April 24, 2019

Other Decks in Programming

Transcript

  1. Problem • Even when a producer only sends a message

    once, the consumer may receive the message more than once.
  2. From the RabbitMQ documentation « In the event of network

    failure (or a node crashing), messages can be duplicated, and consumers must be prepared to handle them. […] the simplest way to handle this is to ensure that your consumers handle messages in an idempotent way […]. » https://www.rabbitmq.com/reliability.html#consumer
  3. How can a consumer deal with duplicate messages? • Design

    a consumer to be an Idempotent Consumer one that can safely receive the same message multiple times.
  4. Idempotent • A function that produces the same result if

    it is applied to itself: f(x) = f(f(x))
  5. How to design an idempotent consumer? • The consumer has

    to keep track of already received messages through a unique ID
  6. Introducing Fency • Homegrown solution available on Github: github.com/fencyio/fency •

    Spring Boot starter using Redis as backend to store message metadata
  7. Runtime Get message MessageListenerContainer MessageListener MessageInterceptor @IdempotentConsumer IdempotencyBarrier @Aspect Message

    Context 1 2 store message properties retrieve intercept 3 4 If found: discarded 5 If not found: save find proceed
  8. Fine. But my storage will explode!!! What about retention period?

    • A scheduled cron will delete your keys from Redis, by default, every 1st of the month • Configurable property according to your load/business
  9. How can I test it? • RabbitMQ installed • Redis

    installed ((docker images ;)) • See sample app
  10. How can I use it in my project? • Dependency:

    io.fency:fency-spring-boot- starter-redis:+ • Annotate your consumers with @IdempotentConsumer
  11. Q&A