<?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: ImmuneFOMO</title>
    <description>The latest articles on DEV Community by ImmuneFOMO (@immunefomo).</description>
    <link>https://dev.to/immunefomo</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%2F1301732%2F224647e7-8872-4b2d-b532-2356acc9e0ed.jpeg</url>
      <title>DEV Community: ImmuneFOMO</title>
      <link>https://dev.to/immunefomo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/immunefomo"/>
    <language>en</language>
    <item>
      <title>What's the Best Decision Model? I Benchmarked 17 Against GPT-6.1 and Claude</title>
      <dc:creator>ImmuneFOMO</dc:creator>
      <pubDate>Wed, 02 Dec 2026 12:46:20 +0000</pubDate>
      <link>https://dev.to/immunefomo/whats-the-best-decision-model-i-benchmarked-17-against-gpt-61-and-claude-548m</link>
      <guid>https://dev.to/immunefomo/whats-the-best-decision-model-i-benchmarked-17-against-gpt-61-and-claude-548m</guid>
      <description>&lt;p&gt;&lt;em&gt;The code, tables and raw scores are &lt;a href="https://github.com/ImmuneFOMO/research/tree/main/llm/decision-models" rel="noopener noreferrer"&gt;on GitHub&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fx5ms62mi32emb0cebsoi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fx5ms62mi32emb0cebsoi.png" alt="Accuracy against price for decision models and regular LLMs" width="800" height="667"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Mean valid-answer accuracy across four tasks against dollars per thousand decisions. Free endpoints sit in a separate category. Only Terra has a smaller full-run sample. Faded points extrapolate fast tiers from small paired samples; use the shared-item comparison below for head-to-head claims.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;An agent has a list of possible actions and needs to pick one. Calling a tool, asking for missing information, choosing which page element to click. You can send that list to a regular LLM. You can also send it to a decision model, an API built for this exact step.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The AlphaVPS link near the end is a referral link; the netcup voucher there comes from my own account.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Take a bot that reads new GitHub issues and decides which ones are worth drafting a reply to. The reply still needs a language model. The filter only has to say whether generating one is worth the time.&lt;/p&gt;

&lt;p&gt;I tested dedicated decision endpoints against cheap chat models and two stronger baselines. For this sort of fixed-choice work, I'd start with a decision model. The cheap LLMs lost by enough that prompt-format failures alone couldn't explain the gap. GPT-6.1 Terra kept up, but cost about 52 times as much as SubQ.&lt;/p&gt;

&lt;p&gt;I compared 17 decision models with six regular LLMs on four public tasks, checking accuracy, calibration, latency, cost and stability, plus whether paid priority tiers were faster. Then I picked a model for each of four jobs.&lt;/p&gt;

&lt;p&gt;The short version:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SubQ Snap 1.2 is my starting point for a filter like that: 199 ms and $0.018 per thousand decisions. It still needs a labelled test on real issues.&lt;/li&gt;
&lt;li&gt;On 713 items every model answered validly, SubQ scored 87.9%, bison-1 88.2%, mammoth-1 88.6% and Terra 86.5%. These results don't settle the order of the leaders.&lt;/li&gt;
&lt;li&gt;At a confidence threshold of 0.9, SubQ acted on 1,123 of 1,600 items and got 97.9% of them right.&lt;/li&gt;
&lt;li&gt;At equal coverage on the shared items, SubQ stayed ahead of Terra: 98.2% against 92.8% on the 70% of answers each was most sure about.&lt;/li&gt;
&lt;li&gt;For web clicks, I'd try Liquid d1.2-ultra direct. For a free starting point, Perplexity Decider v1.3 was competitive. Keep a fallback for low-confidence choices.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  What went into the test
&lt;/h2&gt;

&lt;p&gt;A decision model takes context and a fixed list of options. It returns a choice and a probability for each option. The System One request format looks like this, with a made-up example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"model"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"subq-snap-1.2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"state"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"New issue: the CLI crashes on Windows when the path contains spaces. Steps to reproduce included."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"questions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"decision"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"choice"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nl"&gt;"instructions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Choose whether this issue is worth drafting a reply to."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nl"&gt;"criteria"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"reply"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"A reply can move it forward"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"skip"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Nothing useful to add yet"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The useful part of a response is a distribution such as &lt;code&gt;{"reply": 0.94, "skip": 0.06}&lt;/code&gt;. Those are illustrative probabilities, not a measured result. Pick the largest probability, then decide whether it's high enough to act on. No text generation is needed for that step. For accuracy, I compare the highest-probability option with the dataset's reference answer.&lt;/p&gt;

&lt;p&gt;The benchmark covered 18 decision endpoints, representing 17 distinct models because Liquid d1.2-ultra ran both direct and through OpenRouter. The six chat baselines were Haiku 5.5, DeepSeek V4.1 Flash, GPT-6 Luna, Gemini 3.1 Flash Lite, Sonnet 5.5 and GPT-6.1 Terra.&lt;/p&gt;

&lt;p&gt;Each task had 400 test items: ten banking intents from BANKING77, tool-use choices from NVIDIA When2Call, knowledge questions from MMLU-Pro, and ten candidate elements per click from Mind2Web. Only Terra ran 200 items per task to stay within budget; the other models ran all 1,600 items.&lt;/p&gt;

&lt;p&gt;Chat models received the same options and were asked for a JSON probability vector, with reasoning off and temperature zero where allowed. Claude ran through the Anthropic API, which rejects temperature on Claude 5.5, so those models used default sampling with thinking off. I checked accuracy, confidence calibration, latency and cost. Costs use provider-reported charges where available and list-price estimates otherwise. There were separate checks for repeated requests and shuffled option order.&lt;/p&gt;

&lt;h2&gt;
  
  
  The cheap LLMs weren't close
&lt;/h2&gt;

&lt;p&gt;Bison-1 got 87.5% across the full suite. Mammoth-1 got 87.4%, SubQ 86.7%, and Perplexity Decider v1.3 85.6%. At the cheap-chat end, Luna got 71.3%, Gemini Flash Lite 69.8%, Haiku 67.8% and DeepSeek Flash 65.4%. These headline accuracies count unresolved failures as wrong and weight the tasks equally.&lt;/p&gt;

&lt;p&gt;The hero uses valid-answer accuracy so its points compare returned decisions. That's slightly kinder to the chat models. Even after removing failures, their mean accuracy stayed well below the leaders. The operational scores and valid-only scores are both in &lt;code&gt;tables/summary-test.csv&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The probability vector itself caused trouble. Haiku had 34 unresolved items out of 1,600, Gemini 42, DeepSeek 43 and Luna three. A model that returns numbers which fail validation leaves your agent needing another path before it can use a confidence threshold. Most decision endpoints completed this test without an unresolved failure, although Luna Decisions had nine. The API format doesn't guarantee success.&lt;/p&gt;

&lt;p&gt;The cost gap also favored the decision models. Liquid d1.2 scored 84.6% at about $0.008 per thousand decisions. Luna chat cost about $0.092, with lower accuracy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Terra matched them at 52 times the price
&lt;/h2&gt;

&lt;p&gt;The head-to-head uses &lt;code&gt;tables/paired.csv&lt;/code&gt;: 713 items with valid answers from every saved endpoint, including the earlier OpenRouter Claude runs. The Claude scores below come from the Anthropic-direct runs. That leaves different counts per task, so this comparison pools the shared items. It isn't the same statistic as the full-suite task mean.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Model&lt;/th&gt;
&lt;th&gt;Accuracy on shared items&lt;/th&gt;
&lt;th&gt;Cost per 1,000 decisions&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Baseten mammoth-1&lt;/td&gt;
&lt;td&gt;88.6%&lt;/td&gt;
&lt;td&gt;$0.048&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Baseten bison-1&lt;/td&gt;
&lt;td&gt;88.2%&lt;/td&gt;
&lt;td&gt;$0.048&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SubQ Snap 1.2&lt;/td&gt;
&lt;td&gt;87.9%&lt;/td&gt;
&lt;td&gt;$0.018&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Liquid d1.2-ultra direct&lt;/td&gt;
&lt;td&gt;86.8%&lt;/td&gt;
&lt;td&gt;$0.016&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GPT-6.1 Terra&lt;/td&gt;
&lt;td&gt;86.5%&lt;/td&gt;
&lt;td&gt;$0.957&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sonnet 5.5 (direct)&lt;/td&gt;
&lt;td&gt;82.5%&lt;/td&gt;
&lt;td&gt;$2.905&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Prices come from each model's complete saved run, not just those shared items. Using unrounded run costs, Terra cost about 52 times as much as SubQ. Sonnet cost more again. Neither baseline buys a clear accuracy advantage here.&lt;/p&gt;

&lt;p&gt;SubQ's mean task p50 was 199 ms against Terra's 1,614 ms and Sonnet's 1,866 ms. These are averages of the task medians, including network time from one machine. They aren't server-side inference timings or predictions for your application's tail latency.&lt;/p&gt;

&lt;p&gt;The leaders' uncertainty intervals overlap. I wouldn't choose between bison and mammoth on this accuracy difference, or claim a proven statistical tie from overlapping intervals. I'd choose between them on price and on a test with my own task.&lt;/p&gt;

&lt;h2&gt;
  
  
  Does paying for a fast tier help?
&lt;/h2&gt;

&lt;p&gt;OpenAI's priority tier cut Terra's paired-sample p50 by 32%, about a third, at twice the price. Applied to its full run, that gives about 1.13 seconds and $1.91 per thousand decisions. The base here is the pooled median of 1.665 seconds, a different statistic from the average of task medians above. Luna's p50 fell 19%, about a fifth, to an extrapolated 0.82 seconds, also at roughly twice the price.&lt;/p&gt;

&lt;p&gt;These were small paired samples: 43 items for Terra, 67 for Luna and 97 for Gemini Flash Lite, each measured against standard service on the same provider. The faded hero points apply those latency and cost ratios to the full runs. Their accuracy comes from the standard runs; I didn't measure a full fast-tier accuracy score.&lt;/p&gt;

&lt;p&gt;Flash Lite's priority tier gave no p50 speedup and cut p95 by only 5%, at 1.8 times the price. A 97-item standard sample pinned to Google AI Studio had a p50 of 635 ms, while the full default-route run had 930 ms. Those samples differ, so I'd test provider pinning on matched items before crediting it with the speedup.&lt;/p&gt;

&lt;p&gt;Even fast Terra is roughly 2.5-6 times slower than the leading decision models and 40-120 times pricier than the paid ones, with similar standard-run accuracy. No tested decision model offers a fast tier. Anthropic's fast mode doesn't cover Sonnet or Haiku.&lt;/p&gt;

&lt;h2&gt;
  
  
  What happens when you act only above 0.9
&lt;/h2&gt;

&lt;p&gt;An agent needs somewhere to send uncertain cases. Otherwise it must act even when its best option has a weak score.&lt;/p&gt;

&lt;p&gt;Calibration describes whether confidence matches observed correctness. For a batch of answers near a given probability, you want roughly that fraction to be right. A probability field on an API response doesn't establish this; you have to measure it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7l3en0qf9fh90axag32t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7l3en0qf9fh90axag32t.png" alt="Accuracy and coverage when accepting only probabilities of at least 0.9" width="800" height="914"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Pooled valid items from each model's own run, not a paired comparison (that one is in the next section). Labels include accepted counts. Confidence thresholds trade coverage for accuracy; only Terra used a smaller sample.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;At p ≥ 0.9, bison accepted 1,102 of 1,600 items and got 97.6% right. SubQ accepted 1,123 and got 97.9% right. The rest can go to a slower model, a human, or a skip branch, depending on the job.&lt;/p&gt;

&lt;p&gt;Terra, on its own 800 items, accepted 710 and got 89.3% right. At 0.95 it was still right only 91.4% of the time, on 661 items, while SubQ was right 98.7% of the time on 990. The samples differ, so read this as how each model's confidence behaves, not as a head-to-head.&lt;/p&gt;

&lt;p&gt;Sonnet went the other way. It accepted only 460 of 1,598 valid items at 0.9, about 29%, and got 93.5% right. A cautious distribution can send too much work to your fallback. I care about both columns: how often I can act and how often those actions are correct.&lt;/p&gt;

&lt;p&gt;I'd start with 0.9, then fit the policy to labelled examples from the actual job. This benchmark doesn't establish that threshold for real issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Same coverage, same items
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Added on December 4, 2026, after a reader's question.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A reader asked whether the threshold numbers above are a fair comparison, and they have a point. At 0.9 each model acts on a different share of items, and accuracy among accepted answers says nothing about the good answers a model threw away. So I added two more views. One puts every model at the same coverage, the other counts outcomes against all items.&lt;/p&gt;

&lt;p&gt;On the 713 items every model answered, I ranked each model's answers by the probability of the option it chose and kept its most confident share. Ties at the cutoff count at their average accuracy.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsaxhuj1vjagwy5sgug29.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsaxhuj1vjagwy5sgug29.png" alt="Accuracy of each model's most confident answers at equal coverage" width="800" height="846"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The same 713 items for every model. Dots mark answers with p ≥ 0.9. The numbers are in &lt;code&gt;coverage.md&lt;/code&gt; in the GitHub folder.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;At 70% coverage, SubQ was right on 98.2% of its answers, Liquid d1.2-ultra on 98.6% and bison on 97.4%. Terra was right on 92.8% and Sonnet on 91.0%. The gap between SubQ and Terra shrinks as coverage grows: 5.4 points at 70%, 4.2 at 80%, 2.5 at 90% and 1.4 at full coverage. SubQ stayed ahead at every level I checked. Averaged over all coverage levels, the error rate of the most confident answers (AURC, lower is better) was 0.022 for SubQ, 0.023 for Liquid ultra, 0.026 for bison, 0.072 for Terra and 0.085 for Sonnet.&lt;/p&gt;

&lt;p&gt;Counted against all 713 items, the 0.9 cutoff looks different. Terra acted on 635 items, 572 right and 63 wrong, which is 80.2% and 8.8% of everything. SubQ acted on 517, 507 right and 10 wrong, 71.1% and 1.4%. Terra got 65 more right answers out of the same cutoff and 53 more wrong ones, so its extra actions were right about half the time.&lt;/p&gt;

&lt;p&gt;SubQ did throw away more good answers. Of its 196 answers below 0.9, 120 were right. Terra dropped 78, and 45 of them were right. Sonnet dropped 495, and 386 of those were right, 78%, so its cutoff sends a lot of correct work to the fallback.&lt;/p&gt;

&lt;p&gt;Which view matters depends on what a wrong action costs you. If it's cheap, a lower cutoff gets more done. If it isn't, rank models by how clean their most confident answers are. On these items the decision models came out ahead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tool choices exposed the biggest gap
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Foqv8zdq09jucpnipavhg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Foqv8zdq09jucpnipavhg.png" alt="When2Call accuracy for selected decision models and chat baselines" width="800" height="686"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Valid-answer accuracy on the 400-item When2Call task. Error bars are Wilson 95% intervals. This task's test labels cover tool calls, clarification and refusal; direct answers appear only as distractors.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Bison scored 93.3% and SubQ 92.0% on When2Call. Cheap chat baselines were much weaker. Banking intent classification was less useful for separating the leaders because many models already scored well there.&lt;/p&gt;

&lt;p&gt;There is an awkward detail in this dataset: the pinned test split has no direct-answer correct category. It presents that option, but it is always a distractor. So I'd use this result to shortlist a tool gate, then collect examples where answering directly is actually correct before deploying it.&lt;/p&gt;

&lt;p&gt;Task averages also hide some odd specialists. TypeSafe Jev got 81.5% on MMLU-Pro, despite its middling overall result. Baseten joey-1 got 90.8% on When2Call but only 34.0% on MMLU-Pro.&lt;/p&gt;

&lt;h2&gt;
  
  
  Picking an element to click
&lt;/h2&gt;

&lt;p&gt;Liquid d1.2-ultra scored 91.0% on Mind2Web, mammoth-1 90.3%. At p ≥ 0.9, Liquid accepted 280 of 400 clicks and got 99.3% right. Mammoth accepted 253 and got 99.6% right. That's a useful starting point for a web agent that has to pick which element to click.&lt;/p&gt;

&lt;p&gt;The candidate list makes this easier than a real page. It contains the correct element plus nine random negatives. The benchmark hands the model a list that already includes the answer. It doesn't measure whether your browser agent can find that element, recover from a changed page or finish a multi-step task. I wouldn't read the accepted-click score as a browser completion rate.&lt;/p&gt;

&lt;h2&gt;
  
  
  Free, cheap, and repeatable
&lt;/h2&gt;

&lt;p&gt;Perplexity Decider v1.3's &lt;code&gt;:free&lt;/code&gt; endpoint scored 85.6%, with a mean task p50 of 437 ms. Celeris 1.1 got 80.8% at 147 ms and roughly $0.002 per thousand decisions. Liquid d1.2 scored 84.6% at about $0.008 per thousand decisions. Test them on your own examples; the free tier can change.&lt;/p&gt;

&lt;p&gt;For stability, 15 of the 18 decision endpoints chose identically across all 20 items repeated three times. Jev and Clef did so on 90% of items, Celeris on 95%. Luna chat did so on 75%. It's a small check, but temperature zero didn't make every endpoint repeatable.&lt;/p&gt;

&lt;p&gt;Option order was a separate test. SubQ kept the same choice on all 100 shuffled pairs, with identical probabilities after mapping options back. Bison kept its choice on 96%, mammoth on 97%, and Decider Flash on 98%. The cheap chat baselines kept theirs on roughly 77-79% of valid pairs. That stability is one more reason I'd test SubQ first for the filter.&lt;/p&gt;

&lt;p&gt;The direct Liquid endpoint was faster in this run: d1.2-ultra's mean task p50 was 209 ms direct against 277 ms through OpenRouter. Claude went the other way: Haiku took 1.42 seconds direct against 1.06 through OpenRouter; Sonnet took 1.87 against 1.50. These were separate runs, so I'd measure my own route from the deployment machine before choosing one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which one I'd pick
&lt;/h2&gt;

&lt;p&gt;For the issue-reply filter, SubQ direct. It combines useful confidence scores with low latency and order stability. Label real issues and test a starting policy of drafting a reply only when P(reply) ≥ 0.9 and skipping everything else. Then adjust the cutoff to how many wasted drafts and missed issues you can accept. The benchmark has no task that measures this outcome.&lt;/p&gt;

&lt;p&gt;For web-agent clicks, Liquid d1.2-ultra direct, with uncertain cases sent to a slower path or a human. Mammoth is another candidate if its behavior fits your pages better.&lt;/p&gt;

&lt;p&gt;For intent routing, I'd first try Liquid d1.2 or the free Perplexity Decider. Test the confusable intents in your own product; a high average can hide one expensive misroute.&lt;/p&gt;

&lt;p&gt;For a tool-or-answer gate, I'd shortlist bison and SubQ. Include direct-answer positives in the local evaluation, then pick the automation threshold from the errors you see. The fallback must still know which tools exist and what they're allowed to do.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this doesn't tell you
&lt;/h2&gt;

&lt;p&gt;Reasoning was off for chat baselines, with temperature zero where allowed. Claude 5.5 rejected temperature through the Anthropic API, so it used default sampling with thinking off. Turning reasoning on could improve scores, with a different cost and latency. Public datasets may overlap with training data. The intervals assume independent items and don't account for every source of clustering; this run cannot settle small rank differences.&lt;/p&gt;

&lt;p&gt;Safety and moderation weren't tested because WildGuard access was gated. No task here used labelled issues from a real repository. Liquid d1.3-ultra wasn't callable, Inception Mercury Decide signups were closed, and Fastino GLiDE needed a card, so they aren't ranked.&lt;/p&gt;

&lt;p&gt;Most items had one run. The repeated-request test was separate. Prices are the saved snapshot from November 30, 2026. The public folder includes aggregate scores and preparation code, with raw per-item outputs withheld, including all Mind2Web test material.&lt;/p&gt;

&lt;h2&gt;
  
  
  Run it yourself
&lt;/h2&gt;

&lt;p&gt;Everything ran from my Mac against hosted APIs: OpenRouter, plus direct APIs for SubQ, Liquid, Celeris and Claude. Latency includes the network from that one client. Recorded costs totalled about $6.41 on OpenRouter and $0.073 for SubQ, Liquid and Celeris, plus about $5.28 at Anthropic list price for the direct Claude runs. These sums cover development, test, shuffle, repeat and fast-tier requests.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://github.com/ImmuneFOMO/research/tree/main/llm/decision-models" rel="noopener noreferrer"&gt;GitHub folder&lt;/a&gt; has the scripts, tables and raw scorer reports. Its README separates rebuilding charts from making paid requests. Rerun it with your own task mix, and open an issue or PR if the result changes or you find a mistake.&lt;/p&gt;

&lt;p&gt;A bot built on one of these models has to run around the clock somewhere. The model itself is a hosted API, so a small VPS is enough for the bot. My own projects run on netcup (VPS from €2.21 a month) and &lt;a href="https://alphavps.com/clients/aff.php?aff=1157" rel="noopener noreferrer"&gt;AlphaVPS&lt;/a&gt; (VPS from €2.54 a month). For netcup, message me on &lt;a href="https://www.linkedin.com/in/anatolii-zhadan/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; or DM me on &lt;a href="https://x.com/ImmuneFOMO" rel="noopener noreferrer"&gt;X&lt;/a&gt;, and I'll send you a €5 voucher for new customers.&lt;/p&gt;

&lt;p&gt;Good luck with your agents.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finish
&lt;/h2&gt;

&lt;p&gt;I hope you understood the instructions correctly. If you have any edits to this article, I will be waiting for your comments. If I really helped you, I kindly ask you to subscribe to my LinkedIn and provide feedback on this article.&lt;/p&gt;

&lt;p&gt;Link to follow me:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/anatolii-zhadan/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All my articles and links: &lt;a href="https://fomo.moe" rel="noopener noreferrer"&gt;fomo.moe&lt;/a&gt;&lt;/p&gt;

</description>
      <category>llm</category>
      <category>aiagents</category>
      <category>machinelearning</category>
      <category>ai</category>
    </item>
    <item>
      <title>Postgres 18 skip scan turns 393 ms into 2.4 ms for one column order only</title>
      <dc:creator>ImmuneFOMO</dc:creator>
      <pubDate>Tue, 01 Dec 2026 20:10:47 +0000</pubDate>
      <link>https://dev.to/immunefomo/postgres-18-skip-scan-turns-393-ms-into-24-ms-for-one-column-order-only-3p0i</link>
      <guid>https://dev.to/immunefomo/postgres-18-skip-scan-turns-393-ms-into-24-ms-for-one-column-order-only-3p0i</guid>
      <description>&lt;p&gt;&lt;em&gt;Slow Postgres, part 4 of 8. The SQL and raw numbers for every test are &lt;a href="https://github.com/ImmuneFOMO/research/tree/main/postgres/slow-postgres/04-composite-index-order" rel="noopener noreferrer"&gt;on GitHub&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftzl15b9qugregtzdzc39.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftzl15b9qugregtzdzc39.png" alt="Five queries against both column orders on PostgreSQL 18" width="800" height="835"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Plain query time on PostgreSQL 18.7, each index alone on the table. Black cells read every page of the table.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;On PostgreSQL 17.7, the planner ignored an index on &lt;code&gt;(status, created_at)&lt;/code&gt; for one day of orders and read all ten million rows, 393 ms. On 18.7 the same index answers it in 2.4 ms with B-tree skip scan. The other order got no rescue. Counting pending orders through &lt;code&gt;(created_at, status)&lt;/code&gt; still reads the whole table on 18, 295 ms against 6.6 with the columns swapped.&lt;/p&gt;

&lt;p&gt;One note before the numbers: the AlphaVPS link at the end is a referral link, so I may get a commission at no extra cost to you, and the netcup voucher I offer there comes from my own account.&lt;/p&gt;

&lt;p&gt;The short version:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Equality columns go first and the range column last, on 17 and 18 alike: for the same 4,031 failed orders of one week, &lt;code&gt;(status, created_at)&lt;/code&gt; read 19 index pages and &lt;code&gt;(created_at, status)&lt;/code&gt; 787.&lt;/li&gt;
&lt;li&gt;Skip scan lets 18 use an index with no condition on its first column, searching it about once per value of that column: four statuses, seven searches, 2.4 ms.&lt;/li&gt;
&lt;li&gt;The planner counts on skipping only while the first column's estimated distinct values stay below the index's page count, 38,506 here. At 20,000 values it skipped, in 33 ms; at 50,000 it read the table.&lt;/li&gt;
&lt;li&gt;An &lt;code&gt;IN&lt;/code&gt; list on the first column still breaks &lt;code&gt;ORDER BY ... LIMIT&lt;/code&gt;: the newest 50 failed or pending orders took 357 ms on &lt;code&gt;(status, created_at)&lt;/code&gt; on 18 (381 on 17) and 0.8 on &lt;code&gt;(created_at, status)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Two single-column indexes handled every query, but on 18 the combined filter took 12 to 60 ms on them against 1.1 to 17 on the composite.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  The lab: ten million orders, two versions
&lt;/h2&gt;

