<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://onkarhoysala.com/feed.xml" rel="self" type="application/atom+xml" /><link href="https://onkarhoysala.com/" rel="alternate" type="text/html" /><updated>2026-11-24T08:29:25+00:00</updated><id>https://onkarhoysala.com/feed.xml</id><title type="html">Onkar Hoysala</title><entry><title type="html">Picking up Mastra workflow runs after a crash</title><link href="https://onkarhoysala.com/blog/mastra-rekindle/" rel="alternate" type="text/html" title="Picking up Mastra workflow runs after a crash" /><published>2026-11-24T00:00:00+00:00</published><updated>2026-11-24T00:00:00+00:00</updated><id>https://onkarhoysala.com/blog/mastra-rekindle</id><content type="html" xml:base="https://onkarhoysala.com/blog/mastra-rekindle/"><![CDATA[<p>At Flash we run 11 Mastra agents behind the agentic marketplace at flash.co, plus a WhatsApp commerce agent. Most of the real work happens in long workflows. Every day the agents run 8 to 10 jobs that take 30 minutes or more, and the WhatsApp agent runs all its async jobs as workflows.</p>

<p>Our Mastra servers run on ECS, as tasks. Tasks come and go all the time: a redeploy replaces them, new ones come up when we scale, and now and then one crashes. Pods on Kubernetes behave the same way. Whenever a task went away in the middle of a run, the run stopped there and stayed in <code class="language-plaintext highlighter-rouge">running</code>.</p>

<p>rekindle is the small library I wrote earlier this year to pick those runs back up. All 12 agents run on it, and so far it has resumed 492 runs. Those runs carried on by themselves after a deploy or a crash. A developer didn’t have to restart them, our CD pipeline didn’t have to trigger anything, nobody had to check the data afterwards, and the critical daily jobs finished on time.</p>

<p>It’s on npm as <code class="language-plaintext highlighter-rouge">mastra-rekindle</code>, for Mastra 1.46 to 1.83.</p>

<blockquote>
  <p>If you’re on Mastra 1.84 or later, Mastra now has workflow recovery built in, with run leases, a recovery loop and leader election. Use that. We’re moving to it too.</p>
</blockquote>

<h2 id="what-mastra-14x-gave-us">What Mastra 1.4x gave us</h2>

<p>Mastra saves every run to Postgres as it goes, step by step, and a stopped run could be resumed. <code class="language-plaintext highlighter-rouge">run.restart()</code> picks a run up from its last active step: the step that was interrupted runs again from its start, and the steps that had finished are skipped. <code class="language-plaintext highlighter-rouge">restartAllActiveWorkflowRuns()</code> does that for every active run of a workflow.</p>

<p>Two things were missing around it.</p>

<ul>
  <li>The restart wasn’t automatic. It had to be called for each stopped run, and finding those runs was up to us.</li>
  <li>Mastra 1.4x did not ensure a resume was picked up by only one server. A run whose server died and a run that is still going on another server both say <code class="language-plaintext highlighter-rouge">running</code>, and restart-all restarts both. A server that stalls for a while (a long GC pause, a blocked event loop) can also wake up and carry on with a run that another server already picked up.</li>
</ul>

<p>rekindle adds those two things: it notices which runs have actually been abandoned, and makes sure only one server resumes each of them.</p>

<h2 id="how-it-works">How it works</h2>

<p>Everything lives in the Postgres that Mastra already uses, in two tables next to Mastra’s own: <code class="language-plaintext highlighter-rouge">rekindle_instances</code> and <code class="language-plaintext highlighter-rouge">rekindle_run_owners</code>.</p>

<p><img src="/blog/mastra-rekindle/images/1-overview.jpg" alt="Three servers running Mastra and rekindle, all sharing one Postgres that holds the Mastra tables and the two rekindle tables" /></p>

<h3 id="heartbeats">Heartbeats</h3>

<p>Every server process writes a heartbeat to <code class="language-plaintext highlighter-rouge">rekindle_instances</code> every 5 seconds. It’s one row per process with a timestamp.</p>

<p>A heartbeat older than 20 seconds means that process is dead. That’s the only way rekindle decides something is dead. A process that gets <code class="language-plaintext highlighter-rouge">kill -9</code>’d or loses its machine can’t say goodbye, so rekindle doesn’t wait for one.</p>

<p><img src="/blog/mastra-rekindle/images/2-heartbeats.jpg" alt="Three servers heartbeating every 5 seconds; server C stops and is counted dead after 20 seconds" /></p>

<h3 id="owners">Owners</h3>

<p>Every run of an opted-in workflow gets one row in <code class="language-plaintext highlighter-rouge">rekindle_run_owners</code> saying which process owns it. The row is the lock. Only the process named in it may run that run’s steps.</p>

<h3 id="the-guard">The guard</h3>

