Ingress vs LoadBalancer Service decision in Kubernetes - Performance Comparison
When choosing between Ingress and LoadBalancer Service in Kubernetes, it's important to understand how their processing time grows as the number of requests or services increases.
We want to know how the system's work changes when more traffic or services are added.
Analyze the time complexity of routing requests using an Ingress controller versus a LoadBalancer Service.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
spec:
rules:
- host: example.com
http:
paths:
- path: /app1
pathType: Prefix
backend:
service:
name: app1-service
port:
number: 80
---
apiVersion: v1
kind: Service
metadata:
name: app1-loadbalancer
spec:
type: LoadBalancer
selector:
app: app1
ports:
- port: 80
targetPort: 8080
This snippet shows an Ingress routing multiple paths to services and a LoadBalancer Service exposing a single app directly.
- Primary operation: Ingress controller checks each incoming request against its list of rules to find the matching path and service.
- How many times: For each request, it may check multiple rules until it finds a match.
- LoadBalancer Service: Directly routes requests to one service without rule checks.
- Dominant operation: Ingress rule matching per request.
As the number of Ingress rules grows, the controller checks more rules per request, increasing work.
| Number of Rules (n) | Approx. Checks per Request |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The work grows roughly in direct proportion to the number of rules.
Time Complexity: O(n)
This means the time to route a request grows linearly with the number of Ingress rules.
[X] Wrong: "Ingress routing time stays the same no matter how many rules exist."
[OK] Correct: Each request must be checked against rules until a match is found, so more rules mean more checks and longer processing time.
Understanding how routing scales helps you design systems that handle growth smoothly. This skill shows you can think about system behavior beyond just writing code.
"What if the Ingress controller used a hash map for rules instead of checking them one by one? How would the time complexity change?"