&lt;p&gt;Everything ran on my M3 Max laptop with PostgreSQL 18.7 from Homebrew and, for the side-by-side runs, 17.7, an older 17 than the current 17.12. &lt;code&gt;shared_buffers&lt;/code&gt; is 5 GB, so the table and every index stay in memory, and JIT and parallel workers are off. Nothing is read from disk, so 18's asynchronous I/O and its checksums-on default don't show up here.&lt;/p&gt;

&lt;p&gt;The table holds one order every three seconds from January 1, 2026, inserted in time order, so the heap is sorted by &lt;code&gt;created_at&lt;/code&gt;. The data ends on December 14, so the queries' last week and last day end on December 13. Status comes from id arithmetic: exactly 92.9% paid, 5% refunded, 2% failed and 0.1% pending, spread evenly through the year. Statistics are fresh; &lt;a href="https://hackernoon.com/how-stale-postgresql-statistics-made-a-query-580-times-slower" rel="noopener noreferrer"&gt;part 3&lt;/a&gt; covers what happens when they aren't.&lt;/p&gt;

&lt;p&gt;Four indexes: &lt;code&gt;(status, created_at)&lt;/code&gt; and &lt;code&gt;(created_at, status)&lt;/code&gt; at 305 MB each, &lt;code&gt;(status)&lt;/code&gt; at 66 MB and &lt;code&gt;(created_at)&lt;/code&gt; at 214 MB. To test one setup at a time, the script drops the other indexes inside a transaction, runs the query and rolls back. Do that only on a lab copy: &lt;code&gt;DROP INDEX&lt;/code&gt; holds an &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; lock until &lt;code&gt;ROLLBACK&lt;/code&gt;, so every other query on &lt;code&gt;orders&lt;/code&gt; waits, plain &lt;code&gt;SELECT&lt;/code&gt;s included.&lt;/p&gt;

&lt;p&gt;Each time is the median of 35 plain runs: 7 after two warm-ups in each of five rounds, alternating 18 and 17. The laptop was busy with other work, so timings are noisy. The plans and page counts were identical in every round and don't depend on the machine, so I lean on those.&lt;/p&gt;
&lt;h2&gt;
  
  
  How a two-column B-tree is sorted
&lt;/h2&gt;

&lt;p&gt;An index on &lt;code&gt;(status, created_at)&lt;/code&gt; is sorted by status, and by time only inside each status. An index on &lt;code&gt;(created_at, status)&lt;/code&gt; is sorted by time, and status only breaks ties between equal timestamps, which never happen here. This is where "failed orders this week" starts reading in each:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(status, created_at)              (created_at, status)
failed  2026-12-06 23:58:57       2026-12-06 23:59:57  paid
failed  2026-12-07 00:02:18  &amp;lt;-   2026-12-07 00:00:00  pending   &amp;lt;-
failed  2026-12-07 00:04:36       2026-12-07 00:00:03  paid
failed  2026-12-07 00:06:54       2026-12-07 00:00:06  paid
failed  2026-12-07 00:10:15       2026-12-07 00:00:09  paid
failed  2026-12-07 00:12:33       2026-12-07 00:00:12  refunded
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the left, the week's failed orders sit next to each other. On the right, the first failed one is entry 47.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://www.postgresql.org/docs/18/indexes-multicolumn.html" rel="noopener noreferrer"&gt;PostgreSQL 18 docs&lt;/a&gt; give the rule: equality conditions on the leading columns, plus an inequality on the first column without one, limit the part of the index that's scanned. Later columns are checked inside the index, which saves visits to the table. The &lt;a href="https://www.postgresql.org/docs/17/indexes-multicolumn.html" rel="noopener noreferrer"&gt;17 docs&lt;/a&gt; said they "do not reduce the portion of the index that has to be scanned"; 18 says "do not necessarily reduce", because of skip scan.&lt;/p&gt;

&lt;h2&gt;
  
  
  Equality first, range last
&lt;/h2&gt;

&lt;p&gt;The dashboard query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*), sum(amount_cents)
FROM orders
WHERE status = 'failed'
  AND created_at &amp;gt;= '2026-12-07' AND created_at &amp;lt; '2026-12-14';
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;(status, created_at)&lt;/code&gt;, Postgres goes straight to the first failed order of December 7 and reads forward to the end of the week, every entry a match. With &lt;code&gt;(created_at, status)&lt;/code&gt;, it reads all 201,600 orders of the week, checks each status and keeps one in fifty.&lt;/p&gt;

&lt;p&gt;Both plans for &lt;code&gt;count(*)&lt;/code&gt; alone, answered from the index without the table (18.7, costs and literals cut):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Index Only Scan using orders_status_created_idx on orders (actual time=0.024..0.191 rows=4031.00 loops=1)
  Index Cond: ((status = 'failed'::text) AND (created_at &amp;gt;= ...) AND (created_at &amp;lt; ...))
  Index Searches: 1
  Buffers: shared hit=19

Index Only Scan using orders_created_status_idx on orders (actual time=0.044..3.766 rows=4031.00 loops=1)
  Index Cond: ((created_at &amp;gt;= ...) AND (created_at &amp;lt; ...) AND (status = 'failed'::text))
  Index Searches: 1
  Buffers: shared hit=787
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both list &lt;code&gt;status = 'failed'&lt;/code&gt; under &lt;code&gt;Index Cond&lt;/code&gt;, and nothing there says the first plan used it to find where to start while the second checked it 201,600 times. &lt;code&gt;Buffers&lt;/code&gt; does, the line &lt;a href="https://hackernoon.com/reading-postgres-explain-analyze-when-the-planner-expects-300-rows-and-gets-15000" rel="noopener noreferrer"&gt;part 2&lt;/a&gt; reads in detail: 19 pages against 787. 17 reads the same pages but prints them only with &lt;code&gt;BUFFERS&lt;/code&gt;; 18 prints them by default and adds &lt;code&gt;Index Searches&lt;/code&gt;, the number of times the scan went down the tree from the root.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftnyekveo36wxl3tat600.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftnyekveo36wxl3tat600.png" alt="Failed orders in the last 7 days: 19 index pages against 787" width="800" height="616"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Index pages come from the index-only `count(&lt;/em&gt;)`, heap pages from the full query. PostgreSQL 18.7; 17.7 reads the same pages.*&lt;/p&gt;

&lt;p&gt;The full query also reads &lt;code&gt;amount_cents&lt;/code&gt; from 1,493 heap pages in both orders, so the gap shrinks to 1.1 ms against 4.5 on 18 (1.1 against 5.7 on 17), then grows with the window: 5.4 against 19 ms for 30 days and 17 against 55 for 90, on 18.&lt;/p&gt;

&lt;p&gt;The common advice is to put the most selective column first. Here that's &lt;code&gt;created_at&lt;/code&gt;, unique on every row, and it's the wrong first column: both filters keep 2% of the rows. What decides is how each column is filtered. An equality keeps the matching entries together, and a range spreads them over everything between its two ends.&lt;/p&gt;
&lt;h2&gt;
  
  
  The query an index can't start
&lt;/h2&gt;

&lt;p&gt;The same rule works against each order. Count pending orders with only &lt;code&gt;(created_at, status)&lt;/code&gt;: the condition is on the second column, so there's nowhere to start, and Postgres reads the table, 295 ms on 18 and 396 on 17, against 6.6 and 6.5 on &lt;code&gt;(status, created_at)&lt;/code&gt;. Both versions read the same 74,075 pages. Why 18 needed a quarter less time for them I can't explain; on the one-day filter below, its Seq Scans were about as fast as 17's.&lt;/p&gt;

&lt;p&gt;Skip scan can't help, since &lt;code&gt;created_at&lt;/code&gt; has ten million distinct values. Forced onto the index with &lt;code&gt;enable_seqscan = off&lt;/code&gt;, both versions read the whole index plus the 10,000 matching rows, 48,866 pages, in 169 and 170 ms. 18 shows &lt;code&gt;Index Searches: 1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Ask &lt;code&gt;(status, created_at)&lt;/code&gt; for one day of orders on 17 and the problem flips: a Seq Scan of 393 ms against 2.4 on &lt;code&gt;(created_at, status)&lt;/code&gt;. Forced onto the index, 17 read 39,535 pages in 156 ms, but the planner prices index pages as random reads, the arithmetic from &lt;a href="https://hackernoon.com/postgres-not-using-your-index-its-counting-pages-not-rows" rel="noopener noreferrer"&gt;part 1&lt;/a&gt;, so it costed that walk at 314,033 against 224,225 for the Seq Scan. On 18 this query needs neither.&lt;/p&gt;
&lt;h2&gt;
  
  
  PostgreSQL 18: skip scan
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://www.postgresql.org/docs/18/release-18.html" rel="noopener noreferrer"&gt;release notes&lt;/a&gt; say skip scan lets a multicolumn B-tree be used "when there are no restrictions on the first or early indexed columns". It searches once per value of the first column, as if the query said &lt;code&gt;status = 'failed'&lt;/code&gt;, then &lt;code&gt;status = 'paid'&lt;/code&gt;, and so on. On the one-day query (costs and casts cut):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Index Scan using orders_status_created_idx on orders (actual time=0.019..1.880 rows=28800.00 loops=1)
  Index Cond: ((created_at &amp;gt;= '2026-12-13 00:00:00+00') AND (created_at &amp;lt; '2026-12-14 00:00:00+00'))
  Index Searches: 7
  Buffers: shared hit=808
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Seven searches for four statuses: the &lt;a href="https://www.postgresql.org/docs/18/using-explain.html" rel="noopener noreferrer"&gt;EXPLAIN docs&lt;/a&gt; warn that the count often won't match the query one to one. 808 pages with the table included, where &lt;code&gt;(created_at, status)&lt;/code&gt; reads 329 and the Seq Scan 74,075. 2.4 ms, close to the 2.0 of &lt;code&gt;(created_at, status)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To find where it stops paying off, I built &lt;code&gt;(x, created_at)&lt;/code&gt; indexes with 100 to 200,000 distinct values of x (&lt;code&gt;customer_id % 100&lt;/code&gt; and so on, then &lt;code&gt;customer_id&lt;/code&gt;):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdl8xiv81q8iytouxb12k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdl8xiv81q8iytouxb12k.png" alt="Skip scan on PostgreSQL 18 against the number of values in the first column" width="800" height="1022"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;One day of orders from an index on (x, created_at), the plan the planner chose. PostgreSQL 18.7, with 17.7 for comparison.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Every value costs one more search from the root down to a leaf. At 10,000 values that's 10,001 searches and 30,261 index pages, 19 ms. At 20,000 it's 19,892 searches and 60,079 index pages, more than the 38,506 the whole index has, because each search rereads the root and an inner page. Still 33 ms.&lt;/p&gt;

&lt;p&gt;The cutoff comes from the cost model. In 18's &lt;code&gt;btcostestimate&lt;/code&gt; (&lt;a href="https://github.com/postgres/postgres/blob/REL_18_STABLE/src/backend/utils/adt/selfuncs.c" rel="noopener noreferrer"&gt;selfuncs.c&lt;/a&gt;), a skipped column costs one search per distinct value plus one, with the count taken from &lt;code&gt;n_distinct&lt;/code&gt; in &lt;code&gt;pg_stats&lt;/code&gt;. When that passes the index's page count, the planner drops the skipping from its estimate and prices a scan of the whole index, which loses to the Seq Scan here. &lt;code&gt;ANALYZE&lt;/code&gt; estimated 20,115 values for &lt;code&gt;customer_id % 20000&lt;/code&gt; and 48,944 for &lt;code&gt;customer_id % 50000&lt;/code&gt;, against 38,506 pages. With no statistics on the column, the planner assumes no skipping at all.&lt;/p&gt;

&lt;p&gt;Forced onto the index at 50,000 values, 18 showed 59 index searches and read the whole index, 38,433 pages in 118 ms, against 428 for the planner's Seq Scan. I don't know why it's 59 and not 50,001. 17, forced, read these indexes whole too, in about the same time. Past the cutoff, the planner gives up an index read over three times faster in memory, for the page-pricing reason above.&lt;/p&gt;

&lt;p&gt;What 18 changes is whether you need a second index. With up to 1,000 values in the first column, a query that skips it took 4.3 ms at most, reading ten times the pages of a matching index (3,438 against 329), and may not need an index of its own.&lt;/p&gt;

&lt;h2&gt;
  
  
  ORDER BY and the order the index returns
&lt;/h2&gt;

&lt;p&gt;Sorting adds a third kind of column. For the newest 50 failed orders, both indexes return rows already in order with no Sort node, 0.2 ms and 0.3. Add one status, &lt;code&gt;WHERE status IN ('failed', 'pending') ORDER BY created_at DESC LIMIT 50&lt;/code&gt;, and that changes.&lt;/p&gt;

&lt;p&gt;Now &lt;code&gt;(status, created_at)&lt;/code&gt; holds two groups, each sorted on its own, and Postgres doesn't merge them. It collected all 210,000 failed and pending orders with a bitmap scan over every page of the table, then sorted them to keep 50: 357 ms on 18, 381 on 17. &lt;code&gt;(created_at, status)&lt;/code&gt; walked time backwards past about 2,400 other orders, 0.8 ms on both.&lt;/p&gt;

&lt;p&gt;So the sort column goes right after the equality columns, and an &lt;code&gt;IN&lt;/code&gt; list on the leading column breaks the order the way a range does. Part 5 covers ORDER BY and LIMIT.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two single-column indexes and BitmapAnd
&lt;/h2&gt;

&lt;p&gt;The other common setup is one index per column. Postgres can turn each index scan into a bitmap of row locations, AND the two, and visit only the rows in both. The pair takes 280 MB against 305 for one composite.&lt;/p&gt;

&lt;p&gt;Neither version chose a BitmapAnd for the combined query. Postgres walked one index and filtered on the other column: &lt;code&gt;created_at&lt;/code&gt; for 7 and 30 days (&lt;code&gt;Rows Removed by Filter: 197569&lt;/code&gt; for the week), &lt;code&gt;status&lt;/code&gt; for 90, which visited every page of the table. Forced with &lt;code&gt;enable_indexscan = off&lt;/code&gt;, the BitmapAnd was costed at 18,227 for the week against 8,107 for the walk on 18, which I put down to the &lt;a href="https://www.postgresql.org/docs/18/view-pg-stats.html" rel="noopener noreferrer"&gt;correlation&lt;/a&gt; of 1.0 between &lt;code&gt;created_at&lt;/code&gt; and the table order.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frps3o7kyynjf2cq4d8pl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frps3o7kyynjf2cq4d8pl.png" alt="Failed orders over 7, 30 and 90 days: the composite index against two single indexes and a forced BitmapAnd" width="800" height="848"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Plain query time on PostgreSQL 18.7; 17.7 picked the same plans. Grey: the plan Postgres chose with two single-column indexes.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;On 18 the forced BitmapAnd beat the planner's choice at 30 days, 26 ms against 54. At 90 days the bitmap outgrew the default 4 MB of &lt;code&gt;work_mem&lt;/code&gt; and went partly lossy, 92 ms; part 7 covers lossy bitmaps. The composite stayed ahead at every window.&lt;/p&gt;

&lt;p&gt;On 18 every other query ran fine on the two single indexes, all under 7 ms. For an internal dashboard polled a few times a minute, 12 to 60 ms is fine. For a filter on every API request, build the composite with the equality column first. On 17, keep a &lt;code&gt;created_at&lt;/code&gt; index next to it for time-only queries; on 18, first check whether skip scan covers them. Each index slows every insert, which part 8 measures.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to check this on your own database
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;For each query on the table, mark how it uses every column: equality, range or sort.&lt;/li&gt;
&lt;li&gt;Put the equality columns first, then the one range or sort column. An &lt;code&gt;IN&lt;/code&gt; list counts as a range if you need the rows sorted.&lt;/li&gt;
&lt;li&gt;Compare &lt;code&gt;Buffers&lt;/code&gt;, not &lt;code&gt;Index Cond&lt;/code&gt;, which looks the same for both column orders. On 17, ask for &lt;code&gt;BUFFERS&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;On 18, before adding an index for queries that leave out the first column, compare that column's &lt;code&gt;n_distinct&lt;/code&gt; in &lt;code&gt;pg_stats&lt;/code&gt; (negative means a fraction of the rows) with the index's &lt;code&gt;relpages&lt;/code&gt; in &lt;code&gt;pg_class&lt;/code&gt;. Far below means skip scan can cover them, and &lt;code&gt;Index Searches&lt;/code&gt; above 1 shows it skipped.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Run it on your own server
&lt;/h2&gt;

&lt;p&gt;All the numbers come from my M3 Max laptop (14 cores, 36 GB of RAM) with Homebrew's PostgreSQL 18.7 and 17.7. Your milliseconds will differ; the plans and page counts should match. The SQL, a script that runs the whole lab on either version, every plan and the chart code are &lt;a href="https://github.com/ImmuneFOMO/research/tree/main/postgres/slow-postgres/04-composite-index-order" rel="noopener noreferrer"&gt;on GitHub&lt;/a&gt;. Run it on your own server, and if you get a different picture or think I got something wrong, open an issue or a PR.&lt;/p&gt;

&lt;p&gt;My own projects run on netcup (VPS from €2.21 a month) and &lt;a href="https://alphavps.com/clients/aff.php?aff=1157" rel="noopener noreferrer"&gt;AlphaVPS&lt;/a&gt; (VPS from €2.54 a month). For netcup, message me on &lt;a href="https://www.linkedin.com/in/anatolii-zhadan/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; and I'll send you a €5 voucher for new customers.&lt;/p&gt;

&lt;p&gt;Good luck with your plans. Part 5 is next: ORDER BY and LIMIT, keyset pagination and what OFFSET costs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finish
&lt;/h2&gt;

&lt;p&gt;I hope you understood the instructions correctly. If you have any edits to this article, I will be waiting for your comments. If I really helped you, I kindly ask you to subscribe to my LinkedIn and provide feedback on this article.&lt;/p&gt;

&lt;p&gt;Link to follow me:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/anatolii-zhadan/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All my articles and links: &lt;a href="https://fomo.moe" rel="noopener noreferrer"&gt;fomo.moe&lt;/a&gt;&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>compositeindex</category>
      <category>queryoptimization</category>
      <category>database</category>
    </item>
    <item>
      <title>How Stale PostgreSQL Statistics Made a Query 580 Times Slower</title>
      <dc:creator>ImmuneFOMO</dc:creator>
      <pubDate>Tue, 24 Nov 2026 06:40:40 +0000</pubDate>
      <link>https://dev.to/immunefomo/how-stale-postgresql-statistics-made-a-query-580-times-slower-20lp</link>
      <guid>https://dev.to/immunefomo/how-stale-postgresql-statistics-made-a-query-580-times-slower-20lp</guid>
      <description>&lt;p&gt;&lt;em&gt;Slow Postgres, part 3 of 8. The SQL and raw numbers for every test are &lt;a href="https://github.com/ImmuneFOMO/research/tree/main/postgres/slow-postgres/03-stale-statistics" rel="noopener noreferrer"&gt;on GitHub&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1khca9fyvrl75fzd08hp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1khca9fyvrl75fzd08hp.png" alt="Endpoint time per call" width="800" height="797"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;PostgreSQL 18, sampled every 5 seconds. Gaps: pgbench runs.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A retention job marked 463,418 of five million orders &lt;code&gt;archived&lt;/code&gt;, a status ANALYZE had never seen, so Postgres estimated 1 row for it. An endpoint listing one customer's archived orders kept walking the status index, reading 463,418 entries to return one: 29 ms a call instead of 0.05 ms.&lt;/p&gt;

&lt;p&gt;One note before the numbers: the AlphaVPS link at the end is a referral link, so I may get a commission at no extra cost to you, and the netcup voucher I offer there comes from my own account.&lt;/p&gt;

&lt;p&gt;The short version:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A 1-row estimate for 463,418 archived orders kept a per-customer query on the wrong index: 29 ms a call against 0.05 ms after ANALYZE.&lt;/li&gt;
&lt;li&gt;Autovacuum runs ANALYZE once 50 rows plus 10% of the table have changed, whatever the change. The 9.3% backfill sat under that line until 40,000 more rows changed. A manual ANALYZE took 190 ms.&lt;/li&gt;
&lt;li&gt;PostgreSQL 18.7 and 17.12 picked the same plans with the same analyze threshold and similar timings. The only difference in behavior I measured is that 18 vacuums a frozen table much sooner after inserts, but still doesn't analyze it.&lt;/li&gt;
&lt;li&gt;Run ANALYZE yourself after a job that loads or rewrites part of a table, and on temp tables after you fill them. Where a few percent can flip a plan, lower &lt;code&gt;autovacuum_analyze_scale_factor&lt;/code&gt; for that table.&lt;/li&gt;
&lt;li&gt;Correlated columns stay wrong with fresh statistics. CREATE STATISTICS fixes them after the next ANALYZE.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  The lab: five million orders
&lt;/h2&gt;

&lt;p&gt;PostgreSQL 18.7 in Docker on the 4-vCPU, 8 GB cloud VM from parts 1 and 2, with part 2's settings: 1 GB of &lt;code&gt;shared_buffers&lt;/code&gt;, so the table stays in memory, and JIT and parallel workers off. Autovacuum runs with its defaults. The same scripts then ran on 17.12 in a fresh container. With everything in memory, 18's asynchronous I/O has nothing to speed up.&lt;/p&gt;

&lt;p&gt;Five million orders, one every 11 seconds from January 2025 to September 2026. A million customers have five orders each, and each ships to one address: 20,000 postcodes in 21 cities, each city in one country. There are indexes on &lt;code&gt;customer_id&lt;/code&gt; and &lt;code&gt;status&lt;/code&gt;. The rows come from &lt;code&gt;generate_series&lt;/code&gt; and &lt;code&gt;setseed&lt;/code&gt;, so yours match mine on either version. Your &lt;code&gt;pg_stats&lt;/code&gt; won't, because ANALYZE samples at random; my two runs differ a little too.&lt;/p&gt;

&lt;p&gt;The endpoint lists one customer's orders in one status:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount_cents&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;648065&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'archived'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Where row estimates come from
&lt;/h2&gt;

&lt;p&gt;ANALYZE reads a random sample, 30,000 rows at the default &lt;code&gt;default_statistics_target&lt;/code&gt; of 100, and stores a per-column summary, which you read in &lt;code&gt;pg_stats&lt;/code&gt;. On this table it took 190 ms:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INFO:  "orders": scanned 30000 of 53496 pages, containing 2803890 live rows and 0 dead rows;
       30000 rows in sample, 4999897 estimated total rows
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;18's &lt;code&gt;ANALYZE VERBOSE&lt;/code&gt; adds buffer, WAL and CPU usage under that line; 17 stops there.&lt;/p&gt;

&lt;p&gt;Estimates come from three parts of it. The first is the most common values (MCV) and their shares, here for &lt;code&gt;status&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;most_common_vals:  {delivered,shipped,paid,cancelled,refunded}
most_common_freqs: {0.8021333,0.07926667,0.059,0.039466668,0.020133333}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;status = 'paid'&lt;/code&gt; gets the table's 4,999,995 rows × 0.059 = 295,000. There are 300,322.&lt;/p&gt;

&lt;p&gt;The second is &lt;code&gt;n_distinct&lt;/code&gt;. A value that isn't in the common list gets an even share of the rows the list doesn't cover. &lt;code&gt;customer_id&lt;/code&gt; has no common list, and its &lt;code&gt;n_distinct&lt;/code&gt; is -0.202, meaning 20.2% of the rows: about 1,012,000 customers, so five rows each, which is exact.&lt;/p&gt;

&lt;p&gt;The third is a histogram for ranges: 101 bounds that split the sampled values into 100 buckets with the same number of rows each. One week of June 2025 came out at 51,262 rows against 54,982.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;default_statistics_target&lt;/code&gt; caps the common values and the buckets at 100 each and sets the sample at 300 times the target. At 1,000, ANALYZE read the whole table and took 1.6 seconds; at 10,000, 17 seconds, on both versions. Raise it per column (&lt;code&gt;ALTER TABLE ... ALTER COLUMN ... SET STATISTICS&lt;/code&gt;) when the value you filter on doesn't make the top 100.&lt;/p&gt;

&lt;p&gt;Back to &lt;code&gt;status&lt;/code&gt;: its five frequencies add up to 1. As far as the planner knows, no rows are left for any other value, so it gets the minimum estimate, one row. That's right until somebody writes that status:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;EXPLAIN SELECT * FROM orders WHERE status = 'archived';
Index Scan using orders_status_idx on orders  (cost=0.43..8.45 rows=1 width=50)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  A backfill the statistics never saw
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'archived'&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="s1"&gt;'2025-03-01'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- UPDATE 463418, 3.9 s&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The endpoint's plan before the backfill, after it, and after ANALYZE (full plans in &lt;code&gt;timeline-pg18.out&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;before    Index Scan using orders_status_idx       rows=1  actual rows=0.00  Buffers: shared hit=3     0.019 ms
after     Index Scan using orders_status_idx       rows=1  actual rows=1.00  Buffers: shared hit=5609  32.491 ms
            Rows Removed by Filter: 463417
