<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>E2B on Robin Su</title><link>https://robinsujob.github.io/tags/e2b/</link><description>Recent content in E2B on Robin Su</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Thu, 22 Oct 2026 00:00:00 +0800</lastBuildDate><atom:link href="https://robinsujob.github.io/tags/e2b/index.xml" rel="self" type="application/rss+xml"/><item><title>Building E2B Sandbox Resilience: Lessons from Nomad's Heartbeat Mechanism</title><link>https://robinsujob.github.io/p/e2b-sandbox-resilience-nomad-heartbeats/</link><pubDate>Thu, 22 Oct 2026 00:00:00 +0800</pubDate><guid>https://robinsujob.github.io/p/e2b-sandbox-resilience-nomad-heartbeats/</guid><description>&lt;img src="https://robinsujob.github.io/" alt="Featured image of post Building E2B Sandbox Resilience: Lessons from Nomad's Heartbeat Mechanism" /&gt;&lt;p&gt;Running a self-hosted E2B cluster is about more than getting Firecracker sandboxes to start. We also need to consider how to keep them stable once they host long-running workloads. As a single cluster supports more sandboxes, resilience becomes increasingly important. &lt;strong&gt;Our architecture needs to protect existing runtime state and user connections while limiting the impact on capacity for new sandboxes, so that a local interruption does not cause unnecessary disruption elsewhere.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This article starts with a single Nomad server becoming unavailable. It describes what we learned about Server / Client heartbeats and the changes we explored to task preservation, node failure detection, and blast-radius control.&lt;/p&gt;
&lt;p&gt;In this self-hosted environment, three Nomad servers form the control plane. The failure affected only the VM hosting one of them; the Clients running sandboxes and the other nodes remained healthy. Our initial expectation was straightforward: the remaining servers would keep the control plane available, Clients would switch to another healthy server, and workloads would continue running.&lt;/p&gt;
&lt;p&gt;The tests revealed something counterintuitive: &lt;strong&gt;only one server was lost, yet Orchestrator and other tasks on healthy Nomad Client hosts were replaced.&lt;/strong&gt; To understand why, we first needed to examine how Nomad servers and Clients use heartbeats to maintain contact, and what Nomad does with existing tasks when those heartbeats stop arriving.&lt;/p&gt;
&lt;h2 id="how-a-single-server-failure-affects-an-e2b-deployment"&gt;How a Single Server Failure Affects an E2B Deployment
&lt;/h2&gt;&lt;p&gt;First, consider the relationship between E2B and Nomad. In this deployment, Nomad schedules and manages services such as the API, Orchestrator, session-proxy, and client-proxy. Orchestrator, in turn, manages Firecracker sandboxes on its host. &lt;strong&gt;Sandbox creation and state management are handled by Orchestrator instances deployed on sandbox nodes and managed by Nomad.&lt;/strong&gt;&lt;/p&gt;
&lt;pre class="mermaid" style="visibility:hidden"&gt;flowchart TB
 accTitle: Control and access paths in self-hosted E2B
 accDescr: Nomad manages infrastructure services, while Orchestrator manages sandboxes. Sandbox traffic does not pass through Nomad servers for every request.
 servers["3 x Nomad servers&lt;br/&gt;Leader / Follower"]

 subgraph host["Sandbox host"]
 agent["Nomad Client agent"]
 orchestrator["Orchestrator"]
 proxy["session-proxy"]
 sandboxes["Firecracker sandboxes&lt;br/&gt;Original processes / memory / files"]
 agent --&gt;|"Run tasks"| orchestrator
 agent --&gt;|"Run tasks"| proxy
 orchestrator --&gt;|"Create and manage"| sandboxes
 proxy --&gt;|"Forward workload requests"| sandboxes
 end

 agent -.-&gt;|"Node.UpdateStatus"| servers
 servers -.-&gt;|"Heartbeat response / allocation state"| agent
 user["E2B SDK / user"] --&gt; entry["client-proxy&lt;br/&gt;Shared proxy entry point"]
 entry --&gt; proxy&lt;/pre&gt;&lt;p&gt;A Nomad Client is the agent running on a host. It communicates with servers and executes tasks. When Nomad schedules a task group onto a Client node, the resulting execution instance is called an &lt;strong&gt;allocation&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;To observe whether a single server failure could trigger task replacement, we forcibly stopped one non-Leader server without configuring a disconnect retention policy. The other two servers maintained quorum, and we continued observing for approximately 11 minutes.&lt;/p&gt;
&lt;p&gt;Tracking the identities of the original allocations, we recorded the following replacements for selected tasks:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Task&lt;/th&gt;
 &lt;th style="text-align: right"&gt;Original allocations replaced / original total&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Orchestrator&lt;/td&gt;
 &lt;td style="text-align: right"&gt;&lt;strong&gt;7 / 20&lt;/strong&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;session-proxy&lt;/td&gt;
 &lt;td style="text-align: right"&gt;&lt;strong&gt;7 / 20&lt;/strong&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;client-proxy&lt;/td&gt;
 &lt;td style="text-align: right"&gt;&lt;strong&gt;2 / 3&lt;/strong&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The experiment showed that &lt;strong&gt;the control plane can instruct a Client to abandon its original tasks even when the Client host has neither failed nor restarted.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For an E2B cluster, this is an important detail. Orchestrator manages sandbox runtime resources and participates in sandbox creation and state management. An unnecessary replacement can therefore affect sandboxes that are already running.&lt;/p&gt;
