Core Fundamentals

Connection Timeout

0 of 6 complete

0%

Contents

Back|Core FundamentalsConnection Timeout
1/6
10 min left
  1. Home
  2. System Design: Foundation
  3. Core Fundamentals
  4. Connection Timeout
Prerequisites
Latencyrequired
Related Topics
DNS (Domain Name System)Load Balancing & ProxiesUnicast RoutingLoad Balancing & ProxiesBroadcastLoad Balancing & ProxiesMulticastLoad Balancing & ProxiesAnycast RoutingLoad Balancing & Proxies
Previous lessonSemantic VersioningNext lessonRequest Timeout

System Design

  • Foundation
1 of 6
  • Intermediate
  • Advanced
  • Capstone
  • AI Engineering

    • Foundation
    • Data, RAG and Agents
    • Evaluation, LLM Ops and Security

    systemdesign.academy

    • Home
    • Glossary
    • Interview prep
    • Reviews
    • About
    • Privacy
    • Terms

    Waiting for a Call That Never Connects

    You dial someone's number. It rings. And rings. And rings. After about 30 seconds, you hang up. Nobody answered, no point waiting forever.

    That decision to hang up after 30 seconds? That's a connection timeout.

    In software, a connection timeout is the maximum time your application will wait to establish a connection with another service before giving up. Not the time to get a response, just the time to confirm the other side is even there and willing to talk.

    This matters more than most developers realize. Here's a scenario: your web app calls a payment service. The payment service's server is down, not returning errors, just completely unreachable. Without a connection timeout, your app would wait. And wait. And wait. Meanwhile, the user is staring at a spinner. Your server thread is stuck. As more users hit the same codepath, more threads get stuck. Eventually, your entire application hangs because all threads are blocked waiting for a dead service.

    A missing connection timeout is one of the most common causes of cascading failures in production systems. The fix takes one line of code. The outage it prevents can save your company hours of downtime.

    What Exactly Happens During Connection Establishment?

    Before any data is exchanged between a client and a server, they perform a handshake, a three-step process to establish a connection.

    1. SYN, the client sends a "hey, I want to connect" packet
    2. SYN-ACK, the server responds "ok, I'm here, let's connect"
    3. ACK, the client confirms "great, connection established"

    This entire handshake typically takes 1-100ms depending on network distance. If the server is in the same data center, it's under 1ms. If it's across the ocean, maybe 50-100ms.

    The connection timeout is the maximum time you'll wait for that SYN-ACK. If the server never responds, because it's down, the network is broken, a firewall is blocking traffic, or the server is so overloaded it can't accept new connections, your client gives up after the timeout and throws an error.

    Connection Timeout vs. Read/Request Timeout

    This is an important distinction that many developers confuse:

    Timeout TypeWhat It CoversTypical Value
    Connection TimeoutTime to establish the TCP connection (the handshake)1-5 seconds
    Read/Request TimeoutTime to receive a response after the connection is established

    The TCP Handshake, Step by Step

    Before your application sends a single byte of real data, the client and server perform a three-way handshake. The connection timeout governs how long the client will wait for this handshake to finish.

    When Connection Timeouts Fire

    Walk through what happens when a connection timeout kicks in, and why it prevents cascading failures.

    Setting the Right Value

    The default connection timeout in most clients is way too generous. Java's HttpURLConnection defaults to infinite, it will wait forever. Python's requests library also defaults to no timeout. That's asking for trouble.

    Rules of Thumb

    Same data center (service-to-service): 1-2 seconds. If a service in the same network isn't responding within 1 second, something is seriously wrong. Waiting longer won't help.

    2-3 seconds. A little more slack for the extra network hop.

    Quick Check

    Knowledge Check

    3 questions - Score 80% to pass

    Q1

    What does a connection timeout protect against?

    Q2

    Service A calls Service B in the same data center. The connection typically establishes in 2ms. What's a reasonable connection timeout?

    Q3

    Your app has no connection timeout configured. The payment service goes down. What is most likely to happen?

    5-30 seconds

    The connection timeout is almost always shorter than the request timeout. If the server isn't reachable, you want to know fast. But once connected, the server might legitimately need time to process your request.

    Same region (cross-availability-zone):

    Cross-region or external APIs: 3-5 seconds. International network paths can be slow, but anything over 5 seconds usually means the destination is unreachable.

    Never: Use no timeout. Never. Not in prototype code, not in scripts, not anywhere. Always set a connection timeout.

    What Happens When You Set It Too Low

    If your connection timeout is too aggressive, say 100ms for a service across the Atlantic, you'll get false failures. The connection would have succeeded at 150ms, but you gave up too early. Users see errors for no reason.

    What Happens When You Set It Too High

    If your connection timeout is 60 seconds, and the target service is dead, every request ties up a thread for a full minute before failing. Under load, you run out of threads in minutes and your entire application becomes unresponsive. This is the classic cascading failure pattern.

    The Sweet Spot

    Look at your P99 connection time under normal conditions (most monitoring tools track this). Set your timeout to 2-3x that value. If connections normally complete in 200ms at P99, a timeout of 500ms-1s is reasonable.