ANALYZE   Index Scan using orders_customer_id_idx  rows=1  actual rows=1.00  Buffers: shared hit=10    0.027 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's 18's format: &lt;code&gt;actual rows&lt;/code&gt; with two decimals, plus &lt;code&gt;Buffers&lt;/code&gt; and an &lt;code&gt;Index Searches&lt;/code&gt; line without asking. 17 prints &lt;code&gt;rows=1&lt;/code&gt; and needs &lt;code&gt;BUFFERS&lt;/code&gt; spelled out, but it chose the same plans and read the same 5,609 pages.&lt;/p&gt;

&lt;p&gt;The backfill didn't change the plan. With &lt;code&gt;archived&lt;/code&gt; at 1 row, reading one status index entry and checking its customer costs 8.45 in planner units, against 24.52 for the five rows behind the &lt;code&gt;customer_id&lt;/code&gt; index (&lt;a href="https://hackernoon.com/postgres-not-using-your-index-its-counting-pages-not-rows" rel="noopener noreferrer"&gt;part 1&lt;/a&gt; covers how those units are priced). That was right while the status didn't exist. After the backfill every call fetched 463,418 &lt;code&gt;archived&lt;/code&gt; entries and their rows to keep one.&lt;/p&gt;

&lt;p&gt;Timed every five seconds for five minutes after the backfill, the plain query took a median of 29.0 ms over 52 samples, against 0.04 ms before it and 0.05 ms after ANALYZE (29.8 ms stale on 17). Four pgbench clients got 112 calls a second out of the stale endpoint and 180,353 after ANALYZE (118 and 188,180 on 17). A call that fast is hard to time, so the gap depends on the tool: 580 times by the psql medians, 1,600 times by pgbench throughput.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why autovacuum left the table alone
&lt;/h2&gt;

&lt;p&gt;Autovacuum analyzes a table once the rows inserted, updated or deleted since the last ANALYZE (&lt;code&gt;n_mod_since_analyze&lt;/code&gt; in &lt;code&gt;pg_stat_user_tables&lt;/code&gt;) pass&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;autovacuum_analyze_threshold + autovacuum_analyze_scale_factor × reltuples
          50                 +              0.1               × 4,999,897  = 500,040
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and it checks every &lt;code&gt;autovacuum_naptime&lt;/code&gt;, one minute by default. PostgreSQL 18 kept this formula.&lt;/p&gt;

&lt;p&gt;The backfill was 36,622 rows short. In the chart at the top, the counter sat at 463,418 for five minutes. Then I marked 40,000 shipped orders as delivered, the counter went to 503,418, and autovacuum analyzed the table about a second later. That second was luck, and so were the 60 seconds the 17 run waited at the same step: it depends on where the update lands in autovacuum's one-minute cycle. The ANALYZE took 0.55 s, and the estimate went to 455,197.&lt;/p&gt;

&lt;p&gt;My five minutes ended because I wrote those rows. On a real table they end when 36,622 more rows change: at 1,000 a minute that's 37 minutes, at 100 a minute about six hours.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://www.postgresql.org/docs/18/routine-vacuuming.html" rel="noopener noreferrer"&gt;docs&lt;/a&gt; say it plainly: autovacuum schedules ANALYZE "strictly as a function of the number of rows inserted or updated; it has no knowledge of whether that will lead to meaningful statistical changes." That catches slow drift. It misses:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a change under the line that lands on one value, like the backfill: 9.3% of the table and all of &lt;code&gt;archived&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;the wait for the next round, up to a minute, which a job that loads a batch and queries it right away always hits;&lt;/li&gt;
&lt;li&gt;big tables, where the line moves too: at 100 million rows it's 10 million changed rows;&lt;/li&gt;
&lt;li&gt;temporary tables, which autovacuum can't see, and partitioned tables, whose parent it never analyzes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PostgreSQL 18 changed two autovacuum triggers, both for VACUUM. &lt;code&gt;autovacuum_vacuum_max_threshold&lt;/code&gt; caps the dead-row trigger at 100 million rows, which bites only past 500 million. The insert trigger, 1,000 rows plus 20% of the table, now scales that 20% by the share of pages not yet frozen, and that is the one difference in behavior my lab measured between 17 and 18. With nothing frozen, both versions put the line at about 1,002,000 rows. After &lt;code&gt;VACUUM (FREEZE)&lt;/code&gt; on a fresh copy, 18's line was 1,000 rows, and autovacuum vacuumed the table 19 seconds after a 400,000-row insert. 17's was 1,001,000, and nothing ran in three minutes. Neither analyzed the table, so October was still estimated at 540 rows against 400,000.&lt;/p&gt;

&lt;p&gt;Two cheap fixes. Run ANALYZE at the end of the job that changed the table: 190 ms here, less than seven stale calls. And where a few percent can flip a plan, lower the line for that table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;autovacuum_analyze_scale_factor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.01&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That put the line at 54,090 rows. I moved 60,000 paid orders to a new status, &lt;code&gt;on_hold&lt;/code&gt;, and autovacuum analyzed the table 43 seconds later. The estimate for &lt;code&gt;on_hold&lt;/code&gt; went from 1 to 54,515. On a big table you can set a fixed count instead: &lt;code&gt;autovacuum_analyze_scale_factor = 0&lt;/code&gt; and &lt;code&gt;autovacuum_analyze_threshold = 100000&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the planner sees without ANALYZE
&lt;/h2&gt;

&lt;p&gt;Stale statistics don't leave the planner blind. I loaded 400,000 orders for October and November 2026 in one INSERT, 8% of the table, and looked at estimates before any ANALYZE:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgb1e9uxqxh95ldkl9y9g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgb1e9uxqxh95ldkl9y9g.png" alt="Estimated against actual rows before ANALYZE: new dates up to 746x off, a new status 1 against 463,418" width="800" height="728"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;EXPLAIN estimates on PostgreSQL 18 before ANALYZE, against counted rows. The &lt;code&gt;archived&lt;/code&gt; row is from the backfill. 17's estimates were within 1% of these.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The row count was close: 5,358,467 against 5,400,000. &lt;code&gt;pg_class&lt;/code&gt; still said 5,005,827 rows on 58,953 pages, but the planner reads the table's current size, 63,106 pages, and scales the old rows-per-page up to it.&lt;/p&gt;

&lt;p&gt;The new dates were past the end of the histogram. Without an index on &lt;code&gt;created_at&lt;/code&gt;, &lt;code&gt;created_at &amp;gt;= '2026-10-01'&lt;/code&gt; got 536 rows against 400,000, and &lt;code&gt;&amp;gt;= '2026-11-01'&lt;/code&gt; got 536 too: past the last bound, the planner won't go below a hundredth of one bucket. With an index, October got 44,291: the planner fetches the column's real maximum from the index and stretches the last bucket up to it. Nine times too low beats 746.&lt;/p&gt;

&lt;p&gt;A temp table nobody analyzed has no statistics, so the planner guesses. I copied the 400,000 new orders into one. From its file size and a default row width the planner put it at 187,200 rows, then took 0.5% of that for an equality: &lt;code&gt;status = 'shipped'&lt;/code&gt; came out at 936 rows against 120,138. After &lt;code&gt;ANALYZE&lt;/code&gt;: 120,533.&lt;/p&gt;
&lt;h2&gt;
  
  
  Correlated columns: fresh statistics, wrong estimate
&lt;/h2&gt;

&lt;p&gt;ANALYZE fixes statistics that are old. It can't fix &lt;a href="https://hackernoon.com/reading-postgres-explain-analyze-when-the-planner-expects-300-rows-and-gets-15000" rel="noopener noreferrer"&gt;part 2&lt;/a&gt;'s problem, two correlated columns multiplied into 300 rows instead of 15,000 on PostgreSQL 17, because the planner keeps statistics per column. For &lt;code&gt;country = 'PT' AND city = 'Lisbon'&lt;/code&gt; it multiplies the two shares as if they were independent, 1.99% × 1.54%, and gets 1,650 rows. Of the 5.4 million rows, 81,000 match: every Lisbon order ships to PT.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;CREATE STATISTICS&lt;/code&gt; keeps statistics on a group of columns. Each of its three kinds fixes a different estimate:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F11yzkbgm1h9f3sfig4xp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F11yzkbgm1h9f3sfig4xp.png" alt="Estimates with no extended statistics, dependencies, mcv and ndistinct against the real counts" width="800" height="766"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Estimates from EXPLAIN on PostgreSQL 18 after ANALYZE, on a log scale. 17 came within 10% of each.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;dependencies&lt;/code&gt; records how far one column decides another: city decides country completely (1.0), country decides city 18% of the way. The Lisbon estimate went to 75,574. It assumes the values in your query agree, though: &lt;code&gt;country = 'ES' AND city = 'Lisbon'&lt;/code&gt; matches nothing and also got 75,574, worse than 9,955 without extended statistics. It only works for &lt;code&gt;=&lt;/code&gt; and &lt;code&gt;IN&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mcv&lt;/code&gt; stores the most common combinations, here all 21 pairs, and works for ranges too. It gave 79,919 for PT and Lisbon and 1 for ES and Lisbon.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ndistinct&lt;/code&gt; counts distinct combinations, which GROUP BY needs. &lt;code&gt;GROUP BY city, postcode&lt;/code&gt; was estimated at 21 × 20,164 = 423,444 groups. There are 20,000, since every postcode sits in one city. With &lt;code&gt;ndistinct&lt;/code&gt;: 20,156.&lt;/p&gt;

&lt;p&gt;Two catches. Nothing changes until the next ANALYZE: right after &lt;code&gt;CREATE STATISTICS&lt;/code&gt; the Lisbon estimate was still 1,650. And they go stale with the table. Leave out the kind and you get all three:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="no"&gt;STATISTICS&lt;/span&gt; &lt;span class="n"&gt;orders_country_city&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ANALYZE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that and a second object on &lt;code&gt;(city, postcode)&lt;/code&gt;, the three estimates were 83,942, 1 and 20,123, and ANALYZE went from 175 ms to 352 ms. The planner uses them for conditions on one table, like part 2's users scan, not for joins.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pg_upgrade&lt;/code&gt; to 18 now carries most planner statistics over, but not the ones from &lt;code&gt;CREATE STATISTICS&lt;/code&gt;. The &lt;a href="https://www.postgresql.org/docs/18/pgupgrade.html" rel="noopener noreferrer"&gt;upgrade docs&lt;/a&gt; have you run &lt;code&gt;vacuumdb --all --analyze-in-stages --missing-stats-only&lt;/code&gt; for the gaps, then &lt;code&gt;vacuumdb --all --analyze-only&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to check this on your own database
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;In &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt;, find the lowest node where estimated and actual rows differ by 10x or more.&lt;/li&gt;
&lt;li&gt;See how much its table changed since the last ANALYZE, with the query below.&lt;/li&gt;
&lt;li&gt;Look the value up in &lt;code&gt;pg_stats&lt;/code&gt;: is it in &lt;code&gt;most_common_vals&lt;/code&gt;, and is a range past the last histogram bound?&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;ANALYZE&lt;/code&gt; on the table and EXPLAIN again. If that fixed it, apply one of the two fixes above.&lt;/li&gt;
&lt;li&gt;If it's still wrong and the filter uses columns that depend on each other, create statistics on them and ANALYZE.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;relname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n_live_tup&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n_mod_since_analyze&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last_analyze&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last_autoanalyze&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_stat_user_tables&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;n_mod_since_analyze&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Run it on your own server
&lt;/h2&gt;

&lt;p&gt;Everything here ran on one 4-vCPU AMD EPYC cloud VM with 8 GB of RAM: PostgreSQL 18.7 first, then 17.12, each in a fresh Docker container. Your milliseconds will differ. The SQL, the raw output of both versions and the chart code are &lt;a href="https://github.com/ImmuneFOMO/research/tree/main/postgres/slow-postgres/03-stale-statistics" rel="noopener noreferrer"&gt;on GitHub&lt;/a&gt;, and &lt;code&gt;sh bench/run.sh 18&lt;/code&gt; (or &lt;code&gt;17&lt;/code&gt;) reruns the whole lab in about 20 minutes. Run it on your own server, and if you get a different picture or think I got something wrong, open an issue or a PR.&lt;/p&gt;

&lt;p&gt;My own projects run on netcup (VPS from €2.21 a month) and &lt;a href="https://alphavps.com/clients/aff.php?aff=1157" rel="noopener noreferrer"&gt;AlphaVPS&lt;/a&gt; (VPS from €2.54 a month). For netcup, message me on &lt;a href="https://www.linkedin.com/in/anatolii-zhadan/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; and I'll send you a €5 voucher for new customers.&lt;/p&gt;

&lt;p&gt;Good luck with your plans. Part 4 is next: column order in a composite index, and what PostgreSQL 18's skip scan changes there.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finish
&lt;/h2&gt;

&lt;p&gt;I hope you understood the instructions correctly. If you have any edits to this article, I will be waiting for your comments. If I really helped you, I kindly ask you to subscribe to my LinkedIn and provide feedback on this article.&lt;/p&gt;

&lt;p&gt;Link to follow me:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/anatolii-zhadan/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All my articles and links: &lt;a href="https://fomo.moe" rel="noopener noreferrer"&gt;fomo.moe&lt;/a&gt;&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>queryoptimization</category>
      <category>autovacuum</category>
      <category>database</category>
    </item>
    <item>
      <title>Reading Postgres EXPLAIN ANALYZE when the planner expects 300 rows and gets 15,000</title>
      <dc:creator>ImmuneFOMO</dc:creator>
      <pubDate>Tue, 17 Nov 2026 13:20:54 +0000</pubDate>
      <link>https://dev.to/immunefomo/reading-postgres-explain-analyze-when-the-planner-expects-300-rows-and-gets-15000-5a11</link>
      <guid>https://dev.to/immunefomo/reading-postgres-explain-analyze-when-the-planner-expects-300-rows-and-gets-15000-5a11</guid>
      <description>&lt;p&gt;&lt;em&gt;Slow Postgres, part 2 of 8. The SQL and raw numbers for every test are &lt;a href="https://github.com/ImmuneFOMO/research/tree/main/postgres/slow-postgres/02-reading-explain-analyze" rel="noopener noreferrer"&gt;on GitHub&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flrjhnpyalkwm5jpsdlvq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flrjhnpyalkwm5jpsdlvq.png" alt="The users scan expected 300 rows and got 15,000" width="800" height="784"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Expected and actual rows per node, read from the bottom up.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Postgres guessed that 300 users live in Lisbon. There are 15,000. That one line of the plan explains the rest: a Nested Loop that ran an index lookup 15,000 times, a sort that spilled to disk, and a query that takes 159 ms with the data in memory and 2.15 seconds when it comes off disk.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://hackernoon.com/postgres-not-using-your-index-its-counting-pages-not-rows" rel="noopener noreferrer"&gt;Part 1&lt;/a&gt; was about how the planner prices a plan. This one reads what happened when it ran, and how much EXPLAIN ANALYZE itself adds.&lt;/p&gt;

&lt;p&gt;One note before the numbers: the AlphaVPS link at the end is a referral link, so I may get a commission at no extra cost to you, and the netcup voucher I offer there comes from my own account.&lt;/p&gt;

&lt;p&gt;The short version:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compare estimated and actual rows on every node, from the bottom up. Here a guess of 300 users picked a Nested Loop that won in memory (159 ms against 344) and lost from disk (2.15 s against 455 ms).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;actual time&lt;/code&gt; and &lt;code&gt;rows&lt;/code&gt; are per loop: an index scan that says &lt;code&gt;rows=0 loops=15000&lt;/code&gt; returned 3,071 rows.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;read&lt;/code&gt; in &lt;code&gt;Buffers&lt;/code&gt; means "not in shared_buffers", not "from disk".&lt;/li&gt;
&lt;li&gt;EXPLAIN ANALYZE made &lt;code&gt;count(*)&lt;/code&gt; 2.4 to 3.0 times slower on all four of my machines, so compare plans with &lt;code&gt;TIMING OFF&lt;/code&gt; or the plain query.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  The lab: a million users, five million orders
&lt;/h2&gt;

&lt;p&gt;PostgreSQL 17.12 in Docker, on the same 4-vCPU, 8 GB cloud VM as part 1. Every user has five orders, and &lt;code&gt;orders.user_id&lt;/code&gt; has an index. The city decides the country: 15,000 users live in Lisbon, 5,000 in Porto, and those 20,000 are all of &lt;code&gt;PT&lt;/code&gt;. The rows come from &lt;code&gt;generate_series&lt;/code&gt; and &lt;code&gt;setseed&lt;/code&gt;, so yours match mine. &lt;code&gt;shared_buffers&lt;/code&gt; is 1 GB so everything fits in memory, and JIT and parallel workers are off to keep the plans short.&lt;/p&gt;

&lt;p&gt;The report counts orders of Lisbon users by status:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) AS orders, sum(o.amount_cents) AS cents
FROM users u
JOIN orders o ON o.user_id = u.id
WHERE u.country = 'PT' AND u.city = 'Lisbon'
GROUP BY o.status
ORDER BY orders DESC;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  EXPLAIN guesses, EXPLAIN ANALYZE runs
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;EXPLAIN&lt;/code&gt; prints the plan with the planner's forecast for each node, &lt;code&gt;cost&lt;/code&gt; and &lt;code&gt;rows&lt;/code&gt;. &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; runs the query and adds what happened: &lt;code&gt;actual time&lt;/code&gt;, actual &lt;code&gt;rows&lt;/code&gt; and &lt;code&gt;loops&lt;/code&gt;. &lt;code&gt;BUFFERS&lt;/code&gt; adds the pages each node touched, and PostgreSQL 18 turns it on by default with &lt;code&gt;ANALYZE&lt;/code&gt;. The result rows are thrown away, so &lt;code&gt;Execution Time&lt;/code&gt; leaves out sending them to the client and turning values into text (the &lt;code&gt;SERIALIZE&lt;/code&gt; option, new in PostgreSQL 17, measures the second part).&lt;/p&gt;

&lt;p&gt;Writes run too, so wrap them in a transaction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;DELETE&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'pending'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ROLLBACK&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;ROLLBACK&lt;/code&gt; brought back all 149,701 pending orders, but the work still happened: the DELETE dirtied 36,469 pages that still get written to disk. Sequences don't roll back either. After a rolled-back &lt;code&gt;EXPLAIN ANALYZE INSERT&lt;/code&gt; of 1,000 rows, the next real row got id 1001.&lt;/p&gt;

&lt;h2&gt;
  
  
  Read it from the inside out
&lt;/h2&gt;

&lt;p&gt;The report's plan, with some lines cut (the full output is in &lt;code&gt;02_plans.out&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sort  (cost=31759.19..31759.20 rows=4 width=21) (actual time=209.555..209.560 rows=4 loops=1)
  Buffers: shared hit=129440, temp read=205 written=205
  -&amp;gt;  GroupAggregate  (cost=31744.11..31759.15 rows=4 width=21) (actual time=201.392..209.511 rows=4 loops=1)
        -&amp;gt;  Sort  (cost=31744.11..31747.86 rows=1500 width=9) (actual time=201.037..205.049 rows=75000 loops=1)
              Sort Method: external merge  Disk: 1640kB
              -&amp;gt;  Nested Loop  (cost=0.43..31664.98 rows=1500 width=9) (actual time=0.030..189.952 rows=75000 loops=1)
                    -&amp;gt;  Seq Scan on users u  (cost=0.00..24434.00 rows=300 width=4) (actual time=0.013..47.068 rows=15000 loops=1)
                          Filter: ((country = 'PT'::text) AND (city = 'Lisbon'::text))
                          Rows Removed by Filter: 985000
                    -&amp;gt;  Index Scan using orders_user_id_idx on orders o  (cost=0.43..24.05 rows=5 width=13) (actual time=0.003..0.009 rows=5 loops=15000)
                          Index Cond: (user_id = u.id)
                          Buffers: shared hit=120000
Planning Time: 0.247 ms
Execution Time: 209.990 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A plan prints top-down, but rows flow bottom-up. The users scan read a million rows and kept 15,000. For each of them, the Nested Loop ran the index scan on orders. The inner Sort ordered the 75,000 orders by status, GroupAggregate collapsed them into four rows, and the top Sort ordered those.&lt;/p&gt;

&lt;p&gt;Each node has two sets of parentheses. &lt;code&gt;cost=31744.11..31747.86 rows=1500&lt;/code&gt; is the forecast, in planner units that are priced in pages and don't convert to milliseconds. &lt;code&gt;actual time=201.037..205.049 rows=75000&lt;/code&gt; is what happened: milliseconds until the node's first row and until its last. The inner Sort's first row came at 201 ms because a sort returns nothing until it has read all its input.&lt;/p&gt;

&lt;p&gt;Times are cumulative. The Nested Loop finished at 190 ms, 47 ms of which was the users scan, so the 15,000 index lookups took about 143 ms. To find the slow node, subtract each node's children from it and look for the biggest remainder. &lt;code&gt;Buffers&lt;/code&gt; add up the same way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Estimated rows against actual rows
&lt;/h2&gt;

&lt;p&gt;The users scan expected 300 rows and returned 15,000. The 300 comes from &lt;code&gt;pg_stats&lt;/code&gt;: &lt;code&gt;PT&lt;/code&gt; is 2% of users, &lt;code&gt;Lisbon&lt;/code&gt; is 1.5%, and the planner treats the two conditions as independent and multiplies them, 1,000,000 × 0.02 × 0.015 = 300. Every Lisbon user is also in &lt;code&gt;PT&lt;/code&gt;, so the country condition removes nobody.&lt;/p&gt;

&lt;p&gt;Go up one node. The Nested Loop was planned for 300 index lookups returning 1,500 orders and ran 15,000 lookups returning 75,000. The Sort above it was planned for 1,500 rows, got 75,000 and spilled to disk. So walk up from the leaves comparing the two &lt;code&gt;rows=&lt;/code&gt; and start at the lowest node where they differ by 10x or more. Under a &lt;code&gt;LIMIT&lt;/code&gt; a node stops early, so fewer actual rows there is expected.&lt;/p&gt;

&lt;p&gt;Did the wrong guess hurt? Drop the redundant &lt;code&gt;country = 'PT'&lt;/code&gt; and the planner estimates 74,999 joined rows against 75,000 actual, and switches to a Hash Join: read all five million orders once and match them against a hash of the 15,000 users. I timed both plans without EXPLAIN, with the data in three places:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fced8xhygvlxocoa6nkxn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fced8xhygvlxocoa6nkxn.png" alt="Nested Loop against Hash Join: 159 vs 344 ms in memory, 2.15 s vs 455 ms from disk" width="800" height="744"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Plain query time, median of 7 warm runs and of 5 runs after each restart, on the 4-vCPU cloud VM. The OS cache was dropped before every disk run.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In memory the plan built on the wrong guess won, 159 ms against 344, because 15,000 index lookups in RAM cost less than reading five million rows. From disk it lost, 2.15 s against 455 ms. The planner can't see what's cached, and its page costs put 15,000 scattered lookups far above one pass over the table, which is how they behaved cold. A warm EXPLAIN ANALYZE shows only one side of that trade. Where estimates come from and why they drift is part 3.&lt;/p&gt;
&lt;h2&gt;
  
  
  Loops: the numbers are per loop
&lt;/h2&gt;

&lt;p&gt;The index scan on orders says &lt;code&gt;rows=5 loops=15000&lt;/code&gt;: the Nested Loop ran it once per user. &lt;code&gt;actual time&lt;/code&gt; and &lt;code&gt;rows&lt;/code&gt; are averages per loop, like the estimate next to them: 5 × 15,000 = 75,000 rows in about 0.009 × 15,000 = 135 ms, close to the 143 ms from the subtraction above.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Buffers&lt;/code&gt; is a total over all loops. Its 120,000 pages are 8 per lookup: three levels of the B-tree plus five heap pages for five orders spread through the table. PostgreSQL 18 adds an &lt;code&gt;Index Searches&lt;/code&gt; line to index scans, also a total.&lt;/p&gt;

&lt;p&gt;Per-loop averages are rounded, too. The same join, keeping only failed orders:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Nested Loop  (cost=0.43..31656.73 rows=62 width=20) (actual time=0.240..147.007 rows=3071 loops=1)
  -&amp;gt;  Index Scan using orders_user_id_idx on orders o  (cost=0.43..24.07 rows=1 width=24) (actual time=0.006..0.007 rows=0 loops=15000)
        Index Cond: (user_id = u.id)
        Filter: (status = 'failed'::text)
        Rows Removed by Filter: 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;rows=0&lt;/code&gt; doesn't mean the scan found nothing. 3,071 rows over 15,000 loops is 0.2 per loop, which PostgreSQL 17 rounds to 0 and 18 prints as &lt;code&gt;rows=0.20&lt;/code&gt;. &lt;code&gt;Rows Removed by Filter: 5&lt;/code&gt; is per loop too, about 72,000 rows in all. Parallel workers count as loops: with them on, a Parallel Seq Scan on orders showed &lt;code&gt;rows=1666667 loops=3&lt;/code&gt;, the leader and two workers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rows Removed by Filter
&lt;/h2&gt;

&lt;p&gt;In the report, &lt;code&gt;Rows Removed by Filter: 985000&lt;/code&gt; on the users scan means Postgres fetched a million rows and checked each one to keep 15,000. The orders scan has an &lt;code&gt;Index Cond&lt;/code&gt; instead: the index found the rows, and nothing was thrown away.&lt;/p&gt;