<p>Each step starts with one line:</p>

<div class="language-ts highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">const</span> <span class="nx">ownership</span> <span class="o">=</span> <span class="nx">rekindleWorkflow</span><span class="p">(</span><span class="dl">'</span><span class="s1">scoutRun</span><span class="dl">'</span><span class="p">)</span>

<span class="kd">const</span> <span class="nx">validate</span> <span class="o">=</span> <span class="nx">createStep</span><span class="p">({</span>
  <span class="na">id</span><span class="p">:</span> <span class="dl">'</span><span class="s1">validate</span><span class="dl">'</span><span class="p">,</span>
  <span class="nx">inputSchema</span><span class="p">,</span> <span class="nx">outputSchema</span><span class="p">,</span>
  <span class="na">execute</span><span class="p">:</span> <span class="k">async</span> <span class="p">({</span> <span class="nx">inputData</span><span class="p">,</span> <span class="nx">runId</span> <span class="p">})</span> <span class="o">=&gt;</span> <span class="p">{</span>
    <span class="k">await</span> <span class="nx">ownership</span><span class="p">.</span><span class="nx">guard</span><span class="p">(</span><span class="nx">runId</span><span class="p">)</span>
    <span class="c1">// ... the step's work ...</span>
  <span class="p">},</span>
<span class="p">})</span>
</code></pre></div></div>

<p>The guard takes the row if nobody owns the run, keeps it if this process already owns it, and takes it over if the owner is dead. If a different process owns it and that process is alive, the guard throws and the step stops.</p>

<p>That last case is the stalled server. Say server A freezes for a minute. Its heartbeat goes stale, B claims the run and resumes it. When A wakes up and reaches its next step, the guard finds B in the row with a fresh heartbeat, and A stops there.</p>

<p><img src="/blog/mastra-rekindle/images/3-stalled.jpg" alt="Server A stalls after step 1; server B claims the run and starts step 2; when A wakes up, the guard stops it because B owns the run" /></p>

<p>A guard can also run a few seconds after the owner died, before its heartbeat has gone stale. So before giving up, the guard retries for a little longer than the stale window. A dead owner goes stale inside that time. A live one doesn’t.</p>

<h3 id="the-sweep-and-the-claim">The sweep and the claim</h3>

<p>Every 10 minutes or so, each server sweeps. It lists the <code class="language-plaintext highlighter-rouge">running</code> and <code class="language-plaintext highlighter-rouge">waiting</code> runs of the opted-in workflows and tries to claim each one.</p>

<p>A claim is one SQL statement:</p>

<div class="language-sql highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">UPDATE</span> <span class="n">rekindle_run_owners</span>
   <span class="k">SET</span> <span class="n">owner_instance_id</span> <span class="o">=</span> <span class="err">$</span><span class="n">me</span><span class="p">,</span> <span class="n">claimed_at</span> <span class="o">=</span> <span class="n">now</span><span class="p">(),</span> <span class="n">restarted_at</span> <span class="o">=</span> <span class="n">now</span><span class="p">(),</span>
       <span class="n">attempts</span> <span class="o">=</span> <span class="n">attempts</span> <span class="o">+</span> <span class="mi">1</span>
 <span class="k">WHERE</span> <span class="n">workflow_name</span> <span class="o">=</span> <span class="err">$</span><span class="mi">1</span> <span class="k">AND</span> <span class="n">run_id</span> <span class="o">=</span> <span class="err">$</span><span class="mi">2</span>
   <span class="k">AND</span> <span class="n">done_at</span> <span class="k">IS</span> <span class="k">NULL</span>
   <span class="k">AND</span> <span class="n">attempts</span> <span class="o">&lt;</span> <span class="mi">3</span>
   <span class="k">AND</span> <span class="p">(</span><span class="n">owner_instance_id</span> <span class="k">IS</span> <span class="k">NULL</span> <span class="k">OR</span> <span class="n">owner_instance_id</span> <span class="k">IN</span> <span class="p">(</span>
         <span class="k">SELECT</span> <span class="n">instance_id</span> <span class="k">FROM</span> <span class="n">rekindle_instances</span>
          <span class="k">WHERE</span> <span class="n">heartbeat_at</span> <span class="o">&lt;</span> <span class="n">now</span><span class="p">()</span> <span class="o">-</span> <span class="n">interval</span> <span class="s1">'20 seconds'</span><span class="p">))</span>
   <span class="k">AND</span> <span class="k">EXISTS</span> <span class="p">(</span><span class="k">SELECT</span> <span class="mi">1</span> <span class="k">FROM</span> <span class="n">rekindle_instances</span>
                <span class="k">WHERE</span> <span class="n">instance_id</span> <span class="o">=</span> <span class="err">$</span><span class="n">me</span>
                  <span class="k">AND</span> <span class="n">heartbeat_at</span> <span class="o">&gt;=</span> <span class="n">now</span><span class="p">()</span> <span class="o">-</span> <span class="n">interval</span> <span class="s1">'20 seconds'</span><span class="p">)</span>