&lt;h2 id="the-unexpected-sequence-tasks-stop-after-heartbeats-recover"&gt;The Unexpected Sequence: Tasks Stop After Heartbeats Recover
&lt;/h2&gt;&lt;p&gt;Using the platform&amp;rsquo;s confirmation that the server had stopped as the observation baseline, we aligned Nomad Client logs with workload events:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;At approximately 21 seconds, we had captured a successful heartbeat from the Client to a healthy server.&lt;/li&gt;
&lt;li&gt;At approximately 39 seconds, the original task began &lt;code&gt;Killing&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;At approximately 44 seconds, the original application stream closed.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The node and allocation states showed that &lt;strong&gt;the Client executed a previously made stop decision only after reconnecting and receiving an update.&lt;/strong&gt; The overall sequence was:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Heartbeat expires
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-&amp;gt; Server updates node status and triggers a scheduling evaluation
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-&amp;gt; Scheduler marks the original allocation as stop / lost
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-&amp;gt; Client reconnects and retrieves the allocation update
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-&amp;gt; Local task executes Killing
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-&amp;gt; Replacement allocation starts
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h2 id="understanding-nomad-heartbeats-how-node-failure-detection-affects-workloads"&gt;Understanding Nomad Heartbeats: How Node Failure Detection Affects Workloads
&lt;/h2&gt;&lt;p&gt;With this question in mind, we examined Nomad&amp;rsquo;s GitHub source code and official documentation. We traced the following call chain:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Client sends a heartbeat -&amp;gt; RPC selects a server -&amp;gt; Follower forwards the request -&amp;gt; Leader renews the heartbeat timer -&amp;gt; Expiration updates node status -&amp;gt; Scheduler processes allocations -&amp;gt; Client applies task updates.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The sequence diagram below provides the full picture before we walk through the code. Its timestamps are illustrative and start from the last successfully processed heartbeat at the Leader. The log timestamps above start from the platform&amp;rsquo;s stop confirmation, so the two timelines do not correspond directly.&lt;/p&gt;
&lt;pre class="mermaid" style="visibility:hidden"&gt;sequenceDiagram
 accTitle: From heartbeat expiration to termination of the original task
 accDescr: t=0 is the last successful heartbeat at the Leader. With an illustrative TTL of 15 seconds and grace of 10 seconds, expiration occurs at about 25 seconds. If connectivity returns at 40 seconds, the Client can still execute a stop plan created earlier. These are illustrative, not measured, timestamps.
 autonumber
 participant Time as Relative timeline
 participant C as Nomad Client
 participant F as Follower
 participant L as Leader
 participant S as Scheduler
 participant T as Original task

 Note over C,T: Illustrative: TTL=15s, grace=10s&lt;br/&gt;t=0: last successful heartbeat at Leader&lt;br/&gt;Not measured or to scale, scheduling delay omitted
 Note over Time: 0s&lt;br/&gt;Last successful heartbeat
 C-&gt;&gt;F: Node.UpdateStatus
 F-&gt;&gt;L: t=0: Forward heartbeat and renew timer
 L--&gt;&gt;F: Return dynamic TTL=15s
 F--&gt;&gt;C: Heartbeat succeeds

 Note over Time: 5s
 Note over F: t=5s: Assume the server VM becomes unavailable
 Note over Time: About 15s
 C-xF: t~15s: Next heartbeat RPC does not return promptly
 Note over Time: About 25s&lt;br/&gt;15s TTL + 10s grace
 Note over L: t~25s (15+10): Heartbeat budget expires
 L-&gt;&gt;L: Update node status
 L-&gt;&gt;S: Trigger node-update evaluation
 Note over Time: About 25s+
 S-&gt;&gt;L: t~25s+: Submit stop plan for original allocation

 Note over Time: 40s&lt;br/&gt;Assumed recovery point
 C-&gt;&gt;L: t=40s: Assume heartbeat succeeds through a healthy server
 C-&gt;&gt;L: Retrieve allocation updates
 L--&gt;&gt;C: Original allocation is already marked to stop
 Note over Time: 40s+
 C-&gt;&gt;T: t=40s+: Local runner executes Killing
 Note over C,T: The stop plan predates reconnection&lt;br/&gt;A recovered heartbeat does not automatically cancel it&lt;/pre&gt;&lt;p&gt;The following sections walk through the key code paths. The snippets have been simplified.&lt;/p&gt;
&lt;h3 id="1-client-heartbeats-report-node-status"&gt;1. Client Heartbeats Report Node Status
&lt;/h3&gt;&lt;p&gt;On the Client, &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/client/client.go#L1870-L1923" target="_blank" rel="noopener"
 &gt;&lt;code&gt;registerAndHeartbeat()&lt;/code&gt;&lt;/a&gt; handles registration and periodic heartbeats. The actual request is constructed in &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/client/client.go#L2236-L2274" target="_blank" rel="noopener"
 &gt;&lt;code&gt;updateNodeStatus()&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;span class="lnt"&gt;7
