Mirrored queues for redundancy in RabbitMQ - Time & Space Complexity
We want to understand how the work done by RabbitMQ changes when using mirrored queues for redundancy.
Specifically, how does the number of operations grow as messages increase?
Analyze the time complexity of the following RabbitMQ mirrored queue setup.
{
"queue": "task_queue",
"arguments": {
"x-ha-policy": "all"
}
}
This code creates a queue named "task_queue" that is mirrored across all nodes for redundancy.
Look at what happens when messages are sent and mirrored.
- Primary operation: Each message is copied and sent to every mirror node.
- How many times: Once per mirror node, so the number of copies equals the number of mirrors.
As the number of messages grows, the total work grows because each message is duplicated to all mirrors.
| Input Size (n messages) | Approx. Operations (copies) |
|---|---|
| 10 | 10 x number_of_mirrors |
| 100 | 100 x number_of_mirrors |
| 1000 | 1000 x number_of_mirrors |
Pattern observation: The work grows linearly with the number of messages and also scales with the number of mirrors.
Time Complexity: O(n * m)
This means the work grows directly with the number of messages sent, multiplied by the number of mirrors.
[X] Wrong: "Mirroring does not affect performance because messages are sent once."
[OK] Correct: Each message is actually copied and sent to every mirror, so the total work increases with mirrors.
Understanding how redundancy affects message handling helps you explain trade-offs in reliability versus performance.
What if we changed from mirroring on all nodes to mirroring on only two nodes? How would the time complexity change?
Practice
Solution
Step 1: Understand mirrored queues concept
Mirrored queues replicate messages across multiple RabbitMQ nodes to ensure no message is lost if one node fails.Step 2: Identify the main benefit
This replication provides redundancy and safety, not speed or encryption.Final Answer:
To keep copies of messages on multiple servers for safety -> Option AQuick Check:
Mirroring = Redundancy and safety [OK]
- Thinking mirroring speeds up delivery
- Confusing mirroring with encryption
- Assuming mirroring reduces message count
Solution
Step 1: Review correct syntax for set_policy
The command requires the policy name, pattern in quotes, and JSON string with keys and values in double quotes.Step 2: Identify correct JSON format and quoting
rabbitmqctl set_policy ha-all ".*" '{"ha-mode":"all"}' uses correct JSON with double quotes around keys and values and quotes the pattern ".*" properly.Final Answer:
rabbitmqctl set_policy ha-all ".*" '{"ha-mode":"all"}' -> Option BQuick Check:
Correct JSON and quoting = rabbitmqctl set_policy ha-all ".*" '{"ha-mode":"all"}' [OK]
- Missing quotes around JSON keys or values
- Not quoting the pattern string
- Using single quotes inside JSON instead of double quotes
rabbitmqctl set_policy ha-two ".*" '{"ha-mode":"exactly","ha-params":2}'What will happen when a queue is created?
Solution
Step 1: Understand ha-mode 'exactly' with ha-params
The policy sets ha-mode to 'exactly' with ha-params 2, meaning the queue is mirrored on exactly 2 nodes.Step 2: Apply policy effect on new queues
Any queue matching ".*" will be mirrored on exactly two nodes as per the policy.Final Answer:
The queue will be mirrored on exactly two nodes -> Option CQuick Check:
ha-mode exactly + ha-params 2 = mirror on 2 nodes [OK]
- Confusing 'all' with 'exactly'
- Ignoring ha-params value
- Assuming no mirroring without ha-mode 'all'
rabbitmqctl set_policy ha-all ".*" '{"ha-mode":"all"}'But queues are not mirrored. What is the likely problem?
Solution
Step 1: Check policy application behavior
Policies apply only to new queues or queues restarted after policy set.Step 2: Identify why existing queues are not mirrored
Existing queues created before policy need to be deleted or restarted to apply mirroring.Final Answer:
The policy requires a restart of the queue or server to apply -> Option AQuick Check:
Policy applies only on new or restarted queues [OK]
- Thinking RabbitMQ restart is needed
- Assuming pattern ".*" never matches
- Believing JSON syntax is wrong when it is correct
task_queue is mirrored on at least 3 nodes but no more than 5 using a range specification in ha-params. Which policy configuration achieves this?Solution
Step 1: Understand ha-mode 'exactly' with range ha-params
ha-mode 'exactly' with ha-params as string like '3-5' dynamically mirrors the queue on a number of replicas between 3 and 5.Step 2: Identify correct command
rabbitmqctl set_policy ha-minmax "^task_queue$" '{"ha-mode":"exactly","ha-params":"3-5"}' matches the queue exactly and uses range specification.Step 3: Check other options
B uses fixed exactly 3 on all queues; C uses fixed nodes list of 5; D uses fixed nodes list of 3.Final Answer:
rabbitmqctl set_policy ha-minmax "^task_queue$" '{"ha-mode":"exactly","ha-params":"3-5"}' -> Option DQuick Check:
ha-mode exactly + ha-params "3-5" = dynamic range 3-5 [OK]
- Using 'exactly' with fixed integer instead of range string
- Not matching queue name exactly
- Using 'nodes' mode with fixed list instead of range
Ready to prove it?
Pass the test and this pattern moves unknown