</code></pre></div></div>

<p>It only matches if the run’s owner is dead or has let go, the run isn’t done, it hasn’t been restarted 3 times already, and the server making the claim is alive itself. The heartbeat checks run inside the same statement, against Postgres <code class="language-plaintext highlighter-rouge">now()</code>. There’s no gap between checking and taking, and the servers’ own clocks don’t matter.</p>

<p>When five servers sweep at the same moment, all five send this. Postgres gives the row to one of them, and the other four update 0 rows and move on. The one that got the row calls <code class="language-plaintext highlighter-rouge">restart()</code>.</p>

<p><img src="/blog/mastra-rekindle/images/4-claim.jpg" alt="Servers B, C and D all claim run 42 at once; B wins and calls restart(), C and D get 0 rows and skip" /></p>

<p>A few details around the sweep:</p>

<ul>
  <li>The first sweep waits 30 seconds after boot, plus some jitter. In a deploy the old servers go down first, and their heartbeats need to go stale before anyone can take their runs.</li>
  <li>Sweeps are jittered, so servers don’t all sweep in the same second.</li>
  <li>A sweep restarts at most 25 runs. After a big outage the backlog goes out over a few sweeps instead of all at once.</li>
  <li>A run with no owner row at all is only claimed once Mastra itself hasn’t updated it for 20 seconds. That covers a run whose first step hadn’t reached its guard yet.</li>
</ul>

<h3 id="finishing-and-shutting-down">Finishing and shutting down</h3>

<p>The last step calls <code class="language-plaintext highlighter-rouge">ownership.clear(runId)</code>, which marks the run done. It’s a mark, not a delete. Mastra saves the run’s final status a moment after the last step returns, and until then the run still shows as <code class="language-plaintext highlighter-rouge">running</code>. The mark stops a sweep from picking it up in that gap.</p>

<p>On SIGTERM a server stops claiming new runs but keeps heartbeating. The runs it’s still finishing don’t get taken over while it shuts down.</p>

<h3 id="what-resuming-actually-reruns">What resuming actually reruns</h3>

<p>A resumed run starts the interrupted step again from the top. Steps that finished before the crash don’t run again. So every step has to be safe to run twice: read its inputs again, and write in a way that doesn’t duplicate.</p>

<p><img src="/blog/mastra-rekindle/images/5-rerun.jpg" alt="Server A finishes step 1 and crashes halfway through step 2; server B skips step 1, starts step 2 over from the beginning, then finishes step 3" /></p>

<h2 id="try-it">Try it</h2>

<p>The repo has a demo that does the whole thing on your machine. Two servers share one Postgres. Server A starts a three-step run and gets <code class="language-plaintext highlighter-rouge">kill -9</code>’d in the middle of the slow step. Server B finishes it.</p>

<div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git clone https://github.com/onkarhoysala/mastra-rekindle
<span class="nb">cd </span>mastra-rekindle
npm <span class="nb">install
</span>npm run db:start
<span class="nv">DATABASE_URL</span><span class="o">=</span>postgres://postgres@localhost:54329/postgres npx tsx examples/basic/demo.ts
</code></pre></div></div>

<p><code class="language-plaintext highlighter-rouge">db:start</code> starts a throwaway Postgres on port 54329 from a local install. You can point <code class="language-plaintext highlighter-rouge">DATABASE_URL</code> at any Postgres instead.</p>

<p>Trimmed output:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>  0.7s  [A] RUN_STARTED 4e5497a1
  0.7s  [A] step gather start
  2.7s  [A] step gather end
  2.7s  [A] step slow start
  5.7s  [A]   slow ... 3/10
  5.9s  --- kill -9 A (mid step "slow")
  5.9s  --- starting instance B
 11.5s  [rekindle] resuming abandoned run { workflow: 'demoRun', instanceId: 'B-fe9d48' }
 11.5s  [B] step slow start
 21.6s  [B] step slow end
 21.6s  [B] step publish start
 22.6s  [B] step publish end

step log for run 4e5497a1
  gather   start  on A
  gather   end    on A
  slow     start  on A
  slow     start  on B
  slow     end    on B
  publish  start  on B
  publish  end    on B

PASS: resumed on B at the interrupted step, gather not re-run
</code></pre></div></div>

<p>A runs <code class="language-plaintext highlighter-rouge">gather</code>, starts <code class="language-plaintext highlighter-rouge">slow</code> and gets killed 3 seconds in. B boots, and its sweep claims the run once A’s heartbeat is stale. The demo uses short timings (a 4 second stale window and a 3 second sweep) so it finishes in about 20 seconds. B runs <code class="language-plaintext highlighter-rouge">slow</code> again from its start, then <code class="language-plaintext highlighter-rouge">publish</code>. <code class="language-plaintext highlighter-rouge">gather</code> doesn’t run again.</p>

