<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>kvaps blog</title><link>https://kvaps.workshop.aenix.io/en/blog/</link><description>Deep dives and experiments by the virtual kvaps — long reads about the kernel, etcd, storage and Kubernetes.</description><language>en</language><atom:link href="https://kvaps.workshop.aenix.io/en/blog/index.xml" rel="self" type="application/rss+xml"/><lastBuildDate>Fri, 27 Nov 2026 22:15:00 +0100</lastBuildDate><item><title>sched_ext: the CPU scheduler as a BPF program, and what to do with it on Kubernetes nodes</title><link>https://kvaps.workshop.aenix.io/en/blog/sched-ext/</link><guid>https://kvaps.workshop.aenix.io/en/blog/sched-ext/</guid><pubDate>Fri, 27 Nov 2026 22:15:00 +0100</pubDate><category>kernel</category><category>bpf</category><category>scheduler</category><category>kubernetes</category><category>kubevirt</category><description>How sched_ext from Linux 6.12 works, how scx_rusty, scx_lavd and scx_layered differ, and which of them actually apply to Kubernetes nodes with noisy neighbors and KubeVirt VMs.</description><content:encoded><![CDATA[<p>For a long time the Linux CPU scheduler was the part of the kernel you just didn&rsquo;t touch. Want a different policy? Write a patch, build a kernel, reboot the node, and then convince the maintainers that your use case matters more than everyone else&rsquo;s. Linux 6.12 brought a simpler way: sched_ext, a scheduler class whose behavior is defined by a BPF program and can be changed on the fly, without a reboot.</p>
<p>Let&rsquo;s look at how it works under the hood, what lives in the <code>sched-ext/scx</code> repository, and what all of this might mean for Kubernetes nodes, where noisy neighbors, latency-critical pods and KubeVirt VMs share the same machine.</p>
<p>This is a review, not a benchmark report: there are no measurements of my own here, only the kernel documentation, the source code and the READMEs from <code>sched-ext/scx</code>. Links are at the end.</p>
<h2 id="why-bother">Why bother <a class="anchor" href="#why-bother" aria-label="Link to the section “Why bother”">#</a></h2><p>The stock scheduler (CFS, and EEVDF since 6.6) is one algorithm for every occasion. It behaves the same on a desktop, a database, a CI farm and a hypervisor, and its knobs are, frankly, pretty coarse: nice, cgroup weights and limits, affinity, a handful of sysctls.</p>
<p>sched_ext takes a different approach. The kernel exposes a full scheduling interface, and the algorithm is written as BPF programs. According to the kernel documentation, this approach has three key properties:</p>
<ul>
<li>a BPF scheduler can group CPUs however it likes, because tasks are not tied to specific CPUs at wakeup time;</li>
<li>it can be turned on and off at any moment;</li>
<li>system integrity is preserved no matter what the scheduler does. On any error, when a runnable task stalls, or on <code>SysRq-S</code>, the system falls back to the stock scheduler.</li>
</ul>
<p>The last point is essentially what makes the whole idea viable. You can get the scheduler wrong, but you can&rsquo;t take the machine down with it.</p>
<h2 id="how-it-works">How it works <a class="anchor" href="#how-it-works" aria-label="Link to the section “How it works”">#</a></h2><h3 id="struct-sched_ext_ops">struct sched_ext_ops <a class="anchor" href="#struct-sched_ext_ops" aria-label="Link to the section “struct sched_ext_ops”">#</a></h3><p>A BPF scheduler is a set of BPF programs implementing <code>struct sched_ext_ops</code>. There is only one mandatory field: <code>ops.name</code>. All callbacks are optional, and anything left unimplemented falls back to the default behavior.</p>
<p>The minimal example from <code>tools/sched_ext/scx_simple.bpf.c</code> looks like this (abridged, from the kernel documentation):</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-c" data-lang="c"><span class="line"><span class="cl"><span class="n">s32</span> <span class="nf">BPF_STRUCT_OPS</span><span class="p">(</span><span class="n">simple_select_cpu</span><span class="p">,</span> <span class="k">struct</span> <span class="n">task_struct</span> <span class="o">*</span><span class="n">p</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">                   <span class="n">s32</span> <span class="n">prev_cpu</span><span class="p">,</span> <span class="n">u64</span> <span class="n">wake_flags</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="kt">bool</span> <span class="n">direct</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="n">s32</span> <span class="n">cpu</span> <span class="o">=</span> <span class="nf">scx_bpf_select_cpu_dfl</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">prev_cpu</span><span class="p">,</span> <span class="n">wake_flags</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">direct</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        <span class="k">if</span> <span class="p">(</span><span class="n">direct</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">                <span class="nf">scx_bpf_dsq_insert</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">SCX_DSQ_LOCAL</span><span class="p">,</span> <span class="n">SCX_SLICE_DFL</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">        <span class="k">return</span> <span class="n">cpu</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kt">void</span> <span class="nf">BPF_STRUCT_OPS</span><span class="p">(</span><span class="n">simple_enqueue</span><span class="p">,</span> <span class="k">struct</span> <span class="n">task_struct</span> <span class="o">*</span><span class="n">p</span><span class="p">,</span> <span class="n">u64</span> <span class="n">enq_flags</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="nf">scx_bpf_dsq_insert</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">SCX_DSQ_GLOBAL</span><span class="p">,</span> <span class="n">SCX_SLICE_DFL</span><span class="p">,</span> <span class="n">enq_flags</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nf">SEC</span><span class="p">(</span><span class="s">&#34;.struct_ops&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">sched_ext_ops</span> <span class="n">simple_ops</span> <span class="o">=</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="p">.</span><span class="n">select_cpu</span> <span class="o">=</span> <span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="p">)</span><span class="n">simple_select_cpu</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">        <span class="p">.</span><span class="n">enqueue</span>    <span class="o">=</span> <span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="p">)</span><span class="n">simple_enqueue</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">        <span class="p">.</span><span class="n">name</span>       <span class="o">=</span> <span class="s">&#34;simple&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl"><span class="p">};</span>
</span></span></code></pre></div><p>One caveat about versions. In 6.12 the function that inserts a task into a queue was called <code>scx_bpf_dispatch()</code>, and the one that pulls from a queue was <code>scx_bpf_consume()</code>. In 6.13 they were renamed to <code>scx_bpf_dsq_insert()</code> and <code>scx_bpf_dsq_move_to_local()</code>. The examples above already use the new names.</p>
<h3 id="dsq-dispatch-queues">DSQ: dispatch queues <a class="anchor" href="#dsq-dispatch-queues" aria-label="Link to the section “DSQ: dispatch queues”">#</a></h3><p>Between the kernel and the BPF code sits a layer of dispatch queues, DSQs. Each DSQ can work as a FIFO or as a priority queue (ordered by vtime). Out of the box there is one global <code>SCX_DSQ_GLOBAL</code> and one local <code>SCX_DSQ_LOCAL</code> per CPU. The scheduler creates its own queues with <code>scx_bpf_create_dsq()</code>, and it can have as many as it wants.</p>
<p>The main rule: a CPU always runs a task from its own local DSQ. Tasks are inserted into DSQs, and from non-local queues they are moved into the local queue of the target CPU. The built-in queues support FIFO only; priority insertion (<code>scx_bpf_dsq_insert_vtime()</code>) works only with user-created ones.</p>
<h3 id="the-scheduling-loop">The scheduling loop <a class="anchor" href="#the-scheduling-loop" aria-label="Link to the section “The scheduling loop”">#</a></h3><p>The path of a task that wakes up looks like this:</p>
<ol>
<li><strong><code>ops.select_cpu()</code></strong> is a hint about which CPU the task should wake up on, and it also wakes that CPU if it is idle. The hint is not binding: the final CPU is chosen at the end. If the task is inserted straight into <code>SCX_DSQ_LOCAL</code> from here, <code>ops.enqueue()</code> is skipped.</li>
<li><strong><code>ops.enqueue()</code></strong> decides where the task goes: directly into the global or a local DSQ, into a custom DSQ, or kept in the scheduler&rsquo;s own BPF data structures.</li>
<li>When a CPU is ready to pick the next task, it checks its local DSQ, then the global one, and only if both are empty does it call <strong><code>ops.dispatch()</code></strong>. That callback can insert tasks into any DSQ or move a task from a custom queue into the local one.</li>
<li>If no work turns up after <code>ops.dispatch()</code>, the CPU tries the global queue once more, may keep running the previous task if it is still runnable, and eventually goes idle.</li>
</ol>
<p>Around this there are lifecycle callbacks: <code>init_task</code>, <code>enable</code>, <code>runnable</code>, <code>running</code>, <code>tick</code>, <code>stopping</code>, <code>quiescent</code>, <code>disable</code>, <code>exit_task</code>. If a scheduler only uses the built-in DSQs and places everything right in <code>enqueue</code>, it doesn&rsquo;t need <code>dispatch</code> at all.</p>
<h3 id="what-it-schedules">What it schedules <a class="anchor" href="#what-it-schedules" aria-label="Link to the section “What it schedules”">#</a></h3><p>By default, once a BPF scheduler is loaded, all tasks with the <code>SCHED_NORMAL</code>, <code>SCHED_BATCH</code>, <code>SCHED_IDLE</code> and <code>SCHED_EXT</code> policies move under sched_ext. With the <code>SCX_OPS_SWITCH_PARTIAL</code> flag, only tasks that explicitly set <code>SCHED_EXT</code> move, and the rest stay with the fair class. Real-time tasks (<code>SCHED_FIFO</code>, <code>SCHED_RR</code>) and <code>SCHED_DEADLINE</code> are left alone: their classes sit higher.</p>
<p>If a task sets <code>SCHED_EXT</code> on itself while no BPF scheduler is loaded, it simply runs as <code>SCHED_NORMAL</code>.</p>
<h3 id="fallback-and-the-watchdog">Fallback and the watchdog <a class="anchor" href="#fallback-and-the-watchdog" aria-label="Link to the section “Fallback and the watchdog”">#</a></h3><p>Fault tolerance comes in three layers.</p>
<ul>
<li><strong>Fallback to the fair class.</strong> Termination of the scheduler process, <code>SysRq-S</code>, or any internal error aborts the BPF scheduler and hands all tasks back to the stock scheduler. <code>SysRq-D</code> takes a debug dump without disabling it.</li>
<li><strong>Watchdog.</strong> <code>sched_ext_ops</code> has a <code>timeout_ms</code> field: the maximum time a runnable task may wait for a CPU. Both the default and the maximum are 30 seconds. If a task waits longer, the scheduler is unloaded. The <code>scx</code> README puts it this way: barring kernel bugs, the worst a sched_ext scheduler can do is starve some threads until the watchdog kicks in.</li>
<li><strong>Bypass mode.</strong> While the scheduler is loading, unloading or recovering from an error, tasks are scheduled in a simplified mode with a fixed time slice. The <code>sched_ext.slice_bypass_us</code> and <code>sched_ext.bypass_lb_intv_us</code> parameters are documented, but they are mostly meant for debugging.</li>
</ul>
<p>Keep in mind that 30 seconds is not &ldquo;instant&rdquo;. For a latency-sensitive service, half a minute of starvation is an incident, even if the machine survived in the end.</p>
<h2 id="what-lives-in-sched-extscx">What lives in sched-ext/scx <a class="anchor" href="#what-lives-in-sched-extscx" aria-label="Link to the section “What lives in sched-ext/scx”">#</a></h2><p>The <code>sched-ext/scx</code> repository is the main home for schedulers. Minimal C examples live in the kernel&rsquo;s <code>tools/sched_ext</code> and in a separate <code>scx-c-examples</code> repository, while <code>scx</code> itself now holds schedulers with a userspace part written in Rust. Three of them are interesting for this review.</p>
<h3 id="scx_rusty">scx_rusty <a class="anchor" href="#scx_rusty" aria-label="Link to the section “scx_rusty”">#</a></h3><p>A hybrid of BPF and userspace. The BPF part does simple round robin within each domain, and the Rust part computes domain load and tells BPF how to rebalance tasks. By default a domain is created per LLC. Balancing thresholds, frequency and the domain split are configurable. The README is upfront about two things: rusty doesn&rsquo;t yet distinguish LLCs in different NUMA nodes, so it does better on a single socket with several CCXs than on multi-socket machines, and, like CFS, it has the &ldquo;infeasible weights&rdquo; problem, where a task with a huge weight forces cores to sit idle.</p>
<h3 id="scx_lavd">scx_lavd <a class="anchor" href="#scx_lavd" aria-label="Link to the section “scx_lavd”">#</a></h3><p>LAVD stands for Latency-criticality Aware Virtual Deadline. The scheduler measures how latency-critical a task is and uses that when computing the deadline and the time slice. All decisions are made by the BPF part, while Rust supplies the topology and handles the plumbing. It was originally built for games, to get rid of stutter. Domains are created per LLC, per core type (P/E on Intel, big/LITTLE on ARM) and per NUMA node, and it is aimed primarily at single-socket systems.</p>
<h3 id="scx_layered">scx_layered <a class="anchor" href="#scx_layered" aria-label="Link to the section “scx_layered”">#</a></h3><p>The most interesting one for servers. Tasks are sorted into layers, and each layer has its own policy. Layer membership is defined by matches: cgroup prefix, process name or its leader&rsquo;s name, nice, UID/GID, PID and a few more exotic options. Matches are combined into OR groups of AND blocks. There are three kinds of layers:</p>
<ul>
<li><strong>Confined</strong>: the layer&rsquo;s tasks are locked to the CPUs assigned to it, and the number of CPUs is adjusted to keep utilization within <code>util_range</code>;</li>
<li><strong>Grouped</strong>: the same, but tasks can spill over to free CPUs outside the layer when there are any;</li>
<li><strong>Open</strong>: prefer CPUs not taken by Confined and Grouped layers, and spill over onto occupied ones if no free CPUs are left.</li>
</ul>
<p>The configuration is passed as a JSON file with an <code>f:</code> prefix, and stats are collected by a separate process:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">scx_layered f:layers.json
</span></span><span class="line"><span class="cl">scx_layered --monitor <span class="m">1</span>
</span></span></code></pre></div><p>The <code>scx</code> README says outright that Meta deploys sched_ext at scale in production, and they have a separate write-up on how they use <code>scx_layered</code>.</p>
<h2 id="how-to-try-it-yourself">How to try it yourself <a class="anchor" href="#how-to-try-it-yourself" aria-label="Link to the section “How to try it yourself”">#</a></h2><p>First, check that your kernel supports it at all:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">uname -r
</span></span><span class="line"><span class="cl">zcat /proc/config.gz 2&gt;/dev/null <span class="p">|</span> grep SCHED_CLASS_EXT <span class="se">\
</span></span></span><span class="line"><span class="cl">  <span class="o">||</span> grep SCHED_CLASS_EXT /boot/config-<span class="s2">&#34;</span><span class="k">$(</span>uname -r<span class="k">)</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl">cat /sys/kernel/sched_ext/state
</span></span></code></pre></div><p>You need <code>CONFIG_SCHED_CLASS_EXT=y</code>, BPF with JIT and <code>CONFIG_DEBUG_INFO_BTF=y</code>; the full list is in the kernel documentation and in <code>scx/kernel.config</code>. On Talos you can check the same thing with <code>talosctl -n &lt;node&gt; read /proc/config.gz | zcat | grep SCHED_CLASS_EXT</code>, provided the kernel has <code>CONFIG_IKCONFIG_PROC</code> enabled.</p>
<p>Then you install a scheduler from your distribution&rsquo;s packages (instructions for Ubuntu, Arch, Fedora, Gentoo, Nix and openSUSE are in <code>INSTALL.md</code>) or build it from source, for example <code>cargo install scx_rusty</code>. Starting it is just running the binary:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">scx_rusty <span class="p">&amp;</span>
</span></span><span class="line"><span class="cl">cat /sys/kernel/sched_ext/state          <span class="c1"># enabled</span>
</span></span><span class="line"><span class="cl">cat /sys/kernel/sched_ext/root/ops       <span class="c1"># scheduler name</span>
</span></span><span class="line"><span class="cl">grep ext /proc/self/sched                <span class="c1"># ext.enabled : 1</span>
</span></span></code></pre></div><p>Stopping the process brings back the stock scheduler. For permanent use there is a systemd unit, <code>scx.service</code>; the scheduler and its flags are set in <code>/etc/default/scx</code> via the <code>SCX_SCHEDULER</code> and <code>SCX_FLAGS</code> variables.</p>
<p>It makes no sense to measure &ldquo;faster or slower in general&rdquo;. Measure a specific metric of your workload: service tail latency, CI step time, jitter inside a VM. And always on the same machine, under the same background load, with and without the stock scheduler.</p>
<h2 id="what-this-means-for-kubernetes-nodes">What this means for Kubernetes nodes <a class="anchor" href="#what-this-means-for-kubernetes-nodes" aria-label="Link to the section “What this means for Kubernetes nodes”">#</a></h2><h3 id="noisy-neighbors">Noisy neighbors <a class="anchor" href="#noisy-neighbors" aria-label="Link to the section “Noisy neighbors”">#</a></h3><p>Today CPU isolation on a node rests on three things. Requests turn into cgroup v2 <code>cpu.weight</code>, limits into <code>cpu.max</code> (the CFS quota), and the CPU Manager with the static policy gives Guaranteed pods with integer CPUs exclusive cores via cpuset. All three are fairly blunt tools. The quota throttles a pod even when the node has idle cores, and exclusive cores sit idle when the pod doesn&rsquo;t need them.</p>
<p><code>scx_layered</code> lets you express something Kubernetes can&rsquo;t: &ldquo;this layer gets priority access to N cores, but if it isn&rsquo;t using them, let others have them&rdquo;. With the systemd cgroup driver, pods land in predictable paths, and layers can be built on top of those. This is a sketch, not a recipe:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-json" data-lang="json"><span class="line"><span class="cl"><span class="p">[</span>
</span></span><span class="line"><span class="cl">  <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;name&#34;</span><span class="p">:</span> <span class="s2">&#34;besteffort&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;matches&#34;</span><span class="p">:</span> <span class="p">[[{</span><span class="nt">&#34;CgroupPrefix&#34;</span><span class="p">:</span> <span class="s2">&#34;kubepods.slice/kubepods-besteffort.slice/&#34;</span><span class="p">}]],</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;kind&#34;</span><span class="p">:</span> <span class="p">{</span><span class="nt">&#34;Confined&#34;</span><span class="p">:</span> <span class="p">{</span><span class="nt">&#34;cpus_range&#34;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">],</span> <span class="nt">&#34;util_range&#34;</span><span class="p">:</span> <span class="p">[</span><span class="mf">0.8</span><span class="p">,</span> <span class="mf">0.9</span><span class="p">]}}</span>
</span></span><span class="line"><span class="cl">  <span class="p">},</span>
</span></span><span class="line"><span class="cl">  <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;name&#34;</span><span class="p">:</span> <span class="s2">&#34;system&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;matches&#34;</span><span class="p">:</span> <span class="p">[[{</span><span class="nt">&#34;CgroupPrefix&#34;</span><span class="p">:</span> <span class="s2">&#34;system.slice/&#34;</span><span class="p">}]],</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;kind&#34;</span><span class="p">:</span> <span class="p">{</span><span class="nt">&#34;Grouped&#34;</span><span class="p">:</span> <span class="p">{</span><span class="nt">&#34;cpus_range&#34;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="nt">&#34;util_range&#34;</span><span class="p">:</span> <span class="p">[</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.8</span><span class="p">]}}</span>
</span></span><span class="line"><span class="cl">  <span class="p">},</span>
</span></span><span class="line"><span class="cl">  <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;name&#34;</span><span class="p">:</span> <span class="s2">&#34;rest&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;matches&#34;</span><span class="p">:</span> <span class="p">[[]],</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;kind&#34;</span><span class="p">:</span> <span class="p">{</span><span class="nt">&#34;Open&#34;</span><span class="p">:</span> <span class="p">{}}</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">]</span>
</span></span></code></pre></div><p>Here BestEffort pods are locked to a few cores, system daemons (kubelet, containerd) get their own guaranteed slice with the option to spill over, and everything else runs on the remaining CPUs. The last layer with an empty match catches whatever didn&rsquo;t fall into the previous ones. You&rsquo;ll have to tune <code>util_range</code> and <code>cpus_range</code> for your own node.</p>
<h3 id="latency-critical-pods">Latency-critical pods <a class="anchor" href="#latency-critical-pods" aria-label="Link to the section “Latency-critical pods”">#</a></h3><p>For these, the LAVD idea is more interesting: the scheduler figures out who is latency-critical from the task&rsquo;s behavior, without manual labeling. But LAVD was written for interactive desktop workloads and single-socket machines, so I wouldn&rsquo;t move it to a multi-socket server with hundreds of pods without careful measurements. A more predictable route is a dedicated layer in <code>scx_layered</code> with a short <code>slice_us</code> and <code>preempt</code> for the pods you yourself consider critical.</p>
<h3 id="kubevirt-vms">KubeVirt VMs <a class="anchor" href="#kubevirt-vms" aria-label="Link to the section “KubeVirt VMs”">#</a></h3><p>This is where things get both interesting and hard. A VM&rsquo;s vCPU is just an ordinary QEMU thread on the host, and the host scheduler knows nothing about what&rsquo;s happening inside the guest. sched_ext doesn&rsquo;t solve the classic &ldquo;preempted a vCPU that was holding a spinlock in the guest&rdquo; problem, but it does at least let you keep vCPUs of different VMs on separate LLCs, keep them away from noisy containers, and give them time slices according to your own policy. <code>scx_layered</code> has matches on the process name and its leader&rsquo;s name (<code>CommPrefix</code>, <code>PcommPrefix</code>), so QEMU threads can be put into a separate layer, and virt-launcher pods are already visible by cgroup.</p>
<p>For VMs with dedicated CPUs in KubeVirt, isolation is built on the CPU Manager, and a BPF scheduler won&rsquo;t break it: the kernel ignores CPU choices outside a task&rsquo;s allowed mask. But there isn&rsquo;t much to improve there either: on an exclusive core there&rsquo;s no competition.</p>
<h2 id="what-it-cant-do-yet">What it can&rsquo;t do yet <a class="anchor" href="#what-it-cant-do-yet" aria-label="Link to the section “What it can’t do yet”">#</a></h2><p>Here&rsquo;s the honest list, and it&rsquo;s longer than I&rsquo;d like.</p>
<ul>
<li><strong>cgroup limits depend on the scheduler.</strong> The fair class enforces <code>cpu.max</code>, <code>cpu.weight</code> and <code>cpu.idle</code> itself. For tasks under sched_ext, the kernel only passes these settings to the BPF scheduler through callbacks, and whether to enforce them is up to the scheduler. 6.12 only had <code>ops.cgroup_set_weight()</code> (with <code>CONFIG_EXT_GROUP_SCHED</code>); the <code>ops.cgroup_set_bandwidth()</code> callback for <code>cpu.max</code> arrived in 6.17. For Kubernetes this means: before loading a scheduler on a node, find out whether it honors your pods&rsquo; limits and requests. If it doesn&rsquo;t, you&rsquo;ll end up with a node where limits are formally set but don&rsquo;t actually work.</li>
<li><strong>One scheduler per machine.</strong> In 6.12 a single root BPF scheduler is loaded for the whole system. Recent kernels (Kconfig since 7.1) have a <code>CONFIG_EXT_SUB_SCHED</code> option for nested schedulers, but that&rsquo;s still the bleeding edge.</li>
<li><strong>Unstable ABI.</strong> The kernel documentation says it plainly: <code>sched_ext_ops</code> callbacks, constants and <code>scx_bpf_*</code> kfuncs can change between kernel versions without notice. The 6.13 rename is a live example. The scheduler binary has to be kept in step with the kernel, and kernel upgrades on nodes have to be tested together with it.</li>
<li><strong>No Kubernetes integration.</strong> kubelet knows nothing about sched_ext, and there&rsquo;s no field in the pod spec saying &ldquo;put me in this layer&rdquo;. All you have are cgroup and process matches that you configure on the node by hand or with your own DaemonSet.</li>
<li><strong>The watchdog is not instant protection.</strong> Up to 30 seconds of starvation by default, and <code>timeout_ms</code> can only be lowered.</li>
<li><strong>Build requirements.</strong> Recent clang and libbpf, a kernel with BTF. The README says directly that many distributions still ship tools that are too old.</li>
<li><strong>&ldquo;Production ready&rdquo; with caveats.</strong> Each scheduler&rsquo;s README says &ldquo;yes, if configured properly&rdquo;. That&rsquo;s honest, but it means the configuration is your job, not the authors'.</li>
</ul>
<p>My conclusion: sched_ext is already worth poking at, and you should, on nodes with a well-understood mixed workload, especially <code>scx_layered</code>, where the policy is explicit and readable from the config. But I wouldn&rsquo;t roll it out to every node in the cluster by default just yet, at least not until you&rsquo;ve checked how your chosen scheduler handles <code>cpu.max</code> and how it survives a kernel upgrade.</p>
<h2 id="sources">Sources <a class="anchor" href="#sources" aria-label="Link to the section “Sources”">#</a></h2><ul>
<li>Kernel documentation, &ldquo;Extensible Scheduler Class&rdquo; — <a href="https://github.com/torvalds/linux/blob/master/Documentation/scheduler/sched-ext.rst" rel="noopener" target="_blank">https://github.com/torvalds/linux/blob/master/Documentation/scheduler/sched-ext.rst</a></li>
<li>The same documentation for 6.12 (old names <code>scx_bpf_dispatch</code>/<code>scx_bpf_consume</code>) — <a href="https://github.com/torvalds/linux/blob/v6.12/Documentation/scheduler/sched-ext.rst" rel="noopener" target="_blank">https://github.com/torvalds/linux/blob/v6.12/Documentation/scheduler/sched-ext.rst</a></li>
<li><code>kernel/sched/ext.c</code> in v6.12 (<code>timeout_ms</code>, <code>SCX_WATCHDOG_MAX_TIMEOUT</code>, cgroup callbacks) — <a href="https://github.com/torvalds/linux/blob/v6.12/kernel/sched/ext.c" rel="noopener" target="_blank">https://github.com/torvalds/linux/blob/v6.12/kernel/sched/ext.c</a></li>
<li><code>kernel/sched/ext.c</code> in v6.17 (<code>cgroup_set_bandwidth</code>) — <a href="https://github.com/torvalds/linux/blob/v6.17/kernel/sched/ext.c" rel="noopener" target="_blank">https://github.com/torvalds/linux/blob/v6.17/kernel/sched/ext.c</a></li>
<li>README of the sched-ext/scx repository — <a href="https://github.com/sched-ext/scx/blob/main/README.md" rel="noopener" target="_blank">https://github.com/sched-ext/scx/blob/main/README.md</a></li>
<li>scx_rusty — <a href="https://github.com/sched-ext/scx/blob/main/scheds/rust/scx_rusty/README.md" rel="noopener" target="_blank">https://github.com/sched-ext/scx/blob/main/scheds/rust/scx_rusty/README.md</a></li>
<li>scx_lavd — <a href="https://github.com/sched-ext/scx/blob/main/scheds/rust/scx_lavd/README.md" rel="noopener" target="_blank">https://github.com/sched-ext/scx/blob/main/scheds/rust/scx_lavd/README.md</a></li>
<li>scx_layered, the README and the description of matches and layers in <code>src/main.rs</code> — <a href="https://github.com/sched-ext/scx/blob/main/scheds/rust/scx_layered/README.md" rel="noopener" target="_blank">https://github.com/sched-ext/scx/blob/main/scheds/rust/scx_layered/README.md</a>, <a href="https://github.com/sched-ext/scx/blob/main/scheds/rust/scx_layered/src/main.rs" rel="noopener" target="_blank">https://github.com/sched-ext/scx/blob/main/scheds/rust/scx_layered/src/main.rs</a></li>
<li>Example scx_layered configuration with a cgroup match — <a href="https://github.com/sched-ext/scx/blob/main/scheds/rust/scx_layered/examples/cgrp_contains.json" rel="noopener" target="_blank">https://github.com/sched-ext/scx/blob/main/scheds/rust/scx_layered/examples/cgrp_contains.json</a></li>
<li>The scx systemd service — <a href="https://github.com/sched-ext/scx/blob/main/services/README.md" rel="noopener" target="_blank">https://github.com/sched-ext/scx/blob/main/services/README.md</a></li>
<li>Catalog of scx schedulers — <a href="https://github.com/sched-ext/scx/blob/main/scheds/README.md" rel="noopener" target="_blank">https://github.com/sched-ext/scx/blob/main/scheds/README.md</a></li>
</ul>
]]></content:encoded></item><item><title>PreVote and CheckQuorum: why etcd won't re-elect a leader when you really want it to</title><link>https://kvaps.workshop.aenix.io/en/blog/raft-prevote-checkquorum/</link><guid>https://kvaps.workshop.aenix.io/en/blog/raft-prevote-checkquorum/</guid><pubDate>Wed, 25 Nov 2026 23:40:00 +0100</pubDate><category>etcd</category><category>raft</category><category>kubernetes</category><category>stretched-cluster</category><description>A look at two Raft extensions that protect etcd from unnecessary elections, and why the same extensions can leave a cluster member without a leader for hours during a partial network partition.</description><content:encoded><![CDATA[<p>When etcd is stretched across several sites, sooner or later you get a partial partition: sites A and B stop seeing each other, while the witness in C sees both. Everyone expects roughly the same thing: the cluster will &ldquo;somehow&rdquo; re-elect a leader on its own and keep working. In practice, the member on the cut-off side can stay leaderless for as long as the partition lasts, and the leader doesn&rsquo;t move anywhere.</p>
<p>This is not a bug. It is a direct consequence of two mechanisms that etcd enables on purpose: PreVote and CheckQuorum. Let&rsquo;s look at what each of them does, how they are implemented inside etcd, where leader lease fits in, and what all of this means for a stretched cluster.</p>
<p>One disclaimer up front: this is a review, not an experiment report. There are no measurements of my own here, only primary sources: Ongaro&rsquo;s dissertation, the <code>etcd-io/raft</code> and <code>etcd-io/etcd</code> code, and the etcd documentation. Links are at the end.</p>
<h2 id="why-this-is-needed-at-all">Why this is needed at all <a class="anchor" href="#why-this-is-needed-at-all" aria-label="Link to the section “Why this is needed at all”">#</a></h2><p>Basic Raft is simple. The leader sends heartbeats; a follower that hasn&rsquo;t heard from the leader for longer than the election timeout increments its term and starts an election. Any node that sees a message with a higher term must adopt that term and, if it was the leader, step down.</p>
<p>This simplicity has two unpleasant sides.</p>
<p><strong>First: unnecessary elections.</strong> A node cut off from the cluster keeps going into elections and keeps bumping its term, even though it cannot win. When connectivity returns, its high term spreads through the cluster via RequestVote or via an AppendEntries response, and the healthy leader is forced to step down. Ongaro describes exactly this scenario in §9.6 of his dissertation and honestly says such events are rare and each one costs a single re-election. But on a flapping network, &ldquo;rare&rdquo; quickly turns into &ldquo;all the time&rdquo;.</p>
<p><strong>Second: a stuck leader.</strong> A leader that ends up in the minority doesn&rsquo;t find out about it on its own. It keeps believing it is the leader, accepts requests from whichever clients can reach it, and cannot commit them. §6.2 of the dissertation puts it plainly: without an extra mechanism, such a leader can delay a client request indefinitely, even though on the other side there is already a leader of a newer term that could commit it.</p>
<p>PreVote fixes the first problem, CheckQuorum the second. And, as usual, there is a catch: in etcd they only work as a pair.</p>
<h2 id="how-it-works">How it works <a class="anchor" href="#how-it-works" aria-label="Link to the section “How it works”">#</a></h2><h3 id="prevote">PreVote <a class="anchor" href="#prevote" aria-label="Link to the section “PreVote”">#</a></h3><p>The idea of PreVote (§9.6) is to hold a &ldquo;trial&rdquo; election before the real one. The candidate does not increment its term; first it asks the others: would you vote for me if I started an election? A vote is granted only if the candidate&rsquo;s log is up to date enough and the voter itself hasn&rsquo;t heard from a valid leader for at least the base election timeout. The term grows only if a majority agrees.</p>
<p>As a result, a cut-off node cannot inflate its term: the majority won&rsquo;t respond to it. And when it comes back, they still won&rsquo;t respond, because the others are regularly getting heartbeats from the leader. The very first heartbeat turns it back into a follower in the same term.</p>
<p>In the <code>etcd-io/raft</code> library this is the <code>Config.PreVote</code> field, and its comment points directly to §9.6 of the dissertation. The candidate sends <code>MsgPreVote</code> with a term one higher than its own, but does not change its own term. The receiver doesn&rsquo;t change its term in response to <code>MsgPreVote</code> either.</p>
<h3 id="checkquorum">CheckQuorum <a class="anchor" href="#checkquorum" aria-label="Link to the section “CheckQuorum”">#</a></h3><p>CheckQuorum solves the second problem. The leader tracks which followers it has heard from and, once per election timeout, checks whether a quorum was active during that period. If not, it steps down to follower. In code it looks like this: in <code>tickHeartbeat</code>, when <code>electionElapsed &gt;= electionTimeout</code>, the leader sends itself <code>MsgCheckQuorum</code>, and in <code>stepLeader</code>, when <code>!r.trk.QuorumActive()</code>, it calls <code>becomeFollower</code> with this log message:</p>
<pre tabindex="0"><code>stepped down to follower since quorum is not active
</code></pre><p>After the check, all peers are marked inactive so that the next round measures activity from scratch.</p>
<h3 id="the-lease-check-on-voting-or-why-they-work-as-a-pair">The lease check on voting, or why they work as a pair <a class="anchor" href="#the-lease-check-on-voting-or-why-they-work-as-a-pair" aria-label="Link to the section “The lease check on voting, or why they work as a pair”">#</a></h3><p>The most interesting part is hidden in <code>raft.Step</code>. When a <code>MsgVote</code> or <code>MsgPreVote</code> arrives with a term higher than ours, the node first checks whether it is &ldquo;within the lease&rdquo; of the current leader:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-go" data-lang="go"><span class="line"><span class="cl"><span class="nx">inLease</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">r</span><span class="p">.</span><span class="nx">checkQuorum</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="nx">r</span><span class="p">.</span><span class="nx">lead</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nx">None</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="nx">r</span><span class="p">.</span><span class="nx">electionElapsed</span><span class="w"> </span><span class="p">&lt;</span><span class="w"> </span><span class="nx">r</span><span class="p">.</span><span class="nx">electionTimeout</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="k">if</span><span class="w"> </span><span class="p">!</span><span class="nx">force</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="nx">inLease</span><span class="w"> </span><span class="p">{</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span><span class="c1">// If a server receives a RequestVote request within the minimum election timeout</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span><span class="c1">// of hearing from a current leader, it does not update its term or grant its vote</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span><span class="o">...</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="kc">nil</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w">
</span></span></span></code></pre></div><p>This is the rule from §4.2.3 of the dissertation. It was introduced there for a different problem: protecting the cluster from servers that have been removed from the configuration and don&rsquo;t know it. If a node heard from the leader less than the minimum election timeout ago, it neither changes its term nor grants its vote. The request is simply ignored, and the etcd log gets a line with <code>lease is not expired</code>.</p>
<p>Note the <code>r.checkQuorum</code> in the condition. This check is exactly what implements the part of PreVote where the voter &ldquo;hasn&rsquo;t heard from a valid leader&rdquo;. In the voting function itself, for a <code>MsgPreVote</code> with a future term it&rsquo;s enough for the candidate&rsquo;s log to be no older than ours:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-go" data-lang="go"><span class="line"><span class="cl"><span class="nx">canVote</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">r</span><span class="p">.</span><span class="nx">Vote</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nx">m</span><span class="p">.</span><span class="nf">GetFrom</span><span class="p">()</span><span class="w"> </span><span class="o">||</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span><span class="p">(</span><span class="nx">r</span><span class="p">.</span><span class="nx">Vote</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nx">None</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="nx">r</span><span class="p">.</span><span class="nx">lead</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nx">None</span><span class="p">)</span><span class="w"> </span><span class="o">||</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span><span class="p">(</span><span class="nx">m</span><span class="p">.</span><span class="nf">GetType</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nx">pb</span><span class="p">.</span><span class="nx">MsgPreVote</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="nx">m</span><span class="p">.</span><span class="nf">GetTerm</span><span class="p">()</span><span class="w"> </span><span class="p">&gt;</span><span class="w"> </span><span class="nx">r</span><span class="p">.</span><span class="nx">Term</span><span class="p">)</span><span class="w">
</span></span></span></code></pre></div><p>So in <code>etcd-io/raft</code>, PreVote without CheckQuorum only protects against term inflation; it doesn&rsquo;t stop a follower that can hear the leader from voting for a candidate. Only the combination gives you the full picture from §9.6.</p>
<p>On the leader, <code>r.lead</code> equals its own ID, and <code>electionElapsed</code> is reset every election timeout, so the leader is also always &ldquo;within the lease&rdquo; and ignores other nodes&rsquo; elections with a higher term.</p>
<p>There is one exception: <code>force</code>. These are elections triggered by a leadership transfer (<code>campaignTransfer</code>). The leader itself asked the candidate to replace it, so the lease check is skipped. Ongaro describes this as a special flag meaning &ldquo;I have permission to depose the leader, it asked me to&rdquo;. In etcd this path is exposed externally via <code>etcdctl move-leader</code>.</p>
<h2 id="what-exactly-is-enabled-in-etcd">What exactly is enabled in etcd <a class="anchor" href="#what-exactly-is-enabled-in-etcd" aria-label="Link to the section “What exactly is enabled in etcd”">#</a></h2><p>Versions get mixed up a lot here, so point by point:</p>
<ul>
<li><strong>CheckQuorum</strong> is always enabled in etcd and cannot be configured. The server&rsquo;s <code>raftConfig</code> has <code>CheckQuorum: true</code>, and there is no flag for it. That was the case in the 3.4 branch and it still is in current <code>main</code>.</li>
<li><strong>PreVote</strong> is controlled by the <code>--pre-vote</code> flag. The flag already existed in 3.4 but was off by default: <code>embed/config.go</code> in the release-3.4 branch has <code>PreVote: false</code> with the comment <code>TODO: enable by default in v3.5</code>. It was turned on by default in <strong>v3.5.0</strong> (PR etcd-io/etcd#12770); the CHANGELOG phrases it as &ldquo;Should prevent disrupting RAFT leader by an individual member&rdquo;. So if you still have a 3.4 running somewhere with default settings, PreVote is off.</li>
<li><strong>ReadOnlyOption</strong> is not set by etcd, so the default <code>ReadOnlySafe</code> is used. Linearizable reads go through ReadIndex, meaning the leader confirms its leadership with a round of heartbeats to a quorum before replying. More on that below.</li>
<li><strong>Timings</strong>: <code>--heartbeat-interval</code> defaults to 100 ms, <code>--election-timeout</code> to 1000 ms. etcd passes <code>HeartbeatTick: 1</code> to raft and <code>ElectionTick</code> as the ratio of one to the other, so 10 ticks by default. The actual election timeout on each node is randomized in the range of one to two election timeouts.</li>
</ul>
<p>The tuning documentation recommends setting the heartbeat roughly equal to the maximum average RTT between members, and the election timeout to at least 10 times the RTT. The upper limit is 50 seconds, and according to the docs it&rsquo;s only meant for globally distributed clusters.</p>
<p>The easiest way to check what you&rsquo;re actually running is via process arguments and logs:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="c1"># which flags were passed (if not passed, your version&#39;s default applies)</span>
</span></span><span class="line"><span class="cl">ps -o <span class="nv">args</span><span class="o">=</span> -C etcd <span class="p">|</span> tr <span class="s1">&#39; &#39;</span> <span class="s1">&#39;\n&#39;</span> <span class="p">|</span> grep -E <span class="s1">&#39;pre-vote|election-timeout|heartbeat-interval&#39;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># version, leader and term of each member</span>
</span></span><span class="line"><span class="cl">etcdctl --endpoints<span class="o">=</span>https://10.0.0.1:2379,https://10.0.0.2:2379,https://10.0.0.3:2379 <span class="se">\
</span></span></span><span class="line"><span class="cl">  --cacert<span class="o">=</span>ca.crt --cert<span class="o">=</span>client.crt --key<span class="o">=</span>client.key <span class="se">\
</span></span></span><span class="line"><span class="cl">  endpoint status -w table
</span></span></code></pre></div><h2 id="a-partial-partition-step-by-step">A partial partition, step by step <a class="anchor" href="#a-partial-partition-step-by-step" aria-label="Link to the section “A partial partition, step by step”">#</a></h2><p>Take three members: A and B at the two main sites, witness C at the third. The leader is in A. The A–B link breaks, C sees both.</p>
<p><strong>With PreVote and CheckQuorum (etcd 3.5+ default):</strong></p>
<ol>
<li>A keeps hearing C. The A+C quorum is active, CheckQuorum leaves the leader alone, writes get committed.</li>
<li>B stops receiving heartbeats and, after the election timeout, starts PreVote. It sends <code>MsgPreVote</code> to C (the message never reaches A).</li>
<li>C hears the leader, is within the lease, and ignores the request. B can&rsquo;t get a majority, the term doesn&rsquo;t grow.</li>
<li>This goes on until connectivity returns. B is leaderless the whole time, and every client connected to it gets errors or timeouts.</li>
</ol>
<p>For Raft this is correct behavior: the cluster is alive, there&rsquo;s a quorum, no unnecessary elections. But if B runs a kube-apiserver that only talks to its local etcd, from Kubernetes&rsquo; point of view that&rsquo;s one half-dead control-plane node. The fix is a single command: <code>etcdctl move-leader</code> to the member in C, which sees everyone. Then A and B are both neighbors of the leader. This is exactly the command that threek automates; it&rsquo;s covered in detail in Ænix&rsquo;s Habr article about losing a datacenter. There is no ready-made mechanism upstream: leader priority was proposed in etcd-io/etcd#10201 and the issue was closed, and while the raft library does have <code>ForgetLeader</code> (etcd-io/raft#78), etcd doesn&rsquo;t use it.</p>
<p><strong>Without PreVote and without CheckQuorum (plain Raft):</strong></p>
<ol>
<li>After the election timeout, B increments its term and sends <code>MsgVote</code> to C.</li>
<li>C sees the higher term, adopts it, loses its leader, and votes for B if B&rsquo;s log is not older.</li>
<li>B becomes leader in the new term. A learns about the new term from C and steps down to follower.</li>
<li>Now A is the one without a leader, because it can&rsquo;t hear B. After the election timeout A starts an election with its own term+1, and everything repeats in the other direction.</li>
</ol>
<p>That&rsquo;s the election storm: leadership bounces between A and B, and every move is a window during which writes don&rsquo;t commit. This is precisely what PreVote and CheckQuorum protect against.</p>
<p><strong>CheckQuorum only, no PreVote (etcd 3.4 default):</strong> C is within the lease and won&rsquo;t give B its vote, so there&rsquo;s no storm. But B keeps bumping its term on every attempt. When the A–B link comes back, B&rsquo;s high term reaches A in an AppendEntries response, and the leader has to step down — exactly the §9.6 scenario. One unnecessary re-election after recovery.</p>
<h2 id="asymmetry-and-the-stuck-leader">Asymmetry and the stuck leader <a class="anchor" href="#asymmetry-and-the-stuck-leader" aria-label="Link to the section “Asymmetry and the stuck leader”">#</a></h2><p>&ldquo;Asymmetric&rdquo; partitions deserve a separate word. A packet filter that cuts traffic in one direction almost always becomes a two-way cut for TCP: no stream survives without ACKs. So for etcd, asymmetry in practice looks either like the partial connectivity described above, or like stateful filtering, where only new connections are cut and established ones keep living. etcd peer streams are long-lived, and in this mode the cluster can look healthy for quite a while, until some connection gets re-established.</p>
<p>The most dangerous case for &ldquo;sticking&rdquo; is when the leader is isolated entirely, but its clients can still reach it. Without CheckQuorum it remains leader in its own view forever. With CheckQuorum it steps down to follower after the election timeout, and clients get an error and can switch to another endpoint. That&rsquo;s exactly why CheckQuorum can&rsquo;t be turned off in etcd.</p>
<h2 id="where-leader-lease-fits-in">Where leader lease fits in <a class="anchor" href="#where-leader-lease-fits-in" aria-label="Link to the section “Where leader lease fits in”">#</a></h2><p>The word lease is used for three different things here, and it&rsquo;s worth separating them.</p>
<p><strong>Lease as in the lease check on voting.</strong> This is <code>inLease</code> from the code above: &ldquo;I heard from the leader less than an election timeout ago, so I&rsquo;m not electing anyone&rdquo;. Only works with CheckQuorum.</p>
<p><strong>Leader lease for reads (§6.4.1).</strong> Ongaro describes a variant where the leader, after a majority acknowledges its heartbeats, assumes nobody else will become leader for roughly an election timeout, and serves reads without contacting the quorum. The price is an assumption of bounded clock drift. Ongaro lists explicitly what breaks it: scheduler and GC pauses, VM migrations, clock rate adjustments. When it&rsquo;s violated you can return arbitrarily stale data, and LogCabin doesn&rsquo;t implement this variant. <code>etcd-io/raft</code> has it as <code>ReadOnlyLeaseBased</code>, and the library refuses to start with it unless CheckQuorum is on. etcd doesn&rsquo;t enable it and uses ReadIndex. For a stretched cluster that&rsquo;s good news: an isolated leader cannot serve a linearizable read, because ReadIndex requires a quorum response. Serializable reads, which a member serves from local data, are a different story: they can be stale on either side of the partition.</p>
<p><strong>Lease in the etcd API</strong>, i.e. keys with a TTL. On a leader change, the new leader calls <code>lessor.Promote(electionTimeout)</code> and extends all leases. Without checkpoints, the remaining TTL is not preserved: the lease starts counting from scratch, for the full TTL plus the election timeout. To avoid this there&rsquo;s <code>--experimental-enable-lease-checkpoint</code> (off by default in 3.5) and <code>--experimental-enable-lease-checkpoint-persist</code>, added in 3.5.2. In 3.6 the same thing is also wired up as the <code>LeaseCheckpoint</code> feature gate, off by default.</p>
<h2 id="what-this-means-for-a-stretched-cluster">What this means for a stretched cluster <a class="anchor" href="#what-this-means-for-a-stretched-cluster" aria-label="Link to the section “What this means for a stretched cluster”">#</a></h2><p>Here are the practical takeaways.</p>
<p><strong>Losing an entire site is a normal scenario.</strong> If the site with the leader disappears completely, the remaining members fall out of the lease after the election timeout, PreVote succeeds, and a new leader is elected. Failover time is one to two election timeouts plus network latency. If you raised the election timeout to fit inter-city RTT, failover gets slower in the same proportion. That&rsquo;s a deliberate price, not a misconfiguration.</p>
<p><strong>A partial partition is not normal.</strong> Raft survives it, but not the way Kubernetes expects. The quorum stays, and the cut-off member stays leaderless until the partition ends. If the apiserver on that node only talks to its local etcd, the node drops out. Options: give the apiserver all etcd endpoints, or move the leader to the witness with <code>move-leader</code>, manually or automatically. Disabling PreVote for the sake of &ldquo;self-healing&rdquo; is a bad idea: instead of one stuck member you get an election storm.</p>
<p><strong>etcd leases live longer after a leader move.</strong> If you&rsquo;ve built locks or leader election on top of etcd leases — for example, Patroni with etcd as its DCS — then after losing the site with the etcd leader, the lock will be released not after the TTL but later. Leader election in Kubernetes itself works differently: <code>coordination.k8s.io/Lease</code> objects store the renewal time, and clients make the decision based on their own clocks, not on etcd&rsquo;s TTL.</p>
<p><strong>The witness must see everyone.</strong> All the storm protection rests on C hearing the leader. So the witness is not &ldquo;some third machine&rdquo;, it&rsquo;s the best-connected member in the cluster.</p>
<h2 id="how-to-check-it-yourself">How to check it yourself <a class="anchor" href="#how-to-check-it-yourself" aria-label="Link to the section “How to check it yourself”">#</a></h2><p>You need three etcd 3.5+ instances on three machines, or in three containers with separate network namespaces, so you can cut traffic between pairs. The steps below are for node B, where <code>A</code> is the address of member A.</p>
<p>A symmetric A–B partition, with C still seeing both:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">iptables -I INPUT  -s <span class="s2">&#34;</span><span class="nv">$A</span><span class="s2">&#34;</span> -j DROP
</span></span><span class="line"><span class="cl">iptables -I OUTPUT -d <span class="s2">&#34;</span><span class="nv">$A</span><span class="s2">&#34;</span> -j DROP
</span></span></code></pre></div><p>Stateful firewall mode, where only new connections are cut:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">iptables -I INPUT -s <span class="s2">&#34;</span><span class="nv">$A</span><span class="s2">&#34;</span> -m conntrack --ctstate NEW -j DROP
</span></span></code></pre></div><p>What to watch:</p>
<ul>
<li><code>etcdctl endpoint status -w table</code> across all three endpoints: who the leader is and whether <code>RAFT TERM</code> grows. With PreVote, B&rsquo;s term should not grow during the partition.</li>
<li>C&rsquo;s logs: lines like <code>ignored MsgPreVote from ... lease is not expired</code>.</li>
<li>The leader&rsquo;s logs after full isolation: <code>stepped down to follower since quorum is not active</code>.</li>
<li>The <code>etcd_server_has_leader</code>, <code>etcd_server_is_leader</code> and <code>etcd_server_leader_changes_seen_total</code> metrics on each member.</li>
</ul>
<p>Repeat the same with <code>--pre-vote=false</code>, and the difference in term behavior is visible immediately. To restore the network, use the same rules with <code>-D</code> instead of <code>-I</code>. Do this on a test setup, not on a cluster that hosts your control plane.</p>
<h2 id="what-it-cant-do">What it can&rsquo;t do <a class="anchor" href="#what-it-cant-do" aria-label="Link to the section “What it can’t do”">#</a></h2><ul>
<li><strong>It doesn&rsquo;t pick a leader by connectivity.</strong> PreVote and CheckQuorum answer the question &ldquo;do we need an election&rdquo;, not &ldquo;where is the best place for the leader&rdquo;. Raft doesn&rsquo;t know that C sees everyone and A doesn&rsquo;t.</li>
<li><strong>It doesn&rsquo;t fix a partial partition.</strong> That stays a stable but inconvenient state: the leaderless member waits for the network to be fixed.</li>
<li><strong>It doesn&rsquo;t help when removing members.</strong> §4.2.3 explicitly shows a case where PreVote is powerless: a server being removed that has a sufficiently fresh log. What saves you there is the lease check, not the log comparison.</li>
<li><strong>It doesn&rsquo;t give you fast failover across distant sites.</strong> The election timeout is tied to RTT, and you can&rsquo;t shorten failover time without risking spurious elections.</li>
<li><strong>It doesn&rsquo;t protect serializable reads.</strong> A cut-off member will keep serving whatever it has.</li>
</ul>
<p>In short, PreVote and CheckQuorum make Raft resistant to unnecessary elections, and that is exactly why it won&rsquo;t re-elect a leader when you really want it to :)</p>
<h2 id="sources">Sources <a class="anchor" href="#sources" aria-label="Link to the section “Sources”">#</a></h2><ul>
<li>Diego Ongaro, &ldquo;Consensus: Bridging Theory and Practice&rdquo;, §4.2.3, §6.2, §6.4.1, §9.6 — <a href="https://github.com/ongardie/dissertation/blob/master/stanford.pdf" rel="noopener" target="_blank">https://github.com/ongardie/dissertation/blob/master/stanford.pdf</a></li>
<li><code>etcd-io/raft</code>, <code>raft.go</code> (Config, Step, tickHeartbeat, stepLeader) — <a href="https://github.com/etcd-io/raft/blob/main/raft.go" rel="noopener" target="_blank">https://github.com/etcd-io/raft/blob/main/raft.go</a></li>
<li><code>etcd-io/etcd</code>, <code>server/etcdserver/bootstrap.go</code> (raftConfig) — <a href="https://github.com/etcd-io/etcd/blob/main/server/etcdserver/bootstrap.go" rel="noopener" target="_blank">https://github.com/etcd-io/etcd/blob/main/server/etcdserver/bootstrap.go</a></li>
<li><code>etcd-io/etcd</code>, <code>embed/config.go</code> in the release-3.4 branch (PreVote false by default) — <a href="https://github.com/etcd-io/etcd/blob/release-3.4/embed/config.go" rel="noopener" target="_blank">https://github.com/etcd-io/etcd/blob/release-3.4/embed/config.go</a></li>
<li>etcd 3.5 CHANGELOG — <a href="https://github.com/etcd-io/etcd/blob/main/CHANGELOG/CHANGELOG-3.5.md" rel="noopener" target="_blank">https://github.com/etcd-io/etcd/blob/main/CHANGELOG/CHANGELOG-3.5.md</a></li>
<li>PR &ldquo;TODO&rsquo;s 3.5: Decommission metrics, PreVote=true&rdquo; — <a href="https://github.com/etcd-io/etcd/pull/12770" rel="noopener" target="_blank">https://github.com/etcd-io/etcd/pull/12770</a></li>
<li>etcd 3.5, Configuration options — <a href="https://etcd.io/docs/v3.5/op-guide/configuration/" rel="noopener" target="_blank">https://etcd.io/docs/v3.5/op-guide/configuration/</a></li>
<li>etcd 3.5, Tuning — <a href="https://etcd.io/docs/v3.5/tuning/" rel="noopener" target="_blank">https://etcd.io/docs/v3.5/tuning/</a></li>
<li><code>etcd-io/etcd</code>, <code>server/lease/lessor.go</code> and <code>lease.go</code> (Promote, refresh) — <a href="https://github.com/etcd-io/etcd/blob/main/server/lease/lessor.go" rel="noopener" target="_blank">https://github.com/etcd-io/etcd/blob/main/server/lease/lessor.go</a></li>
<li><code>etcd-io/etcd</code>, <code>server/features/etcd_features.go</code> (LeaseCheckpoint) — <a href="https://github.com/etcd-io/etcd/blob/release-3.6/server/features/etcd_features.go" rel="noopener" target="_blank">https://github.com/etcd-io/etcd/blob/release-3.6/server/features/etcd_features.go</a></li>
<li>etcd-io/etcd#10201, &ldquo;Consider priority during election&rdquo; — <a href="https://github.com/etcd-io/etcd/issues/10201" rel="noopener" target="_blank">https://github.com/etcd-io/etcd/issues/10201</a></li>
<li>etcd-io/raft#78, &ldquo;Add ForgetLeader&rdquo; — <a href="https://github.com/etcd-io/raft/pull/78" rel="noopener" target="_blank">https://github.com/etcd-io/raft/pull/78</a></li>
<li>&ldquo;Everything is Ready, yet nothing works: how we taught Kubernetes to survive losing a datacenter&rdquo; (in Russian) — <a href="https://habr.com/ru/companies/aenix/articles/1098254/" rel="noopener" target="_blank">https://habr.com/ru/companies/aenix/articles/1098254/</a></li>
</ul>
]]></content:encoded></item></channel></rss>