&lt;p&gt;On a Seq Scan, a large count is where an index might help (part 1 covers when it won't). On an index scan, it means the index covers only part of the &lt;code&gt;WHERE&lt;/code&gt; clause and the rest is checked row by row. In the failed-orders join, the index found all five orders of each user and the filter threw away 96% of them. Which index fits a query like that is part 4.&lt;/p&gt;

&lt;h2&gt;
  
  
  BUFFERS: hit, read, and a cold cache
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;shared hit&lt;/code&gt; is a page found in shared_buffers. &lt;code&gt;read&lt;/code&gt; is a page Postgres asked the OS for, which came from the OS page cache or from disk, and &lt;code&gt;BUFFERS&lt;/code&gt; can't tell which. The counts aren't distinct pages: a page visited twenty times is twenty hits. The report's Nested Loop plan in all three states:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                          Buffers                    I/O wait    query time
in shared_buffers         hit=129440                 -           159 ms
restart, OS cache warm    hit=87639  read=41801      164 ms      304 ms
restart, OS cache empty   hit=87639  read=41801      2,011 ms    2.15 s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I/O wait is the median from the EXPLAIN runs, query time from the plain ones. Both restarted runs read the same 41,801 pages, and the second was seven times slower because those reads went to disk.&lt;/p&gt;

&lt;p&gt;The I/O wait comes from &lt;code&gt;track_io_timing&lt;/code&gt;, which is off by default and which a superuser can turn on for the session. Every node then gets an &lt;code&gt;I/O Timings&lt;/code&gt; line. From the median cold run of each plan, costs cut:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Nested Loop plan
-&amp;gt;  Index Scan using orders_user_id_idx on orders o  (actual time=0.032..0.141 rows=5 loops=15000)
      Buffers: shared hit=87633 read=32367
      I/O Timings: shared read=1965.141

Hash Join plan
-&amp;gt;  Seq Scan on orders o  (actual time=0.125..291.938 rows=5000000 loops=1)
      Buffers: shared read=37101
      I/O Timings: shared read=65.418
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The index scan waited almost 2 seconds for 32,367 pages, about 60 µs each, as its lookups jumped around the table. The Seq Scan read 37,101 pages of the same table front to back in 65 ms, under 2 µs each. All 87,633 hits are repeat visits inside the index scan, like the top of the B-tree on every lookup.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sort and Hash: memory or disk
&lt;/h2&gt;

&lt;p&gt;Two kinds of lines say an operation didn't fit in memory. A sort gets &lt;code&gt;work_mem&lt;/code&gt;, 4 MB by default, and a hash twice that, since &lt;code&gt;hash_mem_multiplier&lt;/code&gt; defaults to 2. The report's inner Sort shows &lt;code&gt;Sort Method: external merge  Disk: 1640kB&lt;/code&gt;: it wrote sorted runs to temp files, the &lt;code&gt;temp read=205 written=205&lt;/code&gt; in its &lt;code&gt;Buffers&lt;/code&gt; (205 pages of 8 kB). A Hash shows it as batches. This one joins all 200,120 failed orders to users, sorted by amount:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hash  (cost=19434.00..19434.00 rows=1000000 width=26) (actual time=186.969..186.971 rows=1000000 loops=1)
  Buckets: 131072  Batches: 16  Memory Usage: 4695kB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Batches: 16&lt;/code&gt; means the hash table was split into 16 parts and 15 of them waited in temp files. The final sort spilled too, 7,448 kB.&lt;/p&gt;

&lt;p&gt;At 64 MB of &lt;code&gt;work_mem&lt;/code&gt; (128 MB for the hash) both fit in memory, the hash in one 66,782 kB batch, and on the cloud VM the query got slower: 425 ms to 578 ms with &lt;code&gt;TIMING OFF&lt;/code&gt;, median of seven. On my M3 Max laptop it got faster, 418 ms to 341 ms. I haven't found out why. CPU cache was my first guess, since a 4 MB batch fits in it and a 66 MB table doesn't, but that doesn't explain the laptop, so it stays unconfirmed.&lt;/p&gt;

&lt;p&gt;The report's own 1.6 MB spill cost nothing I could measure: 158.6 ms at 4 MB, 158.0 ms at 64 MB. A &lt;code&gt;Disk:&lt;/code&gt; line says a node spilled, and only a rerun with more &lt;code&gt;work_mem&lt;/code&gt;, on the machine that serves the query, says whether the spill is slow. Each sort and hash in each connection can take that much, so raise it per session or per role.&lt;/p&gt;

&lt;h2&gt;
  
  
  Planning time and execution time
&lt;/h2&gt;

&lt;p&gt;The report spent 0.25 ms planning and 210 ms executing, the usual split for a slow query. &lt;code&gt;Planning Time&lt;/code&gt; counts on queries that run in microseconds, like a primary-key lookup on users right after a restart:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;first run in a new connection:  Planning Time: 0.430 ms   Execution Time: 0.078 ms
                                Planning: Buffers: shared hit=52 read=21
second run:                     Planning Time: 0.013 ms   Execution Time: 0.006 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first plan in a new connection reads table and index details from the system catalogs, 73 pages here, and took five times longer than the query. After that, planning still took twice as long as execution. If a lookup like this runs thousands of times a second over short-lived connections, a pooler that keeps connections open removes that first cost.&lt;/p&gt;

&lt;h2&gt;
  
  
  What EXPLAIN ANALYZE costs
&lt;/h2&gt;

&lt;p&gt;To time each node, EXPLAIN ANALYZE reads the clock before and after every call into it, twice for every row each node returns. If millions of rows pass through a cheap node, the clock reads can cost more than the work.&lt;/p&gt;

&lt;p&gt;On my laptop, the cloud VM, an AlphaVPS server in Los Angeles and an ARM server at netcup, I ran &lt;code&gt;pg_test_timing&lt;/code&gt;, which ships with Postgres and times a loop around one clock read, then &lt;code&gt;SELECT count(*)&lt;/code&gt; over 10 million rows, seven times each way.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftmd35pipebczm9h6u0x0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftmd35pipebczm9h6u0x0.png" alt="count(*) over 10 million rows, plain against EXPLAIN ANALYZE, on four machines" width="800" height="1059"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Median of 7 runs per variant, data in memory, one backend, PostgreSQL 17.7 on the laptop and 17.12 on the three servers.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pg_test_timing&lt;/code&gt; gave 18 to 40 ns per loop, and EXPLAIN ANALYZE still made &lt;code&gt;count(*)&lt;/code&gt; 2.4 to 3.0 times slower everywhere. The extra time per row followed the clock: 24 ns on the laptop, 44 on the VM, 59 on the Xeon, 74 on the ARM server, or 1.3 to 1.9 times the &lt;code&gt;pg_test_timing&lt;/code&gt; figure. On the VM, switching the clock source from &lt;code&gt;tsc&lt;/code&gt; to &lt;code&gt;kvm-clock&lt;/code&gt; raised both, to 28 and 50 ns, and the slowdown to 3.2 times. A nested loop with a million index lookups does more work per row, and there EXPLAIN ANALYZE added 4% to 15%. The report lost 7%, 159 ms to 170.&lt;/p&gt;

&lt;p&gt;I expected a bigger gap between machines, but all four clocks were fast. The &lt;a href="https://www.postgresql.org/docs/17/pgtesttiming.html" rel="noopener noreferrer"&gt;pg_test_timing docs&lt;/a&gt; show a slow one, &lt;code&gt;acpi_pm&lt;/code&gt;, where a loop took 723 ns and EXPLAIN ANALYZE turned a 9.8 ms count into 115.9 ms. The AlphaVPS server offers &lt;code&gt;acpi_pm&lt;/code&gt; too, but I didn't switch a live server's clock.&lt;/p&gt;

&lt;p&gt;The overhead also skews comparisons between plans. Under EXPLAIN ANALYZE the Hash Join version of the report took 591 ms and the Nested Loop 170 ms, 3.5 times apart. Run plain, they took 344 ms and 159 ms, 2.2 times apart. The Hash Join pushes five million orders through its Seq Scan node, ten million clock reads, while no node in the Nested Loop plan returns more than 75,000 rows.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;TIMING OFF&lt;/code&gt; keeps rows, loops and buffers and drops the per-node times. It still counts every row, so &lt;code&gt;count(*)&lt;/code&gt; ran 16% to 19% slower than plain even then. Inside one plan the per-node times are the best map you have, but the nodes passing the most rows carry the most overhead.&lt;/p&gt;

&lt;h2&gt;
  
  
  The order I read a plan in
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Run &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt;, inside &lt;code&gt;BEGIN&lt;/code&gt; and &lt;code&gt;ROLLBACK&lt;/code&gt; if it writes.&lt;/li&gt;
&lt;li&gt;From the innermost nodes up, find the lowest one where estimated and actual &lt;code&gt;rows&lt;/code&gt; differ by 10x or more.&lt;/li&gt;
&lt;li&gt;Multiply per-loop numbers by &lt;code&gt;loops&lt;/code&gt;, then subtract each node's children to see where the time went.&lt;/li&gt;
&lt;li&gt;Compare &lt;code&gt;Rows Removed by Filter&lt;/code&gt; with the rows kept.&lt;/li&gt;
&lt;li&gt;Look for &lt;code&gt;Disk:&lt;/code&gt; and &lt;code&gt;Batches:&lt;/code&gt; above 1, and rerun with more &lt;code&gt;work_mem&lt;/code&gt; before blaming the spill.&lt;/li&gt;
&lt;li&gt;Read &lt;code&gt;Buffers&lt;/code&gt; with &lt;code&gt;track_io_timing&lt;/code&gt; on to see which node waited for reads.&lt;/li&gt;
&lt;li&gt;Before comparing two plans, time the plain queries, warm and cold.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Run it on your own server
&lt;/h2&gt;

&lt;p&gt;The walkthrough plans come from one 4-vCPU AMD EPYC cloud VM with 8 GB of RAM and PostgreSQL 17.12 in Docker, so your milliseconds will differ while the row counts match. The &lt;code&gt;work_mem&lt;/code&gt; check also ran on my M3 Max laptop (Homebrew PostgreSQL 17.7), and the overhead test ran on the laptop, the VM and an AlphaVPS server in Los Angeles, a 4-vCPU Xeon Platinum 8176 with 8 GB of RAM (PostgreSQL 17.12 from conda-forge), and a netcup server with 6 ARM Neoverse-N1 cores and 8 GB of RAM (PostgreSQL 17.12 in Docker). The SQL, scripts, raw output and chart code are &lt;a href="https://github.com/ImmuneFOMO/research/tree/main/postgres/slow-postgres/02-reading-explain-analyze" rel="noopener noreferrer"&gt;on GitHub&lt;/a&gt;, and &lt;code&gt;bench/overhead.sh&lt;/code&gt; runs the overhead test on a new Linux box with one command. Run it on your own server, and if you get a different picture or think I got something wrong, open an issue or a PR.&lt;/p&gt;

&lt;p&gt;My own projects run on netcup (VPS from €2.21 a month) and &lt;a href="https://alphavps.com/clients/aff.php?aff=1157" rel="noopener noreferrer"&gt;AlphaVPS&lt;/a&gt; (VPS from €2.54 a month). For netcup, message me on &lt;a href="https://www.linkedin.com/in/anatolii-zhadan/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; and I'll send you a €5 voucher for new customers.&lt;/p&gt;

&lt;p&gt;Good luck with your plans. Part 3 is next: why Postgres statistics go stale.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finish
&lt;/h2&gt;

&lt;p&gt;I hope you understood the instructions correctly. If you have any edits to this article, I will be waiting for your comments. If I really helped you, I kindly ask you to subscribe to my LinkedIn and provide feedback on this article.&lt;/p&gt;

&lt;p&gt;Link to follow me:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/anatolii-zhadan/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All my articles and links: &lt;a href="https://fomo.moe" rel="noopener noreferrer"&gt;fomo.moe&lt;/a&gt;&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>explainanalyze</category>
      <category>queryoptimization</category>
      <category>database</category>
    </item>
    <item>
      <title>Postgres not using your index? It's counting pages, not rows</title>
      <dc:creator>ImmuneFOMO</dc:creator>
      <pubDate>Fri, 13 Nov 2026 14:07:46 +0000</pubDate>
      <link>https://dev.to/immunefomo/postgres-not-using-your-index-its-counting-pages-not-rows-k4g</link>
      <guid>https://dev.to/immunefomo/postgres-not-using-your-index-its-counting-pages-not-rows-k4g</guid>
      <description>&lt;p&gt;&lt;em&gt;Slow Postgres, part 1 of 8. The SQL and raw numbers for every test are &lt;a href="https://github.com/ImmuneFOMO/research/tree/main/postgres/slow-postgres/01-index-vs-seq-scan" rel="noopener noreferrer"&gt;on GitHub&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fyfsp6v7l417q5zug053o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fyfsp6v7l417q5zug053o.png" alt="Seq Scan reads heap pages in order; Index Scan reads the B-tree, then one heap page per TID" width="800" height="752"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;A Seq Scan reads each page once, in order. An index lookup walks the B-tree and then reads one heap page per TID.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You add an index on &lt;code&gt;status&lt;/code&gt;, run &lt;code&gt;EXPLAIN&lt;/code&gt;, and Postgres reads the whole table anyway. &lt;code&gt;ANALYZE&lt;/code&gt; doesn't change the plan. Someone suggests &lt;code&gt;SET enable_seqscan = off&lt;/code&gt;, the plan finally shows your index, and the query gets slower.&lt;/p&gt;

&lt;p&gt;Usually the planner is right. It prices a plan by the 8 KB pages it has to read and by whether it reads them in order or jumps around between them, and once you count pages the same way, you can answer most "why is it ignoring my index" tickets yourself.&lt;/p&gt;

&lt;p&gt;This series started as my notes from an &lt;a href="https://www.youtube.com/watch?v=AZF9QZeJ-v8" rel="noopener noreferrer"&gt;open lesson on slow Postgres queries&lt;/a&gt; by balun.courses. The tests and numbers are my own.&lt;/p&gt;

&lt;p&gt;One note before the numbers: the AlphaVPS link at the end is a referral link, so I may get a commission at no extra cost to you, and the netcup vouchers come from my own account.&lt;/p&gt;

&lt;p&gt;The short version:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Postgres prices a plan in pages, and in my test table a status with 0.9% of the rows sits on 94% of them, so its index saves 12 page reads out of 221.&lt;/li&gt;
&lt;li&gt;On 5 million rows in RAM, the index beat a full scan up to 10% of the rows when the matches were scattered, and at every share I tried (up to 50%) when they were stored together.&lt;/li&gt;
&lt;li&gt;Read cold from disk, the scattered lookups at 0.5% and 1% of rows ran 2 to 5 times slower than a full scan, and Postgres still picked them.&lt;/li&gt;
&lt;li&gt;A 200-row table gets a Seq Scan even when 2 rows match, so a plan from a tiny test copy says nothing about production.&lt;/li&gt;
&lt;li&gt;Keep &lt;code&gt;enable_seqscan&lt;/code&gt; out of app code, read &lt;code&gt;pg_stats&lt;/code&gt; before &lt;code&gt;CREATE INDEX&lt;/code&gt;, and if you only query the rare values, index only those rows.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  What a B-tree index stores
&lt;/h2&gt;

&lt;p&gt;A regular B-tree index keeps only keys, the values it is sorted by, and next to each key a TID: the row's address in the heap, which is the table's main storage.&lt;/p&gt;

&lt;p&gt;A TID is a page number plus a slot on that page. You can see it as the hidden &lt;code&gt;ctid&lt;/code&gt; column:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;ctid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'chargeback'&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  ctid   |  id  |   status
---------+------+------------
 (7,48)  | 1000 | chargeback
 (14,96) | 2000 | chargeback
 (22,8)  | 3000 | chargeback
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Row 1000 is slot 48 on page 7. An index lookup for &lt;code&gt;'chargeback'&lt;/code&gt; returns a list of addresses like these, and Postgres then visits each of those heap pages to fetch the row and check that your transaction can see it.&lt;/p&gt;

&lt;p&gt;The second step is the one people forget. To fetch one row, Postgres has to read the whole 8 KB page it sits on.&lt;/p&gt;

&lt;h2&gt;
  
  
  The lab: 30,000 orders on 221 pages
&lt;/h2&gt;

&lt;p&gt;The lab runs on PostgreSQL 17.11 in Docker with default settings. My numbers come from a 4-vCPU, 8 GB cloud VM, but any small VPS or a laptop will do.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;           &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="no"&gt;IDENTITY&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;created_at&lt;/span&gt;   &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;amount_cents&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;status&lt;/span&gt;       &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount_cents&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;30000&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="kt"&gt;interval&lt;/span&gt; &lt;span class="s1"&gt;'1 minute'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="k"&gt;CASE&lt;/span&gt;
         &lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt; &lt;span class="s1"&gt;'chargeback'&lt;/span&gt;  &lt;span class="c1"&gt;-- 30 rows, 0.1%&lt;/span&gt;
         &lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt; &lt;span class="s1"&gt;'failed'&lt;/span&gt;      &lt;span class="c1"&gt;-- 270 rows, 0.9%&lt;/span&gt;
         &lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt; &lt;span class="s1"&gt;'created'&lt;/span&gt;     &lt;span class="c1"&gt;-- 2,700 rows, 9%&lt;/span&gt;
         &lt;span class="k"&gt;ELSE&lt;/span&gt; &lt;span class="s1"&gt;'paid'&lt;/span&gt;                          &lt;span class="c1"&gt;-- 27,000 rows, 90%&lt;/span&gt;
       &lt;span class="k"&gt;END&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nf"&gt;generate_series&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;orders_status_idx&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ANALYZE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;relpages&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reltuples&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_class&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;relname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'orders'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; relpages | reltuples
----------+-----------
      221 |     30000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;136 rows fit on a page, so 30,000 rows take 221 pages. I picked 30,000 because &lt;code&gt;ANALYZE&lt;/code&gt; samples 300 × &lt;code&gt;default_statistics_target&lt;/code&gt; rows, which is 30,000 at the default. On this table it reads every row, so the statistics are exact and your plans should come out the same as mine.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a Seq Scan costs
&lt;/h2&gt;

&lt;p&gt;Ask for the most common value and the index goes unused:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;EXPLAIN SELECT * FROM orders WHERE status = 'paid';

Seq Scan on orders  (cost=0.00..596.00 rows=27000 width=25)
  Filter: (status = 'paid'::text)

221 pages  × seq_page_cost     1.0    = 221
30000 rows × cpu_tuple_cost    0.01   = 300
30000 rows × cpu_operator_cost 0.0025 =  75
                                        ---
                                        596
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cost has no unit you can convert to milliseconds. It only means something next to another plan's cost.&lt;/p&gt;

&lt;p&gt;The value you filter on appears nowhere in that formula. Ask for &lt;code&gt;'chargeback'&lt;/code&gt; with the index paths switched off and the Seq Scan still costs 596. Only the index path's price moves with the value, so the planner is choosing between a fixed price and a sliding one.&lt;/p&gt;

&lt;p&gt;For &lt;code&gt;'paid'&lt;/code&gt;, every one of the 221 pages holds paid rows, so the index path would read the whole heap plus the index. Force it with &lt;code&gt;enable_seqscan = off&lt;/code&gt; and you get a Bitmap Heap Scan at cost 872.&lt;/p&gt;

&lt;h2&gt;
  
  
  How many pages each status sits on
&lt;/h2&gt;

&lt;p&gt;Selectivity is the share of rows that pass the filter. The usual rule of thumb goes: one row, use the index; 1%, usually; 20%, it depends; 70%, Seq Scan. Here is the same table counted by pages as well as by rows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*)                               AS rows,
       count(DISTINCT (ctid::text::point)[0]) AS pages
FROM orders
GROUP BY status
ORDER BY rows DESC;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmbuphl2j9uefx2fdhgp0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmbuphl2j9uefx2fdhgp0.png" alt="Bar chart: paid 90% of rows on 100% of pages, created 9% on 100%, failed 0.9% on 94%, chargeback 0.1% on 14%" width="800" height="776"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Share of rows and share of pages for each status in the 30,000-row table. failed is 0.9% of the rows but sits on 94% of the pages.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The rule of thumb says index &lt;code&gt;failed&lt;/code&gt;, and Postgres agrees: Index Scan at cost 235 against 596. Run both with &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt;, though, and the index path touches 209 buffers to the Seq Scan's 221, a saving of twelve page reads.&lt;/p&gt;

&lt;p&gt;It still finished faster, 0.19 ms versus 0.93, because checking 270 rows is less CPU work than filtering 30,000. For &lt;code&gt;'chargeback'&lt;/code&gt;, 30 rows on 30 pages, the index path needs 32 buffers instead of 221.&lt;/p&gt;

&lt;p&gt;Two things decide how rows land on pages. Row width sets how many fit on one, and at 136 rows per page any value much more common than 1 in 136 ends up on nearly every page. Physical order is the other. I spread &lt;code&gt;failed&lt;/code&gt; evenly on purpose. If your failed orders came from one bad deploy on one afternoon, they'd sit on a few neighbouring pages and the index would look great.&lt;/p&gt;
&lt;h2&gt;
  
  
  Timing it on 5 million rows in RAM
&lt;/h2&gt;

&lt;p&gt;On 221 pages everything finishes in a couple of milliseconds, so for timings I built a bigger table: 5 million rows, 326 MB, about 120 rows per page.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;bucket&lt;/code&gt; column holds a random number from 0 to 999,999, so &lt;code&gt;WHERE bucket &amp;lt; 10000&lt;/code&gt; matches 1% of the rows. I loaded the same rows twice, once in random order and once sorted by &lt;code&gt;bucket&lt;/code&gt;, and forced each plan with the &lt;code&gt;enable_*&lt;/code&gt; settings at shares from 0.01% to 50%. &lt;code&gt;shared_buffers&lt;/code&gt; was 2 GB, which kept the whole table in memory.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkn8g92h4o08jcg53dj28.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkn8g92h4o08jcg53dj28.png" alt="Line chart of query time against share of matching rows: Seq Scan between 114 and 159 ms, scattered bitmap scan crossing it between 10% and 25%, clustered Index Scan below it all the way to 50%" width="800" height="784"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Median of 5 warm runs on 5 million rows, one process, JIT off. Dots mark the plan Postgres chose on its own. Grey dotted line: Seq Scan on the clustered copy.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For scattered rows Postgres picked a bitmap scan, which collects all the TIDs first and then reads each heap page once, in page order. Up to 10% of the rows it beat the Seq Scan, 98 ms to 124. At 25% it lost, 174 against 139, and the planner still chose it. A plain Index Scan, one heap visit per row, lost from 5% on.&lt;/p&gt;

&lt;p&gt;By 10% the scattered matches sit on every single page, and the bitmap scan still won. It reads all 41,667 pages but checks only the 499,000 rows the bitmap points to, while the Seq Scan checks all 5 million. With the table in RAM a page read costs next to nothing, and the time goes into checking rows.&lt;/p&gt;

&lt;p&gt;For clustered rows the index won at every share I tried, even 50%: 123 ms against 142.&lt;/p&gt;
&lt;h2&gt;
  
  
  The same queries with a cold cache
&lt;/h2&gt;

&lt;p&gt;RAM is the friendly case. For the cold runs I restarted Postgres and dropped the OS page cache before every run, so every page came off the VM's disk.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftyt7ms73hggtnreodk80.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftyt7ms73hggtnreodk80.png" alt="Line chart of the same queries with a cold cache: Seq Scan flat around 250 to 350 ms, scattered bitmap scan peaking at 1.27 s at 1% of rows, clustered Index Scan below the Seq Scan everywhere" width="800" height="784"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Same tables and queries read cold from disk, median of 5. Dots mark the plan Postgres chose. Grey dotted line: Seq Scan on the clustered copy.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The Seq Scan got about twice as slow, 250 to 350 ms at every share. The clustered index won everywhere again, 23 ms versus 263 at 5%.&lt;/p&gt;

&lt;p&gt;The scattered bitmap scan took 748 ms at 0.5% of rows, to the Seq Scan's 349. At 1% it took 1.27 seconds against 255 ms, five times slower, and that's the plan Postgres picks.&lt;/p&gt;

&lt;p&gt;The bitmap scan at 2% read more heap pages than at 1%, 37,939 versus 29,143, and still finished more than six times faster, 190 ms against 1,275. From 2% to 10% it beat the Seq Scan again.&lt;/p&gt;

&lt;p&gt;The 2% run touched 91% of the pages, so it was close to one long sequential read. The 1% run touched 70%, with holes in between. My best guess is OS readahead, which prefetches reads that look sequential and gives up when they're full of gaps. I haven't confirmed it yet.&lt;/p&gt;

&lt;p&gt;The planner has no way to see that difference. All it has is &lt;code&gt;random_page_cost = 4&lt;/code&gt;, a single guess at how much a jump costs next to a sequential read, and it used the same guess for both runs.&lt;/p&gt;
&lt;h2&gt;
  
  
  Seq Scans on a 200-row table
&lt;/h2&gt;