&lt;/span&gt;&lt;span class="lnt"&gt;8
&lt;/span&gt;&lt;span class="lnt"&gt;9
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;structs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NodeUpdateStatusRequest&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;NodeID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;NodeID&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;structs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NodeStatusReady&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;resp&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;structs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NodeUpdateResponse&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;RPC&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;Node.UpdateStatus&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nx"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heartbeatTTL&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;HeartbeatTTL&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The request reports the &lt;strong&gt;NodeID and node status&lt;/strong&gt;, not the application health of a particular Job. After a successful response, the Client uses &lt;code&gt;HeartbeatTTL&lt;/code&gt; to schedule its next heartbeat. After a failure, the heartbeat loop schedules a retry and triggers server discovery.&lt;/p&gt;
&lt;p&gt;Multiple tasks on the same Client therefore share the node heartbeat. &lt;code&gt;Node.UpdateAlloc&lt;/code&gt; reports allocation state, while Orchestrator&amp;rsquo;s gRPC health check measures application health. Neither replaces this Client-to-Server heartbeat.&lt;/p&gt;
&lt;h3 id="2-one-server-per-rpc-attempt-switching-paths-waits-for-the-call-to-return"&gt;2. One Server per RPC Attempt: Switching Paths Waits for the Call to Return
&lt;/h3&gt;&lt;p&gt;Next, consider the Client&amp;rsquo;s &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/client/rpc.go#L83-L146" target="_blank" rel="noopener"
 &gt;&lt;code&gt;rpc()&lt;/code&gt;&lt;/a&gt;. The key sequence, simplified, is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;servers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FindServer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;rpcErr&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connPool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;RPC&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Region&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Addr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;rpcErr&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;servers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;NotifyFailedServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The Client first selects a server and then makes the RPC through the connection pool. &lt;strong&gt;Only after the current call returns an error&lt;/strong&gt; does it report the server failure and decide whether to retry at the RPC layer, based on the error type and remaining budget. If the error is not safe to retry automatically, it returns to the heartbeat loop above.&lt;/p&gt;
&lt;p&gt;The important delay here is how long it takes an RPC to return an error after a connection fails. Under the &lt;a class="link" href="https://developer.hashicorp.com/nomad/docs/configuration/rpc" target="_blank" rel="noopener"
 &gt;default configuration&lt;/a&gt; and &lt;a class="link" href="https://github.com/hashicorp/yamux/blob/v0.1.2/session.go#L327-L379" target="_blank" rel="noopener"
 &gt;yamux implementation&lt;/a&gt;, when an established connection silently fails, the Keepalive interval is 30 seconds, and a Ping waits approximately 10 seconds for a Pong. Assuming the Ping write does not block and no Ping was already in flight before the failure, detecting the failure can take roughly 10-40 seconds, depending on the Keepalive phase.&lt;/p&gt;
&lt;p&gt;The retry budget is also checked only after the underlying call returns; it cannot interrupt that call early. This creates a possible race: &lt;strong&gt;the connection is still waiting to confirm a failure while the Leader&amp;rsquo;s node heartbeat budget has already expired.&lt;/strong&gt;&lt;/p&gt;
&lt;h3 id="3-heartbeats-can-pass-through-a-follower-but-the-leader-renews-the-timer"&gt;3. Heartbeats Can Pass Through a Follower, but the Leader Renews the Timer
&lt;/h3&gt;&lt;p&gt;When a server receives the request, it first passes through the forwarding check in &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/node_endpoint.go#L655-L678" target="_blank" rel="noopener"
 &gt;&lt;code&gt;Node.UpdateStatus()&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;srv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forward&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;Node.UpdateStatus&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/rpc.go#L603-L618" target="_blank" rel="noopener"
 &gt;&lt;code&gt;forward()&lt;/code&gt;&lt;/a&gt; determines whether the current server should handle the request. If it is not the Leader, it forwards the request to the Leader.&lt;/p&gt;
&lt;p&gt;The Client does not need to contact the Leader directly every time, but its selected first-hop server must deliver the request promptly. &lt;strong&gt;If the Follower stops responding and the Client cannot switch paths in time, a healthy Leader may still fail to receive that Client&amp;rsquo;s heartbeat.&lt;/strong&gt;&lt;/p&gt;
&lt;h3 id="4-the-leader-waits-for-the-dynamic-ttl-plus-grace"&gt;4. The Leader Waits for the Dynamic TTL Plus Grace
&lt;/h3&gt;&lt;p&gt;Once the heartbeat successfully reaches the Leader, it calls &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/heartbeat.go#L93-L135" target="_blank" rel="noopener"
 &gt;&lt;code&gt;resetHeartbeatTimer()&lt;/code&gt;&lt;/a&gt;. The function calculates an interval based on cluster size and then adds random jitter:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;span class="lnt"&gt;7
