Slide 1

Slide 1 text

Message queue overview

Slide 2

Slide 2 text

A starting use case Let’s create a reason to talk about message queues 1

Slide 3

Slide 3 text

www.leblogdefrancis.com ● A blog of articles ● Registered users can start a thread on an article ● When an user reply on a thread, a notification must be sent to every participants of this thread 3

Slide 4

Slide 4 text

Straightforward implementation 1. Get all the users that already commented on the thread 2. Iterate on them 3. Check if they have notifications enabled 4. An infinity of possible checks actually... 5. Send the notifications 4

Slide 5

Slide 5 text

Main issues with it ● Bad for performance ● Not decoupled ● Not scalable in an efficient way ● Bad for exceptions handling ● Less reliability ● Not flexible ● Is it essential to send the notifications there ? 5

Slide 6

Slide 6 text

How can we improve it? Spoiler alert : message queues can help us 2

Slide 7

Slide 7 text

Current synchronous workflow 7

Slide 8

Slide 8 text

Go asynchronous Run the expensive tasks later, in the background. 8

Slide 9

Slide 9 text

Better asynchronous workflow 9

Slide 10

Slide 10 text

Cron? ● Not adapted ● Less functionalities ● Not instantaneous ● Not scalable 10

Slide 11

Slide 11 text

Message queue! ● Something sends a message ● The message ends up in a queue ● Something process the message 11

Slide 12

Slide 12 text

Theory Not too much I promise <3 3

Slide 13

Slide 13 text

A common language ● A producer publishes a message ● A message broker accepts and forwards a message ● A consumer consumes / receives a message 13

Slide 14

Slide 14 text

14

Slide 15

Slide 15 text

The message ● A message is some data ● It should encapsulate everything consumers might need ● In our example use case : ○ The id / uuid of the reply ○ The content of the notification and the query to find the recipients 15

Slide 16

Slide 16 text

The producer ● A program that sends the message ● In our example use case : ○ Our application ○ In a dedicated service ○ In an event listener 16

Slide 17

Slide 17 text

The message broker ● An intermediary program that handle the message between the producer and the consumer ● Objective : decoupling the awareness between producers and consumers ● In our example use case : ○ An existing one 17

Slide 18

Slide 18 text

The consumer ● A program that waits to receive the message ● This program logically does the expensive task ● In our example use case : ○ Our application ○ In a dedicated service ○ Another application ○ In another language ○ On another server 18

Slide 19

Slide 19 text

19

Slide 20

Slide 20 text

The queue ● Inside the message broker ● Managed by the message broker ● FIFO (First In First Out) 20

Slide 21

Slide 21 text

Why is it better? Do not trust me, try it yourselves! 4

Slide 22

Slide 22 text

Performant and decoupled ● Performance : ○ Publishing a message takes a few milliseconds ○ Sending the notifications does not block the response ● Decoupled : ○ Not perfect in our example but : ○ The publish and consume parts are totally separated ○ So extracting the consumer would be way more easier 22

Slide 23

Slide 23 text

Scalable and recoverable ● Scalable : ○ We just have to spawn new instances of our consumer ● Better exceptions handling : ○ Because of the retry mechanism ○ And the dead letter exchange mechanism ○ By splitting one message into several small ones 23

Slide 24

Slide 24 text

Reliable and flexible ● Reliable : ○ If our application (producer) is down, all the currently waiting messages can still be processed ● Flexible : ○ We can replace our current consumer implementation ○ We can add a new behavior with a new consumer 24

Slide 25

Slide 25 text

Do not confuse More vocabulary to speak like a pro 5

Slide 26

Slide 26 text

Standards / protocols ● AMQP (Asynchronous Message Queue Protocol) ● MQTT (Message Queuing Telemetry Transport) ● STOMP (Streaming Text Oriented Messaging Protocol) ● And more... 26

Slide 27

Slide 27 text

Implementations & abstractions ● php-amqplib ● JMS (Java Message Service) ● Symfony Messenger ● And many more... 27

Slide 28

Slide 28 text

Message brokers ● Apache Kafka ● Apache ActiveMQ ● Apache Qpid And dozens more... 28

Slide 29

Slide 29 text

Message queuing services ● Amazon Web Services SQS (Simple Queue Service) ● Google Cloud Platform Pub/Sub ● Microsoft Azure Storage queues & Service Bus queues ● And more... 29

Slide 30

Slide 30 text

Common use cases Directly taken from the RabbitMQ tutorials 6

Slide 31

Slide 31 text

Quick focus on RabbitMQ ● Open Source message broker, originally for the AMQP protocol ● The most widely deployed Open Source message broker in the world ● Written in Erlang, a concurrent and functional programming language 31

Slide 32

Slide 32 text

Mature system ● TLS / SSL support ● Clustering ● Authentication and authorisation mechanisms ● And more... 32

Slide 33

Slide 33 text

The exchange ● The producer actually sends the message to an exchange ● The exchange knows what to do with the message (forwards it to one or several queues, or ignore it) ● 4 Different types : direct, topic, headers and fanout 33

Slide 34

Slide 34 text

34

Slide 35

Slide 35 text

Work queue ● Most simple case like our example ● Delay an expensive task ● Nameless exchange “” 35

Slide 36

Slide 36 text

36

Slide 37

Slide 37 text

Publish / Subscribe ● The same message for several consumers ● Exchange type : fanout ● In our example : ○ We want to send an email in addition to the notification ○ We just need to create a new consumer 37

Slide 38

Slide 38 text

38

Slide 39

Slide 39 text

Routing ● Receive messages selectively ● Bind an exchange and a queue with a routing key ● Exchange type : direct 39

Slide 40

Slide 40 text

40

Slide 41

Slide 41 text

Topics ● Receive message selectively but based on a pattern ● The routing key contains a specific format with special characters ● Exchange type : topic 41

Slide 42

Slide 42 text

42

Slide 43

Slide 43 text

Bonus Because I still have time 7

Slide 44

Slide 44 text

Warning ! ● Message queue might be overkill ● Using an actual implementation is not that easy ● Infrastructure setup is not negligible 44

Slide 45

Slide 45 text

My previous real usage cases ● Imports ● Exports ● Bulk actions 45

Slide 46

Slide 46 text

UNIX power ● There are two common message queue implementations in UNIX ● The first one, System V IPC messages, was done in 1983 ● In other words : message queue is not new at all! 46

Slide 47

Slide 47 text

47 Thank you Any questions?