Bird
Raised Fist0
RabbitMQdevops~20 mins

Fair dispatch with prefetch in RabbitMQ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Fair Dispatch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding the purpose of prefetch in RabbitMQ

What is the main purpose of setting the prefetch count in RabbitMQ when using fair dispatch?

ATo ensure a consumer receives only one message at a time until it acknowledges it
BTo limit the number of messages sent over the network at once
CTo increase the message queue size on the broker
DTo automatically reject messages that take too long to process
Attempts:
2 left
💡 Hint

Think about how RabbitMQ controls message flow to consumers to balance workload.

💻 Command Output
intermediate
2:00remaining
Effect of prefetch count on message delivery

Given two consumers connected to the same RabbitMQ queue with prefetch=1, what will be the output behavior when 4 messages are published?

RabbitMQ
Consumer1: prefetch=1
Consumer2: prefetch=1
Publish 4 messages to queue
AConsumer1 receives all 4 messages before Consumer2 gets any
BBoth consumers receive 2 messages each simultaneously
CMessages are sent randomly without regard to prefetch settings
DMessages are distributed one by one alternately between Consumer1 and Consumer2
Attempts:
2 left
💡 Hint

Consider how prefetch=1 affects message dispatch to multiple consumers.

❓ Configuration
advanced
2:00remaining
Configuring prefetch count in RabbitMQ consumer code

Which of the following code snippets correctly sets the prefetch count to 1 in a RabbitMQ consumer using Python's pika library?

A
channel.set_qos(prefetch=1)
channel.consume('task_queue', callback)
Bchannel.basic_consume(queue='task_queue', on_message_callback=callback, prefetch_count=1)
C
channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue='task_queue', on_message_callback=callback)
D
channel.qos(prefetch=1)
channel.basic_consume('task_queue', callback)
Attempts:
2 left
💡 Hint

Look for the correct method name and parameter for setting prefetch in pika.

❓ Troubleshoot
advanced
2:00remaining
Diagnosing message imbalance despite prefetch=1

You set prefetch=1 for two consumers on the same queue, but one consumer receives most messages while the other is idle. What is the most likely cause?

AThe idle consumer is not acknowledging messages, causing RabbitMQ to stop sending it new messages
BPrefetch count must be set to 0 to enable fair dispatch
CRabbitMQ does not support fair dispatch with multiple consumers
DThe queue is configured as exclusive, so only one consumer can receive messages
Attempts:
2 left
💡 Hint

Think about how unacknowledged messages affect message flow.

🔀 Workflow
expert
2:30remaining
Order of steps to implement fair dispatch with prefetch in RabbitMQ

Arrange the following steps in the correct order to implement fair dispatch with prefetch=1 in a RabbitMQ consumer application.

A1,3,2,4
B1,2,3,4
C3,1,2,4
D2,1,3,4
Attempts:
2 left
💡 Hint

Consider the logical order of setup before starting consumption.

Practice

(1/5)
1. What is the main purpose of setting prefetch_count=1 in RabbitMQ fair dispatch?
easy
A. To ensure each worker receives only one task at a time
B. To allow workers to receive unlimited tasks simultaneously
C. To disable message acknowledgments
D. To increase the queue size automatically

Solution

  1. Step 1: Understand prefetch_count meaning

    Prefetch_count controls how many messages a worker can receive before acknowledging previous ones.
  2. Step 2: Effect of setting prefetch_count=1

    Setting it to 1 means the worker gets one task at a time, preventing overload and balancing workload.
  3. Final Answer:

    To ensure each worker receives only one task at a time -> Option A
  4. Quick Check:

    prefetch_count=1 means one task per worker [OK]
Hint: Prefetch 1 means one task per worker at a time [OK]
Common Mistakes:
  • Thinking prefetch_count controls queue size
  • Believing it disables acknowledgments
  • Assuming workers get unlimited tasks
