Path-based routing in Kubernetes - Time & Space Complexity
We want to understand how the time to route requests changes as the number of paths grows in Kubernetes.
How does the system decide which backend to send a request to when many paths exist?
Analyze the time complexity of the following Kubernetes Ingress path-based routing rules.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
spec:
rules:
- http:
paths:
- path: /app1
pathType: Prefix
backend:
service:
name: app1-service
port:
number: 80
- path: /app2
pathType: Prefix
backend:
service:
name: app2-service
port:
number: 80
# ... more paths ...
This snippet routes incoming requests to different services based on the URL path prefix.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each path rule in order to find a match for the request path.
- How many times: The system may check up to all path rules (n) in the worst case.
As the number of path rules increases, the system checks more paths one by one until it finds a match.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 path checks |
| 100 | Up to 100 path checks |
| 1000 | Up to 1000 path checks |
Pattern observation: The number of checks grows directly with the number of paths.
Time Complexity: O(n)
This means the time to find the right path grows linearly as you add more paths.
[X] Wrong: "The routing time stays the same no matter how many paths exist."
[OK] Correct: Each new path adds a possible check, so more paths mean more work to find the match.
Understanding how routing scales helps you design efficient systems and explain trade-offs clearly in real projects.
"What if the paths were stored in a tree structure instead of a list? How would the time complexity change?"
Practice
Solution
Step 1: Understand Ingress role
Kubernetes Ingress manages external access to services, often HTTP.Step 2: Identify path-based routing function
Path-based routing directs requests to services depending on the URL path.Final Answer:
It sends traffic to different services based on URL paths. -> Option AQuick Check:
Path-based routing = directing traffic by URL path [OK]
- Confusing path-based routing with load balancing
- Thinking it schedules pods
- Assuming it encrypts traffic
Solution
Step 1: Recall Ingress rule syntax
Ingress rules usepathandpathTypekeys to define routing.Step 2: Match correct keys and values
Correct keys arepathandpathType, with values like '/app' and 'Prefix'.Final Answer:
path: /app pathType: Prefix -> Option DQuick Check:
Use path and pathType keys for routing [OK]
- Using wrong keys like 'route' or 'url'
- Mixing up pathType values
- Missing pathType field
rules:
- http:
paths:
- path: /shop
pathType: Prefix
backend:
service:
name: shop-service
port:
number: 80
- path: /blog
pathType: Prefix
backend:
service:
name: blog-service
port:
number: 80Solution
Step 1: Check path matching rules
Path '/shop/cart' starts with '/shop', matching the first path rule with pathType Prefix.Step 2: Identify backend service
The matching path routes to 'shop-service' on port 80.Final Answer:
shop-service -> Option BQuick Check:
Prefix match '/shop' routes to shop-service [OK]
- Choosing blog-service for /shop/cart
- Assuming no match for subpaths
- Confusing default backend with path rules
paths:
- path: api
pathType: Prefix
backend:
service:
name: api-service
port:
number: 80
What is the likely error?Solution
Step 1: Check path syntax
Paths must start with a leading slash '/' to match URLs correctly.Step 2: Identify missing slash
The path is 'api' instead of '/api', so it won't match requests starting with '/api'.Final Answer:
Missing leading slash in path value -> Option CQuick Check:
Paths need leading '/' for correct matching [OK]
- Omitting leading slash in path
- Using wrong pathType values
- Assuming port number must be string
Solution
Step 1: Understand path matching order
Ingress matches paths in order; more specific paths must come first to avoid being shadowed.Step 2: Check rule order and pathType
Placing '/app/admin' before '/app' with pathType Prefix ensures '/app/admin' routes to admin-service, others to app-service.Final Answer:
Place '/app/admin' rule before '/app' rule with Prefix pathType -> Option AQuick Check:
Order specific paths first for correct routing [OK]
- Placing general path before specific path
- Using Exact pathType causing no subpath matches
- Ignoring rule order importance
Ready to prove it?
Pass the test and this pattern moves unknown