&lt;p&gt;A 200-row copy shows the other end of the scale, where the table is too small for any index to win.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders_small&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders_small&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ANALYZE&lt;/span&gt; &lt;span class="n"&gt;orders_small&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders_small&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'failed'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Seq Scan on orders_small  (cost=0.00..4.50 rows=2 width=25)
  Filter: (status = 'failed'::text)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only 2 of the 200 rows match, but reading two pages and checking 200 rows costs just 2 + 2 + 0.5 = 4.50. Turn off &lt;code&gt;enable_seqscan&lt;/code&gt; and the cheapest index plan costs 6.19, because its one index page alone is priced as a random read at &lt;code&gt;random_page_cost = 4&lt;/code&gt;. If you check plans on a 200-row test fixture, this is the arithmetic you're looking at, and it tells you nothing about the same query on 30 million rows in production.&lt;/p&gt;

&lt;p&gt;"We're on SSDs, set it to 1.1." Fair, and on this table that flips the plan to an Index Scan at cost 2.77. Both versions finish in 10 microseconds or less, so on a table this size it makes no difference.&lt;/p&gt;

&lt;h2&gt;
  
  
  A partial index instead of enable_seqscan
&lt;/h2&gt;

&lt;p&gt;The one place for &lt;code&gt;enable_seqscan = off&lt;/code&gt; is a scratch session, where it shows you what the other plan would cost. It applies to every query in the session, so in app code it also overrides the plans that were right, like 596 against 872 for &lt;code&gt;'paid'&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If the queries you care about only ask for the rare values, say a failed-payments dashboard or a chargeback queue, index only those rows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;orders_status_not_paid_idx&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'paid'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It holds 3,000 entries instead of 30,000 and takes 5 pages, where the full index takes 28. Paid orders, 90% of the inserts here, never touch it.&lt;/p&gt;

&lt;p&gt;Postgres only uses it when it can prove your filter implies the index's &lt;code&gt;WHERE&lt;/code&gt;, and a literal &lt;code&gt;status = 'chargeback'&lt;/code&gt; does. With only the partial index in place, a prepared &lt;code&gt;status = $1&lt;/code&gt; under a generic plan falls back to a Seq Scan, because the planner can't prove anything about a value it hasn't seen. Generic plans get their own part later in the series.&lt;/p&gt;

&lt;h2&gt;
  
  
  The planner's statistics in pg_stats
&lt;/h2&gt;

&lt;p&gt;Before adding an index, look at the column in &lt;code&gt;pg_stats&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;null_frac&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n_distinct&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;most_common_vals&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;most_common_freqs&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_stats&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;schemaname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'public'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;tablename&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'orders'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;attname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'status'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; null_frac | n_distinct |         most_common_vals         |   most_common_freqs
-----------+------------+----------------------------------+------------------------
         0 |          4 | {paid,created,failed,chargeback} | {0.9,0.09,0.009,0.001}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's what the planner believes about &lt;code&gt;status&lt;/code&gt; as of the last &lt;code&gt;ANALYZE&lt;/code&gt;. If most of your queries filter on &lt;code&gt;paid&lt;/code&gt;, at 0.9, they get a Seq Scan, and no extra index will change that.&lt;/p&gt;

&lt;p&gt;A negative &lt;code&gt;n_distinct&lt;/code&gt; means a fraction of the row count. On tables bigger than the 30,000-row sample these numbers are estimates; you can raise the sample with &lt;code&gt;ALTER TABLE ... ALTER COLUMN ... SET STATISTICS&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The same view has a &lt;code&gt;correlation&lt;/code&gt; column, the planner's idea of physical order. On my 5-million-row tables it was 0.01 for the scattered copy and 1 for the clustered one, and that difference in physical order is what separates the scattered and clustered lines in both charts.&lt;/p&gt;

&lt;p&gt;Before &lt;code&gt;CREATE INDEX&lt;/code&gt;, run the &lt;code&gt;pg_stats&lt;/code&gt; query, &lt;code&gt;relpages&lt;/code&gt; and the rows-versus-pages count on your own table. If the value you care about sits on most of the pages and the table doesn't stay in memory, the index won't save you reads.&lt;/p&gt;

&lt;h2&gt;
  
  
  Run it on your own server
&lt;/h2&gt;

&lt;p&gt;All the numbers above come from one 4-vCPU AMD EPYC cloud VM with 8 GB of RAM, so yours will differ. The SQL, the scripts, the raw output and the chart code are &lt;a href="https://github.com/ImmuneFOMO/research/tree/main/postgres/slow-postgres/01-index-vs-seq-scan" rel="noopener noreferrer"&gt;on GitHub&lt;/a&gt;. Run them on your own server, and if you get a different picture or think I got something wrong, open an issue or a PR.&lt;/p&gt;

&lt;p&gt;My own projects run on netcup (VPS from €2.21 a month) and &lt;a href="https://alphavps.com/clients/aff.php?aff=1157" rel="noopener noreferrer"&gt;AlphaVPS&lt;/a&gt; (VPS from €2.54 a month). If you need a box for this, either works.&lt;/p&gt;

&lt;p&gt;For netcup I have a few €5 vouchers for new customers. Each one works once, so take one and leave the rest for the next person:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;36nc179418264998
36nc179418264997
36nc179418264996
36nc179418264995
36nc179418264994
36nc179418264993
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Paste it into the "Redeem voucher" field at the bottom of the netcup cart. If they're all gone, message me on &lt;a href="https://www.linkedin.com/in/anatolii-zhadan/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; and I'll send you a new one.&lt;/p&gt;

&lt;p&gt;Good luck with your plans. Part 2 is next: how to read Postgres EXPLAIN ANALYZE.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finish
&lt;/h2&gt;

&lt;p&gt;I hope you understood the instructions correctly. If you have any edits to this article, I will be waiting for your comments. If I really helped you, I kindly ask you to subscribe to my LinkedIn and provide feedback on this article.&lt;/p&gt;

