Unlike fanout exchange that is used to broadcasts messages, which are available to all consumers, direct exchange allows message filtering when the message’s routing key matches the binding’s routing key.
In the example below, Q1 received one message and Q2 two messages because of different bindings.
➜ docker exec -it rabbitmq rabbitmqadmin declare exchange --name=logs_direct --type=direct --durable=true
➜ docker exec -it rabbitmq rabbitmqadmin declare queue --name=Q1
➜ docker exec -it rabbitmq rabbitmqadmin declare queue --name=Q2
➜ docker exec -it rabbitmq rabbitmqadmin declare binding --source=logs_direct --destination-type=queue --destination=Q1 --routing-key=orange
➜ docker exec -it rabbitmq rabbitmqadmin declare binding --source=logs_direct --destination-type=queue --destination=Q2 --routing-key=black
➜ docker exec -it rabbitmq rabbitmqadmin declare binding --source=logs_direct --destination-type=queue --destination=Q2 --routing-key=green
➜ docker exec -it rabbitmq rabbitmqadmin publish message --exchange=logs_direct --routing-key=orange --payload="orange -> Q1"
Message published and routed successfully
➜ docker exec -it rabbitmq rabbitmqadmin publish message --exchange=logs_direct --routing-key=black --payload="black -> Q2"
Message published and routed successfully
➜ docker exec -it rabbitmq rabbitmqadmin publish message --exchange=logs_direct --routing-key=green --payload="green -> Q2"
Message published and routed successfully
➜ docker exec -it rabbitmq rabbitmqadmin publish message --exchange=logs_direct --routing-key=blue --payload="blue -> dropped"
Message published but NOT routed
➜ docker exec -it rabbitmq rabbitmqadmin get messages --queue=Q1 --count=10
┌───────────────┬─────────────┬─────────────┬─────────────┬───────────────┬────────────┬──────────────┬──────────────────┐
│ payload_bytes │ redelivered │ exchange │ routing_key │ message_count │ properties │ payload │ payload_encoding │
├───────────────┼─────────────┼─────────────┼─────────────┼───────────────┼────────────┼──────────────┼──────────────────┤
│ 12 │ false │ logs_direct │ orange │ 0 │ │ orange -> Q1 │ string │
└───────────────┴─────────────┴─────────────┴─────────────┴───────────────┴────────────┴──────────────┴──────────────────┘
➜ docker exec -it rabbitmq rabbitmqadmin get messages --queue=Q2 --count=10
┌───────────────┬─────────────┬─────────────┬─────────────┬───────────────┬────────────┬─────────────┬──────────────────┐
│ payload_bytes │ redelivered │ exchange │ routing_key │ message_count │ properties │ payload │ payload_encoding │
├───────────────┼─────────────┼─────────────┼─────────────┼───────────────┼────────────┼─────────────┼──────────────────┤
│ 11 │ false │ logs_direct │ black │ 1 │ │ black -> Q2 │ string │
├───────────────┼─────────────┼─────────────┼─────────────┼───────────────┼────────────┼─────────────┼──────────────────┤
│ 11 │ false │ logs_direct │ green │ 0 │ │ green -> Q2 │ string │
└───────────────┴─────────────┴─────────────┴─────────────┴───────────────┴────────────┴─────────────┴──────────────────┘