Loading learning content...
Theory becomes actionable through concrete examples. While understanding the mechanics of Layer 4 and Layer 7 load balancing is essential, knowing when to apply each approach—recognizing the patterns in your own requirements—is where engineering judgment materializes.
This page presents comprehensive use cases for both layers, drawn from production systems across industries. By studying these patterns, you'll develop the intuition to select the right approach for your specific needs.
By the end of this page, you will recognize specific scenarios where Layer 4 is essential (gaming, databases, high-frequency trading), scenarios where Layer 7 is required (web applications, microservices, API gateways), and hybrid architectures that combine both layers for optimal results.
Online gaming represents one of the clearest cases for Layer 4 load balancing. The combination of latency sensitivity, custom protocols, and high connection volumes makes Layer 4 essential.
Multiplayer games have unique requirements:
| Game Type | Protocol | Update Rate | Latency Tolerance |
|---|---|---|---|
| First-Person Shooter | UDP custom | 64-128 Hz | < 30ms |
| Battle Royale | UDP custom | 20-60 Hz | < 50ms |
| MOBA | TCP/UDP hybrid | 30-60 Hz | < 80ms |
| MMO | TCP custom | 10-30 Hz | < 150ms |
| Turn-based | TCP/HTTP | On-demand | < 500ms |
Typical architecture:
The load balancer handles initial connection and matchmaking; gameplay traffic often bypasses the LB entirely to minimize latency.
12345678910111213141516171819202122232425
Gaming Infrastructure Load Balancing Pattern============================================= Phase 1: Login and Matchmaking (Through LB) Player → [DNS/Anycast] → [L4 LB] → [Login Service] ↓ [Matchmaking Service] ↓ Assigns server-1.us-east.game.com Phase 2: Gameplay (Direct Connection) Player ←─────── UDP direct ───────→ Game Server Instance Note: L4 LB may still handle: - Friend list / social features (HTTP) - Leaderboards (HTTP) - In-game store (HTTPS) - Voice chat signaling (UDP) Scale Numbers: - Peak: 10M+ concurrent connections - Update rate: 64 packets/sec/player = 640M pps - Latency budget: < 5ms for LB hopModern games often split traffic: HTTP/HTTPS through Layer 7 for login, store, and social features; UDP through Layer 4 (or direct) for gameplay. This combines Layer 7's benefits for web-style traffic with Layer 4's performance for real-time gameplay.
Databases and stateful services require Layer 4 load balancing due to their wire protocols, connection semantics, and performance requirements.
Databases use specialized binary protocols:
These protocols are not HTTP; Layer 7 proxies cannot parse them. Layer 4 is the only option for generic database load balancing.
| Database | Protocol | LB Strategy | Key Consideration |
|---|---|---|---|
| PostgreSQL | libpq binary | L4 to read replicas | Connection pooling (PgBouncer) |
| MySQL | MySQL protocol | L4 with ProxySQL | Read/write splitting |
| Redis | RESP | L4 to replicas or cluster | Cluster mode: no LB needed |
| MongoDB | BSON | L4 or built-in driver | Driver handles replica set |
| Cassandra | CQL | Client-side or L4 | Token-aware routing preferred |
The most common database LB pattern: distributing read queries across multiple replicas:
Layer 4 load balancing for read replicas:
Database connections are expensive (memory, authentication, TLS). Architectures typically use:
The Layer 4 LB distributes application connections; the pooler manages database connections.
Some database proxies (ProxySQL, Vitess, CockroachDB load balancer) understand their specific protocol at Layer 7. They can parse queries, route reads vs. writes, and implement sophisticated logic. These are specialized Layer 7 proxies for specific databases—not generic HTTP Layer 7 load balancers.
Financial trading systems represent the extreme end of the latency spectrum. Here, Layer 4's microsecond overhead advantage over Layer 7's millisecond overhead translates directly to competitive advantage and money.
In high-frequency trading (HFT):
| System Tier | Total Latency Budget | LB Allocation | LB Technology |
|---|---|---|---|
| Ultra-low latency HFT | < 10 µs | < 1 µs | FPGA/ASIC, no LB |
| Low-latency trading | < 100 µs | < 5 µs | DPDK/XDP, L4 only |
| Market making | < 1 ms | < 50 µs | L4 software |
| Execution services | < 10 ms | < 500 µs | L4 or L7 |
| Retail trading | < 100 ms | < 5 ms | L7 acceptable |
Financial systems use specialized protocols:
These protocols require Layer 4 handling—they're not HTTP.
Market data distribution:
Order routing:
Cross-datacenter:
12345678910111213141516171819202122232425
Ultra-Low Latency Trading System Component Breakdown==================================================== Total round-trip budget: 50 microseconds Component Breakdown:--------------------Network (fiber, switch): 15 µs (30%)NIC processing: 3 µs ( 6%)Kernel network stack: 5 µs (10%) ← eliminated with bypassLoad balancer: 5 µs (10%) ← L4 only, kernel bypassApplication logic: 20 µs (40%)Response path: 2 µs ( 4%) With Optimization:------------------FPGA NIC (no kernel): 1 µsNo load balancer (direct): 0 µsCo-located exchange: 5 µs wire------------------------------------Achievable: < 10 µs round-trip Key: Every component that touches packets is measured and optimized. Layer 7 (adding 1-5ms) would consume 20-100x the entire budget.In the most latency-sensitive trading scenarios, load balancers are eliminated entirely. Trading engines connect directly to exchange gateways with redundant paths. Failover is handled at the application or NIC level, not by an intermediary. This is the extreme case where even Layer 4's minimal overhead is unacceptable.
Web applications and REST/GraphQL APIs are the primary domain for Layer 7 load balancing. The HTTP protocol's richness and the need for content-based routing make Layer 7 essential.
Layer 7 load balancers often implement the API Gateway pattern:
A typical e-commerce platform routing configuration:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
# E-commerce Layer 7 Routing ConfigurationapiVersion: networking.k8s.io/v1kind: Ingressmetadata: name: ecommerce-ingress annotations: nginx.ingress.kubernetes.io/rate-limit: "100" nginx.ingress.kubernetes.io/ssl-redirect: "true"spec: tls: - hosts: [shop.example.com, api.example.com] secretName: ecommerce-tls rules: # Main website - host: shop.example.com http: paths: - path: / pathType: Prefix backend: service: name: frontend port: {number: 80} - path: /checkout pathType: Prefix backend: service: name: checkout-service port: {number: 80} # API endpoints - host: api.example.com http: paths: - path: /v1/products pathType: Prefix backend: service: name: product-service port: {number: 8080} - path: /v1/orders pathType: Prefix backend: service: name: order-service port: {number: 8080} - path: /v1/users pathType: Prefix backend: service: name: user-service port: {number: 8080}For typical web applications, backend processing (50-500ms) dwarfs Layer 7 overhead (2-5ms). The operational benefits of Layer 7—observability, routing flexibility, security—provide immense value with negligible performance impact relative to the overall request lifecycle.
Microservices architectures are fundamentally dependent on Layer 7 load balancing. The need to route requests to the appropriate service based on path, header, or content makes Layer 7 essential.
In a microservices architecture:
Layer 4 cannot solve this problem—it has no concept of HTTP paths or headers.
North-South (external entry):
East-West (service-to-service):
Service meshes (Istio, Linkerd, Consul Connect) implement Layer 7 load balancing through sidecar proxies:
Capabilities:
Trade-off:
| Feature | Without Service Mesh | With Service Mesh |
|---|---|---|
| Service discovery | Manual/DNS | Automatic, dynamic |
| Load balancing | Client library or L4 | L7 with intelligent routing |
| mTLS | Manual certificate management | Automatic, transparent |
| Observability | Instrument each service | Automatic per-request metrics |
| Traffic splitting | Application code | Configuration-driven |
| Circuit breaking | Library per service | Centralized policy |
Service mesh is powerful but adds complexity and overhead. It's typically worthwhile when: you have 20+ services, need consistent security/observability across services, want configuration-driven traffic management, or require mTLS for compliance. For smaller deployments, a simple ingress controller may suffice.
Security is often the decisive factor for Layer 7 adoption. Many security functions are inherently Layer 7 operations—they require understanding the application protocol.
WAFs protect against application-layer attacks:
Layer 4 cannot provide this protection—it doesn't understand HTTP.
Layer 7 load balancers can offload authentication from backends:
Token validation:
Benefits:
Layer 7 enables sophisticated rate limiting:
For DDoS, Layer 7 can:
12345678910111213141516171819202122232425262728293031323334353637383940
# Layer 7 Security Configuration (Envoy-style)http_filters: # JWT Authentication - name: envoy.filters.http.jwt_authn typed_config: providers: auth0: issuer: "https://example.auth0.com/" audiences: ["api.example.com"] remote_jwks: http_uri: uri: "https://example.auth0.com/.well-known/jwks.json" rules: - match: {prefix: "/api/"} requires: provider_name: auth0 - match: {prefix: "/public/"} # No auth required # Rate Limiting - name: envoy.filters.http.ratelimit typed_config: domain: api_rate_limit rate_limit_service: grpc_service: envoy_grpc: cluster_name: rate_limit_cluster descriptors: - entries: - key: user_id value: "%REQ(X-User-ID)%" - entries: - key: path value: "%REQ(:path)%" # WAF (ModSecurity rules) - name: envoy.filters.http.wasm typed_config: vm_config: code: local: {filename: "/etc/envoy/wasm/coraza-waf.wasm"}Layer 7 security at the load balancer is one layer of defense, not the only layer. Continue to implement validation in backends, use prepared statements for SQL, and follow secure coding practices. The load balancer provides a valuable first line of defense and consistent policy enforcement.
Production systems rarely use Layer 4 or Layer 7 exclusively. Hybrid architectures leverage both layers strategically, optimizing for their respective strengths.
Pattern 1: Layer 4 in Front of Layer 7
The most common hybrid: Layer 4 handles connection distribution; Layer 7 handles application routing.
Internet → L4 LB → L7 LBs → Services
Benefits:
Example: AWS architecture with NLB in front of ALB (common for WebSocket + HTTP).
Pattern 2: Split by Protocol
Different protocols get different treatment:
HTTP/HTTPS → L7 LB → Web/API services
TCP/UDP (gaming, DB) → L4 LB → Specialized services
Example: Gaming company with HTTP APIs through L7, game traffic through L4.
Pattern 3: Split by Sensitivity
Latency-sensitive traffic bypasses Layer 7:
Critical path → L4 or direct
General traffic → L7 with full features
Example: Trading platform with order execution on L4, everything else on L7.
Pattern 4: TLS Passthrough + HTTP Termination
External TLS → L7 terminates → Backend
Internal TLS (mTLS) → L4 passthrough → Backend terminates
Example: Ingress terminates external TLS; internal mTLS passes through.
| Traffic Type | Layer | Rationale |
|---|---|---|
| Public HTTP/HTTPS | L7 | Routing, TLS, WAF, caching |
| Internal HTTP | L7 (service mesh) or L4 | Observability vs simplicity |
| Database connections | L4 | Binary protocol, performance |
| WebSocket | L7 or L4 | L7 for routing; L4 for simplicity |
| Gaming UDP | L4 | Custom protocol, latency |
| gRPC | L7 (gRPC-aware) | Stream routing, health checks |
Each layer added increases operational complexity. A simple L7-only architecture may be preferable to a complex hybrid if the advanced capabilities aren't needed. Add layers only when they provide concrete value that justifies their operational cost.
Understanding use cases transforms theoretical knowledge into practical decision-making. The choice between Layer 4 and Layer 7—or a hybrid approach—depends on your specific protocol, latency requirements, routing needs, and operational priorities.
What's next:
With use cases established, the final page explores hybrid approaches in depth—architectural patterns that strategically combine Layer 4 and Layer 7 for optimal performance, flexibility, and operational efficiency.
You can now pattern-match your requirements to established use cases. Whether you're building a gaming platform, a microservices architecture, a trading system, or a web application, you know which layer—or combination—best serves your needs.