&lt;p&gt;Link to follow me:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/anatolii-zhadan/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All my articles and links: &lt;a href="https://fomo.moe" rel="noopener noreferrer"&gt;fomo.moe&lt;/a&gt;&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>sql</category>
      <category>performance</category>
    </item>
    <item>
      <title>How to Deploy a Self-Hosted 2-of-3 MPC Signing Cluster (Without Losing Everyone's Keys)</title>
      <dc:creator>ImmuneFOMO</dc:creator>
      <pubDate>Tue, 14 Jul 2026 10:38:28 +0000</pubDate>
      <link>https://dev.to/immunefomo/how-to-deploy-a-self-hosted-2-of-3-mpc-signing-cluster-without-losing-everyones-keys-2mg1</link>
      <guid>https://dev.to/immunefomo/how-to-deploy-a-self-hosted-2-of-3-mpc-signing-cluster-without-losing-everyones-keys-2mg1</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjvheh0da0sucj9kp958q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjvheh0da0sucj9kp958q.png" alt="Three independent MPC nodes, each holding one key share. The backend never holds a key. It sends a 32-byte digest and gets a signature back." width="800" height="426"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Three independent MPC nodes, each holding one key share. The backend never holds a key. It sends a 32-byte digest and gets a signature back.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A restored MPC cluster can boot up, report &lt;code&gt;[READY]&lt;/code&gt; on all three nodes, pass every health check — and have silently lost every private key it was supposed to hold. Ours did exactly that, once, during a disaster-recovery drill. A signature check caught it, with about thirty minutes to spare. In a real emergency, on a bad night, we might not have.&lt;/p&gt;

&lt;p&gt;That's the whole difference between a deploy guide and a production playbook, and it's why this one spends as much time restoring the cluster as standing it up.&lt;/p&gt;

&lt;p&gt;This is the hands-on half of my previous article, &lt;a href="https://medium.com/@Anatolii_Zhadan/how-to-store-your-users-crypto-wallets-so-nobody-can-steal-them-b0dd840211f6" rel="noopener noreferrer"&gt;*How to Store Your Users' Crypto Wallets So Nobody Can Steal Them&lt;/a&gt;&lt;em&gt;. There I argued *why&lt;/em&gt; the private key should never exist as a whole. Here we build the thing: a self-hosted 2-of-3 MPC signing cluster you can run on your own infrastructure, initialize with a distributed key-generation ceremony, back up, and recover from a real disaster.&lt;/p&gt;

&lt;p&gt;Everything below is from an actual deployment. The tools are open source, the numbers are measured, and the gotchas cost real hours. I've kept the architecture generic, a custodial Web3 backend that generates wallets and signs transactions for users, so you can map it onto your own stack.&lt;/p&gt;

&lt;p&gt;If you've deployed distributed systems before and just want the landmines, jump to &lt;strong&gt;Section 5&lt;/strong&gt; (the startup-order trap that can force you to rebuild your message bus) and &lt;strong&gt;Section 8&lt;/strong&gt; (the silent restore bug from the opening). Everything else builds up to those two.&lt;/p&gt;
&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/c38bab5cb0b5#d400" rel="noopener noreferrer"&gt;Architecture overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/c38bab5cb0b5#3400" rel="noopener noreferrer"&gt;The stack&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/c38bab5cb0b5#4580" rel="noopener noreferrer"&gt;Docker Compose — nine services&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/c38bab5cb0b5#c200" rel="noopener noreferrer"&gt;Node configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/c38bab5cb0b5#0240" rel="noopener noreferrer"&gt;The initialization ceremony (DKG)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/c38bab5cb0b5#3700" rel="noopener noreferrer"&gt;One master wallet, infinite users&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/c38bab5cb0b5#f138" rel="noopener noreferrer"&gt;The backup model&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/c38bab5cb0b5#ab00" rel="noopener noreferrer"&gt;The disaster-recovery drill&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/c38bab5cb0b5#f440" rel="noopener noreferrer"&gt;Deploying across separate hosts&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/c38bab5cb0b5#b000" rel="noopener noreferrer"&gt;Production hardening&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/c38bab5cb0b5#4180" rel="noopener noreferrer"&gt;Scaling — splitting hosts and adding nodes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/c38bab5cb0b5#d978" rel="noopener noreferrer"&gt;Troubleshooting cheat sheet&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/c38bab5cb0b5#a000" rel="noopener noreferrer"&gt;Build it so you don't need the luck&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Section 1: Architecture overview
&lt;/h2&gt;

&lt;p&gt;The whole point is one structural property: no single machine can produce a signature alone, and no machine ever holds the whole private key.&lt;/p&gt;

&lt;p&gt;The shape that delivers it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Three MPC nodes, each on its own isolated host, each holding one key share&lt;/li&gt;
&lt;li&gt;A 2-of-3 threshold: any two nodes can sign, one can be down or compromised without breaking anything&lt;/li&gt;
&lt;li&gt;A message bus for the nodes to exchange protocol messages&lt;/li&gt;
&lt;li&gt;A service-discovery / KV store for peer routing and wallet metadata&lt;/li&gt;
&lt;li&gt;A backend that holds zero keys and only ever sends a 32-byte digest&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The trust boundary is the security model. The recommended topology puts the three nodes on three separate hosts over a private encrypted network, so they fail independently. A local Docker Compose file mirrors the same wiring for development — same services, same connections — and the local layout maps straight onto the multi-host production one in Section 9.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Why not one box with three containers?&lt;/strong&gt; Because the isolation &lt;em&gt;is&lt;/em&gt; the security. Three containers share one kernel and one root, so host compromise = all three shares = the master key reconstructed. Co-located nodes give you the security of one machine, not true 2-of-3. There's an honest middle ground, because you won't always get three hosts on day one: run the nodes as separate users on a single memory-encrypted, attested VM, a Confidential VM (AMD SEV-SNP) or an enclave (AWS Nitro). Even host root can't read the nodes' memory there, and attestation proves the running image wasn't tampered with. It's a legitimate compensating control for a soft launch. Just be honest about what it is: it hardens the one box, it does not give you independent-failure 2-of-3. Treat multi-host as the target you graduate to.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Section 2: The stack
&lt;/h2&gt;

&lt;p&gt;I didn't want to build an MPC server from scratch. The cryptography is the easy part now; the hard part is the server around it, networking, retries, encrypted storage, backups, health checks, a client SDK. That's easily a quarter of an engineering budget before you sign your first transaction.&lt;/p&gt;

&lt;p&gt;So the stack sits on &lt;a href="https://github.com/fystack/mpcium" rel="noopener noreferrer"&gt;**Mpcium&lt;/a&gt;** (fystack), the most turnkey open-source MPC server there is, Apache-2.0, written in Go. It hands you a full t-of-n cluster out of the box:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mpcium — What it is: Open-source MPC server (Go) · Role here: Runs the nodes, the signing/keygen lifecycle, the SDK&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/fystack/tss-lib" rel="noopener noreferrer"&gt;tss-lib&lt;/a&gt;** (&lt;code&gt;fystack/tss-lib v2.0.3&lt;/code&gt;)** — What it is: Threshold-signature library (GG18/GG20) · Role here: The cryptographic core, backing Mpcium&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/dgraph-io/badger" rel="noopener noreferrer"&gt;BadgerDB&lt;/a&gt; — What it is: Embedded KV store, AES-256 encrypted · Role here: Each node's local key-share storage&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://docs.nats.io/nats-concepts/jetstream" rel="noopener noreferrer"&gt;NATS JetStream&lt;/a&gt; — What it is: Message bus · Role here: Carries keygen/signing request + result events&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.hashicorp.com/consul" rel="noopener noreferrer"&gt;Consul KV&lt;/a&gt; — What it is: Service discovery + KV · Role here: Peer routing and wallet metadata&lt;/li&gt;
&lt;li&gt;PostgreSQL — What it is: Relational DB · Role here: User rows, including each user's derivation index&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Out of the box you also get ECDSA (secp256k1) + EdDSA (Ed25519), Ed25519 mutual auth on every message, ECDH-encrypted peer channels, incremental encrypted backups, and a TypeScript client (&lt;code&gt;[@fystack/mpcium-ts&lt;/code&gt;](&lt;a href="https://www.npmjs.com/package/@fystack/mpcium-ts)" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/@fystack/mpcium-ts)&lt;/a&gt;) that drops into a NestJS backend.&lt;/p&gt;

&lt;p&gt;Two things it deliberately doesn't have, and how we deal with them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No trusted-dealer key import.&lt;/strong&gt; You can't import an existing private key. Section 6 makes that a non-issue by deriving all user wallets from one generated master instead of importing anything.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No REST/gRPC API&lt;/strong&gt;, it's NATS pub/sub + SDK only. Fine for a backend already on a message bus; wrap it if you need HTTP.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One thing worth doing before you trust any of this. Mpcium runs GG18/GG20, the family with the &lt;a href="https://www.fireblocks.com/blog/gg18-and-gg20-paillier-key-vulnerability-technical-report" rel="noopener noreferrer"&gt;*BitForge&lt;/a&gt;* vulnerability class from the last article. It's acceptable here for two reasons. BitForge needs a malicious &lt;em&gt;signing party&lt;/em&gt;, which is irrelevant when you run all three nodes. And more importantly, I checked the patch is actually in the code path. The tss-lib v2 line carries the two hardening proofs, Paillier-Blum in &lt;code&gt;crypto/modproof/&lt;/code&gt; and no-small-factor in &lt;code&gt;crypto/facproof/&lt;/code&gt;, generated in GG18 keygen round 2 and verified in round 3. Mpcium pins the &lt;code&gt;fystack/tss-lib v2.0.3&lt;/code&gt; fork through a &lt;code&gt;go.mod&lt;/code&gt; replace, so I grepped its whole tree for the flags that would disable those proofs (&lt;code&gt;SetNoProofMod&lt;/code&gt;, &lt;code&gt;SetNoProofFac&lt;/code&gt;) and found them called nowhere in the keygen path, only in the library's own tests. That's the level of due diligence a signing stack earns: don't trust the protocol name, read the code path.&lt;/p&gt;

&lt;p&gt;(Starting fresh and want the newest protocol? &lt;code&gt;[cggmp24&lt;/code&gt;](&lt;a href="https://github.com/LFDT-Lockness/cggmp21" rel="noopener noreferrer"&gt;https://github.com/LFDT-Lockness/cggmp21&lt;/a&gt;) (the &lt;code&gt;cggmp21&lt;/code&gt; crate, which now implements CGGMP24 — patched, &lt;code&gt;v0.7.0-alpha.2+&lt;/code&gt;) and Coinbase's Cure53-audited &lt;code&gt;[cb-mpc&lt;/code&gt;](&lt;a href="https://github.com/coinbase/cb-mpc" rel="noopener noreferrer"&gt;https://github.com/coinbase/cb-mpc&lt;/a&gt;) are the forward-looking cores. But they're libraries, not servers, so you're back to building the server yourself.)&lt;/p&gt;
&lt;h2&gt;
  
  
  Section 3: Docker Compose — nine services
&lt;/h2&gt;

&lt;p&gt;The full stack is nine services, eight long-running plus a one-shot bootstrap. Here's how they fit, and the two traps that will bite you.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;**app&lt;/code&gt;** — Image / build: your backend (NestJS) · Role: Custodial backend + MPC client. Starts last.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;**postgres&lt;/code&gt;** — Image / build: &lt;code&gt;postgres:15-alpine&lt;/code&gt; · Role: User rows, including each user's derivation index&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;**redis&lt;/code&gt;** — Image / build: &lt;code&gt;redis:7-alpine&lt;/code&gt; · Role: Cache (not part of MPC state)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;**nats&lt;/code&gt;** — Image / build: &lt;code&gt;nats:2.10-alpine&lt;/code&gt; (&lt;code&gt;-js&lt;/code&gt;) · Role: Message bus (JetStream enabled)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;**consul&lt;/code&gt;** — Image / build: &lt;code&gt;hashicorp/consul:1.15&lt;/code&gt; · Role: Service discovery + routing KV&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;**mpcium-bootstrap&lt;/code&gt;** — Image / build: &lt;code&gt;hashicorp/consul:1.15&lt;/code&gt; · Role: One-shot: seeds peer IDs into Consul, then exits&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;**mpcium-node0/1/2&lt;/code&gt;** — Image / build: your Mpcium node image · Role: The three MPC nodes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb415efx22k623lic09sz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb415efx22k623lic09sz.png" alt=" raw `depends_on` endraw  enforces the order: Consul healthy → the one-shot bootstrap completes → nodes start → the backend connects last." width="800" height="366"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;&lt;code&gt;depends_on&lt;/code&gt; enforces the order: Consul healthy → the one-shot bootstrap completes → nodes start → the backend connects last.&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  The bootstrap pattern
&lt;/h3&gt;

&lt;p&gt;The nodes need to know about each other before they start. Rather than bake that in, a one-shot &lt;code&gt;mpcium-bootstrap&lt;/code&gt; container writes the three peer UUIDs into Consul and exits &lt;code&gt;0&lt;/code&gt;. Every node then depends on it with &lt;code&gt;service_completed_successfully&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;mpcium-bootstrap&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;                 &lt;span class="c1"&gt;# one-shot: seed peer UUIDs into Consul, then exit 0&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;hashicorp/consul:1.15&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;CONSUL_HTTP_ADDR=consul&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;8500&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
    &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;sh&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;-c&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
        &lt;span class="s"&gt;consul kv put mpc_peers/node0 &amp;lt;node0-uuid&amp;gt; &amp;amp;&amp;amp;&lt;/span&gt;
        &lt;span class="s"&gt;consul kv put mpc_peers/node1 &amp;lt;node1-uuid&amp;gt; &amp;amp;&amp;amp;&lt;/span&gt;
        &lt;span class="s"&gt;consul kv put mpc_peers/node2 &amp;lt;node2-uuid&amp;gt;&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;consul&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;condition&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;service_healthy&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt;
    &lt;span class="na"&gt;restart&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;no"&lt;/span&gt;

  &lt;span class="na"&gt;mpcium-node0&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;./mpc/node&lt;/span&gt;                &lt;span class="c1"&gt;# distroless image, runs as UID 65532&lt;/span&gt;
    &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;start"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-n"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;node0"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
    &lt;span class="na"&gt;working_dir&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/cluster/node0&lt;/span&gt;      &lt;span class="c1"&gt;# mpcium reads identity/ + peers.json CWD-relative&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./mpc/node0:/cluster/node0&lt;/span&gt;                              &lt;span class="c1"&gt;# host dir: chown 65532:65532&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./mpc/config/node0.yaml:/cluster/node0/config.yaml:ro&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;mpcium-bootstrap&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;condition&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;service_completed_successfully&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt;
      &lt;span class="na"&gt;nats&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;   &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;condition&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;service_started&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt;
      &lt;span class="na"&gt;consul&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;condition&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;service_healthy&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt;
    &lt;span class="na"&gt;restart&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;unless-stopped&lt;/span&gt;
  &lt;span class="c1"&gt;# node1, node2 are identical. The `app` service depends on all three and starts LAST.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few load-bearing lines in there:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;restart: "no"&lt;/code&gt; on &lt;code&gt;mpcium-bootstrap&lt;/code&gt; — it's a one-shot: seed the peer IDs, exit &lt;code&gt;0&lt;/code&gt;, done. Restart it and it re-seeds on every boot.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;condition: service_completed_successfully&lt;/code&gt; — the nodes block until that clean exit, so peers always exist before a node reads them.&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;:ro&lt;/code&gt; config mount — each node's &lt;code&gt;config.yaml&lt;/code&gt; is read-only; nothing at runtime should mutate it.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;working_dir&lt;/code&gt; and the volume's owner are both load-bearing enough to be their own traps, below.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;(As of v0.3.5 there's a lighter option: pass &lt;code&gt;--peers=peers.json&lt;/code&gt; to each node and it syncs any new peers into Consul on startup, skipping ones already there — so you can drop the &lt;code&gt;mpcium-bootstrap&lt;/code&gt; container entirely, or run &lt;code&gt;mpcium-cli register-peers --peers peers.json&lt;/code&gt; once. The manual &lt;code&gt;consul kv put mpc_peers/…&lt;/code&gt; still works; it's just no longer required.)&lt;/p&gt;

&lt;h3&gt;
  
  
  Trap #1 — the distroless UID
&lt;/h3&gt;

&lt;p&gt;The Mpcium node image is &lt;code&gt;[FROM gcr.io/distroless/base-debian12&lt;/code&gt;](&lt;a href="https://github.com/GoogleContainerTools/distroless" rel="noopener noreferrer"&gt;https://github.com/GoogleContainerTools/distroless&lt;/a&gt;) with &lt;code&gt;USER nonroot:nonroot&lt;/code&gt;, which is UID/GID 65532. Your host-mounted node dirs are owned by your user, so the moment a node tries to write its BadgerDB it hits permission-denied and crash-loops. One line fixes it, and you'll forget it exactly once:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;chown&lt;/span&gt; &lt;span class="nt"&gt;-R&lt;/span&gt; 65532:65532 ./mpc/node0 ./mpc/node1 ./mpc/node2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Trap #2 — &lt;code&gt;working_dir&lt;/code&gt; is load-bearing
&lt;/h3&gt;

&lt;p&gt;Mpcium resolves &lt;code&gt;identity/&lt;/code&gt; and &lt;code&gt;peers.json&lt;/code&gt; relative to the current working directory, not to a config path. Miss &lt;code&gt;working_dir&lt;/code&gt; (or &lt;code&gt;WorkingDirectory&lt;/code&gt; in a systemd unit) and the node starts in &lt;code&gt;/&lt;/code&gt; and can't find its identity files. Pin it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Section 4: Node configuration
&lt;/h2&gt;

&lt;p&gt;Each node gets its own &lt;code&gt;config.yaml&lt;/code&gt;. Most of it is boring. Two fields will bite you.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;nats&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;   &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;url&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;nats://nats:4222"&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt;
&lt;span class="na"&gt;consul&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;address&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;consul:8500"&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt;

&lt;span class="na"&gt;mpc_threshold&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;                   &lt;span class="c1"&gt;# tss-lib degree t -&amp;gt; t+1 = 2 signers (this is 2-of-3)&lt;/span&gt;
&lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;production"&lt;/span&gt;

&lt;span class="na"&gt;badger_password&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;&amp;lt;32&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;hex&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;chars&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;=&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;32-byte&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;AES-256&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;key,&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;DIFFERENT&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;per&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;node&amp;gt;"&lt;/span&gt;
&lt;span class="na"&gt;event_initiator_algorithm&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ed25519"&lt;/span&gt;
&lt;span class="na"&gt;event_initiator_pubkey&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;&amp;lt;64&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;hex,&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;SAME&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;on&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;all&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;nodes&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;+&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;backend&amp;gt;"&lt;/span&gt;
&lt;span class="na"&gt;chain_code&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;              &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;&amp;lt;64&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;hex,&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;SAME&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;on&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;all&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;nodes&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;+&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;backend&amp;gt;"&lt;/span&gt;

&lt;span class="na"&gt;db_path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;./data"&lt;/span&gt;                  &lt;span class="c1"&gt;# mpcium appends /nodeN — remember this in Section 8&lt;/span&gt;
&lt;span class="na"&gt;backup_enabled&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="na"&gt;backup_period_seconds&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300&lt;/span&gt;
&lt;span class="na"&gt;max_concurrent_keygen&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;
&lt;span class="na"&gt;max_concurrent_signing&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What the important fields do:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;mpc_threshold&lt;/code&gt; — the availability/security knob; &lt;code&gt;1&lt;/code&gt; means 2-of-3 (see the gotcha below).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;db_path&lt;/code&gt; — where the shards live; Mpcium appends &lt;code&gt;/nodeN&lt;/code&gt; to it, which will matter enormously at restore time (Section 8).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;backup_enabled&lt;/code&gt; / &lt;code&gt;backup_period_seconds&lt;/code&gt; — Mpcium's own incremental encrypted backup, every 300 s.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;max_concurrent_keygen&lt;/code&gt; / &lt;code&gt;max_concurrent_signing&lt;/code&gt; — per-node concurrency caps.&lt;/li&gt;
&lt;li&gt;the three identity fields (&lt;code&gt;badger_password&lt;/code&gt;, &lt;code&gt;event_initiator_pubkey&lt;/code&gt;, &lt;code&gt;chain_code&lt;/code&gt;) have a shared-vs-per-node rule of their own, right below.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The threshold gotcha (read this twice)
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;In tss-lib, &lt;code&gt;mpc_threshold&lt;/code&gt; is the polynomial degree &lt;code&gt;t&lt;/code&gt;, and the number of signers required is &lt;code&gt;t + 1&lt;/code&gt;. So a 2-of-3 cluster uses &lt;code&gt;mpc_threshold: 1&lt;/code&gt;, not &lt;code&gt;2&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And here's the part that makes it dangerous: the Mpcium dev &lt;code&gt;config.yaml.template&lt;/code&gt; ships with &lt;code&gt;mpc_threshold: 2&lt;/code&gt; by default (the &lt;code&gt;config.prod.yaml.template&lt;/code&gt; correctly ships &lt;code&gt;1&lt;/code&gt; — but the dev one is what most people copy first). Copy it unthinkingly and you've built a 3-of-3 cluster, which throws away the entire availability benefit of MPC, because now &lt;em&gt;every&lt;/em&gt; node must be online to sign. Set it to &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;One more thing: tss-lib only enforces that the degree is at least 1 and no larger than the node count minus one. For three nodes that leaves 2-of-3 (&lt;code&gt;mpc_threshold: 1&lt;/code&gt;) or 3-of-3 (&lt;code&gt;mpc_threshold: 2&lt;/code&gt;) — a 1-of-3 where a single node could sign alone is degenerate and rejected. (GG18 tolerates a dishonest majority, so the code allows any &lt;code&gt;1 ≤ t ≤ n−1&lt;/code&gt;; Mpcium's README still &lt;em&gt;recommends&lt;/em&gt; an honest-majority &lt;code&gt;t ≥ ⌊n/2⌋+1&lt;/code&gt;, and for three nodes there's no sensible quorum below 2-of-3 anyway.)&lt;/p&gt;

&lt;h3&gt;
  
  
  Shared vs. per-node
&lt;/h3&gt;

&lt;p&gt;This catches people, so let me be blunt:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Different on every node:&lt;/strong&gt; &lt;code&gt;badger_password&lt;/code&gt; (each node encrypts its own BadgerDB with its own key).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Identical on every node and the backend:&lt;/strong&gt; &lt;code&gt;chain_code&lt;/code&gt;, &lt;code&gt;event_initiator_pubkey&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;event_initiator_pubkey&lt;/code&gt; pins who's allowed to ask the cluster to sign, the Ed25519 public key of your backend's initiator identity. &lt;code&gt;chain_code&lt;/code&gt; is what makes deterministic derivation work (Section 6).&lt;/p&gt;

&lt;h3&gt;
  
  
  Identities and peers
&lt;/h3&gt;

&lt;p&gt;Each node dir has an &lt;code&gt;identity/&lt;/code&gt; folder with its own private key (&lt;code&gt;node0_private.key&lt;/code&gt;, mode &lt;code&gt;0600&lt;/code&gt;) plus every node's public identity (&lt;code&gt;nodeX_identity.json&lt;/code&gt;, mode &lt;code&gt;0644&lt;/code&gt;). And every node carries the same &lt;code&gt;peers.json&lt;/code&gt;, whose UUIDs must exactly match what &lt;code&gt;mpcium-bootstrap&lt;/code&gt; seeded into Consul:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"node0"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"00000000-0000-0000-0000-000000000000"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"node1"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"11111111-1111-1111-1111-111111111111"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"node2"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"22222222-2222-2222-2222-222222222222"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Section 5: The initialization ceremony (DKG)
&lt;/h2&gt;

&lt;p&gt;This is the ceremony that generates the master key without ever assembling it. It runs once, on a fresh cluster, with the backend stopped.&lt;/p&gt;

&lt;h3&gt;
  
  
  The one rule that governs everything: startup order
&lt;/h3&gt;

&lt;p&gt;The single most important operational rule in this whole deployment:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The MPC nodes must create the JetStream streams before anything else connects to NATS.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here's why. The Go nodes create their &lt;code&gt;mpc-keygen&lt;/code&gt; and &lt;code&gt;mpc-signing&lt;/code&gt; streams with JetStream's &lt;strong&gt;default (limits)&lt;/strong&gt; retention, but the TypeScript client (&lt;code&gt;@fystack/mpcium-ts&lt;/code&gt;) creates those same streams with &lt;strong&gt;workqueue&lt;/strong&gt; retention (and the &lt;code&gt;mpc&lt;/code&gt; result stream the other way round — &lt;code&gt;interest&lt;/code&gt; on the client, &lt;code&gt;workqueue&lt;/code&gt; on the nodes). If the client wins the race and creates either one first, the nodes come up, refuse to reconcile the retention, and immediately crash-loop with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;err_code=10052 ... can not change retention policy to/from workqueue
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And JetStream won't let you add or remove &lt;code&gt;workqueue&lt;/code&gt; retention on an existing stream, so no config change or API call fixes it in place. The simplest remedy is to recreate the NATS container (or drop just the two offending streams and let the nodes recreate them), either way rebuilding JetStream state you never planned to touch. So the sequence is non-negotiable:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fx0ofdkcn88r576vgfdb8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fx0ofdkcn88r576vgfdb8.png" alt="Get this order wrong and JetStream's fixed workqueue retention forces you to recreate NATS from scratch." width="799" height="374"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Get this order wrong and JetStream's fixed workqueue retention forces you to recreate NATS from scratch.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# 1. Bring up infra + the three nodes (NOT the app yet)&lt;/span&gt;
docker compose up &lt;span class="nt"&gt;-d&lt;/span&gt; postgres redis nats consul &lt;span class="se"&gt;\&lt;/span&gt;
  mpcium-bootstrap mpcium-node0 mpcium-node1 mpcium-node2

&lt;span class="c"&gt;# 2. Wait until each node logs that it owns its streams, e.g.:&lt;/span&gt;
&lt;span class="c"&gt;#    "SigningConsumer: Subscribed to signing events"&lt;/span&gt;
&lt;span class="c"&gt;#    "Subscription created successfully"&lt;/span&gt;
docker compose logs &lt;span class="nt"&gt;-f&lt;/span&gt; mpcium-node0   &lt;span class="c"&gt;# watch for the stream-subscription logs above&lt;/span&gt;

&lt;span class="c"&gt;# 3. ONLY NOW run the init ceremony / start the app&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's a sneakier version of this trap too: a running backend steals the DKG result. If the app is up during init, its consumer grabs the keygen-result event off the queue before the init script can read it, and init hangs forever on "waiting for keygen result." Stop the app before you initialize.&lt;/p&gt;

&lt;h3&gt;
  
  
  What the init script actually does
&lt;/h3&gt;

&lt;p&gt;The init is a one-time bootstrap script. Don't underestimate it, ours is 1,160 lines, and that's normal. A one-time bootstrap for a distributed system is never a ten-liner. Four phases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Validate&lt;/strong&gt; — the compose file, every node config, the initiator key, the backend &lt;code&gt;.env&lt;/code&gt;, and that all three nodes plus NATS and Consul are actually running.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DKG the master wallet&lt;/strong&gt; — connect to NATS, create the Mpcium client, call &lt;code&gt;createWallet(masterWalletId)&lt;/code&gt;, wait for the keygen-result event (60-second timeout). Take the ECDSA public key, compressed to secp256k1.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Write env + back up&lt;/strong&gt; — persist &lt;code&gt;MPC_MASTER_WALLET_ID&lt;/code&gt;, &lt;code&gt;MPC_MASTER_PUBLIC_KEY&lt;/code&gt;, and &lt;code&gt;MPC_CHAIN_CODE&lt;/code&gt; to the backend's &lt;code&gt;.env&lt;/code&gt;; then copy each node's data dir, export the Consul KV, write a &lt;code&gt;metadata.json&lt;/code&gt; (&lt;code&gt;nodeCount: 3&lt;/code&gt;, &lt;code&gt;threshold: "2-of-3"&lt;/code&gt;). Move this backup offline immediately, it's hot-key-equivalent material.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Verify&lt;/strong&gt; — derive addresses at indexes 0 and 1, sign a test 32-byte digest at each (30-second timeout, up to four attempts on transient errors), print &lt;code&gt;=== INITIALIZATION COMPLETE ===&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Timeouts and the slow part of boot
&lt;/h3&gt;

&lt;p&gt;Keygen needs all three nodes at once, so give it the most generous timeout (we use 60 s for keygen, 30 s for signing) — a transient hiccup shouldn't abort a ceremony. The one real cost at boot is Paillier preparameter generation, the bulk of a cold start. Given enough cores the three nodes generate their preparams in parallel; on a smaller host they contend for CPU, so start them serially to avoid starving each other. Either way you pay it once: the preparams are persisted to BadgerDB and reused on every restart, so restarts are cheap.&lt;/p&gt;

&lt;h2&gt;
  
  
  Section 6: One master wallet, infinite users
&lt;/h2&gt;

&lt;p&gt;Give every user their own MPC wallet the obvious way, and every signup runs its own DKG across all three nodes — and your backups grow without bound: ten thousand users is ten thousand ceremonies and ten thousand more sets of shards you can never lose.&lt;/p&gt;

&lt;p&gt;We don't do that. We run DKG exactly once, for a single master wallet, and derive every user wallet from it with BIP-32 child key derivation (CKD):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each user gets an integer &lt;code&gt;wallet_index&lt;/code&gt;, stored as a column in Postgres&lt;/li&gt;
&lt;li&gt;Their address is derived at path &lt;code&gt;m/44/60/0/0/{wallet_index}&lt;/code&gt; (all levels non-hardened — hardened derivation needs the private key, which never exists here) — pure math, zero MPC nodes required online&lt;/li&gt;
&lt;li&gt;To sign, each node applies its own derivation delta to its master share; the child key, like the master, is never assembled&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is what makes the operational model sane, and it's the reason the backup story in the next section is so simple. One derivation path supports about 2.1 billion users (indexes 0 through 2³¹ − 1), so you'll run out of users long before you run out of key space. I covered CKD's security equivalence in the previous article; the short version is that it's &lt;em&gt;identical&lt;/em&gt; to per-user DKG under every node-compromise scenario.&lt;/p&gt;

&lt;h2&gt;
  
  
  Section 7: The backup model — back up three things, not one
&lt;/h2&gt;

&lt;p&gt;The most common way to lose an MPC cluster is to back up the key shares, feel safe, and find out mid-restore that shares alone are useless. You have to back up three things:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fz17kuz54rm7mj3be0njh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fz17kuz54rm7mj3be0njh.png" alt="Restore only BadgerDB and the cluster boots but can't find any wallet — signing fails with ERROR_KEY_NOT_FOUND." width="799" height="374"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Restore only BadgerDB and the cluster boots but can't find any wallet — signing fails with ERROR_KEY_NOT_FOUND.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;1&lt;/strong&gt; — Item: BadgerDB shards · Where: each node's &lt;code&gt;data/&lt;/code&gt; dir · Why it's mandatory: the encrypted key-share state&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;2&lt;/strong&gt; — Item: Consul KV (&lt;code&gt;threshold_keyinfo/*&lt;/code&gt;, &lt;code&gt;mpc_peers/*&lt;/code&gt;) · Where: Consul export · Why it's mandatory: wallet routing; without it every sign fails &lt;code&gt;ERROR_KEY_NOT_FOUND&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;3&lt;/strong&gt; — Item: Metadata (&lt;code&gt;chain_code&lt;/code&gt;, master wallet ID, master pubkey, node configs, identity keys) · Where: configs + backend &lt;code&gt;.env&lt;/code&gt; · Why it's mandatory: needed to derive addresses and boot the nodes&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  One backup, forever
&lt;/h3&gt;

&lt;p&gt;Here's the quiet superpower of CKD. The master shares, the &lt;code&gt;threshold_keyinfo/*&lt;/code&gt; routing entries, and the peer metadata never change after the initial DKG. New users are just new rows in Postgres. So your MPC backup strategy is: take one full MPC backup right after init, then back up Postgres normally from then on. That's it. No incremental shard sync, no window where a user created between backups is unrecoverable. Per-user DKG would need a fresh MPC backup after &lt;em&gt;every&lt;/em&gt; signup.&lt;/p&gt;

&lt;p&gt;And this is the whole disaster-recovery story in one sentence: lose the entire cluster and you still lose nothing. Stand up fresh nodes, restore that single encrypted master backup, and every user wallet comes back, because no user wallet was ever stored on its own. Each one is a deterministic derivation of the master, and its &lt;code&gt;wallet_index&lt;/code&gt; lives safely in your Postgres rows. The master backup plus your normal Postgres backups reconstruct every address and every signing key by index, on brand-new hardware, with nothing lost.&lt;/p&gt;
&lt;h3&gt;
  
  
  The copy-size trap
&lt;/h3&gt;

&lt;p&gt;How you copy BadgerDB matters more than you'd think. A &lt;em&gt;running&lt;/em&gt; node's BadgerDB is ~2.2 GB — SSTables, value logs, and the WAL, mostly churn. What you actually ship is Mpcium's own cold backup: with &lt;code&gt;backup_enabled: true&lt;/code&gt;, each node writes a pruned, encrypted export to &lt;code&gt;backup_dir&lt;/code&gt; (the &lt;code&gt;backup-*.enc&lt;/code&gt; files), a few tens of KB apiece. In the drill the full &lt;code&gt;age&lt;/code&gt;-wrapped bundle — all three nodes' exports plus every config, identity, and the Consul KV dump — came to &lt;strong&gt;164 KB&lt;/strong&gt;, four orders of magnitude below the live directory. Two consequences: never copy a live BadgerDB (and never stage a hot copy in a small &lt;code&gt;tmpfs&lt;/code&gt; — it can exceed 6 GB and blow out a ~2 GB &lt;code&gt;/tmp&lt;/code&gt;); ship the &lt;code&gt;backup_dir&lt;/code&gt; export instead.&lt;/p&gt;
&lt;h3&gt;
  
  
  Restore is a specific sequence
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# 1. Start fresh nodes, then STOP them&lt;/span&gt;
docker compose up &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; docker compose stop mpcium-node0 mpcium-node1 mpcium-node2
&lt;span class="c"&gt;# 2. For each node, decrypt + restore its export INTO the exact db_path/nodeN dir:&lt;/span&gt;
&lt;span class="c"&gt;#    mpcium-cli recover --backup-dir &amp;lt;node's backup-*.enc&amp;gt; \&lt;/span&gt;
&lt;span class="c"&gt;#      --recovery-path &amp;lt;db_path&amp;gt;/nodeN --force   # the nodeN subdir is load-bearing — Section 8&lt;/span&gt;
&lt;span class="c"&gt;#    (recover prompts for the badger_password; drive it with `expect` in automation)&lt;/span&gt;
&lt;span class="c"&gt;# 3. Import the Consul KV dump  (skip this and signing fails ERROR_KEY_NOT_FOUND)&lt;/span&gt;
consul kv import @backup/consul-kv.json
&lt;span class="c"&gt;# 4. Fix ownership, delete stale BadgerDB LOCK files&lt;/span&gt;
&lt;span class="nb"&gt;chown&lt;/span&gt; &lt;span class="nt"&gt;-R&lt;/span&gt; 65532:65532 ./mpc &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; find ./mpc &lt;span class="nt"&gt;-name&lt;/span&gt; LOCK &lt;span class="nt"&gt;-delete&lt;/span&gt;
&lt;span class="c"&gt;# 5. Start the nodes, wait for all three to log [READY]&lt;/span&gt;
docker compose start mpcium-node0 mpcium-node1 mpcium-node2
&lt;span class="c"&gt;# 6. Verify with a signing smoke test — do NOT trust "[READY]" alone (Section 8)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Section 8: The disaster-recovery drill
&lt;/h2&gt;

&lt;p&gt;You don't have a backup until you've restored it. So before go-live we ran the full thing: destroy the cluster, rebuild it from backups on fresh hardware, and prove the recovered cluster signs with the same master key. The pass criterion was strict, recover a signature and confirm via &lt;code&gt;ecrecover&lt;/code&gt; that its master public key was byte-for-byte identical to the original.&lt;/p&gt;

&lt;p&gt;It passed. It also surfaced eleven runbook and config bugs in a single session. A clean re-run took 45–60 minutes; the first pass took about two hours, precisely because of what it caught. Three of them are worth the price of this whole article.&lt;/p&gt;
&lt;h3&gt;
  
  
  The silent empty-database restore (the one from the opening)
&lt;/h3&gt;

&lt;p&gt;This is the bug that would have quietly destroyed every user wallet in a real emergency.&lt;/p&gt;

&lt;p&gt;Mpcium reads BadgerDB from &lt;code&gt;${db_path}/nodeN/&lt;/code&gt;, it auto-appends the &lt;code&gt;nodeN&lt;/code&gt; subdirectory. Drop your restore into &lt;code&gt;.../data&lt;/code&gt; instead of &lt;code&gt;.../data/nodeN&lt;/code&gt; and the files land one level too high. Mpcium then looks in &lt;code&gt;.../data/nodeN/&lt;/code&gt;, finds it empty, and, this is the dangerous part, silently creates a brand-new, empty database.&lt;/p&gt;

&lt;p&gt;The cluster boots. All three nodes report &lt;code&gt;[READY]&lt;/code&gt;. Health checks are green. But the shares are gone — every existing wallet is orphaned, and the first real signing request fails with &lt;code&gt;ERROR_KEY_NOT_FOUND&lt;/code&gt; (newer v0.3.5 builds report the same thing as &lt;code&gt;ERROR_NOT_MAJORITY&lt;/code&gt;, &lt;em&gt;"not enough peers to process signing request"&lt;/em&gt;). Worse, the empty cluster will happily run a fresh DKG and mint a brand-new master key that has nothing to do with your users' funds. You find out on that first signature, long after the green health check lied to you.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8nchweegldlm9w0ukdrv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8nchweegldlm9w0ukdrv.png" alt="" width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;"[READY]" is not "recovered." Restore into the exact &lt;code&gt;db_path/nodeN&lt;/code&gt; subdirectory, then verify by signing, never by status alone.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The drill burned about 30 minutes on exactly this. The fix is trivial once you know it, restore into the explicit &lt;code&gt;.../data/nodeN&lt;/code&gt; subdir and &lt;code&gt;chown&lt;/code&gt; afterward, but you'd never guess it from a green health check. The lesson generalizes: &lt;code&gt;[READY]&lt;/code&gt; means "a database is present," not "the &lt;em&gt;right&lt;/em&gt; database is present." Verify recovery by signing and checking the recovered public key, never by status.&lt;/p&gt;
&lt;h3&gt;
  
  
  The documented command that didn't exist
&lt;/h3&gt;

&lt;p&gt;The first-draft runbook confidently referenced an &lt;code&gt;mpcium-cli init-dkg&lt;/code&gt; command that doesn't exist in the shipped binary, with a flag set to match. There is no keygen subcommand at all: DKG is triggered by publishing a signed keygen-request message onto NATS (subject &lt;code&gt;mpc.keygen_request.*&lt;/code&gt;). Recovery really uses &lt;code&gt;--backup-dir&lt;/code&gt;, &lt;code&gt;--recovery-path&lt;/code&gt;, and &lt;code&gt;--force&lt;/code&gt;, and the binary prompts for its password interactively via &lt;code&gt;term.ReadPassword&lt;/code&gt;, so you drive it with &lt;code&gt;expect&lt;/code&gt; in automation. Dry-run every command in your runbook against the actual binary before you trust it in an emergency.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;openssl rand&lt;/code&gt; will hand you an invalid key
&lt;/h3&gt;

&lt;p&gt;BadgerDB's encryption key must be exactly 16, 24, or 32 bytes. The instinctive command produces neither:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;openssl rand &lt;span class="nt"&gt;-base64&lt;/span&gt; 32     &lt;span class="c"&gt;# WRONG: 44 ASCII chars -&amp;gt; node fatals opening the encrypted BadgerDB:&lt;/span&gt;
                            &lt;span class="c"&gt;# the key must be exactly 16, 24, or 32 bytes.&lt;/span&gt;
openssl rand &lt;span class="nt"&gt;-hex&lt;/span&gt; 16        &lt;span class="c"&gt;# RIGHT: 32 hex characters = 32 bytes = AES-256&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;-base64 32&lt;/code&gt; turns 32 random &lt;em&gt;bytes&lt;/em&gt; into a 44-&lt;em&gt;character&lt;/em&gt; string, and the password is used as raw key bytes — so that's a 44-byte key, not a valid AES length. &lt;code&gt;-hex 16&lt;/code&gt; gives 32 characters = 32 bytes: an AES-256-length key carrying 128 bits of entropy, ample for a local DB key.&lt;/p&gt;

&lt;h3&gt;
  
  
  Five more the drill taught us
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Never regenerate &lt;code&gt;badger_password&lt;/code&gt; after first start.&lt;/strong&gt; The KEYREGISTRY is encrypted with the &lt;em&gt;original&lt;/em&gt; password; change it and the database is permanently unreadable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A wrong or regenerated &lt;code&gt;chain_code&lt;/code&gt; silently orphans every wallet.&lt;/strong&gt; The cluster boots and signs perfectly happily, but it now derives a completely different address space, so none of your users' existing addresses come back. Like the empty-DB bug, it sails past &lt;code&gt;[READY]&lt;/code&gt; and only a sign-and-compare catches it. Treat &lt;code&gt;chain_code&lt;/code&gt; as recovery-critical metadata, not a rotatable secret.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Regenerating node identity keys or peer UUIDs is a re-key, not a recovery.&lt;/strong&gt; Fresh identities make the restored shares reject each other, forcing a brand-new DKG, which produces a &lt;em&gt;new&lt;/em&gt; master public key and abandons every address derived from the old one. Same limit if a node is permanently lost: you can't just drop in a replacement, because a reshare needs the surviving shares as input. Mpcium ships that as &lt;code&gt;Resharing()&lt;/code&gt; (Section 11) — it rebuilds the replacement from the survivors while keeping the master key. A recovered 2-of-3 cluster missing its third node has zero remaining fault tolerance until you re-share, so plan node replacement rather than improvising it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Consul KV doesn't contain the master public key.&lt;/strong&gt; It stores only &lt;code&gt;{participant_peer_ids, threshold, version}&lt;/code&gt;. So you can't verify a restore by reading KV, you sign and &lt;code&gt;ecrecover&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Your cold bundle must include the supporting config&lt;/strong&gt; (&lt;code&gt;nats.conf&lt;/code&gt;, CA templates), or the restored NATS/services crash-loop and you never reach the signing test.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Section 9: Deploying across separate hosts
&lt;/h2&gt;

&lt;p&gt;Everything so far ran from one Docker Compose file — one host, one kernel, one root. That's the right way to &lt;em&gt;learn&lt;/em&gt; the stack, and fine while you're testing, but it isn't 2-of-3: own the box and you own all three shares. Production means each node on its own machine, so a single host compromise costs the attacker exactly one share. None of the cryptography changes — same three nodes, same DKG, same backups. What changes is the wiring: instead of a Compose network, the nodes live on separate hosts joined by a private encrypted overlay, and they reach the bus over that overlay instead of a Docker bridge.&lt;/p&gt;

&lt;h3&gt;
  
  
  The shape
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Three node hosts&lt;/strong&gt; — one Mpcium node each. Ideally memory-encrypted, attested VMs (AMD SEV-SNP, AWS Nitro), each with &lt;strong&gt;no public IP&lt;/strong&gt; and a scoped machine identity. Run the node as its own locked-down user with a &lt;code&gt;0700&lt;/code&gt; data dir, so a compromised node process still can't read another node's BadgerDB.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One control host&lt;/strong&gt; — NATS + Consul (and a small internal CA, below). The bus and the KV store live here; every node and the backend point at it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The backend&lt;/strong&gt; — sends digests, holds zero keys, also on the overlay.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The nodes never talk to each other directly — all protocol traffic goes through NATS. So the only reachability requirement is: every node and the backend can reach the control host's NATS and Consul over the private network. That keeps the network model a simple star, not a full mesh.&lt;/p&gt;

&lt;h3&gt;
  
  
  The private overlay (WireGuard)
&lt;/h3&gt;

&lt;p&gt;Put every host on one &lt;a href="https://www.wireguard.com/" rel="noopener noreferrer"&gt;WireGuard&lt;/a&gt; network so nothing MPC-related ever touches a public interface. On each node host, one peer pointing at the control host:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight conf"&gt;&lt;code&gt;&lt;span class="c"&gt;# /etc/wireguard/wg0.conf  (a node host)
&lt;/span&gt;[&lt;span class="n"&gt;Interface&lt;/span&gt;]
&lt;span class="n"&gt;Address&lt;/span&gt;    = &lt;span class="m"&gt;10&lt;/span&gt;.&lt;span class="m"&gt;10&lt;/span&gt;.&lt;span class="m"&gt;0&lt;/span&gt;.&lt;span class="m"&gt;11&lt;/span&gt;/&lt;span class="m"&gt;24&lt;/span&gt;
&lt;span class="n"&gt;PrivateKey&lt;/span&gt; = &amp;lt;&lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="n"&gt;private&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&amp;gt;
&lt;span class="n"&gt;ListenPort&lt;/span&gt; = &lt;span class="m"&gt;51820&lt;/span&gt;
&lt;span class="n"&gt;MTU&lt;/span&gt;        = &lt;span class="m"&gt;1380&lt;/span&gt;          &lt;span class="c"&gt;# provider MTU minus WireGuard's 80-byte header
&lt;/span&gt;
[&lt;span class="n"&gt;Peer&lt;/span&gt;]                     &lt;span class="c"&gt;# control host (NATS + Consul)
&lt;/span&gt;&lt;span class="n"&gt;PublicKey&lt;/span&gt;           = &amp;lt;&lt;span class="n"&gt;control&lt;/span&gt; &lt;span class="n"&gt;host&lt;/span&gt; &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&amp;gt;
&lt;span class="n"&gt;AllowedIPs&lt;/span&gt;          = &lt;span class="m"&gt;10&lt;/span&gt;.&lt;span class="m"&gt;10&lt;/span&gt;.&lt;span class="m"&gt;0&lt;/span&gt;.&lt;span class="m"&gt;1&lt;/span&gt;/&lt;span class="m"&gt;32&lt;/span&gt;          &lt;span class="c"&gt;# just the control host — nodes reach each other via NATS, not directly
&lt;/span&gt;&lt;span class="n"&gt;Endpoint&lt;/span&gt;            = &amp;lt;&lt;span class="n"&gt;control&lt;/span&gt; &lt;span class="n"&gt;host&lt;/span&gt; &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;ip&lt;/span&gt;&amp;gt;:&lt;span class="m"&gt;51820&lt;/span&gt;
&lt;span class="n"&gt;PersistentKeepalive&lt;/span&gt; = &lt;span class="m"&gt;25&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The control host runs the mirror image — one &lt;code&gt;[Peer]&lt;/code&gt; block per node, each with that node's &lt;code&gt;/32&lt;/code&gt; — and forwards nothing else. Only public keys cross the wire; private keys never leave their host. Then lock the provider firewall so UDP/51820 is the &lt;em&gt;only&lt;/em&gt; inbound port and only from the overlay's peers, deny the rest, and bind SSH to the WG address instead of the public one. On a cloud MPC VM that means launching it with &lt;strong&gt;no external IP at all&lt;/strong&gt; and reaching it purely over WG.&lt;/p&gt;

&lt;h3&gt;
  
  
  Per-node config
&lt;/h3&gt;

&lt;p&gt;Each node's &lt;code&gt;config.yaml&lt;/code&gt; is identical to the single-host one, except the addresses now point at the control host over WG and the shared secrets stay shared:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;nats&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;   &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;url&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;nats://10.10.0.1:4222"&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt;   &lt;span class="c1"&gt;# control host, over WG&lt;/span&gt;
&lt;span class="na"&gt;consul&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;address&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;10.10.0.1:8500"&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt;
&lt;span class="na"&gt;mpc_threshold&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;                            &lt;span class="c1"&gt;# still 2-of-3&lt;/span&gt;
&lt;span class="na"&gt;badger_password&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;&amp;lt;per-node,&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;32&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;hex&amp;gt;"&lt;/span&gt;       &lt;span class="c1"&gt;# distinct per node&lt;/span&gt;
&lt;span class="na"&gt;chain_code&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;             &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;&amp;lt;same&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;on&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;all&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;nodes&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;+&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;backend&amp;gt;"&lt;/span&gt;
&lt;span class="na"&gt;event_initiator_pubkey&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;&amp;lt;same&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;on&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;all&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;nodes&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;+&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;backend&amp;gt;"&lt;/span&gt;
&lt;span class="na"&gt;db_path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/var/lib/mpcium/data"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lock the bus itself down with &lt;strong&gt;mTLS&lt;/strong&gt;: issue a client certificate per node from a small internal CA (&lt;a href="https://smallstep.com/docs/step-ca/" rel="noopener noreferrer"&gt;step-ca&lt;/a&gt; is plenty), so only your nodes and backend can speak to NATS — a stray connection that lands on the overlay still can't.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bringing it up
&lt;/h3&gt;

&lt;p&gt;The startup order from Section 5 still holds, just spread across hosts: bring up NATS + Consul on the control host, &lt;code&gt;register-peers&lt;/code&gt; (or start the nodes with &lt;code&gt;--peers&lt;/code&gt;) so the UUIDs land in Consul, start the three nodes on their own hosts (streams first — the retention trap doesn't care that the nodes are remote now), wait for &lt;code&gt;[READY]&lt;/code&gt; on all three, then run the DKG exactly as before. The backend connects last.&lt;/p&gt;

&lt;p&gt;This is the topology every backup, restore, and reshare in this article already assumes — the wiring is all that stood between you and it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Section 10: Production hardening
&lt;/h2&gt;

&lt;p&gt;Once the cluster works, the remaining work is isolation and discipline.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Split the nodes across hosts&lt;/strong&gt; — the from-scratch recipe is Section 9, migrating an existing single-box cluster is Section 11. Three separate machines mean a single host compromise yields at most one share; anything less is theater. Can't split yet? Fall back to a memory-encrypted attested VM (&lt;a href="https://www.amd.com/en/developer/sev.html" rel="noopener noreferrer"&gt;AMD SEV-SNP&lt;/a&gt;, &lt;a href="https://aws.amazon.com/ec2/nitro/nitro-enclaves/" rel="noopener noreferrer"&gt;AWS Nitro&lt;/a&gt;) — the Section 1 note covers exactly what that buys you and what it doesn't. One caveat that lands here: SEV-SNP VMs generally can't live-migrate, so an abrupt host termination becomes your disaster-recovery trigger — exactly why the drill in Section 8 isn't optional.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Private encrypted network.&lt;/strong&gt; Put inter-node and node-to-backend traffic on a private mesh (&lt;a href="https://www.wireguard.com/" rel="noopener noreferrer"&gt;WireGuard&lt;/a&gt;). The message bus and KV store should never be exposed publicly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Egress firewall.&lt;/strong&gt; MPC nodes have no business making arbitrary outbound connections. Lock egress to what the protocol needs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Encrypt every backup with &lt;code&gt;[age&lt;/code&gt;](&lt;a href="https://github.com/FiloSottile/age" rel="noopener noreferrer"&gt;https://github.com/FiloSottile/age&lt;/a&gt;)&lt;/strong&gt; before it leaves the host, and store it on physically separate, offline media. A stolen backup is all shares plus metadata, equivalent to stealing the funds.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Plan for the memory profile.&lt;/strong&gt; Each node is lightweight at rest, on the order of tens of MB idle, but spikes during Paillier preparameter generation, so provision for the spike, not the idle state, and remember the serial-start caveat on smaller hosts. For reference, the entire dev stack (backend, three nodes, Postgres, Consul, NATS, Redis) fits comfortably in 3.8 GiB.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Make init lazy, not fatal.&lt;/strong&gt; The backend shouldn't crash if MPC is briefly unavailable at boot; log a warning and retry on first real use. And handle the result-before-request race: register the pending request &lt;em&gt;before&lt;/em&gt; you check for an early result, because a fast cluster can return a signature before your handler is registered.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Retry the transient errors.&lt;/strong&gt; Treat these as retryable with a short backoff (&lt;code&gt;[0, 500, 2000] ms&lt;/code&gt;): &lt;code&gt;symmetrickey key not found&lt;/code&gt;, &lt;code&gt;failed to decrypt message&lt;/code&gt;, &lt;code&gt;encrypt tss message error&lt;/code&gt;, &lt;code&gt;not enough peers to process signing request&lt;/code&gt;, &lt;code&gt;timed out waiting for signing result&lt;/code&gt;. They're normal under load, not failures.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Section 11: Scaling the cluster — splitting hosts, and adding nodes
&lt;/h2&gt;

&lt;p&gt;Two operations get conflated here, and they cost wildly different amounts. Moving your three nodes onto separate hosts keeps the same key and costs only a maintenance window. &lt;em&gt;Adding&lt;/em&gt; a node changes the committee and needs a resharing ceremony — which Mpcium does ship, just not as a one-command move. Know which one you're doing before you touch production.&lt;/p&gt;

&lt;h3&gt;
  
  
  Splitting one host into three (same key, just downtime)
&lt;/h3&gt;

&lt;p&gt;This is the common "started on one box, now you want real 2-of-3" move, and it is &lt;em&gt;not&lt;/em&gt; a re-key. The identities, the shares, the &lt;code&gt;chain_code&lt;/code&gt;, and the peer UUIDs all stay byte-for-byte the same — you're only changing where each node runs. Because none of the key material changes, the master public key and every derived address survive untouched. Mechanically it's the Section 8 restore, aimed at three hosts instead of one:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Stand up the private network first — a &lt;a href="https://www.wireguard.com/" rel="noopener noreferrer"&gt;WireGuard&lt;/a&gt; mesh across the three hosts and the backend. Nodes talk through NATS, not node-to-node, so what actually has to be reachable is one NATS and one Consul over that private network.&lt;/li&gt;
&lt;li&gt;Stop the cluster. Your downtime starts here, and it's a hard stop: nodes down, no signing.&lt;/li&gt;
&lt;li&gt;Move each node's data dir, its &lt;code&gt;identity/&lt;/code&gt; folder, and its &lt;code&gt;config.yaml&lt;/code&gt; onto its own host. Keep the peer UUIDs and identity keys identical — regenerating them is a re-key, not a move (Section 8), and it abandons every address.&lt;/li&gt;
&lt;li&gt;Point each node's config at the relocated &lt;code&gt;nats.url&lt;/code&gt; and &lt;code&gt;consul.address&lt;/code&gt;, re-import the Consul KV dump, &lt;code&gt;chown -R 65532:65532&lt;/code&gt;, and delete stale &lt;code&gt;LOCK&lt;/code&gt; files.&lt;/li&gt;
&lt;li&gt;Start the nodes, wait for three &lt;code&gt;[READY]&lt;/code&gt;, then &lt;em&gt;verify by signing&lt;/em&gt; and &lt;code&gt;ecrecover&lt;/code&gt; that the master public key is byte-identical to before. Never trust &lt;code&gt;[READY]&lt;/code&gt; alone — that's the whole lesson of Section 8.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Downtime is the stop-to-verified-restart window — minutes if you've rehearsed it. Rehearse it exactly like the DR drill, because mechanically it is one.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding a node (2-of-3 → 2-of-4, or 3-of-5): resharing
&lt;/h3&gt;

&lt;p&gt;Reshaping the committee — a fourth node, a higher threshold — can't be a plain &lt;code&gt;docker run&lt;/code&gt;: a new node has no share, and the existing shares were generated for a three-party polynomial. The primitive that fixes this is &lt;strong&gt;key resharing&lt;/strong&gt; — the current shareholders jointly re-distribute the secret to a new set of parties, producing fresh shares for the new committee &lt;em&gt;while keeping the same master public key&lt;/em&gt;, so every user address is preserved and nothing moves on-chain.&lt;/p&gt;

&lt;p&gt;Mpcium ships resharing as a first-class, signed operation (it landed in v0.3.0 and was hardened for new-node joins in v0.3.5). You publish a &lt;code&gt;ResharingMessage&lt;/code&gt; and wait for the result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;mpcClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Resharing&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;sessionID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;walletID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;keyType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;nodeIDs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;      &lt;span class="nx"&gt;newCommitteePeerIDs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;// include a brand-new peer ID to add a node&lt;/span&gt;
  &lt;span class="nx"&gt;newThreshold&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;                       &lt;span class="c1"&gt;// change t here&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="nx"&gt;mpcClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;OnResharingResult&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* new shares are written */&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Results arrive on &lt;code&gt;mpc.mpc_reshare_result.*&lt;/code&gt;; there's a runnable &lt;code&gt;examples/reshare/main.go&lt;/code&gt; and it's e2e-tested for both ECDSA and EdDSA. The redistribution stores the new shares under the same &lt;code&gt;walletID&lt;/code&gt; with only an internal share-version bump, so the master public key — and every CKD-derived address — survives untouched. Add a node by including its peer ID in &lt;code&gt;nodeIDs&lt;/code&gt;; remove one or raise the threshold by editing &lt;code&gt;nodeIDs&lt;/code&gt; / &lt;code&gt;newThreshold&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Two honest caveats, because this is where "read the code, not the pitch" earns its keep:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;It's a two-part move, not one command.&lt;/strong&gt; There's no &lt;code&gt;mpcium-cli reshare&lt;/code&gt;. A full membership change is: register the new peer first (generate its &lt;code&gt;identity.json&lt;/code&gt;, then &lt;code&gt;mpcium-cli register-peers&lt;/code&gt;), &lt;em&gt;then&lt;/em&gt; the &lt;code&gt;Resharing()&lt;/code&gt; call that actually hands it a share. &lt;a href="https://github.com/fystack/mpcium/issues/84" rel="noopener noreferrer"&gt;Issue #84&lt;/a&gt; is still open — but for a turnkey add/remove-node CLI and end-to-end docs, not because the crypto is missing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A reshare rotates the master shares&lt;/strong&gt; (new version, new committee in &lt;code&gt;threshold_keyinfo&lt;/code&gt;), so it breaks Section 7's "one backup, forever": re-take the MPC backup after every reshare.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Still, pick your &lt;code&gt;n&lt;/code&gt; and threshold before the master DKG when you can — a reshare is real coordinated work, and the fewer you run against a cluster holding real funds, the better.&lt;/p&gt;

&lt;h2&gt;
  
  
  Section 12: Troubleshooting cheat sheet
&lt;/h2&gt;

&lt;p&gt;The failure modes you're most likely to hit, and the one-line fix for each:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;err_code=10052 ... retention policy&lt;/code&gt; crash-loop&lt;/strong&gt; — Cause: App/client created NATS streams before the nodes (wrong retention) · Fix: Recreate NATS; always start nodes first&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Node permission-denied on &lt;code&gt;data/&lt;/code&gt;&lt;/strong&gt; — Cause: Distroless UID 65532 vs. host ownership · Fix: &lt;code&gt;chown -R 65532:65532&lt;/code&gt; the node dirs&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Signing fails &lt;code&gt;ERROR_KEY_NOT_FOUND&lt;/code&gt; (&lt;code&gt;Key info not found&lt;/code&gt;)&lt;/strong&gt; — Cause: Consul KV (&lt;code&gt;threshold_keyinfo/*&lt;/code&gt;) not restored · Fix: &lt;code&gt;consul kv import&lt;/code&gt; the KV dump&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cluster &lt;code&gt;[READY]&lt;/code&gt; but signing fails &lt;code&gt;ERROR_KEY_NOT_FOUND&lt;/code&gt; (&lt;code&gt;ERROR_NOT_MAJORITY&lt;/code&gt; on v0.3.5)&lt;/strong&gt; — Cause: Backup restored one dir too high → empty DB, shares gone · Fix: Restore into the exact &lt;code&gt;.../data/nodeN&lt;/code&gt; subdir; verify by signing&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DKG hangs on "waiting for keygen result"&lt;/strong&gt; — Cause: The backend consumed the result event · Fix: Stop the app before running init&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DKG times out&lt;/strong&gt; — Cause: Not all three nodes were ready · Fix: Keygen needs all nodes; wait for three &lt;code&gt;[READY]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Node fatals opening the encrypted BadgerDB&lt;/strong&gt; — Cause: &lt;code&gt;badger_password&lt;/code&gt; is not 16/24/32 bytes · Fix: Generate with &lt;code&gt;openssl rand -hex 16&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nodes misbehave after re-running &lt;code&gt;docker compose up&lt;/code&gt;&lt;/strong&gt; — Cause: An override recreated a stateful service (NATS/Consul), wiping its state · Fix: Restart the nodes after any infra bring-up; treat NATS/Consul as stateful&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Look at the pattern. Not one of these is a cryptography failure. They're orchestration, filesystem, and config failures. That's the real lesson of running MPC in production: the math is the part that works, and the operations are where you earn your keep.&lt;/p&gt;

&lt;h2&gt;
  
  
  Section 13: Build it so you don't need the luck
&lt;/h2&gt;

&lt;p&gt;A self-hosted 2-of-3 MPC signing cluster isn't exotic infrastructure. It's a handful of open-source components wired together with a few load-bearing rules: get the startup order right, get the threshold semantics right, back up three things instead of one, and drill your disaster recovery before you need it. Because the failure that actually hurts you isn't a dramatic one. It's a cluster that looks perfectly healthy while it has silently lost every key.&lt;/p&gt;

&lt;p&gt;So if you take one thing from all of this, take the opening. Run the drill. A cluster that boots &lt;code&gt;[READY]&lt;/code&gt; with every key silently gone isn't a hypothetical, it's the first thing that goes wrong the day you finally need your backup, and it stays invisible until you sign and check. We found ours with half an hour to spare and a lot of luck. Build the thing so you don't need the luck.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If this was useful, I write about backend, crypto infrastructure, and shipping distributed systems to production.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow me:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;LinkedIn: &lt;a href="https://www.linkedin.com/in/anatolii-zhadan/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/anatolii-zhadan/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Hackernoon: &lt;a href="https://hackernoon.com/u/zhadan" rel="noopener noreferrer"&gt;https://hackernoon.com/u/zhadan&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Github: &lt;a href="https://github.com/ImmuneFOMO" rel="noopener noreferrer"&gt;https://github.com/ImmuneFOMO&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Tags: MPC · Multi-Party Computation · DevOps · Docker · Self-Hosted · Threshold Signatures · Disaster Recovery · Backend · Web3&lt;/em&gt;&lt;/p&gt;

</description>
      <category>web3</category>
      <category>softwaredevelopment</category>
      <category>devops</category>
      <category>cryptography</category>
    </item>
    <item>
      <title>How to Store Your Users' Crypto Wallets So Nobody Can Steal Them</title>
      <dc:creator>ImmuneFOMO</dc:creator>
      <pubDate>Tue, 07 Jul 2026 09:00:00 +0000</pubDate>
      <link>https://dev.to/immunefomo/how-to-store-your-users-crypto-wallets-so-nobody-can-steal-them-5fli</link>
      <guid>https://dev.to/immunefomo/how-to-store-your-users-crypto-wallets-so-nobody-can-steal-them-5fli</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5kkzauf6j0zklumyo1qc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5kkzauf6j0zklumyo1qc.png" alt="Encrypting the private key still leaves one decryptable key behind one server — the honeypot that MPC later removes." width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Encrypting the private key still leaves one decryptable key behind one server — the honeypot that MPC later removes.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Your backend can decrypt every user's private key. So can your &lt;code&gt;ENCRYPTION_KEY&lt;/code&gt;. So can whoever gets far enough into your servers: a leaked cloud token, a poisoned CI job, one unlucky RCE.&lt;/p&gt;

&lt;p&gt;You encrypted the keys. You feel safe. You've quietly built the biggest honeypot in the company.&lt;/p&gt;

&lt;p&gt;I know, because that's how we started: &lt;code&gt;Wallet.createRandom()&lt;/code&gt;, AES-256-GCM, an encrypted blob in Postgres, one &lt;code&gt;ENCRYPTION_KEY&lt;/code&gt; in the env. It ships fast and it passes the demo. It also means one stolen secret unlocks every user at once. That's the exact failure that drained Ronin (~$625M) and Harmony (~$100M). Nobody cracked the cryptography in either case. They found the one place that could.&lt;/p&gt;

&lt;p&gt;This article is the fix I wish I'd read first: MPC-based threshold signing, where the private key is never assembled anywhere. We'll cover what it actually changes, what it does &lt;em&gt;not&lt;/em&gt; solve, which protocols and libraries are safe to build on in 2026, and how to migrate an existing "encrypted key in a database" system without a big-bang cutover.&lt;/p&gt;

&lt;p&gt;If you already run custodial Web3 infra and know how MPC-TSS works, skip to &lt;strong&gt;Section 4&lt;/strong&gt; (the protocol reality check) and &lt;strong&gt;Section 7&lt;/strong&gt; (one master wallet for a million users). That's where the real decisions live.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/b0dd840211f6#4e40" rel="noopener noreferrer"&gt;The real problem — it's not encryption, it's centralization&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/b0dd840211f6#2c00" rel="noopener noreferrer"&gt;What MPC-TSS actually changes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/b0dd840211f6#0200" rel="noopener noreferrer"&gt;MPC in 60 seconds, for engineers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/b0dd840211f6#da00" rel="noopener noreferrer"&gt;The protocol reality check (2026)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/b0dd840211f6#5540" rel="noopener noreferrer"&gt;The open-source options: build your own vs. a turnkey server&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/b0dd840211f6#c300" rel="noopener noreferrer"&gt;Licensing traps&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/b0dd840211f6#b900" rel="noopener noreferrer"&gt;One master wallet for a million users (CKD)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/b0dd840211f6#f400" rel="noopener noreferrer"&gt;Who actually runs MPC in production&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/b0dd840211f6#5f00" rel="noopener noreferrer"&gt;Migrating from encrypted keys to MPC&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/b0dd840211f6#0000" rel="noopener noreferrer"&gt;The mistakes teams make (and what MPC won't save you from)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/p/b0dd840211f6#9a00" rel="noopener noreferrer"&gt;One strong primitive, not the whole system&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Section 1: The real problem — not encryption, but centralization
&lt;/h2&gt;

&lt;p&gt;Here's the custodial flow almost every team ships first. It works, and that's the trap:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Generate a wallet, a &lt;code&gt;secp256k1&lt;/code&gt; private key&lt;/li&gt;
&lt;li&gt;Encrypt it with AES-256-GCM&lt;/li&gt;
&lt;li&gt;Store the encrypted blob in PostgreSQL&lt;/li&gt;
&lt;li&gt;Decrypt it in the backend whenever you need to sign&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This protects you against exactly one thing: a database-only leak. Steal the Postgres dump and nothing else, and all you have is ciphertext.&lt;/p&gt;

&lt;p&gt;But your backend has to decrypt to function. So an attacker who gets enough control of it, through RCE, stolen cloud credentials, a CI/CD compromise, or a bad insider, doesn't need to break any cryptography. They use your own signing path. The AES does nothing, because your server was always going to decrypt.&lt;/p&gt;

&lt;p&gt;This keeps draining real money, and it always looks the same:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://www.elliptic.co/blog/540-million-stolen-from-the-ronin-defi-bridge" rel="noopener noreferrer"&gt;Ronin Bridge&lt;/a&gt; (2022, ~$625M):&lt;/strong&gt; attackers got 5 of 9 validator keys. Control was too concentrated; a Lazarus phishing campaign was enough to reach them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://www.halborn.com/blog/post/explained-the-harmony-horizon-bridge-hack" rel="noopener noreferrer"&gt;Harmony Horizon Bridge&lt;/a&gt; (2022, ~$100M):&lt;/strong&gt; a 2-of-5 multisig where the attacker collected just 2 keys and drained it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Nobody cracked ECDSA. They collected enough key material from too few places. So the goal isn't "encrypt the private key better." It's this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Remove the possibility that any single machine, process, or operator can unilaterally use the key.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's what multi-party computation gives you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Section 2: What MPC-TSS actually changes
&lt;/h2&gt;

&lt;p&gt;In threshold MPC signing (MPC-TSS), the private key is never assembled in one place. Not in memory, not on disk, not for a microsecond.&lt;/p&gt;

&lt;p&gt;The key is split into &lt;strong&gt;shares&lt;/strong&gt;, each share lives on an independent node, and a threshold of nodes cooperates to produce a signature. The blockchain sees a completely normal ECDSA signature and can't tell the difference.&lt;/p&gt;

&lt;p&gt;The canonical setup is three nodes with a 2-of-3 threshold:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2vpmv8wyuklk3br49hw8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2vpmv8wyuklk3br49hw8.png" alt="Any two of three nodes can jointly sign. One node, or the backend, is never enough." width="800" height="443"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Any two of three nodes can jointly sign. One node, or the backend, is never enough.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Walk the attack scenarios and the difference is structural, not cosmetic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The database dump&lt;/strong&gt; — Traditional (encrypted key): Ciphertext (safe, for now) · MPC 2-of-3: Nothing useful&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Database + encryption key&lt;/strong&gt; — Traditional (encrypted key): All keys stolen · MPC 2-of-3: Nothing useful&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Full backend / RCE&lt;/strong&gt; — Traditional (encrypted key): All keys stolen · MPC 2-of-3: No key exists to steal&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One MPC node&lt;/strong&gt; — Traditional (encrypted key): — · MPC 2-of-3: Not enough to sign&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One disk snapshot of one node&lt;/strong&gt; — Traditional (encrypted key): — · MPC 2-of-3: Not enough to sign&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;One honest caveat on that last row: an attacker who owns the backend can't exfiltrate a key, because there isn't one. But they can still ask the cluster to sign while they hold the box. MPC removes key theft, not a hijacked signing path. We come back to that in Section 10.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The backend holds zero private keys. When it needs a signature, it hashes the payload locally and sends a 32-byte digest to the cluster, which returns &lt;code&gt;(r, s, v)&lt;/code&gt;. Own the application server completely, and you get nothing to steal, because the thing you came for was never there.&lt;/p&gt;

&lt;h2&gt;
  
  
  Section 3: MPC in 60 seconds (for engineers)
&lt;/h2&gt;

&lt;p&gt;Two different things get called "MPC," and mixing them up causes half the confusion in this space:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;General MPC / SMPC&lt;/strong&gt; — parties jointly compute a function over private inputs without revealing them. Banks scoring fraud together, hospitals running joint analytics, private ML.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Threshold signature MPC (TSS)&lt;/strong&gt; — the specialized case for key management and signing.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Wallet infra lives in the second one: threshold ECDSA (Ethereum, Bitcoin, most EVM/L2 chains) or threshold EdDSA (Solana and friends).&lt;/p&gt;

&lt;p&gt;There are only two ceremonies to understand:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DKG (distributed key generation).&lt;/strong&gt; The nodes jointly generate key shares. No node ever sees the whole private key; each ends up holding its own share plus the shared public key. Keygen needs every participating node online.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Signing.&lt;/strong&gt; Your backend hashes the message and sends the 32-byte digest. A threshold of nodes runs the multi-round protocol and returns a standard &lt;code&gt;(r, s, v)&lt;/code&gt;, valid on-chain.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxh8clqphh183go4x3xud.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxh8clqphh183go4x3xud.png" alt="DKG runs once. After that, signing only needs a threshold of nodes, not all of them." width="800" height="366"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;DKG runs once. After that, signing only needs a threshold of nodes, not all of them.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;One detail trips up everyone on day one:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In &lt;code&gt;tss-lib&lt;/code&gt;-style implementations, &lt;code&gt;threshold&lt;/code&gt; is the polynomial degree &lt;code&gt;t&lt;/code&gt;, and the number of signers required is &lt;code&gt;t + 1&lt;/code&gt;. So a 2-of-3 wallet is configured with &lt;code&gt;threshold: 1&lt;/code&gt; (degree 1 → 2 signers), &lt;em&gt;not&lt;/em&gt; &lt;code&gt;threshold: 2&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Get that wrong and you've mis-provisioned your security by one. And remember the asymmetry: keygen needs every node, signing needs only the threshold. A node can be down and you can still sign, which is exactly the availability you want.&lt;/p&gt;

&lt;h2&gt;
  
  
  Section 4: The protocol reality check (2026)
&lt;/h2&gt;

&lt;p&gt;This is where naive advice goes wrong. People pick a protocol by name and stop. A protocol name is not a security guarantee. You have to evaluate implementation, version, and threat model together.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F55w1jfrvkrzqmdf6qy1f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F55w1jfrvkrzqmdf6qy1f.png" alt="The same protocol family can be safe or unusable depending on the exact library and version. The name alone tells you nothing." width="799" height="340"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The same protocol family can be safe or unusable depending on the exact library and version. The name alone tells you nothing.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  GG18 / GG20
&lt;/h3&gt;

&lt;p&gt;The most widely implemented family, and the basis of most turnkey servers. It also had serious holes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.fireblocks.com/blog/gg18-and-gg20-paillier-key-vulnerability-technical-report" rel="noopener noreferrer"&gt;**BitForge (CVE-2023-33241)&lt;/a&gt;*&lt;em&gt;, disclosed August 2023, CVSS 9.1–9.6. A malicious signer crafts a bad Paillier modulus and, in some implementations, extracts another party's key share in as few as 16 signing attempts (the real range runs from 16 up to ~a billion, depending on the implementation). The catch: it needs a malicious *participating&lt;/em&gt; party. If you control all your own nodes, an outside attacker can't mount it.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://verichains.io/tsshock/" rel="noopener noreferrer"&gt;**TSSHOCK&lt;/a&gt;** (Verichains, Black Hat USA 2023), a related class against GG18/GG20/CGGMP21 that can "leave no trace and appear innocent to the other parties."&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The fix, Paillier-Blum plus no-small-factor proofs, is in &lt;code&gt;tss-lib&lt;/code&gt; v2.x and maintained forks. So GG18/GG20 on a &lt;em&gt;patched&lt;/em&gt; implementation, with all nodes under your control, is a defensible self-hosted choice. On an unpatched one, it isn't.&lt;/p&gt;

&lt;h3&gt;
  
  
  CGGMP21
&lt;/h3&gt;

&lt;p&gt;Newer, academically stronger, and carrying a live problem in Rust. &lt;a href="https://rustsec.org/advisories/RUSTSEC-2025-0127.html" rel="noopener noreferrer"&gt;**CVE-2025-66017 / RUSTSEC-2025-0127&lt;/a&gt;**: signature forgery through altered presignatures, CVSS 8.2. The advisory is blunt: "Patched: no patched versions" for the &lt;code&gt;cggmp21&lt;/code&gt; crate. There's a second twist that lands right on our path here: presignatures plus HD/BIP-32 derivation drop effective security to 85 bits, and this whole article is about to steer you toward HD derivation (Section 7).&lt;/p&gt;

&lt;p&gt;Every serious writeup says the same thing: don't start a new system on raw &lt;code&gt;cggmp21&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  CGGMP24
&lt;/h3&gt;

&lt;p&gt;The safer forward path. The same class of issue is patched in &lt;code&gt;[v0.7.0-alpha.2&lt;/code&gt;](&lt;a href="https://github.com/LFDT-Lockness/cggmp21" rel="noopener noreferrer"&gt;https://github.com/LFDT-Lockness/cggmp21&lt;/a&gt;), and the API hardening is real. If you're choosing a modern protocol today, this is the direction.&lt;/p&gt;

&lt;h3&gt;
  
  
  DKLS-family (DKLS23)
&lt;/h3&gt;

&lt;p&gt;Strong research, good performance in specific stacks. Watch the license (Section 6). Implementation quality and licensing vary wildly by vendor.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GG18 / GG20&lt;/strong&gt; — Status 2026: Mature, patched forks exist · New self-hosted system?: Yes, if patched and all nodes yours&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CGGMP21&lt;/strong&gt; — Status 2026: Unpatched forgery CVE · New self-hosted system?: No, don't start here&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CGGMP24&lt;/strong&gt; — Status 2026: Patched (&lt;code&gt;v0.7.0-alpha.2&lt;/code&gt;) · New self-hosted system?: Yes, modern forward path&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DKLS23&lt;/strong&gt; — Status 2026: Strong, license-dependent · New self-hosted system?: Only after reading the LICENSE&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Never trust a "we use protocol X" claim. Check the exact library, the exact version, whether the CVE patch is actually in the code path you call, and whether your threat model even includes a malicious party.&lt;/p&gt;

&lt;h2&gt;
  
  
  Section 5: Build your own vs. a turnkey server
&lt;/h2&gt;

&lt;p&gt;If you want self-hosted MPC today, you have two real options.&lt;/p&gt;

&lt;h3&gt;
  
  
  Option A — build on a library
&lt;/h3&gt;

&lt;p&gt;You take a cryptographic library and build the server, networking, orchestration, storage, backups, and observability yourself.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[**bnb-chain/tss-lib&lt;/code&gt;](&lt;a href="https://github.com/bnb-chain/tss-lib)**" rel="noopener noreferrer"&gt;https://github.com/bnb-chain/tss-lib)**&lt;/a&gt; — Language: Go · License: MIT · Audit: Kudelski (Oct 2019) · Notes: The de-facto GG18/GG20 lib&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[**coinbase/cb-mpc&lt;/code&gt;](&lt;a href="https://github.com/coinbase/cb-mpc)**" rel="noopener noreferrer"&gt;https://github.com/coinbase/cb-mpc)**&lt;/a&gt; — Language: C++ · License: MIT · Audit: Cure53 (Q4 2024) · Notes: High-quality, library only — no server/storage/API&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LFDT-Lockness &lt;code&gt;[cggmp24&lt;/code&gt;](&lt;a href="https://github.com/LFDT-Lockness/cggmp21" rel="noopener noreferrer"&gt;https://github.com/LFDT-Lockness/cggmp21&lt;/a&gt;)&lt;/strong&gt; — Language: Rust · License: MIT/Apache-2.0 · Audit: Kudelski · Notes: Modern protocol, patched&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You get total control and protocol-level flexibility. You also now own networking, retries, key storage, backups, disaster recovery, monitoring, and the entire incident-response surface. That's weeks to months of work before you sign your first production transaction.&lt;/p&gt;

&lt;h3&gt;
  
  
  Option B — an open-source MPC server
&lt;/h3&gt;

&lt;p&gt;The most turnkey route right now is &lt;a href="https://github.com/fystack/mpcium" rel="noopener noreferrer"&gt;**Mpcium&lt;/a&gt;**: a Go server backed by &lt;code&gt;tss-lib&lt;/code&gt;, shipping a full cluster (nodes, message bus, service discovery, encrypted storage) plus client SDKs, under Apache-2.0.&lt;/p&gt;

&lt;p&gt;You get a real cluster out of the box and a dramatically lower starting cost. In exchange you accept an opinionated architecture, feature gaps you'll fork around (trusted-dealer key import, a REST façade over its NATS pub/sub), and a DevOps burden that's still yours.&lt;/p&gt;

&lt;p&gt;The heuristic I keep coming back to: weeks of building networking, storage, and DR from scratch, versus adding one feature to a server that already works. Unless you have a hard protocol requirement, extend the existing server.&lt;/p&gt;

&lt;p&gt;A quick note on the rest of the field, so you don't lose a week evaluating them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ZenGo&lt;/strong&gt; — the &lt;code&gt;[multi-party-ecdsa&lt;/code&gt;](&lt;a href="https://github.com/ZenGo-X/multi-party-ecdsa" rel="noopener noreferrer"&gt;https://github.com/ZenGo-X/multi-party-ecdsa&lt;/a&gt;) library is explicitly abandoned; &lt;code&gt;[gotham-city&lt;/code&gt;](&lt;a href="https://github.com/ZenGo-X/gotham-city" rel="noopener noreferrer"&gt;https://github.com/ZenGo-X/gotham-city&lt;/a&gt;) is 2-of-2 only, demo-grade, and effectively dormant (last commit 2024).&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/safeheron" rel="noopener noreferrer"&gt;**Safeheron&lt;/a&gt;** — open crypto libraries, proprietary server.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.sodot.dev/" rel="noopener noreferrer"&gt;**Sodot&lt;/a&gt;** — self-hostable, but proprietary: a closed-source SDK you can't inspect or fork.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://litprotocol.com/" rel="noopener noreferrer"&gt;**Lit Protocol&lt;/a&gt;** — a decentralized node network you consume, not a private cluster you run yourself.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Section 6: Licensing traps
&lt;/h2&gt;

&lt;p&gt;This one surprises people in legal review, not code review.&lt;/p&gt;

&lt;p&gt;A repo can be source-available or non-commercial while looking open at a glance. The protocol paper can be fully open while the implementation's license is restrictive.&lt;/p&gt;

&lt;p&gt;The cleanest example: one popular &lt;code&gt;dkls23&lt;/code&gt; implementation (&lt;a href="https://github.com/silence-laboratories/dkls23" rel="noopener noreferrer"&gt;Silence Labs&lt;/a&gt;) ships under a non-commercial license (SLL), and commercial use needs a separate paid license. A &lt;em&gt;different&lt;/em&gt;, independent DKLs23 implementation (&lt;a href="https://github.com/0xCarbon/DKLs23" rel="noopener noreferrer"&gt;0xCarbon&lt;/a&gt;) is dual MIT/Apache-2.0. Same protocol, completely different rights.&lt;/p&gt;

&lt;p&gt;Open the actual &lt;code&gt;LICENSE&lt;/code&gt; file, in the exact repo and version you plan to ship. Not the README, not the paper. The license, pinned to your version.&lt;/p&gt;

&lt;h2&gt;
  
  
  Section 7: One master wallet for a million users (CKD)
&lt;/h2&gt;

&lt;p&gt;Give every user their own wallet the obvious way and each one gets its own DKG across your nodes. That's Mpcium's default, and it works fine. The catch isn't performance, it's what it does to your backups and your operations.&lt;/p&gt;

&lt;p&gt;Every per-user DKG needs all three nodes online, and every wallet it creates is another independent set of shards on every node. So the MPC state you have to protect grows with your user base: every signup is one more ceremony to run and one more set of shards you can never lose. At a handful of users that's nothing. At a few million, provisioning and backup are the whole problem.&lt;/p&gt;

&lt;p&gt;CKD collapses all of it into one ceremony. BIP-32 hierarchical deterministic wallets, the trick Bitcoin gave us in 2012, derive an effectively infinite tree of child keys from one master. Run the DKG once, and every user after that is a pure derivation off the master — no ceremony, no nodes online, and a single backup covers all of them. That's &lt;strong&gt;CKD (child key derivation)&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Run one DKG at init → a single master MPC wallet (still split into 3 shards, still never assembled)&lt;/li&gt;
&lt;li&gt;For each user, derive &lt;code&gt;child = derive(masterPubKey, chainCode, userIndex)&lt;/code&gt;. Pure math, no ceremony, no nodes required online&lt;/li&gt;
&lt;li&gt;To sign, each node applies the derivation offset to its own share and signs. The child key, like the master, is never assembled&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmkw1l28q2kve6noymsu0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmkw1l28q2kve6noymsu0.png" alt="Per-user DKG on the left runs a full ceremony per user. CKD on the right derives every user from one master, with no per-user ceremony." width="800" height="409"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Per-user DKG on the left runs a full ceremony per user. CKD on the right derives every user from one master, with no per-user ceremony.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The operational gap isn't incremental, it's categorical:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Creating a user&lt;/strong&gt; — Per-user DKG: a key ceremony, all nodes · CKD (one master): a derivation, no ceremony&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;10,000 users&lt;/strong&gt; — Per-user DKG: 10k ceremonies, 30k shards · CKD (one master): 1 ceremony, 3 shards&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Backup cadence&lt;/strong&gt; — Per-user DKG: after every new user · CKD (one master): once, at init&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MPC node down&lt;/strong&gt; — Per-user DKG: new users wait · CKD (one master): new users still created&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Disaster recovery&lt;/strong&gt; — Per-user DKG: restore everything · CKD (one master): restore master; users are DB rows&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;"Is it as secure?" is the fair question, and the answer is yes, under the same threat model:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;1 node compromised&lt;/strong&gt; — Per-user DKG: Safe · CKD: Safe&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;2 of 3 nodes compromised&lt;/strong&gt; — Per-user DKG: Compromised · CKD: Compromised&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Backup stolen&lt;/strong&gt; — Per-user DKG: Compromised · CKD: Compromised&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;2 nodes in a secure enclave&lt;/strong&gt; — Per-user DKG: Protected · CKD: Protected&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That equivalence is about &lt;em&gt;node compromise&lt;/em&gt;. Be clear-eyed about what CKD gives up, because this is the part the marketing pages skip:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can't rotate or revoke a single user's key in isolation. Everyone derives from the same master; there's no per-user share to roll.&lt;/li&gt;
&lt;li&gt;You can't prove a user's key is cryptographically independent of the others, which some regulatory or audit regimes want.&lt;/li&gt;
&lt;li&gt;Anyone holding the master public key and chain code can enumerate every user address. For an opt-in custodial service that's a non-issue; for a privacy-sensitive product it's a real deanonymization vector.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If none of that bites your product, CKD is a clean way to scale. If one does — and for many products one will — per-user DKG is the safer default, and that's when it earns its cost.&lt;/p&gt;

&lt;p&gt;The BIP-32 caveat, stated honestly: with the parent public key, the chain code, and a full child private key, you can recover the parent key. In MPC that's defused, because the child private key never exists as a whole; each node holds only a share delta. The &lt;code&gt;chain_code&lt;/code&gt; isn't secret-shared either (every node has a full copy), but that doesn't change the picture: recovering the parent still needs a full child private key, and that needs a threshold of nodes, which is total compromise already.&lt;/p&gt;

&lt;p&gt;I didn't take any of this on faith. An end-to-end recovery test ran the full flow, create the master, derive several users, sign with each, back up, destroy the cluster, restore, verify, and passed 12 of 12 checks: every derived address matched, every signature still verified, and a brand-new user created &lt;em&gt;after&lt;/em&gt; the restore worked too. A separate cross-machine drill, destroy the cluster and rebuild on fresh hardware, is what flushed out eleven runbook bugs, one of which would have quietly lost every key in a real emergency. The next article takes that drill apart in full.&lt;/p&gt;

&lt;h2&gt;
  
  
  Section 8: Who actually runs MPC in production
&lt;/h2&gt;

&lt;p&gt;MPC is no longer a crypto-startup curiosity. The clearest sign of how seriously the industry takes it: &lt;strong&gt;Fireblocks&lt;/strong&gt;, one of the largest custody providers, is the team that discovered and disclosed the &lt;a href="https://www.fireblocks.com/blog/bitforge-fireblocks-researchers-uncover-vulnerabilities-in-over-15-major-wallet-providers" rel="noopener noreferrer"&gt;BitForge&lt;/a&gt; vulnerability class from Section 4. When your custody vendor is the one running the offensive research that finds MPC's own weak spots, the primitive has left the lab.&lt;/p&gt;

&lt;p&gt;Who else is standing on it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://newsroom.ibm.com/ibm-digital-asset-haven" rel="noopener noreferrer"&gt;**IBM Digital Asset Haven&lt;/a&gt;** (2025), developed with &lt;strong&gt;Dfns&lt;/strong&gt;, combines Dfns's threshold signing with IBM's HSM and offline-signing models for institutional custody. MPC under an IBM-branded product is about as mainstream as it gets.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dfns&lt;/strong&gt; runs its &lt;a href="https://dfns.co/article/ku23/" rel="noopener noreferrer"&gt;KU23 protocol&lt;/a&gt; on its key-management network — by its own figures 10M+ keys and ~$1B signed per month — and plans to open-source it and move governance to the Linux Foundation's LF Decentralized Trust (the Lockness project).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Coinbase&lt;/strong&gt; open-sourced its own MPC library, &lt;code&gt;[cb-mpc&lt;/code&gt;](&lt;a href="https://github.com/coinbase/cb-mpc" rel="noopener noreferrer"&gt;https://github.com/coinbase/cb-mpc&lt;/a&gt;) (MIT, Cure53-audited). You don't open-source your custody crypto unless it's core to how you operate.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://fordefi.com/" rel="noopener noreferrer"&gt;Fordefi&lt;/a&gt;, &lt;a href="https://www.bitgo.com/" rel="noopener noreferrer"&gt;BitGo&lt;/a&gt;, and &lt;a href="https://copper.co/" rel="noopener noreferrer"&gt;Copper&lt;/a&gt;&lt;/strong&gt; ship MPC/TSS custody to institutional desks and DeFi.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Adopting MPC isn't an exotic bet. It's the same primitive regulated, multi-billion-dollar custodians standardized on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Section 9: Migrating from encrypted keys to MPC
&lt;/h2&gt;

&lt;p&gt;You rarely start clean. Here's a rollout that doesn't need a big-bang cutover.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fm1sg1veziabpliqfdpm6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fm1sg1veziabpliqfdpm6.png" alt="The safe path is a signer abstraction: your service code never changes, only what sits behind the interface does." width="800" height="366"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The safe path is a signer abstraction: your service code never changes, only what sits behind the interface does.&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Inventory every signing call site.&lt;/strong&gt; Every place a key is created, decrypted, or used, across services, jobs, and scripts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Introduce a signer abstraction.&lt;/strong&gt; Replace direct wallet creation with a single signing interface so the rest of your code stops caring where the key lives.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="c1"&gt;// Before: the key exists in your process&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;wallet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ethers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Wallet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;decrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;encryptedKey&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;wallet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;signTransaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="c1"&gt;// After: the key never exists here — you send a digest out to sign&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;signer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mpc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getSigner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;derivationPath&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;signer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;signTransaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// digest -&amp;gt; MPC -&amp;gt; (r, s, v)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Add MPC signer adapters.&lt;/strong&gt; One thin adapter for &lt;code&gt;ethers&lt;/code&gt;, one for &lt;code&gt;viem&lt;/code&gt;, both just forwarding the digest to the cluster.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dual-run behind a flag.&lt;/strong&gt; &lt;code&gt;MPC_ENABLED&lt;/code&gt;, rolled out to a controlled cohort first.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cut over new users first.&lt;/strong&gt; New wallets go through MPC; the legacy path becomes read-only.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Migrate or rotate legacy keys.&lt;/strong&gt; Import if your server supports trusted-dealer import; otherwise move funds to MPC-managed wallets and retire the old ones.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Delete the decryption path.&lt;/strong&gt; Rip out every &lt;code&gt;getPrivateKey()&lt;/code&gt;-style helper. If the code can't decrypt a key, neither can an attacker who owns the process.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test disaster recovery before launch.&lt;/strong&gt; Restore drills are mandatory, not optional. (The next article is one long argument for why.)&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Section 10: The mistakes teams make
&lt;/h2&gt;

&lt;p&gt;After shipping this to production, the most useful thing I learned is that the cryptography almost never breaks. The orchestration does. The mistakes repeat:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Misreading threshold semantics, the &lt;code&gt;t&lt;/code&gt; vs &lt;code&gt;t + 1&lt;/code&gt; count from Section 3.&lt;/li&gt;
&lt;li&gt;Underestimating the message bus and metadata store. The signing protocol is chatty and stateful, and your NATS/Consul/BadgerDB layer is now load-bearing.&lt;/li&gt;
&lt;li&gt;Assuming one datastore backup is enough. It isn't; you need the key shares &lt;em&gt;and&lt;/em&gt; the routing metadata.&lt;/li&gt;
&lt;li&gt;Running keygen while application consumers race for the result events, silently stealing DKG output from your init flow.&lt;/li&gt;
&lt;li&gt;Treating MPC as "security complete" and skipping the policy layer, which brings us to the most important caveat here.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  MPC is not magic
&lt;/h3&gt;

&lt;p&gt;MPC dramatically reduces key-&lt;em&gt;theft&lt;/em&gt; risk. On its own it does nothing about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;signing the wrong payload, if your backend is tricked into it&lt;/li&gt;
&lt;li&gt;compromised approval or policy logic&lt;/li&gt;
&lt;li&gt;frontend / UI supply-chain attacks that fool an operator into approving a bad action&lt;/li&gt;
&lt;li&gt;broad application-layer abuse after a backend compromise&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The cautionary tale is &lt;a href="https://www.elliptic.co/blog/bybit-hack-largest-in-history" rel="noopener noreferrer"&gt;**Bybit (February 2025, ~$1.5B)&lt;/a&gt;*&lt;em&gt;, the largest crypto theft to date. Bybit *had&lt;/em&gt; multisig (Safe). The attackers didn't break the keys; they compromised the UI so operators approved a malicious transaction. MPC wouldn't have stopped it, because the failure was in intent, not custody.&lt;/p&gt;

&lt;p&gt;So treat MPC as a very strong primitive inside a layered system. Policy engine, transaction simulation, allowlists, anomaly detection, segregated duties, hardened ops: all still matter.&lt;/p&gt;

&lt;h2&gt;
  
  
  Section 11: One strong primitive, not the whole system
&lt;/h2&gt;

&lt;p&gt;If your backend can decrypt every user's private key today, you don't have a cryptography problem. You have a system-design problem: one key, one server, one blast radius.&lt;/p&gt;

&lt;p&gt;MPC-TSS is the most practical way I know to redesign that system so key theft becomes structurally hard and the blast radius of a compromise shrinks. Not impossible. Materially harder. In security architecture, that difference is the whole game.&lt;/p&gt;

&lt;p&gt;One warning before you run off and build it, though. The day I moved our first wallet onto MPC and deleted the old &lt;code&gt;getPrivateKey()&lt;/code&gt; helper felt great, right up until I realized MPC had solved the &lt;em&gt;theft&lt;/em&gt; problem and none of the others. It will faithfully sign the wrong transaction if something upstream tells it to. So ship it as one strong primitive in a layered system, not as the thing that lets you stop worrying.&lt;/p&gt;

&lt;p&gt;Next up, the hands-on half: deploying a self-hosted 2-of-3 MPC signing cluster with Docker. The DKG ceremony, the backup model, and the disaster-recovery drill that caught a silent, cluster-killing bug — the one I mentioned above, in full.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If this was useful, I write about backend, crypto infrastructure, and shipping distributed systems to production.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow me:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;LinkedIn: &lt;a href="https://www.linkedin.com/in/anatolii-zhadan/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/anatolii-zhadan/&lt;/a&gt;&lt;br&gt;
Hackernoon: &lt;a href="https://hackernoon.com/u/zhadan" rel="noopener noreferrer"&gt;https://hackernoon.com/u/zhadan&lt;/a&gt;&lt;br&gt;
Github: &lt;a href="https://github.com/ImmuneFOMO" rel="noopener noreferrer"&gt;https://github.com/ImmuneFOMO&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mpc</category>
      <category>cryptowalletsecurity</category>
      <category>web3</category>
      <category>custody</category>
    </item>
  </channel>
</rss>
