Listener rules and routing in AWS - Time & Space Complexity
When using listener rules and routing in AWS, it is important to understand how the number of rules affects the time it takes to process incoming requests.
We want to know how the system's work grows as we add more rules to the listener.
Analyze the time complexity of the listener evaluating rules to route requests.
// Example: Listener with multiple rules
listener = elbv2.Listener(
port=80,
protocol='HTTP',
default_target_group=default_tg
)
listener.add_rule(
priority=1,
conditions=[path_pattern('/images/*')],
target_group=images_tg
)
listener.add_rule(
priority=2,
conditions=[host_header('example.com')],
target_group=example_tg
)
// More rules can be added similarly
This sequence shows a listener with multiple rules that check conditions to decide where to send requests.
Each incoming request triggers the listener to check its rules in order.
- Primary operation: Evaluating each listener rule's condition against the request.
- How many times: Once per rule, in priority order, until a match is found.
As the number of rules increases, the listener may need to check more rules before finding a match.
| Input Size (n rules) | Approx. Rule Checks per Request |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The number of checks grows directly with the number of rules, since rules are checked one by one.
Time Complexity: O(n)
This means the time to route a request grows linearly with the number of listener rules.
[X] Wrong: "Listener rules are checked all at once in parallel, so adding more rules does not affect routing time."
[OK] Correct: Listener rules are evaluated one by one in priority order until a match is found, so more rules mean more checks and longer processing time.
Understanding how listener rules scale helps you design efficient routing and shows you can think about system behavior as it grows.
"What if the listener used a hash-based lookup for rules instead of checking them in order? How would the time complexity change?"