Exchange type use cases
- When should you use a Fanout exchange? And when is a direct exchange your best option? It can be confusing to know which exchange type is best for your specific scenario. And more so because you can often achieve the same result with different exchange types. So let's look at some common use cases and when to use which type. Now even though it looks like the exchange is a concern of the producing application, it's actually the consumers that will determine which exchange type you should use. First the Fanout. A fanout exchange will broadcast the message to all bound queues. A good example is a service announcement that needs to be sent to all connected to mobile applications. Other examples include games that need to send out leaderboard updates or distributed systems that have to notify components of configuration changes. Then we have the Direct Exchange. This exchange requires the routing key to match the long defined in the binding. An example here is a system where a user can upload an image and you can have a message sent out with a routing key image uploaded for example. The direct exchange might pass the message to a queue for an application that will create a thumbnail. And another queue might be for an application that does some facial recognition. Topic Exchanges are ideal for cases where the routing key is constructed dynamically and consumers want to filter out certain messages. Examples are consumers that perform background tasks for specific events needing to log all events in a certain category and basically any messaging that involves categories of messages. For a specific example, picture a system that distributes the workload based on geographic location. This could be a point-of-sale system. In this example, a sale was completed in the United States and so the message is processed by a service that is also located in the United States. The last type, Headers Exchanges, are a special type. They work with the message headers instead of the routing key. You can match on all or any of the headers. And the headers don't have to be strings. They can be an integer or a dictionary for example. And this allows for some creative filtering. One thing to keep in mind is that RabbitMQ will load balance messages between consumers not queues. If the two applications are consuming the same queue, they will receive the messages one after the other. This is regardless of the exchange type. So which exchange type should you use when you start out? If your message is going to be sent to all consumers, choose a Fanout exchange. If you have a simple scenario that won't involve too many exchanges bindings and queues then Direct exchanges will work for you. But whenever you expect your system to scale in the future, or you need to some kind of categorization or filtering go for topic exchanges. They provide the most flexibility if required. And if not they'll still work very well for you. Headers exchanges are a special case. I would only recommend them if you really need the special filtering mechanisms that it provides. So now you can more easily identify the best exchange type for your scenario.
No comments:
Post a Comment