&lt;/span&gt;&lt;span class="lnt"&gt;8
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;ttl&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;helper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;RateScaledInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;srv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MaxHeartbeatsPerSecond&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;srv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MinHeartbeatTTL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heartbeatTimers&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;ttl&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;helper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;RandomStagger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ttl&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resetHeartbeatTimerLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;ttl&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;srv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;HeartbeatGrace&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;ttl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;strong&gt;The returned &lt;code&gt;ttl&lt;/code&gt; determines the Client&amp;rsquo;s next heartbeat interval, while the Leader sets its expiration timer to &lt;code&gt;ttl + HeartbeatGrace&lt;/code&gt;.&lt;/strong&gt; &lt;code&gt;heartbeat_grace&lt;/code&gt; adds time for the Server to wait for the next successful heartbeat.&lt;/p&gt;
&lt;p&gt;Following the &lt;a class="link" href="https://developer.hashicorp.com/nomad/docs/configuration/server#client-heartbeats" target="_blank" rel="noopener"
 &gt;official heartbeat documentation&lt;/a&gt;, the normal path can be understood as:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Reference node expiration time
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; = Time when the Leader last successfully processed a heartbeat and reset its timer
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; + Dynamic heartbeat TTL for that round
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; + heartbeat_grace
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;In this small cluster, the normal TTL was approximately 10-20 seconds. With the default 10-second grace, the Leader&amp;rsquo;s budget before declaring a Client&amp;rsquo;s heartbeat expired was approximately 20-30 seconds.&lt;/p&gt;
&lt;h3 id="5-timer-expiration-changes-node-status-before-triggering-scheduling"&gt;5. Timer Expiration Changes Node Status Before Triggering Scheduling
&lt;/h3&gt;&lt;p&gt;When the timer expires, the Leader runs &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/heartbeat.go#L137-L181" target="_blank" rel="noopener"
 &gt;&lt;code&gt;invalidateHeartbeat()&lt;/code&gt;&lt;/a&gt;. The key node-status branch is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Status&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;structs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NodeStatusDown&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;canDisconnect&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;hasPendingReconnects&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Status&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;structs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NodeStatusDisconnected&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Whether the node enters &lt;code&gt;disconnected&lt;/code&gt; depends on whether its allocations support reconnection and remain within their recovery windows. Not every heartbeat expiration leads to the same state transition.&lt;/p&gt;
&lt;p&gt;The &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/node_endpoint.go#L816-L873" target="_blank" rel="noopener"
 &gt;node-status update path&lt;/a&gt; then persists the necessary state changes through Raft and creates scheduling evaluations for the relevant transitions. The scheduler uses the Job type and disconnect policy to decide which allocations to retain temporarily, which to mark &lt;code&gt;lost&lt;/code&gt;, and whether to create replacements.&lt;/p&gt;
&lt;p&gt;For example, &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/scheduler/reconciler/reconcile_cluster.go#L1097-L1127" target="_blank" rel="noopener"
 &gt;stop calculations for service allocations&lt;/a&gt; handle the lost set, while the &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/scheduler/reconciler/reconcile_node.go#L185-L263" target="_blank" rel="noopener"
 &gt;node reconciliation logic for system Jobs&lt;/a&gt; distinguishes reconnectable &lt;code&gt;unknown&lt;/code&gt; allocations from those that need to be treated as lost.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;A heartbeat timeout does not mean the Leader directly sends a kill signal to an application process.&lt;/strong&gt; Node-status updates and scheduling plans sit between those events, and that is where the following configuration changes can intervene.&lt;/p&gt;
&lt;h3 id="6-after-reconnecting-the-client-applies-an-allocation-update-that-already-exists"&gt;6. After Reconnecting, the Client Applies an Allocation Update That Already Exists
&lt;/h3&gt;&lt;p&gt;The Client retrieves server-side allocation changes through &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/client/client.go#L2482-L2545" target="_blank" rel="noopener"
 &gt;&lt;code&gt;watchAllocations()&lt;/code&gt;&lt;/a&gt;. Once an update reaches the local runner, &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/client/allocrunner/alloc_runner.go#L1078-L1101" target="_blank" rel="noopener"
 &gt;&lt;code&gt;handleAllocUpdate()&lt;/code&gt;&lt;/a&gt; checks whether the allocation is transitioning to a terminal state:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;stopping&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;ar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Alloc&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;TerminalStatus&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;TerminalStatus&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;ar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAlloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;stopping&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;ar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;killTasks&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The terminal-state check also includes the Server&amp;rsquo;s desired state. Even if the original task is still running, the local runner stops it if the received update already requires termination.&lt;/p&gt;
&lt;p&gt;This connects the cause and effect seen in the logs: &lt;strong&gt;the Client did not switch heartbeat paths before its budget expired, so the Server updated node status and the scheduler created a stop plan. When the Client later reconnected, it applied the update and stopped the original task. Replacement means a new allocation starts, not that a task restarts within the original allocation.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The community has also discussed similar symptoms in &lt;a class="link" href="https://github.com/hashicorp/nomad/issues/17974" target="_blank" rel="noopener"
 &gt;hashicorp/nomad#17974&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Understanding this chain reveals two intervention points: &lt;strong&gt;allow more time to switch heartbeat paths before declaring a node disconnected, or give the original tasks a chance to reconnect after that declaration.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id="solution-1-preserve-original-tasks-and-access-paths-with-disconnect"&gt;Solution 1: Preserve Original Tasks and Access Paths with disconnect
