A/B testing with Ingress in Kubernetes - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using Kubernetes Ingress for A/B testing, it's important to understand how the routing rules affect processing time.
We want to know how the number of routing rules impacts the time it takes to handle incoming requests.
Analyze the time complexity of the following Ingress configuration snippet.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ab-testing-ingress
spec:
rules:
- host: example.com
http:
paths:
- path: /v1
pathType: Prefix
backend:
service:
name: service-v1
port:
number: 80
- path: /v2
pathType: Prefix
backend:
service:
name: service-v2
port:
number: 80
This Ingress routes traffic to two different service versions based on the URL path for A/B testing.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each path rule in the Ingress to find a match for the incoming request.
- How many times: The number of path rules (here 2) determines how many checks happen per request.
As the number of path rules grows, the Ingress controller checks more rules to find the right backend.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 path checks |
| 100 | 100 path checks |
| 1000 | 1000 path checks |
Pattern observation: The number of checks grows directly with the number of path rules.
Time Complexity: O(n)
This means the time to route a request grows linearly with the number of path rules in the Ingress.
[X] Wrong: "Adding more path rules won't affect request routing time because it's just configuration."
[OK] Correct: Each incoming request is checked against all path rules until a match is found, so more rules mean more checks and longer routing time.
Understanding how routing rules scale helps you design efficient Kubernetes Ingress setups and shows you can think about system performance beyond just writing configs.
"What if we changed the Ingress to use a single rule with a more complex path matching pattern? How would the time complexity change?"
Practice
Solution
Step 1: Understand A/B testing concept in Kubernetes
A/B testing with Ingress is used to route traffic between different app versions to test new features safely.Step 2: Identify the purpose of Ingress in traffic management
Ingress controls how external traffic reaches services, enabling traffic splitting for A/B testing.Final Answer:
To split user traffic between different versions of an application -> Option CQuick Check:
A/B testing = traffic split [OK]
- Confusing A/B testing with scaling pods
- Thinking Ingress is for monitoring only
- Assuming Ingress handles backups
Solution
Step 1: Identify annotations for traffic splitting
The annotation 'nginx.ingress.kubernetes.io/canary-weight' is used to specify the percentage of traffic sent to the canary version.Step 2: Differentiate from other annotations
Other annotations like rewrite-target or ssl-redirect serve different purposes unrelated to traffic splitting.Final Answer:
nginx.ingress.kubernetes.io/canary-weight -> Option AQuick Check:
Canary weight = traffic percentage [OK]
- Using rewrite-target for traffic splitting
- Confusing SSL redirect with traffic control
- Ignoring canary annotations
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
annotations:
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-weight: "30"
spec:
rules:
- host: example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: stable-service
port:
number: 80
- path: /
pathType: Prefix
backend:
service:
name: canary-service
port:
number: 80
Solution
Step 1: Read the canary-weight annotation
The annotation 'nginx.ingress.kubernetes.io/canary-weight' is set to "30", meaning 30% of traffic goes to canary.Step 2: Understand traffic split logic
Traffic is split by percentage; 30% to canary-service, remaining 70% to stable-service.Final Answer:
30% -> Option BQuick Check:
Canary weight = 30% traffic [OK]
- Assuming canary gets 70% instead of 30%
- Confusing service names with traffic percentages
- Ignoring canary annotation
metadata:
annotations:
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-weight: "50"
spec:
rules:
- host: test.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: stable-service
port:
number: 80
Solution
Step 1: Check Ingress spec for canary backend
The spec only has one backend for stable-service; no path defined for canary-service.Step 2: Understand traffic routing requirements
For canary traffic to work, a separate path with canary annotations and backend service must be defined.Final Answer:
Missing canary backend path in spec -> Option AQuick Check:
Canary needs separate backend path [OK]
- Assuming canary-weight alone routes traffic
- Ignoring missing canary backend path
- Blaming host or port without checking paths
X-User-Type: beta to the canary service and the rest to stable. Which Ingress annotation setup achieves this?Solution
Step 1: Identify header-based routing annotations
To route traffic based on header, use 'canary-by-header' and 'canary-by-header-value' annotations.Step 2: Understand difference from weight-based routing
Weight-based routing splits traffic by percentage regardless of headers; header-based routing targets specific users.Final Answer:
Use nginx.ingress.kubernetes.io/canary-by-header: "X-User-Type" and nginx.ingress.kubernetes.io/canary-by-header-value: "beta" -> Option DQuick Check:
Header-based routing uses canary-by-header annotations [OK]
- Using only canary-weight for header routing
- Confusing cookie-based routing with header routing
- Setting canary to false disables routing