2. Which of the following is the correct way to set prefetch count to 1 using RabbitMQ's channel in Python?
easy
A. channel.set_prefetch(1)
B. channel.qos(prefetch=1)
C. channel.prefetch(1)
D. channel.basic_qos(prefetch_count=1)

Solution

  1. Step 1: Recall RabbitMQ Python API method

    The method to set prefetch count is basic_qos with prefetch_count argument.
  2. Step 2: Match correct syntax

    Only channel.basic_qos(prefetch_count=1) matches the official API syntax.
  3. Final Answer:

    channel.basic_qos(prefetch_count=1) -> Option D
  4. Quick Check:

    basic_qos with prefetch_count sets prefetch [OK]
Hint: Use basic_qos(prefetch_count=1) to set prefetch in Python [OK]
Common Mistakes:
  • Using incorrect method names like set_prefetch
  • Passing wrong argument names
  • Confusing prefetch with qos
3. Given the following Python code snippet using RabbitMQ, what will be the output behavior when multiple workers consume from the same queue with prefetch_count=1?
channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue='task_queue', on_message_callback=callback, auto_ack=False)
channel.start_consuming()
medium
A. Messages are processed only by the first worker
B. All workers receive all messages simultaneously
C. Each worker processes one message at a time, distributing tasks evenly
D. Workers process messages without waiting for acknowledgments

Solution

  1. Step 1: Understand prefetch_count=1 effect

    With prefetch_count=1, each worker gets one message and must ack before next.
  2. Step 2: Effect on multiple workers

    This causes RabbitMQ to distribute messages fairly, so tasks are balanced among workers.
  3. Final Answer:

    Each worker processes one message at a time, distributing tasks evenly -> Option C
  4. Quick Check:

    prefetch_count=1 balances tasks fairly [OK]
Hint: Prefetch 1 means tasks spread evenly, one per worker [OK]
Common Mistakes:
  • Thinking all workers get all messages
  • Assuming one worker gets all tasks
  • Ignoring manual acknowledgments
4. You set prefetch_count=1 but notice one worker is still overloaded while others are idle. What is the most likely cause?
medium
A. The worker is not sending message acknowledgments properly
B. Prefetch count is set too high
C. The queue is empty
D. Multiple workers have the same consumer tag

Solution

  1. Step 1: Analyze prefetch_count=1 behavior

    Prefetch_count=1 limits unacknowledged messages to one per worker.
  2. Step 2: Identify cause of overload

    If a worker does not ack messages, RabbitMQ won't send new ones, causing that worker to hold tasks and appear overloaded.
  3. Final Answer:

    The worker is not sending message acknowledgments properly -> Option A
  4. Quick Check:

    Missing ack blocks new messages [OK]
Hint: Check if worker sends acknowledgments to avoid overload [OK]
Common Mistakes:
  • Assuming prefetch count is too high
  • Ignoring message acknowledgments
  • Confusing consumer tags with load balancing
5. You want to optimize a RabbitMQ system where some workers are slower. How should you configure prefetch to ensure fair dispatch and prevent slow workers from blocking others?
hard
A. Set prefetch_count to a high number to maximize throughput
B. Set prefetch_count=1 so each worker gets one task at a time
C. Disable acknowledgments to speed up processing
D. Use multiple queues instead of prefetch settings

Solution

  1. Step 1: Understand slow worker impact

    Slow workers holding many tasks block others if prefetch is high.
  2. Step 2: Apply prefetch_count=1 for fair dispatch

    Setting prefetch_count=1 ensures each worker processes one task at a time, preventing blocking.
  3. Step 3: Evaluate other options

    High prefetch or disabling acks causes imbalance; multiple queues add complexity.
  4. Final Answer:

    Set prefetch_count=1 so each worker gets one task at a time -> Option B
  5. Quick Check:

    Prefetch 1 prevents slow worker blocking [OK]
Hint: Use prefetch_count=1 to balance slow and fast workers [OK]
Common Mistakes:
  • Setting prefetch too high causing imbalance
  • Disabling acknowledgments causing message loss
  • Ignoring prefetch and using multiple queues unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard