Try It Yourself
Try It Yourself
Try It Yourself
Hands-on exercises to practice AMQP exchange routing concepts:
Exercise 1: Design Topic Exchange Routing
Scenario: Smart building with 50 rooms across 5 floors, each room has 3 sensor types (temperature, humidity, occupancy).
Task: Design routing keys and binding patterns for:
First: HVAC system needs all temperature sensors
Next: Security system needs all occupancy sensors
Then: Floor 3 manager needs all sensors on floor 3
After that: Energy dashboard needs everything
Solution Template:
# Routing key format: building.floor{N}.room{ID}.{sensor_type}
# Example: building.floor3.room15.temperature
# HVAC binding:
channel.queue_bind(
queue='hvac_queue',
exchange='building_sensors',
routing_key='building.*.*.temperature' # Your pattern here
)
# Security binding:
# Exercise: Write binding for occupancy sensors
# Floor 3 manager binding:
# Exercise: Write binding for floor 3 only
# Energy dashboard binding:
# Exercise: Write binding for all sensorsExpected Result:
Also inspect: Message building.floor3.room15.temperature should route to HVAC, Floor 3 manager, and Energy dashboard (3 queues)
Finally: Message building.floor1.room05.occupancy should route to Security and Energy dashboard (2 queues)
Exercise 2: Calculate Message Distribution
Given:
First: 50 rooms × 3 sensors = 150 publishers
Next: Each sensor publishes 1 msg/minute
Then: 4 consumers with different binding patterns (from Exercise 1)
Tasks:
After that: Calculate messages/hour for each queue
Also inspect: Estimate queue memory with 200-byte average message size
Finally: Determine if fanout exchange would waste bandwidth (compare to topic)
Exercise 3: Debug Routing Problem
Symptom: Temperature dashboard receives no messages, but messages are being published.
Given Config:
# Publisher
channel.basic_publish(
exchange='sensors',
routing_key='sensor.temperature.zone1',
body='22.5C'
)
# Consumer
channel.queue_bind(
queue='temp_dashboard',
exchange='sensors',
routing_key='sensor.temp.#' # Binding pattern
)Question: Why doesn’t the message reach the queue?
Hint: Compare routing key structure to binding pattern carefully.
Exercise 4: Implement Competing Consumers
Objective: Set up 3 worker processes that share a single queue for load distribution.
Requirements:
First: Each worker processes messages independently
Next: If one worker crashes, others continue processing
Then: Ensure fair distribution (no worker hoarding)
Code skeleton:
# Exercise: Set prefetch count for fair distribution
channel.basic_qos(prefetch_count=???)
def process_order(ch, method, properties, body):
# Exercise: Process order
# Exercise: Send acknowledgment only after success
pass
channel.basic_consume(
queue='orders',
on_message_callback=process_order,
auto_ack=??? # True or False?
)Questions:
After that: Should you use auto_ack=True or False? Why?
Also inspect: What prefetch count ensures fair distribution?
Finally: What happens if a worker crashes mid-processing with auto_ack=True?
Next Steps: Try these exercises with a local RabbitMQ instance. See AMQP Implementations and Labs for broker setup instructions.
Technical boundaries. The message distribution calculation fixes 50 rooms, 3 sensors per room, 1 message per minute per sensor, and a 200-byte average message size. The routing results use the specified routing keys and binding patterns.