&lt;/h2&gt;&lt;p&gt;For E2B, the first priority is to avoid unnecessarily replacing Orchestrator on a healthy Client because of a brief control-plane disconnection.&lt;/p&gt;
&lt;p&gt;We used Nomad&amp;rsquo;s &lt;code&gt;disconnect&lt;/code&gt; policy to separate &amp;ldquo;detect that the node is disconnected&amp;rdquo; from &amp;ldquo;immediately abandon its original tasks.&amp;rdquo; The configuration belongs in an existing Job&amp;rsquo;s &lt;strong&gt;task group&lt;/strong&gt;, for example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;span class="lnt"&gt;7
&lt;/span&gt;&lt;span class="lnt"&gt;8
&lt;/span&gt;&lt;span class="lnt"&gt;9
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-hcl" data-lang="hcl"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;group&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;orchestrator&amp;#34;&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;disconnect&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt; lost_after&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;50s&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt; replace&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;false&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt; reconcile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;keep_original&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; }&lt;span class="c1"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt; # Keep the existing task, network, and other definitions unchanged.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Orchestrator, session-proxy, and client-proxy should each be evaluated and configured in their respective groups where protection is needed.&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Setting&lt;/th&gt;
 &lt;th&gt;What it addresses&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;code&gt;lost_after = &amp;quot;50s&amp;quot;&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Provides a bounded reconnection window after an allocation enters &lt;code&gt;unknown&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;code&gt;replace = false&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Prevents immediate replacement during the disconnect retention phase&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;code&gt;reconcile = &amp;quot;keep_original&amp;quot;&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Prefers the original instance during applicable reconnection reconciliation&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Explicitly setting &lt;code&gt;replace = false&lt;/code&gt; prevents an immediate replacement from being created within the retention window. The corresponding &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/scheduler/reconciler/filters.go#L527-L544" target="_blank" rel="noopener"
 &gt;scheduler check&lt;/a&gt; is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;isDisconnecting&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReplaceOnDisconnect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The other distinction is when the window starts. &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/structs/alloc.go#L756-L779" target="_blank" rel="noopener"
 &gt;&lt;code&gt;Allocation.Expired()&lt;/code&gt;&lt;/a&gt; calculates expiration from the allocation&amp;rsquo;s most recent transition into &lt;code&gt;unknown&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;lastUnknown&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LastUnknown&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;expiry&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;lastUnknown&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetDisconnectLostAfter&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;expiry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;now&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Therefore, &lt;code&gt;lost_after = &amp;quot;50s&amp;quot;&lt;/code&gt; does not mean waiting 50 seconds from the server&amp;rsquo;s failure, and it is not equivalent to &lt;code&gt;heartbeat_grace = &amp;quot;50s&amp;quot;&lt;/code&gt;. Assuming a TTL of 15 seconds and a grace of 10 seconds, the approximate sequence is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;t = 0 Last successful heartbeat at the Leader
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;t ~ 25s Heartbeat expires; node disconnection handling begins
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;t ~ 25s+ Relevant allocation enters unknown
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;t ~ (25+50)=75s+ If recovery has not completed, the 50-second retention window expires
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id="disconnect-sequence-retain-the-original-task-then-wait-for-reconnection"&gt;disconnect Sequence: Retain the Original Task, Then Wait for Reconnection
&lt;/h3&gt;&lt;p&gt;The following diagram assumes that the original task on a healthy Client is still running. It shows how the &lt;code&gt;50s / false / keep_original&lt;/code&gt; policy handles a brief disconnection:&lt;/p&gt;
&lt;pre class="mermaid" style="visibility:hidden"&gt;sequenceDiagram
 accTitle: How disconnect retains the original task after node disconnection
 accDescr: t=0 is the last successful heartbeat. With an illustrative TTL of 15 seconds and grace of 10 seconds, expiration occurs at about 25 seconds. A separate 50-second window starts when the allocation enters unknown. The example either reconnects at 40 seconds or proceeds to expiration handling after approximately 75 seconds. These are not measured timestamps.
 autonumber
 participant Time as Relative timeline
 participant C as Nomad Client
 participant L as Leader
 participant S as Scheduler
 participant T as Original task

 Note over C,T: Illustrative: TTL=15s, grace=10s, lost_after=50s&lt;br/&gt;t=0: last successful heartbeat at Leader&lt;br/&gt;Not measured or to scale, scheduling delay omitted
 Note over Time: 0s&lt;br/&gt;Last successful heartbeat
 C-&gt;&gt;L: t=0: Last successful heartbeat through a server
 L--&gt;&gt;C: Return dynamic TTL=15s
 Note over Time: 5s
 Note over C,L: t=5s: Assume the original first-hop server fails
 Note over Time: About 15s
 C-xL: t~15s: Heartbeat through the original path does not succeed
 Note over Time: About 25s&lt;br/&gt;Heartbeat budget expires
 Note over L: t~25s (15+10): Heartbeat budget expires
 L-&gt;&gt;L: Mark node as disconnected
 L-&gt;&gt;S: Trigger scheduling evaluation
 Note over Time: tU ~ 25s+&lt;br/&gt;Retention window starts
 S-&gt;&gt;L: tU~25s+: Mark original allocation as unknown
 Note over L,S: Start a separate 50s window at tU&lt;br/&gt;Expiration: tU+50s~75s+
 Note over S,T: replace = false: No immediate replacement
 Note over T: Original task keeps running on the healthy host

 alt Assume connectivity returns at t=40s within the window
 Note over Time: 40s&lt;br/&gt;Still within the retention window
 C-&gt;&gt;L: Restore heartbeat through another healthy server
 C-&gt;&gt;L: Synchronize original allocation runtime state
 L-&gt;&gt;S: Trigger reconnection reconciliation
 S-&gt;&gt;L: Retain original instance under keep_original
 Note over T: t=40s+: Preserve task identity without a disconnection-induced restart
 else Still disconnected at tU+50s~75s+
 Note over Time: tU + 50s ~ 75s+&lt;br/&gt;Retention window expires
 S-&gt;&gt;L: Window expires, proceed to lost and subsequent recovery
 end&lt;/pre&gt;&lt;p&gt;This does not extend the node&amp;rsquo;s heartbeat budget. Instead, it delays the decision to abandon the original allocation: &lt;strong&gt;enter &lt;code&gt;unknown&lt;/code&gt;, avoid immediate replacement, and wait for the original instance to reconnect.&lt;/strong&gt; The node may still not be &lt;code&gt;ready&lt;/code&gt; during this period, and retaining the original instance is no longer guaranteed after the window expires.&lt;/p&gt;
