CodePark/ questions / Load balancing & proxies
START PRACTICINGPRACTICE
Backend question · System design & architecture › Load balancing & proxies

What is the difference between an L4 and an L7 load balancer? Give a case where you would pick each.

An L4 load balancer routes connections by IP address and port. An L7 load balancer reads the HTTP request and routes by its content.

An L4 load balancer works at the transport layer (layer 4: TCP and UDP). It does not read the application data inside the connection. This makes it fast, and it works with any protocol. Use it for:

  • very high connection rates
  • non-HTTP protocols, such as a database or MQTT
  • cases where TLS should pass through to the backend unchanged

An L7 load balancer works at the application layer (layer 7), usually HTTP. It can:

  • terminate TLS, which means decrypt HTTPS itself
  • route by host, path or header, for example send /api to one service and /static to another
  • use cookies to keep a user on the same server (stickiness)
  • rewrite headers
  • retry failed requests

The cost is more processing work for every request.

Pick L7 for HTTP microservices that need path-based routing. Pick L4 for raw throughput or non-HTTP traffic. On AWS, these map roughly to the Application Load Balancer (L7) and the Network Load Balancer (L4).

  • L4 routes connections by IP and port and never reads the data inside.
  • L7 reads HTTP and can route by host, path, header or cookie.
  • L7 can terminate TLS, rewrite headers and retry requests.
  • Use L4 for raw throughput and for non-HTTP protocols.