<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: charmingdisaster</title>
    <description>The latest articles on DEV Community by charmingdisaster (@charmingdisaster).</description>
    <link>https://dev.to/charmingdisaster</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4166507%2F6d733eb3-01e4-4978-afc6-9beeca490885.png</url>
      <title>DEV Community: charmingdisaster</title>
      <link>https://dev.to/charmingdisaster</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/charmingdisaster"/>
    <language>en</language>
    <item>
      <title>The Low-Level Mechanics of Connection Timeouts vs Read Timeouts (SO_TIMEOUT)</title>
      <dc:creator>charmingdisaster</dc:creator>
      <pubDate>Tue, 06 Oct 2026 13:14:13 +0000</pubDate>
      <link>https://dev.to/charmingdisaster/the-low-level-mechanics-of-connection-timeouts-vs-read-timeouts-sotimeout-1fcl</link>
      <guid>https://dev.to/charmingdisaster/the-low-level-mechanics-of-connection-timeouts-vs-read-timeouts-sotimeout-1fcl</guid>
      <description>&lt;p&gt;Something I've seen take down production backends a few times is a simple misunderstanding of how network timeouts actually work under the hood.&lt;/p&gt;

&lt;p&gt;A lot of developers set &lt;code&gt;readTimeout = 5000&lt;/code&gt; on their HTTP client and assume: &lt;em&gt;"Great, if the remote service takes longer than 5 seconds, it will fail fast."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Except &lt;code&gt;SO_TIMEOUT&lt;/code&gt; (Read Timeout) doesn't limit the total request duration at all. In POSIX-compliant socket abstractions, it only measures the delay &lt;strong&gt;between incoming TCP packets&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If a degraded downstream service or reverse proxy trickles just a single byte of data every 4.9 seconds, a 5-second socket timeout will continuously reset back to zero. A single hanging request can keep your worker thread trapped for hours without ever throwing a timeout exception.&lt;/p&gt;




&lt;h3&gt;
  
  
  Two Other Network Traps
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Connect Timeout vs Read Timeout:&lt;/strong&gt; If intermediate network infrastructure or a firewall silently drops your &lt;code&gt;SYN&lt;/code&gt; packet without returning a &lt;code&gt;TCP RST&lt;/code&gt;, the Linux kernel defaults to retrying for 75 to 127 seconds (&lt;code&gt;tcp_syn_retries&lt;/code&gt;) before giving up with &lt;code&gt;ETIMEDOUT&lt;/code&gt;. Without an explicit Connect Timeout on the client, your execution thread freezes before a socket is even established.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The "UNKNOWN" State:&lt;/strong&gt; Remote calls are never strictly binary. If your socket times out waiting for a response, you have no deterministic way to know if the payment gateway processed the charge or crashed right after saving it. Retrying blindly without idempotency keys is how double-billing happens.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  How to Configure This Properly in Spring Boot (Java 21)
&lt;/h3&gt;

&lt;p&gt;To protect your system, always set both timeouts explicitly using &lt;code&gt;RestClient&lt;/code&gt; or &lt;code&gt;RestTemplate&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.context.annotation.Bean&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.context.annotation.Configuration&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.http.client.JdkClientHttpRequestFactory&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.web.client.RestClient&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.net.http.HttpClient&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.time.Duration&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;@Configuration&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RestClientConfig&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Bean&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;RestClient&lt;/span&gt; &lt;span class="nf"&gt;restClient&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;HttpClient&lt;/span&gt; &lt;span class="n"&gt;httpClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;HttpClient&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newBuilder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;connectTimeout&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Duration&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ofSeconds&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="nc"&gt;JdkClientHttpRequestFactory&lt;/span&gt; &lt;span class="n"&gt;requestFactory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;JdkClientHttpRequestFactory&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;httpClient&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;requestFactory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setReadTimeout&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Duration&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ofSeconds&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;RestClient&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;requestFactory&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;requestFactory&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;baseUrl&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"https://api.example.com"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To guard against the "slow trickle" issue, enforce an absolute wall-clock execution deadline around critical calls (e.g., using CompletableFuture.orTimeout()).&lt;/p&gt;

&lt;p&gt;Note: This breakdown is excerpted from the opening chapter of my field guide "Surviving Distributed Systems"—a practical book covering production failure modes, concurrency, and fault tolerance in Java 21.&lt;/p&gt;

&lt;p&gt;You can download the full 16-page free sample PDF (Chapter 1) directly on Leanpub:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://leanpub.com/surviving-distributed-systems" rel="noopener noreferrer"&gt;https://leanpub.com/surviving-distributed-systems&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>architecture</category>
      <category>webdev</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