&lt;p&gt;Protection also needs to follow the user access path. Preserving Orchestrator alone does not necessarily preserve an existing connection. If client-proxy is replaced, the sandbox can remain alive while the connection breaks. We therefore included session-proxy and client-proxy in the validation.&lt;/p&gt;
&lt;p&gt;In one test with a 50-second disconnect window, Nomad Clients did briefly enter &lt;code&gt;disconnected&lt;/code&gt;. The observed &lt;code&gt;unknown&lt;/code&gt; intervals for their original allocations were approximately 1.7-31.6 seconds, followed by recovery within the window:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Validation item&lt;/th&gt;
 &lt;th&gt;Observation&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Orchestrator, session-proxy, and client-proxy&lt;/td&gt;
 &lt;td&gt;Original allocations preserved; no additional task restarts&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Original sandbox process identities, memory, and files&lt;/td&gt;
 &lt;td&gt;10 / 10 preserved; final independent checks completed&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Original HTTP NDJSON streams&lt;/td&gt;
 &lt;td&gt;23 / 23 preserved&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Short requests during the formal observation period&lt;/td&gt;
 &lt;td&gt;12,581 requests, 0 errors&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;With disconnect configured, &lt;strong&gt;the Clients briefly lost control-plane contact, but their original tasks remained in place until reconnection, without being replaced.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id="beyond-task-preservation-how-node-status-affects-sandbox-creation-capacity"&gt;Beyond Task Preservation: How Node Status Affects Sandbox Creation Capacity
&lt;/h2&gt;&lt;p&gt;&lt;code&gt;disconnect&lt;/code&gt; preserves original tasks; it does not prevent a Client from being declared disconnected after its heartbeat expires. In other words, &lt;strong&gt;Orchestrator and existing sandboxes can remain running without the node remaining eligible to receive new sandbox placements.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In this E2B implementation, the API normally synchronizes node information from Nomad every 20 seconds, selecting nodes in the sandbox node pool whose status is &lt;code&gt;ready&lt;/code&gt;. The core filter is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nx"&gt;nomadapi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;QueryOptions&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Filter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;`Status == &amp;#34;ready&amp;#34; and NodePool == &amp;#34;default&amp;#34;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;If the query succeeds but a previously known node no longer appears in the results, the API marks it &lt;code&gt;unhealthy&lt;/code&gt;, affecting subsequent placement.&lt;/p&gt;
&lt;p&gt;The public E2B runtime also has an implementation that discovers Orchestrators through allocations. Its &lt;a class="link" href="https://github.com/e2b-dev/runtime/blob/afaaeaef96aae86f65ce60bf38fe083e7638a28e/packages/shared/pkg/clusters/discovery/nomad.go#L29-L57" target="_blank" rel="noopener"
 &gt;discovery filter&lt;/a&gt; requires &lt;code&gt;ClientStatus == &amp;quot;running&amp;quot;&lt;/code&gt;. Its &lt;a class="link" href="https://github.com/e2b-dev/runtime/blob/afaaeaef96aae86f65ce60bf38fe083e7638a28e/packages/api/internal/orchestrator/cache.go#L195-L211" target="_blank" rel="noopener"
 &gt;API synchronization logic&lt;/a&gt; also removes an original instance from the local node pool when that instance no longer appears in valid discovery results.&lt;/p&gt;
