Differences Between HTTP Long Connections and Short Connections

Concept

  • HTTP Short Connection (Non-persistent Connection): A TCP connection is established for each HTTP request-response, and it is immediately closed after completion.
  • HTTP Long Connection (Persistent Connection): Multiple HTTP requests and responses can be sent continuously over the same TCP connection, which remains open for a period of time, allowing the reuse of that connection for multiple requests.

Summary of Differences

Differences Between HTTP Long Connections and Short Connections

Advantages and Disadvantages

Short Connection

Advantages:

  • Simple implementation, easy to manage, and resources are easily released.
  • Avoids resource waste caused by long-term connection occupation.

Disadvantages:

  • Each request requires 3-way handshake and 4-way teardown, resulting in high network overhead and latency.
  • Suitable for low-frequency, single-request scenarios, not suitable for high-frequency or large numbers of requests.

Long Connection

Advantages:

  • Multiple requests can reuse the same TCP connection, significantly reducing the overhead and latency of establishing/closing connections.
  • Increases system throughput, suitable for high-concurrency access.
  • Avoids the consumption of ports and kernel resources caused by frequent TCP connection establishment.

Disadvantages:

  • Long-term connection occupation requires reasonable timeout settings; otherwise, it wastes server resources and may be exploited for DoS attacks.
  • The server needs a mechanism to manage and recycle idle connections.

Typical Application Scenarios

Short Connection

  • Email, simple form submissions, one-time API calls, and other low-interaction frequency products.
  • Services with few TCP connections, low pressure, and no need for connection reuse.

Long Connection

  • High-frequency communication between web front-end and back-end (e.g., single-page applications, API services).
  • Instant messaging (IM), online chat, real-time push notifications.
  • Businesses requiring multiple, frequent requests (e.g., image carousel loading, message push servers).

Supplement

  • HTTP/1.0 defaults to short connections, which can be made long connections using <span>Connection: keep-alive</span>;
  • HTTP/1.1 defaults to long connections unless explicitly stated with <span>Connection: close</span>;
  • New protocols like WebSocket/HTTP2 are more efficient and stateful than HTTP long connections, but their principles are similar to long connections.

Friends who want to learn about testing development can add Teacher Wu on WeChat: wulaoshi1978

Leave a Comment