<h2 id="adding-it-to-your-app">Adding it to your app</h2>

<p>In each workflow, register it once, guard every step, and clear in the last one:</p>

<div class="language-ts highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">import</span> <span class="p">{</span> <span class="nx">rekindleWorkflow</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">mastra-rekindle</span><span class="dl">'</span>

<span class="kd">const</span> <span class="nx">ownership</span> <span class="o">=</span> <span class="nx">rekindleWorkflow</span><span class="p">(</span><span class="dl">'</span><span class="s1">myWorkflow</span><span class="dl">'</span><span class="p">)</span>  <span class="c1">// same key as in new Mastra({ workflows })</span>

<span class="c1">// first line of every step</span>
<span class="k">await</span> <span class="nx">ownership</span><span class="p">.</span><span class="nx">guard</span><span class="p">(</span><span class="nx">runId</span><span class="p">)</span>

<span class="c1">// in the last step</span>
<span class="k">await</span> <span class="nx">ownership</span><span class="p">.</span><span class="nx">clear</span><span class="p">(</span><span class="nx">runId</span><span class="p">)</span>
</code></pre></div></div>

<p>Use the <code class="language-plaintext highlighter-rouge">runId</code> that Mastra passes to <code class="language-plaintext highlighter-rouge">execute</code>. Don’t guard on a field from your input, even one called <code class="language-plaintext highlighter-rouge">runId</code>. If you start runs with your own ids, the two can differ, and then the guard protects a run the sweep never looks at.</p>

<p>When the server boots:</p>

<div class="language-ts highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">import</span> <span class="p">{</span> <span class="nx">createRekindle</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">mastra-rekindle</span><span class="dl">'</span>

<span class="kd">const</span> <span class="nx">rekindle</span> <span class="o">=</span> <span class="nx">createRekindle</span><span class="p">({</span> <span class="nx">mastra</span> <span class="p">})</span>  <span class="c1">// uses Mastra's Postgres pool, schema and logger</span>
<span class="k">await</span> <span class="nx">rekindle</span><span class="p">.</span><span class="nx">migrate</span><span class="p">()</span>
<span class="k">await</span> <span class="nx">rekindle</span><span class="p">.</span><span class="nx">start</span><span class="p">()</span>

<span class="nx">process</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="dl">'</span><span class="s1">SIGTERM</span><span class="dl">'</span><span class="p">,</span> <span class="k">async</span> <span class="p">()</span> <span class="o">=&gt;</span> <span class="p">{</span>
  <span class="nx">rekindle</span><span class="p">.</span><span class="nx">drain</span><span class="p">()</span>
  <span class="k">await</span> <span class="nx">yourShutdown</span><span class="p">()</span>
  <span class="nx">process</span><span class="p">.</span><span class="nx">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="p">})</span>
</code></pre></div></div>

<p>On Mastra versions that have the <code class="language-plaintext highlighter-rouge">recovery</code> option (eg 1.78), also set <code class="language-plaintext highlighter-rouge">recovery: { workflows: 'off' }</code>. Otherwise Mastra restarts every active run on every boot, alongside rekindle.</p>

<h2 id="limits">Limits</h2>

<ul>
  <li>It’s Postgres only.</li>
  <li>Resume is per step. A step interrupted halfway reruns from its start, so steps have to be safe to run twice.</li>
  <li>The guard only checks at the start of a step. If a live server can’t heartbeat for 20 seconds (pool exhausted, a long database failover), another server takes its runs and the step it was in the middle of runs twice. Giving rekindle a small pool of its own helps.</li>
  <li>A step without a guard isn’t covered while it runs. An agent passed straight to <code class="language-plaintext highlighter-rouge">createStep</code>, a <code class="language-plaintext highlighter-rouge">.map</code> or a nested workflow needs wrapping in a step that guards.</li>
  <li>A hung step on a live server isn’t detected.</li>
  <li>On the first rollout, runs that were already in flight have no owner row yet. If Mastra hasn’t updated one for 20 seconds, it can be restarted.</li>
</ul>

<hr />

<p><code class="language-plaintext highlighter-rouge">npm install mastra-rekindle</code>. The code, tests and docs are on <a href="https://github.com/onkarhoysala/mastra-rekindle">GitHub</a>. It’s for Mastra 1.46 to 1.83. On 1.84 or later, use Mastra’s own recovery.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[rekindle is a small library that picks Mastra workflow runs back up after a deploy or a crash. On npm as mastra-rekindle, for Mastra 1.46 to 1.83.]]></summary></entry></feed>