&lt;p&gt;The two implementations filter different objects, but they highlight the same issue: &lt;strong&gt;placement decisions depend on Nomad&amp;rsquo;s state, not only on whether the original Orchestrator process is alive.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Under a high sandbox creation rate, some healthy hosts may continue running existing sandboxes while temporarily being unable to share the load of new requests. Creation pressure shifts to the remaining eligible nodes. If they lack sufficient capacity, creation can fail, queue, or time out.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;This is fundamentally a blast-radius problem.&lt;/strong&gt; The failure originates in a single server and directly disrupts communication for some Clients. Node state then affects API placement, transferring pressure to other healthy nodes. &lt;code&gt;disconnect&lt;/code&gt; contains the &amp;ldquo;destroy original tasks&amp;rdquo; path, but it does not automatically contain the &amp;ldquo;exclude healthy capacity&amp;rdquo; and &amp;ldquo;overload the remaining nodes&amp;rdquo; paths.&lt;/p&gt;
&lt;h2 id="solution-2-reduce-false-node-failure-detections-with-heartbeat_grace"&gt;Solution 2: Reduce False Node Failure Detections with heartbeat_grace
&lt;/h2&gt;&lt;p&gt;&lt;code&gt;disconnect&lt;/code&gt; addresses the situation after a node has been declared disconnected: do not abandon its original tasks immediately. Since node state also affects creation capacity, we need to look one step earlier. Can we give the Client enough time to switch paths before declaring it disconnected?&lt;/p&gt;
&lt;p&gt;This is where the Server-side &lt;strong&gt;&lt;code&gt;heartbeat_grace&lt;/code&gt;&lt;/strong&gt; setting applies:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-hcl" data-lang="hcl"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;server&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt; heartbeat_grace&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;50s&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;It adds time beyond the dynamic TTL. Changing the default grace from 10 to 50 seconds adds 40 seconds of headroom. Combined with the normal TTL of approximately 10-20 seconds, the reference budget becomes roughly 60-70 seconds from the last successful heartbeat.&lt;/p&gt;
&lt;h3 id="heartbeat_grace-sequence-allow-more-switching-time-before-declaring-a-node-disconnected"&gt;heartbeat_grace Sequence: Allow More Switching Time Before Declaring a Node Disconnected
&lt;/h3&gt;&lt;p&gt;Unlike disconnect, this changes how long the Leader waits for the next successful heartbeat. The example still assumes that the same Leader remains healthy throughout:&lt;/p&gt;
&lt;pre class="mermaid" style="visibility:hidden"&gt;sequenceDiagram
 accTitle: How heartbeat_grace provides switching time before node failure detection
 accDescr: t=0 is the last successful heartbeat. With an illustrative TTL of 15 seconds and grace of 50 seconds, expiration is at 65 seconds. A heartbeat restored at 40 seconds prevents this disconnection. If no heartbeat succeeds by 65 seconds, failure handling begins. These are illustrative, not measured, timestamps.
 autonumber
 participant Time as Relative timeline
 participant C as Nomad Client
 participant F as Original Follower
 participant L as Leader
 participant T as Original task

 Note over C,T: Illustrative: TTL=15s, grace=50s&lt;br/&gt;t=0: last successful heartbeat at Leader&lt;br/&gt;Not measured or to scale, scheduling delay omitted
 Note over Time: 0s&lt;br/&gt;Last successful heartbeat
 C-&gt;&gt;F: Node.UpdateStatus
 F-&gt;&gt;L: t=0: Forward heartbeat and renew timer
 L-&gt;&gt;L: Set expiration to t=65s (15+50)
 L--&gt;&gt;F: Return dynamic TTL=15s
 F--&gt;&gt;C: Schedule next heartbeat using the dynamic TTL

 Note over Time: 5s
 Note over F: t=5s: Assume the server fails
 Note over Time: About 15s
 C-xF: t~15s: Next heartbeat RPC does not return promptly
 Note over C: Wait for failure detection and switch to a healthy server
 Note over Time: About 25s&lt;br/&gt;Expiration with default grace
 Note over L: t~25s: Default grace would have expired&lt;br/&gt;The extended budget still runs until t=65s

 alt Assume heartbeat recovers at t=40s, before t=65s
 Note over Time: 40s&lt;br/&gt;Before the 65s expiration point
 C-&gt;&gt;L: Complete heartbeat through a healthy server
 L-&gt;&gt;L: Reset the TTL + grace timer
 Note over L,T: No node disconnection or task replacement triggered by this heartbeat failure
 else Still no heartbeat at t=65s, or the Client has actually failed
 Note over Time: About 65s&lt;br/&gt;15s TTL + 50s grace
 Note over L: t~65s (15+50): Heartbeat budget expires
 L-&gt;&gt;L: t~65s+: Declare node failure and begin subsequent task handling
 end&lt;/pre&gt;&lt;p&gt;Across two complete server-stop tests with grace set to 50 seconds and no disconnect policy on the key tasks, none of the 20 surviving Clients in either run was observed entering &lt;code&gt;down&lt;/code&gt; or &lt;code&gt;disconnected&lt;/code&gt;, and no original tasks were replaced. This matched the expected behavior: &lt;strong&gt;if switching completes before the budget expires, there is no need to enter the subsequent disconnection and task-handling paths.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;heartbeat_grace&lt;/code&gt; gives the Leader more time to wait.&lt;/strong&gt; If connectivity recovers in time, that heartbeat failure does not lead to a node-disconnection decision.&lt;/p&gt;
&lt;p&gt;However, the cost becomes clearer when a Client actually fails. When a Server fails but the Client is healthy, we want to wait a little longer. When the Client&amp;rsquo;s host really has failed, we want to detect it quickly and stop placing work there. The same heartbeat signal cannot guarantee both &amp;ldquo;never falsely mark a healthy node down&amp;rdquo; and &amp;ldquo;immediately detect a real failure.&amp;rdquo; A longer grace delays real Client-failure detection at the Nomad heartbeat layer, although application health checks may exclude the failed node earlier.&lt;/p&gt;
&lt;p&gt;From a blast-radius perspective, the two adjustments place the cost at different points:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Adjustment&lt;/th&gt;
 &lt;th&gt;Benefit&lt;/th&gt;
 &lt;th&gt;Cost and boundary&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Moderately increase &lt;code&gt;heartbeat_grace&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Give the Leader more time for the next heartbeat, reduce false failure detections on healthy Clients, and lower the risk of unnecessary task replacement or exclusion from the creation pool&lt;/td&gt;
 &lt;td&gt;Real Client failures also become visible later at the Nomad heartbeat layer&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Shorter grace + targeted &lt;code&gt;disconnect&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Expose disconnection sooner after a real Client failure while giving original tasks a short reconnection window&lt;/td&gt;
 &lt;td&gt;During a Server failure, some healthy Clients may still temporarily leave the creation pool, shifting pressure to the remaining nodes&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id="summary-turning-heartbeat-findings-into-sandbox-resilience"&gt;Summary: Turning Heartbeat Findings into Sandbox Resilience
&lt;/h2&gt;&lt;p&gt;This investigation started with a simple question: why were tasks on healthy hosts rebuilt when only one Server was lost? Following the logs and code revealed a chain of architectural decisions that affect workloads: &lt;strong&gt;whether a heartbeat arrives in time, whether the node is declared disconnected, whether the original tasks are retained, and whether the layer above still treats the node as available capacity.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For E2B, we need to protect three goals separately:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Existing sandbox runtime state&lt;/strong&gt;: Separate node disconnection from immediately abandoning original tasks, leaving room to recover from brief control-plane failures.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Existing user connections&lt;/strong&gt;: Cover Orchestrator, session-proxy, and client-proxy rather than only verifying that the sandbox still exists.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Creation capacity and real-failure recovery&lt;/strong&gt;: Observe both the erroneous exclusion of healthy nodes and delays in detecting failed nodes or replacing tasks, while maintaining headroom on the remaining capacity.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Ultimately, resilience does not mean every failure causes zero interruption. It means preserving valuable runtime state during short disconnections, keeping recovery bounded when a real failure occurs, and limiting the knock-on effects to a level the workload can tolerate.&lt;/p&gt;
&lt;h2 id="references"&gt;References
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://developer.hashicorp.com/nomad/docs/configuration/server#heartbeat_grace" target="_blank" rel="noopener"
 &gt;Nomad Server Configuration: heartbeat_grace&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://developer.hashicorp.com/nomad/docs/configuration/server#client-heartbeats" target="_blank" rel="noopener"
 &gt;Nomad Client Heartbeats&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://developer.hashicorp.com/nomad/docs/job-specification/disconnect" target="_blank" rel="noopener"
 &gt;Nomad disconnect Specification&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/client/client.go#L1870-L1923" target="_blank" rel="noopener"
 &gt;Nomad Client Heartbeat Scheduling&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/heartbeat.go#L56-L181" target="_blank" rel="noopener"
 &gt;Nomad Leader Heartbeat Timers and Expiration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/structs/alloc.go#L756-L779" target="_blank" rel="noopener"
 &gt;Nomad Allocation Disconnect Expiration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/e2b-dev/runtime/blob/afaaeaef96aae86f65ce60bf38fe083e7638a28e/packages/shared/pkg/clusters/discovery/nomad.go#L29-L57" target="_blank" rel="noopener"
 &gt;E2B Runtime: Nomad Discovery Filters&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/e2b-dev/runtime/blob/afaaeaef96aae86f65ce60bf38fe083e7638a28e/packages/api/internal/orchestrator/cache.go#L53-L120" target="_blank" rel="noopener"
 &gt;E2B Runtime: API Node Synchronization&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/hashicorp/nomad/issues/17974" target="_blank" rel="noopener"
 &gt;hashicorp/nomad#17974: Non-Leader Connectivity Problems and Allocation Restarts&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item></channel></rss>