{"title":"Sitegui's Blog","id":"https:\/\/sitegui.dev\/","updated":"2026-11-28T03:00:38.266555057+00:00","author":{"name":"sitegui"},"category":[{"@attributes":{"term":"programming"}},{"@attributes":{"term":"math"}},{"@attributes":{"term":"boardgames"}}],"icon":"https:\/\/sitegui.dev\/static\/favicon.png","link":{"@attributes":{"href":"https:\/\/sitegui.dev\/feed.xml","rel":"self"}},"subtitle":"My personal webspace, with content about programming, math and boardgames","entry":[{"title":"A Rust trick: use method-call resolution order to implement trait fallback","id":"98423876-043a-4bb3-8c17-9895f8c69715","updated":"2026-11-04T00:00:00+00:00","author":{"name":"sitegui"},"category":{"@attributes":{"term":"rust"}},"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/11\/a-rust-trick-use-method-call-resolution-order-to-implement-trait-fallback","rel":"alternate"}},"published":"2026-11-04T00:00:00+00:00","content":"<p>Before explaining what the trick is and how it works, I'll take a general example: let's say in a Rust codebase or\nlibrary you find a better way of doing something, but you still want to keep compatibility with the rest of the codebase\nor ecosystem. Say you have a new trait <code>Awesome<\/code> that does what <code>Debug<\/code> does, but better (for some definition of\nbetter).<\/p>\n<p>You may want to express code that can consume a value that it either <code>Awesome<\/code> or <code>Debug<\/code> like this:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">do_it<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f2777a;\">value<\/span><span style=\"color:#d3d0c8;\">: impl Awesome or Debug) {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">if <\/span><span style=\"color:#d3d0c8;\">impls!(value, Awesome) {\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#747369;\">\/\/ use Awesome&#39;s logic\n<\/span><span style=\"color:#d3d0c8;\">    } <\/span><span style=\"color:#cc99cc;\">else <\/span><span style=\"color:#d3d0c8;\">{\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#747369;\">\/\/ use Debug&#39;s logic\n<\/span><span style=\"color:#d3d0c8;\">    }\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<p>This syntax is invented, but the idea is: if the caller gives you a type that implements <code>Awesome<\/code> use its\nimplementation, otherwise fallback to <code>Debug<\/code>.<\/p>\n<p>A concrete example of such a need is the crate <a href=\"https:\/\/docs.rs\/maud\/latest\/maud\/\">maud<\/a>, that declares the <code>Render<\/code>\ntrait, which is a better <code>Display<\/code> because it handles html-escaping more efficiently. A simplifier version of it is:<\/p>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#cc99cc;\">trait <\/span><span style=\"color:#d3d0c8;\">Render {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#747369;\">\/\/\/ Append the data to the buffer,\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#747369;\">\/\/\/ escaping it as necessary\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">render_to<\/span><span style=\"color:#d3d0c8;\">(&amp;<\/span><span style=\"color:#f2777a;\">self<\/span><span style=\"color:#d3d0c8;\">, <\/span><span style=\"color:#f2777a;\">buffer<\/span><span style=\"color:#d3d0c8;\">: &amp;<\/span><span style=\"color:#cc99cc;\">mut<\/span><span style=\"color:#d3d0c8;\"> String);\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<h2>The trick<\/h2>\n<p>To illustrate, say this is your <code>Awesome<\/code> trait and that you've implemented it for a list of integers:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#cc99cc;\">trait <\/span><span style=\"color:#d3d0c8;\">Awesome {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">make_it_awesome<\/span><span style=\"color:#d3d0c8;\">(&amp;<\/span><span style=\"color:#f2777a;\">self<\/span><span style=\"color:#d3d0c8;\">) -&gt; String;\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#cc99cc;\">impl <\/span><span style=\"color:#d3d0c8;\">Awesome <\/span><span style=\"color:#cc99cc;\">for <\/span><span style=\"color:#d3d0c8;\">Vec&lt;<\/span><span style=\"color:#cc99cc;\">i32<\/span><span style=\"color:#d3d0c8;\">&gt; {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">make_it_awesome<\/span><span style=\"color:#d3d0c8;\">(&amp;<\/span><span style=\"color:#f2777a;\">self<\/span><span style=\"color:#d3d0c8;\">) -&gt; String {\n<\/span><span style=\"color:#d3d0c8;\">        &quot;<\/span><span style=\"color:#99cc99;\">AWESOME<\/span><span style=\"color:#d3d0c8;\">&quot;.<\/span><span style=\"color:#66cccc;\">to_string<\/span><span style=\"color:#d3d0c8;\">()\n<\/span><span style=\"color:#d3d0c8;\">    }\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<p>Then you prepare the incantation with:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#cc99cc;\">struct <\/span><span style=\"color:#d3d0c8;\">Choose&lt;T&gt;(T);\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#cc99cc;\">trait <\/span><span style=\"color:#d3d0c8;\">UseDebug {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">choose<\/span><span style=\"color:#d3d0c8;\">&lt;T: Debug&gt;(&amp;<\/span><span style=\"color:#f2777a;\">self<\/span><span style=\"color:#d3d0c8;\">, <\/span><span style=\"color:#f2777a;\">value<\/span><span style=\"color:#d3d0c8;\">: &amp;T) -&gt; String {\n<\/span><span style=\"color:#d3d0c8;\">        format!(&quot;<\/span><span style=\"color:#f99157;\">{:?}<\/span><span style=\"color:#d3d0c8;\">&quot;, value)\n<\/span><span style=\"color:#d3d0c8;\">    }\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#cc99cc;\">trait <\/span><span style=\"color:#d3d0c8;\">UseAwesome {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">choose<\/span><span style=\"color:#d3d0c8;\">&lt;T: Awesome&gt;(&amp;<\/span><span style=\"color:#f2777a;\">self<\/span><span style=\"color:#d3d0c8;\">, <\/span><span style=\"color:#f2777a;\">value<\/span><span style=\"color:#d3d0c8;\">: &amp;T) -&gt; String {\n<\/span><span style=\"color:#d3d0c8;\">        value.<\/span><span style=\"color:#66cccc;\">make_it_awesome<\/span><span style=\"color:#d3d0c8;\">()\n<\/span><span style=\"color:#d3d0c8;\">    }\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#cc99cc;\">impl<\/span><span style=\"color:#d3d0c8;\">&lt;T: Debug&gt; UseDebug <\/span><span style=\"color:#cc99cc;\">for <\/span><span style=\"color:#d3d0c8;\">Choose&lt;&amp;T&gt; {}\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#cc99cc;\">impl<\/span><span style=\"color:#d3d0c8;\">&lt;T: Awesome&gt; UseAwesome <\/span><span style=\"color:#cc99cc;\">for <\/span><span style=\"color:#d3d0c8;\">&amp;Choose&lt;&amp;T&gt; {}\n<\/span><\/code><\/pre>\n<p>Then you can use it like this:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">main<\/span><span style=\"color:#d3d0c8;\">() {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#747369;\">\/\/ Uses the `Awesome` implementation\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">let<\/span><span style=\"color:#d3d0c8;\"> x = vec![<\/span><span style=\"color:#f99157;\">3<\/span><span style=\"color:#d3d0c8;\">, <\/span><span style=\"color:#f99157;\">14<\/span><span style=\"color:#d3d0c8;\">, <\/span><span style=\"color:#f99157;\">15<\/span><span style=\"color:#d3d0c8;\">];\n<\/span><span style=\"color:#d3d0c8;\">    assert_eq!((&amp;&amp;Choose(&amp;x)).<\/span><span style=\"color:#66cccc;\">choose<\/span><span style=\"color:#d3d0c8;\">(&amp;x), &quot;<\/span><span style=\"color:#99cc99;\">AWESOME<\/span><span style=\"color:#d3d0c8;\">&quot;);\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#747369;\">\/\/ Uses the `Debug` implementation\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">let<\/span><span style=\"color:#d3d0c8;\"> x = <\/span><span style=\"color:#f99157;\">31415<\/span><span style=\"color:#d3d0c8;\">;\n<\/span><span style=\"color:#d3d0c8;\">    assert_eq!((&amp;&amp;Choose(&amp;x)).<\/span><span style=\"color:#66cccc;\">choose<\/span><span style=\"color:#d3d0c8;\">(&amp;x), &quot;<\/span><span style=\"color:#99cc99;\">31415<\/span><span style=\"color:#d3d0c8;\">&quot;);\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<p>Unfortunately, I don't think that you can write a <em>function<\/em> like <code>fn do_it()<\/code> in the opening of this blog post, instead\nyou can declare it as a macro:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#66cccc;\">macro_rules! <\/span><span style=\"color:#d3d0c8;\">do_it {\n<\/span><span style=\"color:#d3d0c8;\">    (<\/span><span style=\"color:#f2777a;\">$value<\/span><span style=\"color:#d3d0c8;\">:<\/span><span style=\"color:#cc99cc;\">expr<\/span><span style=\"color:#d3d0c8;\">) =&gt; {{\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#cc99cc;\">let<\/span><span style=\"color:#d3d0c8;\"> ref_to_value = &amp;<\/span><span style=\"color:#f2777a;\">$value<\/span><span style=\"color:#d3d0c8;\">;\n<\/span><span style=\"color:#d3d0c8;\">        (&amp;&amp;Choose(ref_to_value)).<\/span><span style=\"color:#66cccc;\">choose<\/span><span style=\"color:#d3d0c8;\">(ref_to_value)\n<\/span><span style=\"color:#d3d0c8;\">    }}\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">main<\/span><span style=\"color:#d3d0c8;\">() {\n<\/span><span style=\"color:#d3d0c8;\">    assert_eq!(do_it!(<\/span><span style=\"color:#f99157;\">31415<\/span><span style=\"color:#d3d0c8;\">), &quot;<\/span><span style=\"color:#99cc99;\">31415<\/span><span style=\"color:#d3d0c8;\">&quot;);\n<\/span><span style=\"color:#d3d0c8;\">    assert_eq!(do_it!(vec![<\/span><span style=\"color:#f99157;\">3<\/span><span style=\"color:#d3d0c8;\">, <\/span><span style=\"color:#f99157;\">14<\/span><span style=\"color:#d3d0c8;\">, <\/span><span style=\"color:#f99157;\">15<\/span><span style=\"color:#d3d0c8;\">]), &quot;<\/span><span style=\"color:#99cc99;\">AWESOME<\/span><span style=\"color:#d3d0c8;\">&quot;);\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<p>Here's <a href=\"https:\/\/play.rust-lang.org\/?version=stable&amp;mode=debug&amp;edition=2024&amp;gist=6350f82125f782e41dd1431ecaf0fc63\">a link to Rust's language playground<\/a>\nif you want to tweak and play with it yourself!<\/p>\n<h2>How and why it works<\/h2>\n<p>The trick exploits how Rust looks for the implementation of the method when you write something like <code>a.b()<\/code>. You can\ncheck <a href=\"https:\/\/doc.rust-lang.org\/reference\/expressions\/method-call-expr.html\">the language's reference<\/a> for the details,\nbut in short: if the type of the <em>receiver<\/em> <code>a<\/code> is <code>T<\/code>, Rust will:<\/p>\n<ol>\n<li>first look for a candidate implemented for <code>T<\/code>, <code>&amp;T<\/code> or <code>&amp;mut T<\/code>. Return it if found.<\/li>\n<li>if possible, dereference <code>a<\/code> with <code>Deref::deref(&amp;a)<\/code> into <code>T2 = &lt;T as Deref&gt;::Target<\/code> then look for a candidate\nimplemented for <code>T2<\/code>, <code>&amp;T2<\/code> or <code>&amp;mut T2<\/code>. Return it if found.<\/li>\n<li>repeat 2 until a candidate is found or the expression can no longer be dereferenced<\/li>\n<\/ol>\n<p>This may seem quite convoluted and arbitrary, but it's central to how Rust works. For example, there is no method\n<code>len()<\/code> in <code>String<\/code>. Instead, it's declared in <code>str<\/code>. When you write <code>a.len()<\/code> and <code>a<\/code> is a <code>String<\/code>, Rust will first\nsearch <code>len()<\/code> in <code>String<\/code>, <code>&amp;String<\/code> and <code>&amp;mut String<\/code>. Since it doesn't exist, it will then deref <code>String<\/code> to\n<code>str<\/code> and repeat the search, finding it in <code>&amp;str<\/code>.<\/p>\n<p>So you probably already see where I'm going with all of this: the trick is to use these rules and the helper struct\n<code>Choose&lt;T&gt;<\/code> to first try to match <code>Awesome<\/code> and, only in after a dereference step, <code>Debug<\/code>.<\/p>\n<p>When I wrote <code>(&amp;&amp;Choose(&amp;x)).choose(&amp;x)<\/code>, the <em>receiver<\/em> is now <code>&amp;&amp;Choose(&amp;x)<\/code>. That's a lot of borrows, for sure, but\nlook carefully: the type of this expression is <code>&amp;&amp;Choose&lt;&amp;T&gt;<\/code>, where <code>T<\/code> is the type of the value <code>x<\/code>. Rust will look\nfor the implementation of the method <code>choose()<\/code> in it.<\/p>\n<p>Let's assume that <code>T<\/code> implements both <code>Awesome<\/code> and <code>Debug<\/code>. So the declaration\n<code>impl&lt;T: Debug&gt; UseDebug for Choose&lt;&amp;T&gt; {}<\/code> means that <code>UseDebug<\/code> is available for <code>Choose&lt;&amp;T&gt;<\/code>, which means that\n<code>UseDebug::choose()<\/code> is available for <code>&amp;Choose&lt;&amp;T&gt;<\/code>. This extra <code>&amp;<\/code> comes from the fact that <code>choose()<\/code> takes <code>&amp;self<\/code>.<\/p>\n<p>Likewise, the declaration <code>impl&lt;T: Awesome&gt; UseAwesome for &amp;Choose&lt;&amp;T&gt; {}<\/code> means that <code>UseAwesome::choose()<\/code> is\navailable for <code>&amp;&amp;Choose&lt;&amp;T&gt;<\/code>. Note one extra <code>&amp;<\/code> when compared with <code>UseDebug<\/code>, that's the heart of this trick!<\/p>\n<p>Think like the compiler: Rust will consider <code>&amp;&amp;Choose&lt;&amp;T&gt;<\/code>: <code>UseAwesome::choose()<\/code> is a candidate, however\n<code>UseDebug::choose()<\/code> is not because it's declared for <code>&amp;Choose&lt;&amp;T&gt;<\/code> (one less <code>&amp;<\/code>). So the single candidate is chosen\nand the code compiled is <code>x.make_it_awesome()<\/code>.<\/p>\n<p>On the other hand, if <code>T<\/code> does not implement <code>Awesome<\/code>, then <code>UseAwesome::choose()<\/code> is no longer a candidate for\n<code>&amp;&amp;Choose&lt;&amp;T&gt;<\/code>. Due to the lack of candidates, Rust dereferences <code>&amp;&amp;Choose&lt;&amp;T&gt;<\/code> into <code>&amp;Choose&lt;&amp;T&gt;<\/code> and find the only\ncandidate now <code>UseDebug::choose()<\/code>. The compile code then is <code>format!(&quot;{:?}&quot;, x)<\/code>.<\/p>\n<h2>Other possible (semi-)solutions<\/h2>\n<p>I'll show some possible attempts and semi-solutions to the problem, that I can think of:<\/p>\n<h3>Use blanket implementations<\/h3>\n<p>You can implement your trait <code>Awesome<\/code> to every type that implements <code>Debug<\/code> with something like:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#cc99cc;\">impl<\/span><span style=\"color:#d3d0c8;\">&lt;T&gt; Awesome <\/span><span style=\"color:#cc99cc;\">for <\/span><span style=\"color:#d3d0c8;\">T\n<\/span><span style=\"color:#cc99cc;\">where\n<\/span><span style=\"color:#d3d0c8;\">    T: Debug\n<\/span><span style=\"color:#d3d0c8;\">{\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#747369;\">\/\/ ...\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<p>This works, <em>but<\/em> you cannot manually implement <code>Awesome<\/code> to types that have <code>Debug<\/code>. If great part of the reason to\nhave created <code>Awesome<\/code> is to provide something better than <code>Debug<\/code> (performance and\/or correctness), then it's sad.<\/p>\n<h3>Use an adaptor wrapper<\/h3>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#cc99cc;\">struct <\/span><span style=\"color:#d3d0c8;\">MakeItAwesome&lt;T&gt;(pub T);\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#cc99cc;\">impl<\/span><span style=\"color:#d3d0c8;\">&lt;T&gt; Awesome <\/span><span style=\"color:#cc99cc;\">for <\/span><span style=\"color:#d3d0c8;\">MakeItAwesome&lt;T&gt;\n<\/span><span style=\"color:#cc99cc;\">where\n<\/span><span style=\"color:#d3d0c8;\">    T: Debug\n<\/span><span style=\"color:#d3d0c8;\">{\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#747369;\">\/\/ ...\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">do_it<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f2777a;\">value<\/span><span style=\"color:#d3d0c8;\">: impl Awesome) {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#747369;\">\/\/ ...\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<p>This forces the caller to pass you either something that implements <code>Awesome<\/code> directly, or wrap it on a helper struct\n<code>MakeItAwesome<\/code>. This can range from verbose to impossible, if the calling code is itself generic.<\/p>\n<h3>Use an enum<\/h3>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#cc99cc;\">enum <\/span><span style=\"color:#d3d0c8;\">DebugOrAwesome&lt;D = (), A = ()&gt; {\n<\/span><span style=\"color:#d3d0c8;\">    Debug(D),\n<\/span><span style=\"color:#d3d0c8;\">    Awesome(A)\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">do_it<\/span><span style=\"color:#d3d0c8;\">&lt;D, A&gt;(<\/span><span style=\"color:#f2777a;\">value<\/span><span style=\"color:#d3d0c8;\">: DebugOrAwesome&lt;D, A&gt;) <\/span><span style=\"color:#cc99cc;\">where\n<\/span><span style=\"color:#d3d0c8;\">    D: Debug,\n<\/span><span style=\"color:#d3d0c8;\">    A: Awesome\n<\/span><span style=\"color:#d3d0c8;\">{\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">match<\/span><span style=\"color:#d3d0c8;\"> value {\n<\/span><span style=\"color:#d3d0c8;\">        DebugOrAwesome::Debug(value) =&gt; { <\/span><span style=\"color:#747369;\">\/* ... *\/ <\/span><span style=\"color:#d3d0c8;\">}\n<\/span><span style=\"color:#d3d0c8;\">        DebugOrAwesome::Awesome(value) =&gt; { <\/span><span style=\"color:#747369;\">\/* ... *\/ <\/span><span style=\"color:#d3d0c8;\">}\n<\/span><span style=\"color:#d3d0c8;\">    }\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<p>But this requires the caller to choose the right variant and provide a &quot;dumb&quot; type for the other variants.<\/p>\n"},{"title":"Type-safe HTML templates in Rust with maud","id":"f3e42dd6-de10-45ce-9c97-01978fecef77","updated":"2026-11-03T00:00:00+00:00","author":{"name":"sitegui"},"category":{"@attributes":{"term":"rust"}},"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/11\/type-safe-html-templates-in-rust-with-maud","rel":"alternate"}},"published":"2026-11-03T00:00:00+00:00","content":"<p>I've refactored this blog and some other web apps in Rust that I maintain from Jinja templates to a type-safe Rust\nmacro.<\/p>\n<p>To illustrate the difference, let's start with a concrete Jinja template example:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-html\"><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">&lt;<\/span><span style=\"color:#f2777a;\">style<\/span><span style=\"color:#d3d0c8;\">&gt;\n<\/span><span style=\"color:#cc99cc;\">    <\/span><span style=\"color:#6699cc;\">.<\/span><span style=\"color:#f99157;\">comment <\/span><span style=\"color:#d3d0c8;\">{\n<\/span><span style=\"color:#d3d0c8;\">        border-left: thin <\/span><span style=\"color:#66cccc;\">var<\/span><span style=\"color:#d3d0c8;\">(--secondary-bg) solid;\n<\/span><span style=\"color:#d3d0c8;\">        padding-left: <\/span><span style=\"color:#f99157;\">10px<\/span><span style=\"color:#d3d0c8;\">;\n<\/span><span style=\"color:#d3d0c8;\">    }\n<\/span><span style=\"color:#d3d0c8;\">&lt;\/<\/span><span style=\"color:#f2777a;\">style<\/span><span style=\"color:#d3d0c8;\">&gt;\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">&lt;<\/span><span style=\"color:#f2777a;\">div <\/span><span style=\"color:#f99157;\">class<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">comment<\/span><span style=\"color:#d3d0c8;\">&quot;&gt;\n<\/span><span style=\"color:#d3d0c8;\">    &lt;<\/span><span style=\"color:#f2777a;\">p<\/span><span style=\"color:#d3d0c8;\">&gt;\n<\/span><span style=\"color:#d3d0c8;\">        &lt;<\/span><span style=\"color:#f2777a;\">strong<\/span><span style=\"color:#d3d0c8;\">&gt;{{ name }}&lt;\/<\/span><span style=\"color:#f2777a;\">strong<\/span><span style=\"color:#d3d0c8;\">&gt;\n<\/span><span style=\"color:#d3d0c8;\">        {% if site %}\n<\/span><span style=\"color:#d3d0c8;\">        (&lt;<\/span><span style=\"color:#f2777a;\">a <\/span><span style=\"color:#f99157;\">href<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">https:\/\/{{ site }}<\/span><span style=\"color:#d3d0c8;\">&quot; <\/span><span style=\"color:#f99157;\">target<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">_blank<\/span><span style=\"color:#d3d0c8;\">&quot;&gt;{{ site }}&lt;\/<\/span><span style=\"color:#f2777a;\">a<\/span><span style=\"color:#d3d0c8;\">&gt;)\n<\/span><span style=\"color:#d3d0c8;\">        {% endif %}\n<\/span><span style=\"color:#d3d0c8;\">    &lt;\/<\/span><span style=\"color:#f2777a;\">p<\/span><span style=\"color:#d3d0c8;\">&gt;\n<\/span><span style=\"color:#d3d0c8;\">    {{ html_content | safe }}\n<\/span><span style=\"color:#d3d0c8;\">&lt;\/<\/span><span style=\"color:#f2777a;\">div<\/span><span style=\"color:#d3d0c8;\">&gt;\n<\/span><\/code><\/pre>\n<p>This Jinja template uses variable replacement with <code>{{ ... }}<\/code>, control flow with <code>{% if ... %}...{% endif %}<\/code> and some\nkind of functional transformation (called &quot;filters&quot;) with <code>... | ...<\/code>.<\/p>\n<p>It works and the Rust crate <a href=\"https:\/\/docs.rs\/minijinja\/latest\/minijinja\/\">minijinja<\/a> is pretty good. However, as it's\ncommon in template languages, the type system is weak and doesn't help catch common mistakes at all! To know which\nvariables are used you have to carefully read the code. Since the IDE cannot understand the usage of struct fields,\nactions like &quot;show usages&quot; do not list them.<\/p>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<p>This makes evolving and refactoring more cumbersome, and specially awkward in a Rust codebase, where the rest of the\ncode is well-typed.<\/p>\n<p>For a while I had in mind the idea to migrate to a typed html macro in Rust. I've compared some options and went\nwith <a href=\"https:\/\/docs.rs\/maud\/latest\/maud\/\">maud<\/a>. Check below the above example rewritten:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#cc99cc;\">use <\/span><span style=\"color:#d3d0c8;\">maud::{html, Markup, PreEscaped};\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">render<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f2777a;\">name<\/span><span style=\"color:#d3d0c8;\">: &amp;<\/span><span style=\"color:#cc99cc;\">str<\/span><span style=\"color:#d3d0c8;\">, <\/span><span style=\"color:#f2777a;\">site<\/span><span style=\"color:#d3d0c8;\">: Option&lt;&amp;<\/span><span style=\"color:#cc99cc;\">str<\/span><span style=\"color:#d3d0c8;\">&gt;, <\/span><span style=\"color:#f2777a;\">html_content<\/span><span style=\"color:#d3d0c8;\">: &amp;<\/span><span style=\"color:#cc99cc;\">str<\/span><span style=\"color:#d3d0c8;\">) -&gt; Markup {\n<\/span><span style=\"color:#d3d0c8;\">    html! {\n<\/span><span style=\"color:#d3d0c8;\">        style { \n<\/span><span style=\"color:#d3d0c8;\">            (PreEscaped(<\/span><span style=\"color:#cc99cc;\">r<\/span><span style=\"color:#d3d0c8;\">#<\/span><span style=\"color:#99cc99;\">&quot;\n<\/span><span style=\"color:#99cc99;\">            .comment {\n<\/span><span style=\"color:#99cc99;\">                border-left: thin var(--secondary-bg) solid;\n<\/span><span style=\"color:#99cc99;\">                padding-left: 10px;\n<\/span><span style=\"color:#99cc99;\">            }<\/span><span style=\"color:#d3d0c8;\">&quot;#))\n<\/span><span style=\"color:#d3d0c8;\">        }\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">        div.comment {\n<\/span><span style=\"color:#d3d0c8;\">            p {\n<\/span><span style=\"color:#d3d0c8;\">                strong { (name) }\n<\/span><span style=\"color:#d3d0c8;\">                @<\/span><span style=\"color:#cc99cc;\">if let <\/span><span style=\"color:#d3d0c8;\">Some(site) = site {\n<\/span><span style=\"color:#d3d0c8;\">                    &quot;<\/span><span style=\"color:#99cc99;\"> (<\/span><span style=\"color:#d3d0c8;\">&quot;\n<\/span><span style=\"color:#d3d0c8;\">                    a href={ &quot;<\/span><span style=\"color:#99cc99;\">https:\/\/<\/span><span style=\"color:#d3d0c8;\">&quot; (site) } target=&quot;<\/span><span style=\"color:#99cc99;\">_blank<\/span><span style=\"color:#d3d0c8;\">&quot; {\n<\/span><span style=\"color:#d3d0c8;\">                        (site)\n<\/span><span style=\"color:#d3d0c8;\">                    }\n<\/span><span style=\"color:#d3d0c8;\">                    &quot;<\/span><span style=\"color:#99cc99;\">)<\/span><span style=\"color:#d3d0c8;\">&quot;\n<\/span><span style=\"color:#d3d0c8;\">                }\n<\/span><span style=\"color:#d3d0c8;\">            }\n<\/span><span style=\"color:#d3d0c8;\">            (PreEscaped(html_content))\n<\/span><span style=\"color:#d3d0c8;\">        }\n<\/span><span style=\"color:#d3d0c8;\">    }\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<p>It may look funny at first, but the macro seems pretty readable to me now. The docs are well written with many\nexamples and the language is quite small. A crash course from Jinja's syntax:<\/p>\n<ul>\n<li>instead of <code>&lt;div class=&quot;hey&quot;&gt;...&lt;\/div&gt;<\/code> use <code>div.hey { ... }<\/code><\/li>\n<li>instead of <code>{{ ... }}<\/code> use <code>(...)<\/code>. The parenthesis is a Rust expression, and the IDE understands it well!<\/li>\n<li>instead of <code>{% if ... %}...{% endif %]<\/code> use <code>@if ... { ... }<\/code>. Again, Rust can be used, so if-let can be used to\nhandle optional data, for example<\/li>\n<li>instead of filters, just use Rust expressions. For example, the type <code>PreEscaped(...)<\/code> disables HTML-escaping for the\ngiven data<\/li>\n<\/ul>\n<p>The compiler runs some maud code that parses and converts each <code>html! { ... }<\/code> call directly to Rust code, just as if\nyou had written string manipulation code by hand. The example above will be translated to:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">render<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f2777a;\">name<\/span><span style=\"color:#d3d0c8;\">: &amp;<\/span><span style=\"color:#cc99cc;\">str<\/span><span style=\"color:#d3d0c8;\">, <\/span><span style=\"color:#f2777a;\">site<\/span><span style=\"color:#d3d0c8;\">: Option&lt;&amp;<\/span><span style=\"color:#cc99cc;\">str<\/span><span style=\"color:#d3d0c8;\">&gt;, <\/span><span style=\"color:#f2777a;\">html_content<\/span><span style=\"color:#d3d0c8;\">: &amp;<\/span><span style=\"color:#cc99cc;\">str<\/span><span style=\"color:#d3d0c8;\">) -&gt; Markup {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">let mut<\/span><span style=\"color:#d3d0c8;\"> out = String::with_capacity(<\/span><span style=\"color:#f99157;\">923<\/span><span style=\"color:#cc99cc;\">usize<\/span><span style=\"color:#d3d0c8;\">);\n<\/span><span style=\"color:#d3d0c8;\">    out.<\/span><span style=\"color:#66cccc;\">push_str<\/span><span style=\"color:#d3d0c8;\">(&quot;<\/span><span style=\"color:#99cc99;\">&lt;style&gt;<\/span><span style=\"color:#d3d0c8;\">&quot;);\n<\/span><span style=\"color:#d3d0c8;\">    render_to!(&amp;PreEscaped(&quot;<\/span><span style=\"color:#99cc99;\">.comment {\n<\/span><span style=\"color:#99cc99;\">        border-left: thin var(--secondary-bg) solid;\n<\/span><span style=\"color:#99cc99;\">        padding-left: 10px;\n<\/span><span style=\"color:#99cc99;\">    }<\/span><span style=\"color:#d3d0c8;\">&quot;), &amp;<\/span><span style=\"color:#cc99cc;\">mut<\/span><span style=\"color:#d3d0c8;\"> out);\n<\/span><span style=\"color:#d3d0c8;\">    out.<\/span><span style=\"color:#66cccc;\">push_str<\/span><span style=\"color:#d3d0c8;\">(&quot;<\/span><span style=\"color:#99cc99;\">&lt;\/style&gt;&lt;div class=<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">comment<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">&gt;&lt;p&gt;&lt;strong&gt;<\/span><span style=\"color:#d3d0c8;\">&quot;);\n<\/span><span style=\"color:#d3d0c8;\">    render_to!(&amp;name, &amp;<\/span><span style=\"color:#cc99cc;\">mut<\/span><span style=\"color:#d3d0c8;\"> out);\n<\/span><span style=\"color:#d3d0c8;\">    out.<\/span><span style=\"color:#66cccc;\">push_str<\/span><span style=\"color:#d3d0c8;\">(&quot;<\/span><span style=\"color:#99cc99;\">&lt;\/strong&gt;<\/span><span style=\"color:#d3d0c8;\">&quot;);\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">if let <\/span><span style=\"color:#d3d0c8;\">Some(site) = site {\n<\/span><span style=\"color:#d3d0c8;\">        out.<\/span><span style=\"color:#66cccc;\">push_str<\/span><span style=\"color:#d3d0c8;\">(&quot;<\/span><span style=\"color:#99cc99;\"> (&lt;a href=<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">https:\/\/<\/span><span style=\"color:#d3d0c8;\">&quot;);\n<\/span><span style=\"color:#d3d0c8;\">        render_to!(&amp;site, &amp;<\/span><span style=\"color:#cc99cc;\">mut<\/span><span style=\"color:#d3d0c8;\"> out);\n<\/span><span style=\"color:#d3d0c8;\">        out.<\/span><span style=\"color:#66cccc;\">push_str<\/span><span style=\"color:#d3d0c8;\">(&quot;<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\"> target=<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">_blank<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">&gt;<\/span><span style=\"color:#d3d0c8;\">&quot;);\n<\/span><span style=\"color:#d3d0c8;\">        render_to!(&amp;site, &amp;<\/span><span style=\"color:#cc99cc;\">mut<\/span><span style=\"color:#d3d0c8;\"> out);\n<\/span><span style=\"color:#d3d0c8;\">        out.<\/span><span style=\"color:#66cccc;\">push_str<\/span><span style=\"color:#d3d0c8;\">(&quot;<\/span><span style=\"color:#99cc99;\">&lt;\/a&gt;)<\/span><span style=\"color:#d3d0c8;\">&quot;);\n<\/span><span style=\"color:#d3d0c8;\">    }\n<\/span><span style=\"color:#d3d0c8;\">    out.<\/span><span style=\"color:#66cccc;\">push_str<\/span><span style=\"color:#d3d0c8;\">(&quot;<\/span><span style=\"color:#99cc99;\">&lt;\/p&gt;<\/span><span style=\"color:#d3d0c8;\">&quot;);\n<\/span><span style=\"color:#d3d0c8;\">    render_to!(&amp;PreEscaped(html_content), &amp;<\/span><span style=\"color:#cc99cc;\">mut<\/span><span style=\"color:#d3d0c8;\"> out);\n<\/span><span style=\"color:#d3d0c8;\">    out.<\/span><span style=\"color:#66cccc;\">push_str<\/span><span style=\"color:#d3d0c8;\">(&quot;<\/span><span style=\"color:#99cc99;\">&lt;\/div&gt;<\/span><span style=\"color:#d3d0c8;\">&quot;);\n<\/span><span style=\"color:#d3d0c8;\">    Markup(out)\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<p>Quite direct and powerful! After some days of using it, I can report on some downsides:<\/p>\n<ul>\n<li>editing CSS and JS inside a string it not fun. I try to keep them small (which is a good thing!). If I need more\n&quot;space&quot; I simply do <code>style { (PreEscaped(include_str!(&quot;file.css&quot;))) }<\/code><\/li>\n<li>with Jinja, the file is mostly HTML and the IDE has good support for it. Thinks like auto-complete, MDN docs, go to\nCSS selector, etc do not work with maud<\/li>\n<\/ul>\n<p>Initially, I thought that it would be slow to compile, but so far I could not notice it. For my taste, the trade-offs\nare worth it.<\/p>\n<p>For fun, I've added to the footer of this blog how long the server took to render each page. Currently, it's\nusually around 0.1 ms (that is, 10 000 faster than a second). Check for you self in the footer of this page :)<\/p>\n<p>I've compared the median time of rendering the home page of this blog in <code>minijinja<\/code> against <code>maud<\/code> over 5 runs:<\/p>\n<ul>\n<li><code>minijinja<\/code>: 1.0 ms<\/li>\n<li><code>maud<\/code>: 0.07 ms<\/li>\n<\/ul>\n<p><span class=\"fun\">fast!<\/span><\/p>\n"},{"title":"First J'auto H\u00e9berge workshop","id":"469cd91a-f7ab-4062-b7aa-2bfe609b3f41","updated":"2026-10-28T00:00:00+00:00","author":{"name":"sitegui"},"category":[{"@attributes":{"term":"jauto-heberge"}},{"@attributes":{"term":"homelab"}}],"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/10\/first-jauto-heberge-workshop","rel":"alternate"}},"published":"2026-10-28T00:00:00+00:00","content":"<p>I'm organising a workshop around self-hosting next month here where I live:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/10\/first-jauto-heberge-workshop.png\" alt=\"Workshop invitation for November 18th\" \/><\/p>\n<p>The idea is to help people create and improve their self-hosting setups.<\/p>\n<p>From experience, one of the hardest parts of this hobby is to choose where to start and what to do! There are soooo many\noptions for every minutia.<\/p>\n<p>That's why I'll be focusing on providing &quot;good enough&quot; answers for the more common questions: which system\nto use, where to run it, how to expose it and how to do backups?<\/p>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<p>I'll be using <a href=\"https:\/\/yunohost.org\/\">Yunohost<\/a> as the base installation, which is an amazing project! It's designed for\nthose people that don't know how or don't want to fiddle so much with their systems.<\/p>\n<p>For the other questions, I'm building a wiki with tutorials in <a href=\"https:\/\/jauto-heberge.fr\">https:\/\/jauto-heberge.fr<\/a> (in French, for now).<\/p>\n<p>As a &quot;surprise element&quot; for people that come to the workshop, I'll bring the very server of jauto-heberge.fr with me:\nit's a Raspberry Pi 5. I can plug it to my phone in USB tethering mode and use a battery to have a fully mobile server!<\/p>\n"},{"title":"Uptime monitoring for my homelab","id":"f6272fb0-a342-4986-8172-ba6ac8922d12","updated":"2026-10-06T00:00:00+00:00","author":{"name":"sitegui"},"category":[{"@attributes":{"term":"homelab"}},{"@attributes":{"term":"caddy"}}],"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/10\/uptime-monitoring-for-my-homelab","rel":"alternate"}},"published":"2026-10-06T00:00:00+00:00","content":"<p>In this post I share the third and most recent iteration on the uptime monitoring for all my homelab services. The main\njob of this subsystem is to alert me when some web service is down.<\/p>\n<p>I have two different systems that detect and alert uptime issues on my homelab services: <code>cloud-status<\/code> and <code>uptime<\/code>.\nBoth are small pieces of Rust code that I've written for my use case. They both read my Caddyfile to know which hosts\nI have and probe them one by one.<\/p>\n<p>The main difference is that <code>cloud-status<\/code> probe from the point of view of the outside, using an hourly job that runs on\nsome cloud provider. While <code>uptime<\/code> probes from the inside of the homelab, every fifteen seconds.<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/10\/uptime-monitoring-for-my-homelab-1.png\" alt=\"\" \/><\/p>\n<h2>Cloud status<\/h2>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<p>This is a job that will run on a cloud server every hour to check that:<\/p>\n<ul>\n<li>my private services that only answer to private IPs are not available on the Internet<\/li>\n<li>my protected services that\nrequire <a href=\"https:\/\/git.sitegui.dev\/sitegui\/home-lab\/src\/commit\/93f40d7b5c96e2a44ae19cdfe8a8b42b8e8de3fa\/knock\">knock protection<\/a>\nredirect to the login page<\/li>\n<li>my public services answer with 200<\/li>\n<\/ul>\n<p>In case of any problem, it will notify me by email, describing the issue. When the issue is resolved, it notifies me\nagain.<\/p>\n<p>I've deployed this as a cloud job in Scaleway. I was surprised by two limitations of their platform:<\/p>\n<ul>\n<li>jobs do not have IP v6 connectivity. Yes, it's 2026 and their service only offers IP v4... So I unfortunately cannot\ntest if the IP v6 routing is still good. This is not a hypothetical problem: I once had a problem in which a friend\ncould not access a website I host because their phone only had IP v6 and my IP v6 setup was wrong. It confused me for\na while because &quot;it worked for me&quot;, but not for him...<\/li>\n<li>jobs cannot connect to SMTP port to send me an email. It's a measure to combat SPAM on their infra. So I had to enable\ntheir transaction email service and use their API for this.<\/li>\n<\/ul>\n<p>In all, this costs me around \u20ac 0.30 \/ month.<\/p>\n<p><a href=\"https:\/\/git.sitegui.dev\/sitegui\/home-lab\/src\/commit\/93f40d7b5c96e2a44ae19cdfe8a8b42b8e8de3fa\/cloud-status\">The source code is here<\/a>\nif you want to take a look.<\/p>\n<h2>Uptime<\/h2>\n<p>To monitor and alert about private and protected services being down, I need to do it from the inside.<\/p>\n<p>My first attempt was made\nusing <a href=\"https:\/\/caddyserver.com\/docs\/caddyfile\/directives\/reverse_proxy#active-health-checks\">Caddy's health check feature<\/a>,\nbut it had a major downside: Caddy also uses the current health information to decide whether it allows the request to\ncontinue to the underlying service or, when unhealthy, respond right away with 502 Bad Gateway.<\/p>\n<p>This behaviour was painful for two reasons:<\/p>\n<ol>\n<li>the Collabora Online container on startup needs information from the Nextcloud instance, and it uses the public\ndomain name to try to reach the instance. Well, when the homelab is starting, Caddy would not have yet detected that\nNextcloud is healthy, answering Collabora with an error and breaking it.<\/li>\n<li>when I would deploy some other service, depending on how unlucky I was with the health check probes timing, Caddy\nwould mark a service as unhealthy, increasing the downtime more than necessary.<\/li>\n<\/ol>\n<p>My second attempt was made using <a href=\"https:\/\/github.com\/louislam\/uptime-kuma\">Uptime Kuma<\/a>, but it didn't fit well my use\ncase for two reasons:<\/p>\n<ol>\n<li>I would need to manually enter each domain to monitor. I want something automatic or at least that fails safe if I\nforget to add the monitor.<\/li>\n<li>it used too many resources! It would consume around 130 MiB of memory and 1.5 % of a CPU core. That's insane for such\na simple task.<\/li>\n<\/ol>\n<p>So my third attempt was writing my own, which parses my Caddyfile to automatically monitor all my hosts. It exposes a\n<code>\/metrics<\/code> endpoint that is scrapped by Prometheus and with an alarm rule in alertmanager, I can receive email\nnotifications.<\/p>\n<p><a href=\"https:\/\/git.sitegui.dev\/sitegui\/home-lab\/src\/commit\/93f40d7b5c96e2a44ae19cdfe8a8b42b8e8de3fa\/uptime\">The source code is here<\/a>\nif you want to take a look. It works wonders for me and only uses 4 MiB and 0.2 % of a CPU core.<\/p>\n"},{"title":"We welcome Olympia to our family","id":"f61f6049-7274-4dbf-9064-ef78a8634310","updated":"2026-09-28T00:00:00+00:00","author":{"name":"sitegui"},"category":[{"@attributes":{"term":"typewriter"}},{"@attributes":{"term":"friends"}}],"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/09\/we-welcome-olympia-to-our-family","rel":"alternate"}},"published":"2026-09-28T00:00:00+00:00","content":"<p>This summer we got a gift from a friend: a portable A3 typewriter!<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/09\/we-welcome-olympia-to-our-family-1.jpg\" alt=\"An Olympia typewriter machine\" \/><\/p>\n<p>I say portable because it came with this good-looking travel suitcase, very practical to transport its 10.9 kilograms\nof pure beauty around:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/09\/we-welcome-olympia-to-our-family-2.jpg\" alt=\"\" \/><\/p>\n<p>We got an expert helping hand from Achille\n(<a href=\"https:\/\/achilletoupin.com\/\">check his blog, he writes about typewrites and other geekeries<\/a>) to dust off\nsome rust (not crab-related) and put a touch of oil at the right places.<\/p>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/09\/we-welcome-olympia-to-our-family-3.jpg\" alt=\"\" \/><\/p>\n<p>We've tested it to write some Brazilian songs for child for future reference:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/09\/we-welcome-olympia-to-our-family-4.jpg\" alt=\"\" \/><\/p>\n<p>(the author of this blog does not condemn any violence against cats, especially in the presence of Dona Chica)<\/p>\n<p>It was so nice to hear Achille explain to us all the features of the machine! I didn't know a bunch of them, like:<\/p>\n<ul>\n<li>it's possible to set tab stops that the machine mechanically records. This way, you can come back to the exact column<\/li>\n<li>there's a small hole to put a pen into and make nice borders for tables<\/li>\n<li>there's no key for the numbers one and zero! Instead, we just use the letters l and o<\/li>\n<li>the ding! (so lovely) indicates that you have 7 more letters before the end of the line. But there's a button to\noverride the lock and continue anyhow<\/li>\n<li>when the ink band fully unrolls in one direction, it will automatically reverse rotation and start unrolling in the\nother one<\/li>\n<\/ul>\n<p>He also presented <a href=\"https:\/\/typepals.com\/\">a community of typewriter lovers<\/a> that exchange paper letters over the mail.\nHhahaha, people are fun.<\/p>\n<!-- Note: these items should not appear on the front page -->\n<style>\n@font-face {\n    font-family: 'TT2020';\n    src: url('\/static\/fonts\/TT2020.ttf') format('opentype');\n    font-weight: normal;\n    font-style: normal;\n    font-display: swap;\n}\n\nbody {\n    font-family: 'TT2020', monospace;\n    font-size: 18px;\n}\n\n.tt-error {\n    text-decoration-line: line-through;\n}\n<\/style>\n<script>\n\/\/ Insert some random typo errors into text elements\n\ndocument.addEventListener( 'DOMContentLoaded', function () {\n    function insertTypos(el) {\n        const terms = el.textContent.replace(\/\\s+\/g, ' ').split(' ')\n        const wordIndexes = []\n        for (let i = 0; i < terms.length; i++) {\n            if (terms[i].trim().length > 2) {\n                wordIndexes.push(i)\n            }\n        }\n\n        if (wordIndexes.length < 3) {\n            return\n        }\n\n        const wordIndexWithTypo = wordIndexes[Math.floor(Math.random() * wordIndexes.length)]\n\n        const termsBefore = terms.slice(0, wordIndexWithTypo)\n        const termsAtAndAfter = terms.slice(wordIndexWithTypo)\n\n        const originalWord = terms[wordIndexWithTypo]\n        const typoIndex = 1 + Math.floor(Math.random() * (originalWord.length - 2))\n        const typoWord = originalWord.slice(0, typoIndex) + originalWord.slice(typoIndex + 1)\n\n        const newSpan = document.createElement('span')\n        newSpan.appendChild(document.createTextNode(termsBefore.join(' ') + ' '))\n        const typoEl = document.createElement('span')\n        typoEl.classList.add('tt-error')\n        typoEl.textContent = typoWord\n        newSpan.appendChild(typoEl)\n        newSpan.appendChild(document.createTextNode(' ' + termsAtAndAfter.join(' ')))\n\n        el.parentNode.replaceChild(newSpan, el)\n    }\n\n    const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT)\n    \n    const textTargets = []\n    while (walker.nextNode()) {\n        const el = walker.currentNode\n        if (el.parentNode.tagName !== 'SCRIPT' && el.parentNode.tagName !== 'STYLE') {\n            textTargets.push(el)\n        }\n    }\n\n    textTargets.forEach(insertTypos)\n})\n<\/script>\n"},{"title":"ANEBA: Avoid Nextcloud Ecosystem BreakAge","id":"20ca096f-7d4c-4136-9136-a09dabc28a08","updated":"2026-09-26T00:00:00+00:00","author":{"name":"sitegui"},"category":[{"@attributes":{"term":"programming"}},{"@attributes":{"term":"nextcloud"}},{"@attributes":{"term":"php"}},{"@attributes":{"term":"rust"}}],"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/09\/aneba-avoid-nextcloud-ecosystem-breakage","rel":"alternate"}},"published":"2026-09-26T00:00:00+00:00","content":"<p>Everybody loves stuff that just work \ud83e\udd1f\ud83c\udffd. But getting there is a long journey!<\/p>\n<p>Discussing with some people at the Nextcloud Conference in Berlin last week, a recurrent pain is app compatibility\nwith each new server version. Nextcloud releases new versions every 4 months. Then, each individual app's maintainer\nneeds to test if they work with the new version, fix any possible breakage, and <em>manually<\/em> self-report as compatible.<\/p>\n<p>A common reason for an app to break is that it uses APIs that are not marked as stable. So I tried to help the\ndiscussion by\nbringing some data with the ANEBA prototype \ud83e\udda0.<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/09\/aneba-avoid-nextcloud-ecosystem-breakage-2.png\" alt=\"The ANEBA search portal\" \/><\/p>\n<p>You can access it here: <a href=\"https:\/\/aneba.sitegui.dev\">aneba.sitegui.dev<\/a><\/p>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<p>With the declared goal of the project to have more apps available, pains like this will likely only grow if left\nunattended.<\/p>\n<h2>A quick word about Nextcloud<\/h2>\n<p><a href=\"https:\/\/nextcloud.com\/\">Nextcloud<\/a> is an open source cloud platform. It can host files, photos,\noffice documents, notes, lists, calendars, chats, you name it! It's made of a core server, some\npre-installed apps (like Files) and other community-driven apps.<\/p>\n<p>I use it for almost two years for me and my family, replacing my previous use of Google Drive, Photos, Calendar. I\nself-host it at home <a href=\"https:\/\/sitegui.dev\/post\/2026\/03\/my-homelab-an-old-laptop-behind-the-fridge\">in an old laptop<\/a>, but the software can\nimpressively scale down to a Raspberry Pi and up to multiple federated servers. For example, in Technische Universit\u00e4t\nBerlin:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/09\/aneba-avoid-nextcloud-ecosystem-breakage-1.jpg\" alt=\"Nextcloud in Technische Universit\u00e4t Berlin\" \/><\/p>\n<h1>PHP and contracts<\/h1>\n<p>Beware that I haven't written a Nextcloud app myself, I only have second-hand knowledge about the matter. So my\nexplanations here are probably correct and flawed at the same time \ud83e\udee3<\/p>\n<p>It's interesting to analyse some of the technological aspects behind the scenes: Nextcloud server is written in PHP and\nprovides a rich way for apps to hook into it. There are two major ways that apps can be implemented: &quot;classic&quot; apps and\n&quot;external&quot; apps (ExApp). For ExApps, their backend is an independent process or container, and it talks to the server\nusing an HTTP API.<\/p>\n<p>But this post is mostly about classic apps, which are the majority. A classic app provides new PHP\nsource files that implement server logic, declare new database tables, and bundle UI files for its pages. For each\nrequest routed to the app, its code runs in the same process as the Nextcloud server, using PHP's autoloader to import\nand run the necessary files. The Nextcloud server has a public PHP API available at the <code>\\OCP<\/code> namespace (side note: OC\nmeans Own Cloud, the project from which Nextcloud forked in 2016).<\/p>\n<p>However, by the nature of the PHP language, apps can also import and use server internals at <code>\\OC<\/code> (or <code>OC_App<\/code> if\nyou're vintage). Some of this use is justifiable when there is no equivalent public API available for what the app\nneeds. However, some uses should no longer be. Maybe the code is old, or was copied from an old example, or\nAI generated, and in the meantime the necessary functionality was added to <code>\\OCP<\/code>.<\/p>\n<p>Talking to the team responsible for the server and these APIs, they are accutely aware of these problems. The good path\nforward is to implement new <code>\\OCP<\/code> APIs that answer to the apps' needs and keep these new APIs stable for longer.<\/p>\n<h1>The first prototype<\/h1>\n<p>If we simplify the problem, we could try to answer: which apps are using <code>\\OC<\/code> and where? A simple regex could help, but\nI wanted to have more precision and insights.<\/p>\n<p>Luckily, there is <a href=\"https:\/\/crates.io\/crates\/php-rs-parser\">a PHP parser<\/a> written in Rust (which seems to be linked to a\npretty cool project for a Language Server Protocol for PHP) \ud83e\udd80<\/p>\n<p>So the steps to draw the owl are:<\/p>\n<ol>\n<li>download all apps<\/li>\n<li>parse all their PHP files<\/li>\n<li>resolve all referenced symbols<\/li>\n<li>detect all uses of <code>\\OC<\/code><\/li>\n<li>do something with it<\/li>\n<\/ol>\n<p>The first version worked, but we wanted to know more! For example, we would also like to detect the use of items in\n<code>\\OCP<\/code> that are marked as deprecated. We would like to know which apps use another app's internals.<\/p>\n<p>So during the contributor week, I <a href=\"https:\/\/git.sitegui.dev\/sitegui\/nextcloud-aneba\">wrote ANEBA<\/a> for this.<\/p>\n<p>To show the maginitude of the computational problem:<\/p>\n<table>\n<thead>\n<tr>\n<th>Server version<\/th>\n<th>35<\/th>\n<th>34<\/th>\n<th>33<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Compatible apps<\/td>\n<td>311<\/td>\n<td>477<\/td>\n<td>517<\/td>\n<\/tr>\n<tr>\n<td>Archive volume<\/td>\n<td>2.2 GiB<\/td>\n<td>4.1 GiB<\/td>\n<td>4.4 GiB<\/td>\n<\/tr>\n<tr>\n<td>PHP source files<\/td>\n<td>132 k<\/td>\n<td>176 k<\/td>\n<td>187 k<\/td>\n<\/tr>\n<tr>\n<td>Analysis time<\/td>\n<td>7 s<\/td>\n<td>9 s<\/td>\n<td>11 s<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Rust is fast \ud83e\udd13<\/p>\n<h1>Making it useful<\/h1>\n<p>The results of the scan are an unexplored gold mine for both the server team and app developers! So to help them use\nthis data, I've created a search portal at <a href=\"https:\/\/aneba.sitegui.dev\">aneba.sitegui.dev<\/a>. Data is only useful if\ntrustworthy and actionable.<\/p>\n<p>There, the server team can search which private APIs are most dependent upon to propose public and more stable\nalternatives. They can also search which private APIs are no longer necessary and can be dropped. They can anticipate\nbreakage on future releases and alert maintainers.<\/p>\n<p>The app developers can see the scan result of their apps and where possible, replace with alternatives.<\/p>\n<p>If you find any problem with the results or a missing use-case, <a href=\"https:\/\/sitegui.dev\/about\">send me an email<\/a>.<\/p>\n<h1>Next steps and dreams<\/h1>\n<p>Displaying the data in a search portal is just a first step. We would like to integrate these analyses directly where\ndevelopers are:<\/p>\n<ul>\n<li>for app developers, we could add a lint rule to point those usages and make this lint part of the suggested workflow\nin the template app repo<\/li>\n<li>for server developers, we could detect if a given PR breaks too many apps and pushes a message to the PR with this\ninformation<\/li>\n<li>for the Nextcloud administrators, we could give transparent information of apps that depend on internal APIs, to alert\nthat these apps may take longer to update to newer server versions<\/li>\n<\/ul>\n<p>ANEBA uses static analysis to parse the declaration and use of PHP types (classes, interfaces, etc). But currently, it\ndoes not do any type analysis. So to achieve these greater goals, it has to get smarter.<\/p>\n<p>To illustrate the limits of the current approach, consider this simple code <code>$a-&gt;b()<\/code>. To know which exact method &quot;b&quot;\nit's using, we need to resolve the type of <code>$a<\/code>. That's a larger can of dancing worms.<\/p>\n"},{"title":"Let the compiler check your URLs","id":"1be09485-a5e1-4079-ba30-c9d144c3f7e2","updated":"2026-09-15T00:00:00+00:00","author":{"name":"sitegui"},"category":[{"@attributes":{"term":"rust"}},{"@attributes":{"term":"axum"}}],"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/09\/let-the-compiler-check-your-urls","rel":"alternate"}},"published":"2026-09-15T00:00:00+00:00","content":"<p>It's nice when some classes of bugs cannot be fabricated. For example, some languages like Kotlin and Rust bake the\nconcept of &quot;empty&quot; especially in the language and standard library to remind and force you (the developer) to deal with\nthe case when something is missing.<\/p>\n<p>This post is about <a href=\"https:\/\/crates.io\/crates\/typed_web_routes\">a library I wrote<\/a> that brings a similar compile-verified\nassurance to URL construction when writing web servers in Rust. \ud83e\udd80<\/p>\n<p>The problem it solves is: in a web server, you usually declare some &quot;handlers&quot; (like web pages, assets or\napi endpoints) and hook them to a global path router. It works well, but what about when you want to link from one page\nto another? How to do that correctly and let the compiler help you keep all links correct as your app evolves?<\/p>\n<p>To illustrate, consider this basic example using the library <code>axum<\/code> in Rust to create a website with a home page and a\ndynamic &quot;posts&quot; page, with the home linking to a post:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#cc99cc;\">use <\/span><span style=\"color:#d3d0c8;\">axum::extract::Path;\n<\/span><span style=\"color:#cc99cc;\">use <\/span><span style=\"color:#d3d0c8;\">axum::response::Html;\n<\/span><span style=\"color:#cc99cc;\">use <\/span><span style=\"color:#d3d0c8;\">axum::routing::get;\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">#[<\/span><span style=\"color:#f2777a;\">tokio<\/span><span style=\"color:#d3d0c8;\">::<\/span><span style=\"color:#f2777a;\">main<\/span><span style=\"color:#d3d0c8;\">]\n<\/span><span style=\"color:#d3d0c8;\">async <\/span><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">main<\/span><span style=\"color:#d3d0c8;\">() {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">let<\/span><span style=\"color:#d3d0c8;\"> app = axum::Router::new()\n<\/span><span style=\"color:#d3d0c8;\">        .<\/span><span style=\"color:#66cccc;\">route<\/span><span style=\"color:#d3d0c8;\">(&quot;<\/span><span style=\"color:#99cc99;\">\/<\/span><span style=\"color:#d3d0c8;\">&quot;, <\/span><span style=\"color:#66cccc;\">get<\/span><span style=\"color:#d3d0c8;\">(home))\n<\/span><span style=\"color:#d3d0c8;\">        .<\/span><span style=\"color:#66cccc;\">route<\/span><span style=\"color:#d3d0c8;\">(&quot;<\/span><span style=\"color:#99cc99;\">\/post\/{id}<\/span><span style=\"color:#d3d0c8;\">&quot;, <\/span><span style=\"color:#66cccc;\">get<\/span><span style=\"color:#d3d0c8;\">(about));\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">let<\/span><span style=\"color:#d3d0c8;\"> listener = tokio::net::TcpListener::bind(&quot;<\/span><span style=\"color:#99cc99;\">127.0.0.1:3000<\/span><span style=\"color:#d3d0c8;\">&quot;)\n<\/span><span style=\"color:#d3d0c8;\">        .await\n<\/span><span style=\"color:#d3d0c8;\">        .<\/span><span style=\"color:#66cccc;\">unwrap<\/span><span style=\"color:#d3d0c8;\">();\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">    axum::serve(listener, app).await.<\/span><span style=\"color:#66cccc;\">unwrap<\/span><span style=\"color:#d3d0c8;\">();\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">async <\/span><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">home<\/span><span style=\"color:#d3d0c8;\">() -&gt; Html&lt;String&gt; {\n<\/span><span style=\"color:#d3d0c8;\">    Html(&quot;<\/span><span style=\"color:#99cc99;\">Hello! &lt;a href=&#39;\/post\/17&#39;&gt;See post&lt;\/a&gt;<\/span><span style=\"color:#d3d0c8;\">&quot;.<\/span><span style=\"color:#66cccc;\">to_string<\/span><span style=\"color:#d3d0c8;\">())\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">async <\/span><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">about<\/span><span style=\"color:#d3d0c8;\">(Path(<\/span><span style=\"color:#f2777a;\">id<\/span><span style=\"color:#d3d0c8;\">): Path&lt;<\/span><span style=\"color:#cc99cc;\">u32<\/span><span style=\"color:#d3d0c8;\">&gt;) -&gt; Html&lt;String&gt; {\n<\/span><span style=\"color:#d3d0c8;\">    Html(format!(&quot;<\/span><span style=\"color:#99cc99;\">Thanks for visiting post <\/span><span style=\"color:#f99157;\">{}<\/span><span style=\"color:#d3d0c8;\">&quot;, id))\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<p>Note how the home page has a hard-coded link to the other page. This doesn't work well for bigger apps because the paths\ncan evolve, and also the parameters have to be correctly escaped.<\/p>\n<h2>The dynamic URL resolution<\/h2>\n<p>A typical solution in some frameworks is to give each route a name and then use some function to produce the URL given\nthe route name and parameters.<\/p>\n<p>Vue.js does this:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-js\"><span style=\"color:#cc99cc;\">const <\/span><span style=\"color:#f2777a;\">routes <\/span><span style=\"color:#d3d0c8;\">= [{path: &#39;<\/span><span style=\"color:#99cc99;\">\/post\/:id<\/span><span style=\"color:#d3d0c8;\">&#39;, name: &#39;<\/span><span style=\"color:#99cc99;\">post<\/span><span style=\"color:#d3d0c8;\">&#39;}]\n<\/span><\/code><\/pre>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-html\"><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">&lt;<\/span><span style=\"color:#f2777a;\">router-link <\/span><span style=\"color:#f99157;\">:to<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">{ name: &#39;post&#39;, params: { id: 17 } }<\/span><span style=\"color:#d3d0c8;\">&quot;&gt;\n<\/span><span style=\"color:#d3d0c8;\">    See post\n<\/span><span style=\"color:#d3d0c8;\">&lt;\/<\/span><span style=\"color:#f2777a;\">router-link<\/span><span style=\"color:#d3d0c8;\">&gt;\n<\/span><\/code><\/pre>\n<p>Django does this:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-python\"><span style=\"color:#d3d0c8;\">urlpatterns = [\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#6699cc;\">path<\/span><span style=\"color:#d3d0c8;\">(&quot;<\/span><span style=\"color:#99cc99;\">post\/&lt;int:id&gt;<\/span><span style=\"color:#d3d0c8;\">&quot;, views.post, <\/span><span style=\"color:#f2777a;\">name<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">post<\/span><span style=\"color:#d3d0c8;\">&quot;),\n<\/span><span style=\"color:#d3d0c8;\">]\n<\/span><\/code><\/pre>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-html\"><span style=\"color:#d3d0c8;\">&lt;<\/span><span style=\"color:#f2777a;\">a <\/span><span style=\"color:#f99157;\">href<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">{% url &#39;post&#39; 17 %}<\/span><span style=\"color:#d3d0c8;\">&quot;&gt;See post&lt;\/<\/span><span style=\"color:#f2777a;\">a<\/span><span style=\"color:#d3d0c8;\">&gt;\n<\/span><\/code><\/pre>\n<p>This dynamic approach works and solves two problems:<\/p>\n<ul>\n<li>if route URLs evolve, the links will point to the new URL<\/li>\n<li>URL path and query parameters are correctly escaped<\/li>\n<\/ul>\n<p>However, it still has some problems:<\/p>\n<ul>\n<li>looking up the route can fail at runtime if the route doesn't exist, for example, because of a typo or some distant\ncode evolution<\/li>\n<li>producing the URL may fail at runtime if parameters are missing or unexpected<\/li>\n<li>the produced URL may ultimately fail when the user navigates to it if the type of the parameter is wrong, for\nexample, the post's id was confounded with the post's title<\/li>\n<\/ul>\n<p>Most of the time, these runtime errors are noticed by the developer during manual or automatic testing. However this\npost is not about leaving it to change! This post is about writing code that cannot be wrong, at least not in the ways\ndescribed above \ud83d\ude1c<\/p>\n<h2>The compile-time URL resolution<\/h2>\n<p>Enter <code>typed_web_routes<\/code> and its derive macro to help you:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#cc99cc;\">use <\/span><span style=\"color:#d3d0c8;\">typed_web_routes::WebRoutes;\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">#[<\/span><span style=\"color:#f2777a;\">derive<\/span><span style=\"color:#d3d0c8;\">(WebRoutes)]\n<\/span><span style=\"color:#cc99cc;\">enum <\/span><span style=\"color:#d3d0c8;\">Routes {\n<\/span><span style=\"color:#d3d0c8;\">    #[<\/span><span style=\"color:#f2777a;\">route<\/span><span style=\"color:#d3d0c8;\">(pattern = &quot;<\/span><span style=\"color:#99cc99;\">\/<\/span><span style=\"color:#d3d0c8;\">&quot;)]\n<\/span><span style=\"color:#d3d0c8;\">    Home,\n<\/span><span style=\"color:#d3d0c8;\">    #[<\/span><span style=\"color:#f2777a;\">route<\/span><span style=\"color:#d3d0c8;\">(pattern = &quot;<\/span><span style=\"color:#99cc99;\">\/post\/{id}<\/span><span style=\"color:#d3d0c8;\">&quot;)]\n<\/span><span style=\"color:#d3d0c8;\">    Post { id: <\/span><span style=\"color:#cc99cc;\">u32 <\/span><span style=\"color:#d3d0c8;\">},\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<p>With this, you can now use <code>Routes::builder()<\/code> to produce URLs:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#d3d0c8;\">async <\/span><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">home<\/span><span style=\"color:#d3d0c8;\">() -&gt; Html&lt;String&gt; {\n<\/span><span style=\"color:#d3d0c8;\">    Html(format!(\n<\/span><span style=\"color:#d3d0c8;\">        &quot;<\/span><span style=\"color:#99cc99;\">Hello! Check &lt;a href=&#39;<\/span><span style=\"color:#f99157;\">{}<\/span><span style=\"color:#99cc99;\">&#39;&gt;this post&lt;\/a&gt;<\/span><span style=\"color:#d3d0c8;\">&quot;,\n<\/span><span style=\"color:#d3d0c8;\">        Routes::builder().<\/span><span style=\"color:#66cccc;\">post<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f99157;\">17<\/span><span style=\"color:#cc99cc;\">u32<\/span><span style=\"color:#d3d0c8;\">)\n<\/span><span style=\"color:#d3d0c8;\">    ))\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<p>Side note: the example is manually gluing strings to build HTML. This is bad, and you should be using some templating\nlike <a href=\"https:\/\/crates.io\/crates\/minijinja\"><code>minijinja<\/code><\/a> or <a href=\"https:\/\/crates.io\/crates\/maud\"><code>maud<\/code><\/a>.<\/p>\n<p>The <code>#[derive(WebRoutes)]<\/code> marker will write some Rust code to you:<\/p>\n<ul>\n<li>it creates the <code>builder()<\/code> static method that returns a URL builder<\/li>\n<li>this builder has one method for each route<\/li>\n<li>each method takes all the required route parameters, with the expected types<\/li>\n<\/ul>\n<p>Let's look how it holds against Vue.js' and Django's solutions:<\/p>\n<ul>\n<li><em>looking up the route can fail at runtime if the route doesn't exist<\/em>: not possible because\n<code>Routes::builder().pooost(17)<\/code> would not compile: no such method &quot;pooost ()&quot;<\/li>\n<li><em>producing the URL may fail at runtime if parameters are missing or unexpected<\/em>: not possible because\n<code>Routes::builder().post()<\/code> would not compile: missing method argument<\/li>\n<li><em>the produced URL may ultimately fail when the user navigates to it if the type of the parameter is wrong<\/em>: harder to\nhappen, because <code>Routes::builder().post(post_title)<\/code> would not compile: expected u32, got String. However, this\nproblem can still happen if the types happen to be compatible.<\/li>\n<\/ul>\n<p>I'm now using <code>WebRoutes<\/code>\n<a href=\"https:\/\/git.sitegui.dev\/sitegui\/nidimages\/src\/commit\/5e4f9b4889500d8b50b5afbaf95208dbbf600580\/src\/routes.rs#L25\">in nidimages<\/a>,\nand it brings a nice touch of compile-time check!<\/p>\n<p>How fast is it? A non-scientific micro benchmark showed around 75 ns for <code>Routes::builder().post(id)<\/code>, that is enough to\nproduce around 10 million URLs per second.<\/p>\n"},{"title":"Your database can be a single variable in memory","id":"72fec734-5c7c-4b08-bf0f-796b19ece402","updated":"2026-09-09T00:00:00+00:00","author":{"name":"sitegui"},"category":[{"@attributes":{"term":"programming"}},{"@attributes":{"term":"performance"}}],"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/09\/your-database-can-be-a-single-variable-in-memory","rel":"alternate"}},"published":"2026-09-09T00:00:00+00:00","content":"<p>Well... There are trade-offs, but don't trash the idea just yet :)<\/p>\n<p>Be assured that nobody is coming to take your beloved MongoDB, Postgres or other database away. They are\nmarvellous piece of software that solve complex problems quite well. However, in this post I invite you to think what\ndo you need them for, which problems they solve for your apps and, crucially, which problems they create on their own.<\/p>\n<p>I got tangled in these questions while\ndeveloping <a href=\"..\/08\/presenting-nidimages-photo-sharing-for-family-and-friends\">nidimages<\/a>, a self-hosted app to share\nphotos with family and friends. So I decided to write a synthetic load test to compare from\ndifferent perspectives Postgres and a file-backed variable.<\/p>\n<h2>A file-backed variable<\/h2>\n<p>That's a fancy name for a rather dangerously-simple concept: you parse a file on startup, then you rewrite the file from\ntime to time. Let's implement the gist of it in Python:<\/p>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-python\"><span style=\"color:#cc99cc;\">def <\/span><span style=\"color:#6699cc;\">main<\/span><span style=\"color:#d3d0c8;\">():\n<\/span><span style=\"color:#d3d0c8;\">    database = json.<\/span><span style=\"color:#6699cc;\">loads<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#6699cc;\">Path<\/span><span style=\"color:#d3d0c8;\">(&#39;<\/span><span style=\"color:#99cc99;\">data.json<\/span><span style=\"color:#d3d0c8;\">&#39;).<\/span><span style=\"color:#6699cc;\">read_text<\/span><span style=\"color:#d3d0c8;\">())\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">def <\/span><span style=\"color:#6699cc;\">save_loop<\/span><span style=\"color:#d3d0c8;\">():\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#cc99cc;\">while <\/span><span style=\"color:#f99157;\">True<\/span><span style=\"color:#d3d0c8;\">:\n<\/span><span style=\"color:#d3d0c8;\">            <\/span><span style=\"color:#6699cc;\">sleep<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f99157;\">10<\/span><span style=\"color:#d3d0c8;\">)\n<\/span><span style=\"color:#d3d0c8;\">            <\/span><span style=\"color:#6699cc;\">Path<\/span><span style=\"color:#d3d0c8;\">(&#39;<\/span><span style=\"color:#99cc99;\">data.json<\/span><span style=\"color:#d3d0c8;\">&#39;).<\/span><span style=\"color:#6699cc;\">write_text<\/span><span style=\"color:#d3d0c8;\">(json.<\/span><span style=\"color:#6699cc;\">dumps<\/span><span style=\"color:#d3d0c8;\">(database))\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#6699cc;\">Thread<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f2777a;\">target<\/span><span style=\"color:#d3d0c8;\">=save_loop).<\/span><span style=\"color:#6699cc;\">start<\/span><span style=\"color:#d3d0c8;\">()\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#747369;\"># continue backend startup logic\n<\/span><\/code><\/pre>\n<p>If reading the above code gave you some nausea ... good. The above &quot;solution&quot; is full of traps:<\/p>\n<ul>\n<li>what happens if the file is missing or ill-formed? crash!<\/li>\n<li>what happens if the process is shutdown during the sleep? data loss!<\/li>\n<li>what happens if the process is shutdown during the writing? full data corruption!<\/li>\n<li>what happens if you execute multiple instances of the code? data race and full data corruption!<\/li>\n<li>what happens if the <code>database<\/code> variable is changed during the writing? data race and inconsistent data!<\/li>\n<li>what happens if nothing change in the database? it gets fully rewritten every 10s nonetheless!<\/li>\n<li>what about data schema and schema migration? none!<\/li>\n<\/ul>\n<p>Most of these problems can be solved\nwith <a href=\"https:\/\/git.sitegui.dev\/sitegui\/rust-commons\/src\/commit\/3d086a43e06e66d4c19d1b8215984aa1a8636372\/file_database\/src\/lib.rs\">a better implementation<\/a>\nand a language that gives more guarantees against data\nraces. But the base concept stays the same: it's a variable that gets persisted on the disk automatically.<\/p>\n<blockquote>\n<p>I'm going to use it as my main and only database, and nobody can stop me <span class=\"fun\">MUAHHAH<\/span><br \/>\n(some super-nerdy super-villain)<\/p>\n<\/blockquote>\n<p>Suppose that it works. Why do it this way? Using a database app (like Postgres, MongoDB) or library (like Sqlite)\nrequires writing your application is a certain way. There are tools that abstract the exact database technology, but\nthey are always leaky in some way. If the data is already in memory, there is no need to quack like the database API\nmandates.<\/p>\n<h2>The database app<\/h2>\n<p>Let's step out of the madness for a while and back to a rather typical and simplified backend architecture. To improve\nsystem availability, specially during deploys, there are multiple running instances of each service.<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/09\/your-database-can-be-a-single-variable-in-memory-1.png\" alt=\"typical and simplified backend architecture\" \/><\/p>\n<p>I invite you to think of the database as just an application. A specialised app of course, but just an app. An app that,\nnotably, doesn't restart when you deploy new versions of the backend. An app that can scale independently. An app that\nexposes an API, usually receiving string query as input and binary format as output.<\/p>\n<p>One may dismiss the idea of using a file as database as clearly limited. It doesn't &quot;scale&quot;, says the intuition. Well,\nthere is truth to that: using files is not feasible to have multiple machines writing on the same data. Even in the\nsame machine, it's hard to have multiple processes writing to the same data.<\/p>\n<p>Sqlite, DuckDB and RocksDB, for example, use a different model: they are not standalone apps. Instead, they reside\ninside your app and operate directly on files. They are powerful technologies, as long as your scale allows &quot;same\nmachine&quot; processing.<\/p>\n<h2>It's the scale, darling<\/h2>\n<p>Even with a more rigours implementation, the file-backed variable suffers from a major limitation: it can only exist\nonce, in a single process in a single machine. So this puts a hard ceiling on its scalability: you cannot overgrow a\nsingle node.<\/p>\n<p>Even worse than that, you cannot use more than one process. So every deploy implies downtime: turn off the previous\nprocess, then turn on the new version.<\/p>\n<p>For my use-case in nidimages, those two trade-offs are really okay. Also, I'm using Rust, that unlike Python and\nNode.js, can use multiple cores concurrently in the same process while safely sharing the file-backed variable among\nthem. In other languages, the scale ceiling of a single core can be too much to bear.<\/p>\n<p>But the burning question now becomes:<\/p>\n<h2>Which scale can this solution actually sustain?<\/h2>\n<p>To answer this as honestly as I can, I wrote two implementations of a simple backend app that allows users to create\nalbums and publish photos in them. Then, I deployed these two versions in a cloud and wrote some code simulating many\nusers:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/09\/your-database-can-be-a-single-variable-in-memory-2.png\" alt=\"the load-test benchmark architecture\" \/><\/p>\n<p>The service implements these HTTP endpoints:<\/p>\n<ul>\n<li><strong>login<\/strong> (username) -&gt; session<\/li>\n<li><strong>list_albums<\/strong> (session) -&gt; album[] (sorted by name)<\/li>\n<li><strong>list_photos<\/strong> (session, album_id) -&gt; photo[] (sorted by date)<\/li>\n<li><strong>publish_photo<\/strong> (session, album_id, photo)<\/li>\n<li><strong>take_notifications<\/strong> (session) -&gt; notification[] (sorted by photo_date)<\/li>\n<\/ul>\n<p>And has these SQL tables:<\/p>\n<ul>\n<li><strong>user<\/strong>: (user_id UUID, username VARCHAR)<\/li>\n<li><strong>album<\/strong>: (album_id UUID, name VARCHAR)<\/li>\n<li><strong>user_album<\/strong>: (user_id UUID, album_id UUID)<\/li>\n<li><strong>session<\/strong>: (session_id UUID, user_id UUID)<\/li>\n<li><strong>photo<\/strong>: (photo_id UUID, album_id UUID, date TIMESTAMPTZ)<\/li>\n<li><strong>notification<\/strong>: (notification_id UUID, user_id UUID, album_id UUID, photo_id UUID)<\/li>\n<\/ul>\n<p>Or this database variable:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#cc99cc;\">struct <\/span><span style=\"color:#d3d0c8;\">FileData {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">sessions<\/span><span style=\"color:#d3d0c8;\">: HashMap&lt;Uuid, Session&gt;,\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">users<\/span><span style=\"color:#d3d0c8;\">: HashMap&lt;Uuid, User&gt;,\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">albums<\/span><span style=\"color:#d3d0c8;\">: HashMap&lt;Uuid, Album&gt;,\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><span style=\"color:#cc99cc;\">struct <\/span><span style=\"color:#d3d0c8;\">User {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">id<\/span><span style=\"color:#d3d0c8;\">: Uuid,\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">username<\/span><span style=\"color:#d3d0c8;\">: Box&lt;<\/span><span style=\"color:#cc99cc;\">str<\/span><span style=\"color:#d3d0c8;\">&gt;,\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">notifications<\/span><span style=\"color:#d3d0c8;\">: Vec&lt;Notification&gt;,\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><span style=\"color:#cc99cc;\">struct <\/span><span style=\"color:#d3d0c8;\">Album {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">id<\/span><span style=\"color:#d3d0c8;\">: Uuid,\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">name<\/span><span style=\"color:#d3d0c8;\">: Box&lt;<\/span><span style=\"color:#cc99cc;\">str<\/span><span style=\"color:#d3d0c8;\">&gt;,\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">users<\/span><span style=\"color:#d3d0c8;\">: HashSet&lt;Uuid&gt;,\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">photos<\/span><span style=\"color:#d3d0c8;\">: HashMap&lt;Uuid, Photo&gt;,\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><span style=\"color:#cc99cc;\">struct <\/span><span style=\"color:#d3d0c8;\">Session {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">id<\/span><span style=\"color:#d3d0c8;\">: Uuid,\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">user<\/span><span style=\"color:#d3d0c8;\">: Uuid,\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><span style=\"color:#cc99cc;\">struct <\/span><span style=\"color:#d3d0c8;\">Photo {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">id<\/span><span style=\"color:#d3d0c8;\">: Uuid,\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">date<\/span><span style=\"color:#d3d0c8;\">: DateTime&lt;Utc&gt;,\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><span style=\"color:#cc99cc;\">struct <\/span><span style=\"color:#d3d0c8;\">Notification {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">id<\/span><span style=\"color:#d3d0c8;\">: Uuid,\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">album<\/span><span style=\"color:#d3d0c8;\">: Uuid,\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">photo<\/span><span style=\"color:#d3d0c8;\">: Uuid,\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<p>You can read more details of the setup and the code in\nthe <a href=\"https:\/\/git.sitegui.dev\/sitegui\/database-architecture-load-tests\">database-architecture-load-tests<\/a> repo.<\/p>\n<h2>Benchmark results<\/h2>\n<p>This first set of results compares the performance of 4 candidates:<\/p>\n<ul>\n<li>file-2c: file-backed variable with 2 CPUs<\/li>\n<li>file-4c: same but with 4 CPUs<\/li>\n<li>postgres-2c: postgres with 2 CPUs for the backend and 2 CPUs for the database<\/li>\n<li>postgres-4c: same but with 4 CPUs each<\/li>\n<\/ul>\n<p>Each point in the charts below represent a test run. Before each, the application was preloaded with 10k users and 10k\nalbums. Each album had a number of users and photos between 5k and 1,\nfollowing <a href=\"https:\/\/en.wikipedia.org\/wiki\/Zipf%27s_law\">Zipf's curve<\/a>. Then 30 seconds of simulated load followed.<\/p>\n<p>The horizontal axis shows the number of concurrent simulated user visits in each test run. A visit is a sequence of one\ncall to <code>login<\/code>, one to <code>list_albums<\/code>, one to <code>list_photos<\/code>, one to <code>publish_photo<\/code> 10% of the time and one to\n<code>take_notifications<\/code>. Each call has a timeout of 1 second. If there is any problem during the visit, it is abandoned and\nmarked as failed.<\/p>\n<p>There is no additional wait between the calls and the visits: once the backend answers, the simulated user immediate\nbegins the next request. This is important, because it means that even &quot;1 concurrent user&quot; is already orders of\nmagnitude more than my app will likely ever encounter!<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/09\/your-database-can-be-a-single-variable-in-memory-3.png\" alt=\"\" \/><\/p>\n<p>Or as a table view for the 1 concurrent user and 2 CPUs:<\/p>\n<table>\n<thead>\n<tr>\n<th>candidate<\/th>\n<th align=\"right\">file<\/th>\n<th align=\"right\">postgres<\/th>\n<th align=\"right\">ratio<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>simulated visits<\/td>\n<td align=\"right\">45.5 k<\/td>\n<td align=\"right\">5.9 k<\/td>\n<td align=\"right\">7.7x<\/td>\n<\/tr>\n<tr>\n<td>failed visits<\/td>\n<td align=\"right\">0 %<\/td>\n<td align=\"right\">0 %<\/td>\n<td align=\"right\">-<\/td>\n<\/tr>\n<tr>\n<td>visit latency p50<\/td>\n<td align=\"right\">0 ms<\/td>\n<td align=\"right\">3 ms<\/td>\n<td align=\"right\">-<\/td>\n<\/tr>\n<tr>\n<td>visit latency p90<\/td>\n<td align=\"right\">1 ms<\/td>\n<td align=\"right\">38 ms<\/td>\n<td align=\"right\">-<\/td>\n<\/tr>\n<tr>\n<td>CPU time \/ visit<\/td>\n<td align=\"right\">0.34 ms<\/td>\n<td align=\"right\">3.85 ms<\/td>\n<td align=\"right\">11.3x<\/td>\n<\/tr>\n<tr>\n<td>avg memory<\/td>\n<td align=\"right\">0.69 GiB<\/td>\n<td align=\"right\">1.60 GiB<\/td>\n<td align=\"right\">2.3x<\/td>\n<\/tr>\n<tr>\n<td>network \/ visit<\/td>\n<td align=\"right\">16.7 kiB<\/td>\n<td align=\"right\">32.1 kiB<\/td>\n<td align=\"right\">1.9x<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li><strong>simulated visits (k)<\/strong>: the total of simulated visits (with success or not) in the 30-second test run. Note that\ndoubling the number of cores does not double the total of processed visits. This can be explained by the fact that\nwriting and reading to the file-backed variable or the database requires some kind of locking mechanism, so with more\nconcurrent visitors, contention increases, but not throughput<\/li>\n<li><strong>failed visits (%)<\/strong>: visits that had any call timeout after 1 second. <code>postgres<\/code> is clearly close to the breaking\npoint in the larger benchmarks<\/li>\n<li><strong>visit latency (ms)<\/strong>: the median (p50) and 90th percentile time to answer the calls. It clearly increases with\nconcurrent load, due to the saturation effect of locks. Also, <code>postgres<\/code> is larger than <code>file<\/code> because it's more\ncomplex to answer the calls.<\/li>\n<li><strong>CPU time (ms\/visit)<\/strong>: the total CPU execution time necessary to answer a visit<\/li>\n<li><strong>avg memory (GiB)<\/strong>: the average active memory usage during the 30-second test run.<\/li>\n<li><strong>network in+out (kib\/visit)<\/strong>: the average number of bytes transited (in and out) of the machines to answer each\nvisit<\/li>\n<\/ul>\n<p>For <code>postgres<\/code>, the CPU time, memory and network are the sum of the values in both the backend and database machines.<\/p>\n<p>So clearly, for this very simple app, 10 000 users with 128 concurrent ungodly-fast users is sustainable with\nfile-backed variable: <em>all requests<\/em> are answered in less than 1 second and the app consistently uses less than 1 GiB of\nmemory.<\/p>\n<h3>Scale up 10 times<\/h3>\n<p>Now the same idea, but with 100 000 users and using machines with 4 and 8 CPUs:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/09\/your-database-can-be-a-single-variable-in-memory-4.png\" alt=\"\" \/><\/p>\n<p>At this scale, the file-backed variable works well until 4 concurrent users, but starts to break apart after that. So\nthis should leave plenty of space for my nidimages future!<\/p>\n<h2>Final words<\/h2>\n<p>It's important to state some trade-offs of the simplicity of the file-backed variable:<\/p>\n<h3>The ugly<\/h3>\n<ul>\n<li>all your data is in memory. If your app uses large datasets, having it all in memory is not a good idea. For my own\ndeployment of nidimages, I currently have a dozen albums and close to 20 000 photos, and the process uses less than 30\nMiB, so I'm okay with that for now<\/li>\n<li>you have to build indexes and ensure uniqueness yourself. A database usually take care of that for you, so instead you\nare on your own. However, indexes are much less important, because iterating over millions of elements in memory is\nstill very fast<\/li>\n<li>changing course is costly. If you app complexity grows, and you need to break it apart, the single variable models\nbreaks. Refactoring to use a database will probably be painful. You can mitigate it by isolating the places in the\ncode where you touch the database, but still...<\/li>\n<\/ul>\n<h3>The good<\/h3>\n<ul>\n<li><span class=\"fun\">types<\/span>! In Rust, I can correctly type all my data and the compiler helps catch most misuse<\/li>\n<li>it is fast. As fast as expensive RAM can get, as few CPU instructions as you need<\/li>\n<li>it is operable. Running your app doesn't require any external dependency<\/li>\n<\/ul>\n"},{"title":"Presenting Nidimages: photo sharing for family and friends","id":"f961d8d4-cb80-4fe1-859b-3604103fa0f6","updated":"2026-08-01T00:00:00+00:00","author":{"name":"sitegui"},"category":[{"@attributes":{"term":"rust"}},{"@attributes":{"term":"nidimages"}}],"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/08\/presenting-nidimages-photo-sharing-for-family-and-friends","rel":"alternate"}},"published":"2026-08-01T00:00:00+00:00","content":"<p>This year I'm taking a sabbatical pause to explore the world of tech, especially in Europe. So I've searched for a way\nto keep a record of what I'm up to: the cities I visit, the events I attend, the people I talk to, the tech I learn...\nThis way, my family can follow my adventures, and I can help my forgetful brain to keep these memories for longer!<\/p>\n<p>The obvious answer is a shared photo album. But none of the apps that I've tested had quite all that I wanted:<\/p>\n<ul>\n<li>I don't want to use a cloud service like Photo Circle, Google Photos, iCloud, etc because I want to own my data and\nhost it in <a href=\"https:\/\/sitegui.dev\/tag\/homelab\">my homelab<\/a><\/li>\n<li>I use Nextcloud and Memories for my private photos, and they work well. But not so much for sharing albums with people\noutside the instance that do not have an account. Also, the UI\/UX don't work well for my intended usage.<\/li>\n<li>Immich has shared albums with comments, which I wanted, but it doesn't seem to notify users on new comments.<\/li>\n<\/ul>\n<p>So I've decided to develop a new alternative:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/08\/presenting-nidimages-photo-sharing-for-family-and-friends-01.png\" alt=\"Nidimages banner\" \/><\/p>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<p>(pronounced like <em>ni.di.ma.ge<\/em>)<\/p>\n<p>It's simpler than those apps, but it serves me well, and creating it taught me a lot on the way!<\/p>\n<h2>Features<\/h2>\n<p>Nidimages aims to be simple, yet effective. Here's a list of its main features:<\/p>\n<ul>\n<li>create albums with photos, videos and text<\/li>\n<li>the album shows as a timeline, using the creation date of the photos when present in the file<\/li>\n<li>share and invite people to your albums<\/li>\n<li>connected users can comment on photos and videos<\/li>\n<li>optionally, be notified of activity with push notifications<\/li>\n<li>customise album name, URL, description, cover photo and appearance<\/li>\n<li>share the right to post new content with others<\/li>\n<li>original file quality is preserved for download<\/li>\n<li>fast and clean web navigation, light on Javascript usage<\/li>\n<li>accessible interface: supports keyboard navigation and screen reader<\/li>\n<li>multiple language support: English, French and Portuguese<\/li>\n<li>download multiple files as a single archive<\/li>\n<li>mark an album as archived to keep it around without clobbering the home page<\/li>\n<li>no tracking, no external connections nor dependencies<\/li>\n<li>it can be installed as a home shortcut in Android and iOS<\/li>\n<li>as the instance administrator, you can see all albums and users and edit user permissions<\/li>\n<li>easy to host (very lean on CPU and memory: uses less than 100 MiB, simple on-disk file structure, one binary with few\nsystem dependencies, available as a container image for Podman or Docker)<\/li>\n<\/ul>\n<p>You can visit the demo instance at <a href=\"https:\/\/demo.nidimages.sitegui.dev\">https:\/\/demo.nidimages.sitegui.dev<\/a> to browse the photos and add comments. You can\nalso create a new album and upload your own photos.<\/p>\n<p><strong>Note: this is an ephemeral test instance. All data is erased after 15 minutes.<\/strong><\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/08\/presenting-nidimages-photo-sharing-for-family-and-friends-02.png\" alt=\"\" \/><\/p>\n<p>And check <a href=\"https:\/\/git.sitegui.dev\/sitegui\/nidimages\">the source repo<\/a> if you're interested in hosting it yourself!<\/p>\n<h2>What I've learned<\/h2>\n<p>This project had some &quot;firsts&quot; for me:<\/p>\n<p>It's the first time that I took unit tests for UI, keyboard navigation and screen-reader accessibility seriously. I'm\nusing <a href=\"https:\/\/playwright.dev\/\">Playwright<\/a> to write the UI tests, which is an amazing tool that I've discovered in a\ntalk by Manon Carbonnel at Breizhcamp in Rennes.<\/p>\n<p>It's my largest Rust web project so far. I have opted not to use frameworks like <a href=\"https:\/\/leptos.dev\/\">Leptos<\/a> and the\nnew <a href=\"https:\/\/tokio.rs\/blog\/2026-07-22-announcing-topcoat\">Tokio Topcoat<\/a> so that I learn the fundamentals first. In a\nnext project, I'd like to prototype with those to compare the benefits.<\/p>\n<p>My approach for Nidimages was to generate all HTML on the backend (like the good ol' days!), because it produces web\npages that are faster to navigate and interact. This means that heavy JS frameworks were out of the question.<br \/>\nI'm happy with the results, but I'm not so happy with my choice of using Jinja as the templating engine, because I lose\nthe advantages of the type system from Rust. Naturally, the Rust compiler is unaware of the connection between the Rust\nstructs and the templates. So refactoring or evolving the Rust code always requires find-and-replace and testing (both\nmanual and automatic), because silent bugs can pop anywhere.<\/p>\n<p>I've also discovered <a href=\"https:\/\/developer.mozilla.org\/docs\/Web\/HTML\/Reference\/Elements\/dialog\">the\n<code>&lt;dialog&gt;<\/code> HTML element<\/a>, which is pretty nice!\nIt implements the oh-so-common &quot;modal&quot; pattern where a new element pops on the screen for immediate attention (like\nasking for some information or confirmation):<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-html\"><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">&lt;<\/span><span style=\"color:#f2777a;\">button <\/span><span style=\"color:#f99157;\">command<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">show-modal<\/span><span style=\"color:#d3d0c8;\">&quot; <\/span><span style=\"color:#f99157;\">commandfor<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">my-modal<\/span><span style=\"color:#d3d0c8;\">&quot;&gt;\n<\/span><span style=\"color:#d3d0c8;\">    Open modal\n<\/span><span style=\"color:#d3d0c8;\">&lt;\/<\/span><span style=\"color:#f2777a;\">button<\/span><span style=\"color:#d3d0c8;\">&gt;\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">&lt;<\/span><span style=\"color:#f2777a;\">dialog <\/span><span style=\"color:#f99157;\">closedby<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">any<\/span><span style=\"color:#d3d0c8;\">&quot; <\/span><span style=\"color:#6699cc;\">id<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">my-modal<\/span><span style=\"color:#d3d0c8;\">&quot;&gt;\n<\/span><span style=\"color:#d3d0c8;\">    &lt;<\/span><span style=\"color:#f2777a;\">form <\/span><span style=\"color:#f99157;\">action<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">\/save-pi<\/span><span style=\"color:#d3d0c8;\">&quot; <\/span><span style=\"color:#f99157;\">method<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">post<\/span><span style=\"color:#d3d0c8;\">&quot;&gt;\n<\/span><span style=\"color:#d3d0c8;\">        &lt;<\/span><span style=\"color:#f2777a;\">label<\/span><span style=\"color:#d3d0c8;\">&gt;\n<\/span><span style=\"color:#d3d0c8;\">            Favourite passage of \u03c0:\n<\/span><span style=\"color:#d3d0c8;\">            &lt;<\/span><span style=\"color:#f2777a;\">input <\/span><span style=\"color:#f99157;\">class<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">input<\/span><span style=\"color:#d3d0c8;\">&quot; <\/span><span style=\"color:#f99157;\">name<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">pi<\/span><span style=\"color:#d3d0c8;\">&quot;&gt;\n<\/span><span style=\"color:#d3d0c8;\">        &lt;\/<\/span><span style=\"color:#f2777a;\">label<\/span><span style=\"color:#d3d0c8;\">&gt;\n<\/span><span style=\"color:#d3d0c8;\">        &lt;<\/span><span style=\"color:#f2777a;\">br<\/span><span style=\"color:#d3d0c8;\">&gt;\n<\/span><span style=\"color:#d3d0c8;\">        &lt;<\/span><span style=\"color:#f2777a;\">button <\/span><span style=\"color:#f99157;\">commandfor<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">my-modal<\/span><span style=\"color:#d3d0c8;\">&quot; <\/span><span style=\"color:#f99157;\">command<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">close<\/span><span style=\"color:#d3d0c8;\">&quot; <\/span><span style=\"color:#f99157;\">type<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">button<\/span><span style=\"color:#d3d0c8;\">&quot;&gt;\n<\/span><span style=\"color:#d3d0c8;\">            Cancel\n<\/span><span style=\"color:#d3d0c8;\">        &lt;\/<\/span><span style=\"color:#f2777a;\">button<\/span><span style=\"color:#d3d0c8;\">&gt;\n<\/span><span style=\"color:#d3d0c8;\">        &lt;<\/span><span style=\"color:#f2777a;\">button <\/span><span style=\"color:#f99157;\">type<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">submit<\/span><span style=\"color:#d3d0c8;\">&quot;&gt;Save&lt;\/<\/span><span style=\"color:#f2777a;\">button<\/span><span style=\"color:#d3d0c8;\">&gt;\n<\/span><span style=\"color:#d3d0c8;\">    &lt;\/<\/span><span style=\"color:#f2777a;\">form<\/span><span style=\"color:#d3d0c8;\">&gt;\n<\/span><span style=\"color:#d3d0c8;\">&lt;\/<\/span><span style=\"color:#f2777a;\">dialog<\/span><span style=\"color:#d3d0c8;\">&gt;\n<\/span><\/code><\/pre>\n<p><button command=\"show-modal\" commandfor=\"my-modal\">Open modal<\/button><\/p>\n<dialog closedby=\"any\" id=\"my-modal\">\n    <form action=\"\/\" method=\"get\">\n        <label>Favourite passage of \u03c0: <input class=\"input\" name=\"pi\"><\/label><br>\n        <button commandfor=\"my-modal\" command=\"close\" type=\"button\">Cancel<\/button>\n        <button type=\"submit\">Save<\/button>\n    <\/form>\n<\/dialog>\n<p><em>Just use a div for this!<\/em>, is what past me would say. But past me was unaware of how <code>&lt;dialog&gt;<\/code> is more accessible. It\nsolves all these problems, some of which are very hard to implement correctly:<\/p>\n<ul>\n<li>the dialog starts hidden<\/li>\n<li>when opened, the first interactive element is focused. This is important for screen-readers and people navigating with\nthe keyboard<\/li>\n<li>keyboard navigation is constrained to the modal, so pressing <code>Tab<\/code> will not focus elements behind the modal<\/li>\n<li>pressing <code>Esc<\/code> or clicking outside dismisses the modal<\/li>\n<li>when opened, the page behind becomes dimmed and inert<\/li>\n<li>it integrates with <code>&lt;button&gt;<\/code> without Javascript! Use <code>command=&quot;show-modal&quot;<\/code> to open and <code>command=&quot;close&quot;<\/code> to close.<\/li>\n<\/ul>\n<p>And I also\nhad <a href=\"https:\/\/git.sitegui.dev\/sitegui\/nidimages\/src\/commit\/767c567b6f8f6cd3f6b7ba2e0029f26b91f081c4\/resources\/web\/static\/itemFullScreen.js#L333\">some math fun<\/a>\nto implement the pinch zoom interaction \ud83d\ude07<\/p>\n"},{"title":"Exploring Prometheus' time series data format","id":"a39c66c1-4ba1-4c7a-80fd-5d98a1234883","updated":"2026-07-21T00:00:00+00:00","author":{"name":"sitegui"},"category":[{"@attributes":{"term":"prometheus"}},{"@attributes":{"term":"programming"}},{"@attributes":{"term":"data"}}],"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/07\/exploring-prometheus-time-series-data-format","rel":"alternate"}},"published":"2026-07-21T00:00:00+00:00","content":"<p>I like bytes, they're handy! I also like laying them in the right way to make things work and work fast. It turns out\nI'm far from the only byte-nerd out there :)<\/p>\n<p>In this post I'll dive deep into how <a href=\"https:\/\/prometheus.io\/\">Prometheus<\/a>, an open source metrics and monitoring\nservice, represents time series information on the disk. To write this post, I've\nread <a href=\"https:\/\/github.com\/prometheus\/prometheus\/tree\/2cf323988931bd586a2ab25160e46bcace9398ae\/tsdb\">Prometheus' implementation<\/a>,\nplayed around with real data and also read\nthis <a href=\"https:\/\/ganeshvernekar.com\/blog\/prometheus-tsdb-the-head-block\/\">series of posts by Ganesh Vernekar<\/a>.<\/p>\n<p>My goal is not to provide full documentation, instead I want to explore and learn with others' designs. I'll also\nspeculate why some things are the way they are and compare trade-offs with other alternatives I know.<\/p>\n<p>This is the first post of a two-post series. The second installment will come soon, stay tuned with the RSS feed or the\nnewsletter!<\/p>\n<h2>The basic model<\/h2>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<p>Prometheus' database subsystem is called <code>tsdb<\/code> (for Time Series Data Base) and stores &quot;series&quot;. A series is uniquely\nidentified by a set of labels, for example: <code>__name__ = cpu_usage, host = 123.4.5.6<\/code>. While the label <code>__name__<\/code>\nis treated specially on the UI and query syntax, internally it's just another label.<\/p>\n<p>A series is made of an immutable sorted list of samples <code>(timestamp: i64, value: f64)<\/code>. The timestamp is measured in\nmilliseconds.<\/p>\n<p>And that's it! The actual model has two more features that I'm not covering here: histograms and tombstones.<\/p>\n<h2>The tiered representation<\/h2>\n<p>A typical deployment of Prometheus is constantly ingesting new samples for many series. For a given series, the samples\nare received in order. But for different series, this may not be the case.<\/p>\n<p>To strike a balance between memory usage and performance, Prometheus uses a three-tier system to represent its data:<\/p>\n<ol>\n<li>recent samples, up to 30 minutes ago<\/li>\n<li>somewhat recent samples, up to 3 hours ago<\/li>\n<li>history samples, more than 3 hours ago<\/li>\n<\/ol>\n<p>The actual timings depend on system load and configuration, but it's a good initial guess.<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/07\/exploring-prometheus-time-series-data-format-1.png\" alt=\"The sequence of different memory and disk representations\" \/><\/p>\n<p>New samples are stored directly in memory, and a copy of the information is appended to the write-ahead log file. This\nfile is mostly used during disaster recovery to reconstruct the in-memory state, when the database crashes or is\notherwise shutdown abruptly.<\/p>\n<p>After a while, samples are compacted and saved to the disk as &quot;head blocks&quot;. This works especially well for Prometheus\nbecause series are immutable and samples arrive mostly with a timestamp close to the current clock. So these files once\npersisted do not need to be modified any more. At this tier, the index which keeps information about the series labels\nand where they are in the head chunk file is still in active memory.<\/p>\n<p>After a while, the head chunk is compacted together with the index to the disk. Prometheus also merges older blocks to\nreduce the number of files and reuse index information. It also deletes very old data to reduce storage usage.<\/p>\n<p>On the disk, the files look like this:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code><span style=\"color:#d3d0c8;\">\u251c\u2500\u2500 01KWRYYXKSATHRGKQ2CWPDBX1B # history block\n<\/span><span style=\"color:#d3d0c8;\">\u2502   \u251c\u2500\u2500 chunks\n<\/span><span style=\"color:#d3d0c8;\">\u2502   \u2502   \u2514\u2500\u2500 000001\n<\/span><span style=\"color:#d3d0c8;\">\u2502   \u251c\u2500\u2500 index\n<\/span><span style=\"color:#d3d0c8;\">\u2502   \u251c\u2500\u2500 meta.json\n<\/span><span style=\"color:#d3d0c8;\">\u2502   \u2514\u2500\u2500 tombstones\n<\/span><span style=\"color:#d3d0c8;\">\u251c\u2500\u2500 ...\n<\/span><span style=\"color:#d3d0c8;\">\u251c\u2500\u2500 chunks_head                # head block\n<\/span><span style=\"color:#d3d0c8;\">\u2502   \u251c\u2500\u2500 005151\n<\/span><span style=\"color:#d3d0c8;\">\u2502   \u2514\u2500\u2500 ...\n<\/span><span style=\"color:#d3d0c8;\">\u251c\u2500\u2500 queries.active\n<\/span><span style=\"color:#d3d0c8;\">\u2514\u2500\u2500 wal                        # write-ahead log\n<\/span><span style=\"color:#d3d0c8;\">    \u251c\u2500\u2500 00005291\n<\/span><span style=\"color:#d3d0c8;\">    \u251c\u2500\u2500 ...\n<\/span><span style=\"color:#d3d0c8;\">    \u2514\u2500\u2500 checkpoint.00005290\n<\/span><span style=\"color:#d3d0c8;\">        \u2514\u2500\u2500 00000000\n<\/span><\/code><\/pre>\n<p>In this post I'll focus on the &quot;history&quot; data format stored in block folders like &quot;01KWRYYXKSATHRGKQ2CWPDBX1B&quot;\nin the example above.<\/p>\n<h2>What is in the index?<\/h2>\n<p>Let's start with the <code>index<\/code> file, like &quot;01KWRYYXKSATHRGKQ2CWPDBX1B\/index&quot; in my example. It has two main purposes:<\/p>\n<ol>\n<li>store series labels and reference the actual samples data stored in files like &quot;chunks\/000001&quot;<\/li>\n<li>store a reverse index of label names and values, so that Prometheus can use this condensed information to know which\nseries match a given user query<\/li>\n<\/ol>\n<p>Before discussing the binary representation of the data, I think it's helpful to show what &quot;fields&quot; are stored in the\nindex file. I'll use Python for illustrative purposes, but there is no relationship with the actual Go codebase.<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-python\"><span style=\"color:#cc99cc;\">class <\/span><span style=\"color:#ffcc66;\">Index<\/span><span style=\"color:#f2f0ec;\">:\n<\/span><span style=\"color:#d3d0c8;\">    strings: list[str]\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#747369;\"># purpose nb. 1\n<\/span><span style=\"color:#d3d0c8;\">    series: list[Series]\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#747369;\"># purpose nb. 2\n<\/span><span style=\"color:#d3d0c8;\">    postings: list[Posting]\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#cc99cc;\">class <\/span><span style=\"color:#ffcc66;\">Series<\/span><span style=\"color:#f2f0ec;\">:\n<\/span><span style=\"color:#d3d0c8;\">    labels: list[tuple(int, int)]\n<\/span><span style=\"color:#d3d0c8;\">    chunks: list[Chunk]\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#cc99cc;\">class <\/span><span style=\"color:#ffcc66;\">Chunk<\/span><span style=\"color:#f2f0ec;\">:\n<\/span><span style=\"color:#d3d0c8;\">    min_timestamp: int\n<\/span><span style=\"color:#d3d0c8;\">    max_timestamp: int\n<\/span><span style=\"color:#d3d0c8;\">    file: int\n<\/span><span style=\"color:#d3d0c8;\">    offset_in_file: int\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#cc99cc;\">class <\/span><span style=\"color:#ffcc66;\">Posting<\/span><span style=\"color:#f2f0ec;\">:\n<\/span><span style=\"color:#d3d0c8;\">    name: str\n<\/span><span style=\"color:#d3d0c8;\">    value: str\n<\/span><span style=\"color:#d3d0c8;\">    series_indexes: list[int]\n<\/span><\/code><\/pre>\n<p>Let's start with <code>Index.strings<\/code>: it stores all the label names and values present in this block. This allows the\n<code>Series.labels<\/code> elements to use the index of string on the list instead of storing the whole string again. This\ntechnique is called &quot;string interning&quot; or also &quot;dictionary coding&quot; and it's very common in performance-sensitive\napplications. It works well because operating with integers (copying, comparing, etc) is just one instruction, while\nstrings require external allocation and work proportional to the string's size.<\/p>\n<p>Then <code>Index.series<\/code> contains the list of all series stored in this block. Remember that series are uniquely identified\nby their set of labels, represented in <code>Series.labels<\/code> as a list of entries, each one with the string id for the label\nname and value, respectively.<\/p>\n<p>The <code>Series.chunks<\/code> stores some metadata about all the chunks of samples that compose this series in this block. The\nchunks here are non-overlapping and sorted, that is: <code>chunks[n].min_timestamp &gt; chunks[n-1].max_timestamp<\/code>. The actual\nsamples data is not present in the index, making the index file light-weight. The actual data is stored in another chunk\nfile, referenced by <code>Chunk.file<\/code> and <code>Chunk.offset_in_file<\/code>.<\/p>\n<p>Note how <code>Series.chunks<\/code> is useful for answering user timestamp queries, since it's very easy to identify which chunks\ndo not cover the desired period and therefore don't need to be read and decoded.<\/p>\n<p>And finally, <code>Index.postings<\/code>. The term &quot;posting&quot; may not mean much to you, you can think of it as &quot;index entry&quot;. This\nwill contain <strong>all<\/strong> the label name and value pairs, in order, and list the index of the series that have this pair.<\/p>\n<p>For example, if the block has the series <code>a=1, b=2<\/code> and <code>a=1, b=3<\/code>, posting would be:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-python\"><span style=\"color:#d3d0c8;\">[\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#6699cc;\">Posting<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f2777a;\">name<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">a<\/span><span style=\"color:#d3d0c8;\">&quot;, <\/span><span style=\"color:#f2777a;\">value<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">1<\/span><span style=\"color:#d3d0c8;\">&quot;, <\/span><span style=\"color:#f2777a;\">series_indexes<\/span><span style=\"color:#d3d0c8;\">=[<\/span><span style=\"color:#f99157;\">0<\/span><span style=\"color:#d3d0c8;\">, <\/span><span style=\"color:#f99157;\">1<\/span><span style=\"color:#d3d0c8;\">]),\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#6699cc;\">Posting<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f2777a;\">name<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">b<\/span><span style=\"color:#d3d0c8;\">&quot;, <\/span><span style=\"color:#f2777a;\">value<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">2<\/span><span style=\"color:#d3d0c8;\">&quot;, <\/span><span style=\"color:#f2777a;\">series_indexes<\/span><span style=\"color:#d3d0c8;\">=[<\/span><span style=\"color:#f99157;\">0<\/span><span style=\"color:#d3d0c8;\">]),\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#6699cc;\">Posting<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f2777a;\">name<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">b<\/span><span style=\"color:#d3d0c8;\">&quot;, <\/span><span style=\"color:#f2777a;\">value<\/span><span style=\"color:#d3d0c8;\">=&quot;<\/span><span style=\"color:#99cc99;\">3<\/span><span style=\"color:#d3d0c8;\">&quot;, <\/span><span style=\"color:#f2777a;\">series_indexes<\/span><span style=\"color:#d3d0c8;\">=[<\/span><span style=\"color:#f99157;\">1<\/span><span style=\"color:#d3d0c8;\">]),\n<\/span><span style=\"color:#d3d0c8;\">]\n<\/span><\/code><\/pre>\n<p>In a sense, Prometheus indexes all the labels for you. That is part of the reason why Prometheus does not scale well if\nyou use labels with too many distinct values. To illustrate, imagine that you have &quot;user_id&quot; as a label. Then each user\nin your system that produced a sample in this block timespan will have an entry in the postings. And this information\nrepeats on the index of each &quot;history&quot; block!<\/p>\n<p>This <code>postings<\/code> list is used by the query subsystem to find which series in this block match a given user query. Since\nit's stored in order, it's efficient to search. For example, if your Promql is\n<code>cpu_usage{app=&quot;email&quot;, host=&quot;example.com&quot;}<\/code> it will do a multi-intersection of the series indexes for\n<code>Posting(name=&quot;__name__&quot;, value=&quot;cpu_usage&quot;)<\/code>, <code>Posting (name=&quot;app&quot;, value=&quot;email&quot;)<\/code> and\n<code>Posting(name=&quot;host&quot;, value=&quot;host&quot;)<\/code>. Also, note that <code>Posting.series_indexes<\/code> is sorted, so doing set operations\n(union, intersection, etc) can be done efficiently.<\/p>\n<p>And a final note: the <code>Posting.name<\/code> and <code>Posting.value<\/code> do not use string ids to reference <code>Index.strings<\/code>! It's\nunclear to me why, though.<\/p>\n<h2>I've lied to you<\/h2>\n<p>This post is about the time series format on the <em>disk<\/em>. So the Python classes above are all a lie, but a useful lie\nnonetheless. In practice, the index file contains these pieces of information, in sequence:<\/p>\n<ol>\n<li>magic number (4 bytes): the value 0xBAAAD700<\/li>\n<li>version (1 byte): the value 0x02<\/li>\n<li><strong>symbol table<\/strong>: corresponds to the <code>Index.strings<\/code> data above<\/li>\n<li><strong>series<\/strong>: corresponds to the <code>Index.series<\/code> data above<\/li>\n<li>label index: not used<\/li>\n<li><strong>postings<\/strong>: correspond to the <code>Posting.series_indexes<\/code> in <code>Index.postings<\/code> data above<\/li>\n<li>label offset table: not used<\/li>\n<li><strong>postings offset table<\/strong>: corresponds to the <code>Index.postings<\/code> data above (except for <code>Posting.series_indexes<\/code>)<\/li>\n<li>table of contents: contains the byte offset where each section above starts<\/li>\n<\/ol>\n<p>If you are curious, you can check\nthe <a href=\"https:\/\/github.com\/prometheus\/prometheus\/blob\/2cf323988931bd586a2ab25160e46bcace9398ae\/tsdb\/docs\/format\/index.md\">full format documentation<\/a>.<\/p>\n<h3>The symbol table<\/h3>\n<p>What in Python was <code>Index.strings: list[str]<\/code> is represented as the following:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/07\/exploring-prometheus-time-series-data-format-2.png\" alt=\"The byte sections of the symbol table\" \/><\/p>\n<p>The format &quot;u32&quot; means 32-bit unsigned integer in big endian. So, for example, the decimal value 31415 is represented\nwith the bytes 0x00 0x00 0x7a 0xb7.<\/p>\n<p>The format &quot;var u32&quot; means a variable integer that can represent up to an unsigned integer of 32 bits, with smaller\nvalues using fewer bytes. The algorithm is: take the binary representation of the number and split into groups of 7\nbits; prepend a bit zero to the last group and one to the others.<\/p>\n<p>For example:<\/p>\n<table>\n<thead>\n<tr>\n<th align=\"right\">Decimal<\/th>\n<th align=\"right\">Binary<\/th>\n<th>var u32<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"right\">3<\/td>\n<td align=\"right\">11<\/td>\n<td>0x03<\/td>\n<\/tr>\n<tr>\n<td align=\"right\">314<\/td>\n<td align=\"right\">10 0111010<\/td>\n<td>0xba 0x02<\/td>\n<\/tr>\n<tr>\n<td align=\"right\">31415<\/td>\n<td align=\"right\">1 1110101 0110111<\/td>\n<td>0xb7 0xf5 0x01<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>And finally, CRC32 allows detecting (but not correcting) errors in the encoding.<\/p>\n<h3>An alternative to Prometheus' symbol table format<\/h3>\n<p>Prometheus strangely uses this variable encoding that does not allow efficient random access. For example, to get the\nstring at index 7, the code needs to iterate and decode the 7 preceding elements. Another approach that does allow\nrandom access is how Apache Arrow encodes <code>list[str]<\/code>, with two lists: one with all the strings back to back and one\nwith the start offsets in that buffer:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/07\/exploring-prometheus-time-series-data-format-3.png\" alt=\"An alternative byte representation based on Apache Arrow\" \/><\/p>\n<p>For example, encoding <code>[&quot;Hello&quot;, &quot;world&quot;, &quot;!&quot;]<\/code> uses 39 bytes:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code><span style=\"color:#d3d0c8;\">0x00 0x00 0x00 0x23 # 35 bytes follow\n<\/span><span style=\"color:#d3d0c8;\">0x00 0x00 0x00 0x03 # 3 entries\n<\/span><span style=\"color:#d3d0c8;\">0x00 0x00 0x00 0x00 # String 0 starts at offset 0\n<\/span><span style=\"color:#d3d0c8;\">0x00 0x00 0x00 0x05 # String 1 starts at offset 5\n<\/span><span style=\"color:#d3d0c8;\">0x00 0x00 0x00 0x0a # String 2 starts at offset 10\n<\/span><span style=\"color:#d3d0c8;\">0x00 0x00 0x00 0x0b # String 2 ends at offset 11\n<\/span><span style=\"color:#d3d0c8;\">H    e    l    l    # all the strings\n<\/span><span style=\"color:#d3d0c8;\">o    w    o    r\n<\/span><span style=\"color:#d3d0c8;\">l    d    !    0x..\n<\/span><span style=\"color:#d3d0c8;\">0x.. 0x.. 0x..      # the CRC32\n<\/span><\/code><\/pre>\n<p>Accessing the string at index <code>n<\/code> means reading the offsets at bytes <code>8 + 4*n<\/code> and <code>12 + 4n<\/code>, and then slicing the\nbuffer between those offsets.<\/p>\n<p>I've tested with a real index file of mine that has 1 635 strings. With Prometheus' encoding it has 37.4 kiB. With the\nalternative discussed above, it has 42.2 kiB (+13%).<\/p>\n<p>Prometheus' seems to get around the lack of random access support by eagerly decoding the table into a Go map. But with\nrandom access support backed into the format, this step would not be necessary. It also makes it possible to do binary\nsearch on the encoded data directly to find the id of a given string.<\/p>\n<h2>The series<\/h2>\n<p>What in Python is<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-python\"><span style=\"color:#cc99cc;\">class <\/span><span style=\"color:#ffcc66;\">Index<\/span><span style=\"color:#f2f0ec;\">:\n<\/span><span style=\"color:#d3d0c8;\">    series: list[Series]\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#cc99cc;\">class <\/span><span style=\"color:#ffcc66;\">Series<\/span><span style=\"color:#f2f0ec;\">:\n<\/span><span style=\"color:#d3d0c8;\">    labels: list[tuple(int, int)]\n<\/span><span style=\"color:#d3d0c8;\">    chunks: list[Chunk]\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#cc99cc;\">class <\/span><span style=\"color:#ffcc66;\">Chunk<\/span><span style=\"color:#f2f0ec;\">:\n<\/span><span style=\"color:#d3d0c8;\">    min_timestamp: int\n<\/span><span style=\"color:#d3d0c8;\">    max_timestamp: int\n<\/span><span style=\"color:#d3d0c8;\">    file: int\n<\/span><span style=\"color:#d3d0c8;\">    offset_in_file: int\n<\/span><\/code><\/pre>\n<p>in Prometheus' index binary encoding is:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/07\/exploring-prometheus-time-series-data-format-4.png\" alt=\"The encoding for the series section\" \/><\/p>\n<p>Note again the design choice of the format to privilege compactness at the cost of random access: all the labels and\nchunk information are stored as variable-sized values. For example, if the reader is only interested in the chunk\ninformation, it still needs to decode and iterate over all labels, since it cannot &quot;jump to&quot; the <code>num_chunks<\/code> value.<\/p>\n<p>In the Python code above I've represented <code>Chunk.file: int<\/code> and <code>Chunk.offset_in_file: int<\/code> as separate fields. However,\nPrometheus uses a single value with both information as <code>Chunk.file &lt;&lt; 32 | Chunk.offset_in_file<\/code>.<\/p>\n<p>To make the representation even more compact, the max timestamp and (for the second chunk onwards) the min timestamp and\nthe offset are represented as delta values:<\/p>\n<ul>\n<li><code>\u0394max_ts[i] = max_ts[i] - min_ts[i]<\/code><\/li>\n<li><code>\u0394min_ts[i] = min_ts[i] - min_ts[i-1]<\/code><\/li>\n<li><code>\u0394offset[i] = offset[i] - offset[i-1]<\/code><\/li>\n<\/ul>\n<p>This works well for timestamps because the chunks are sorted and also the delta is in the order of 100k milliseconds\n(minutes), while the original value is the number of milliseconds since 1970-01-01.<\/p>\n<p>A few words about the <code>var i64<\/code> encoding used above: it simply appends a zero at the end for positive values. For\nnegative values, it inverts the bits then appends a one. So decoding can be done as:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-python\"><span style=\"color:#cc99cc;\">def <\/span><span style=\"color:#6699cc;\">read_var_i64<\/span><span style=\"color:#d3d0c8;\">():\n<\/span><span style=\"color:#d3d0c8;\">    raw = <\/span><span style=\"color:#6699cc;\">read_var_u64<\/span><span style=\"color:#d3d0c8;\">()\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">if <\/span><span style=\"color:#d3d0c8;\">raw % <\/span><span style=\"color:#f99157;\">2 <\/span><span style=\"color:#d3d0c8;\">== <\/span><span style=\"color:#f99157;\">0<\/span><span style=\"color:#d3d0c8;\">:\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#747369;\"># Positive\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#cc99cc;\">return <\/span><span style=\"color:#d3d0c8;\">raw &gt;&gt; <\/span><span style=\"color:#f99157;\">1\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">return <\/span><span style=\"color:#d3d0c8;\">~(raw &gt;&gt; <\/span><span style=\"color:#f99157;\">1<\/span><span style=\"color:#d3d0c8;\">)\n<\/span><\/code><\/pre>\n<h2>The postings<\/h2>\n<p>What in Python is quite simple:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-python\"><span style=\"color:#cc99cc;\">class <\/span><span style=\"color:#ffcc66;\">Index<\/span><span style=\"color:#f2f0ec;\">:\n<\/span><span style=\"color:#d3d0c8;\">    postings: list[Posting]\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#cc99cc;\">class <\/span><span style=\"color:#ffcc66;\">Posting<\/span><span style=\"color:#f2f0ec;\">:\n<\/span><span style=\"color:#d3d0c8;\">    name: str\n<\/span><span style=\"color:#d3d0c8;\">    value: str\n<\/span><span style=\"color:#d3d0c8;\">    series_indexes: list[int]\n<\/span><\/code><\/pre>\n<p>in Prometheus' format gets a bit funky:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/07\/exploring-prometheus-time-series-data-format-5.png\" alt=\"Encoding for the postings\" \/><\/p>\n<p>That's a lot to unpack! I don't fully understand the why's of this design, but I'll do my best to speculate ;)<\/p>\n<p>The green section basically encodes <code>list[Posting(name, value)]<\/code> and the blue section encodes\n<code>list[Posting(series_indexes)]<\/code>. The green section has the byte offset of the index file that contains the corresponding\nblue section. When I first saw this format, I thought that it could be a trick to encode each set of series only once:\nthat is, if the set of series with <code>name_1=value_1<\/code> is the same as the set of series with <code>name_2=value_2<\/code>, then two\ndistinct green sections would point the same blue one. But this does not seem to happen! In a real block that I had in\nmy server, there are 1 609 postings that represent all the distinct <code>(name, value)<\/code> pairs that are present in the block.\nThese 1 609 green sections are each one pointing to a distinct blue section, while in fact there are only 1 437 distinct\nsets of series!<\/p>\n<p>As you can see in the format above, the postings do not use the string ids in <code>name<\/code> and <code>value<\/code>, instead repeating\nthem. This is a bit sad for memory density, and I guess it's slower for exact matches. For example, when searching for\n<code>name=value<\/code>, if the postings only contained the string ids, the query engine could translate <code>name<\/code> and <code>value<\/code> to\ntheir equivalent ids and then search the sorted list of postings for this pair.<\/p>\n<p>I <em>guess<\/em> that instead of string ids, Prometheus uses this &quot;inlined&quot; repetitive representation in order to be faster\nwhen searching for regex patterns. For example, when searching for <code>name~=.*-value<\/code> (that is, label values that end with\n&quot;-value&quot;), there are fewer memory indirections to read the value, since it's already inlined in the posting itself.<\/p>\n<h3>An alternative to Prometheus' postings format<\/h3>\n<p>As I've discussed in the previous &quot;alternative&quot; heading, in using variable encoding, the format loses the ability of\nrandom access. For the postings, random access is nice because it allows for fast binary searches.<\/p>\n<p>If I were to redesign this part of the format for random acccess and higher memory density, I think this would be a nice\nstarting point:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/07\/exploring-prometheus-time-series-data-format-6.png\" alt=\"Alternative encoding for the postings\" \/><\/p>\n<p>To understand the trade-offs, let's do a query <code>name=value<\/code> step by step:<\/p>\n<ol>\n<li>first we need to translate <code>name<\/code> and <code>value<\/code> to their corresponding string ids, using the alternative symbol table\nformat presente before. For this example, say it wields <code>name=31<\/code>, <code>value=41<\/code>.<\/li>\n<li>do a binary search on the green area for the pair <code>(31, 41)<\/code>. This is <em>very<\/em> fast because the code can interpret each\npair as a single u64 element. For this example, say it wields index <code>59<\/code>.<\/li>\n<li>find the offset start and end using the read area. For this example, we access the indexes <code>59<\/code> and <code>60<\/code>. Say the\noffsets are <code>26535<\/code> and <code>26635<\/code><\/li>\n<li>now just read the series ids from the blue area between those two offsets<\/li>\n<\/ol>\n<h2>Next station: the actual samples encoding<\/h2>\n<p>This post is getting a bit too long, so keep tuned for the next half that will discuss the neat packing of the series'\nsamples. \ud83e\udd13 You can subscribe to the newsletter or consume the RSS feed.<\/p>\n"},{"title":"How I encrypt my backups","id":"03828614-9b2e-4fbb-9c7d-7dcf91beed1f","updated":"2026-07-08T00:00:00+00:00","author":{"name":"sitegui"},"category":[{"@attributes":{"term":"programming"}},{"@attributes":{"term":"homelab"}}],"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/07\/how-i-encrypt-my-backups","rel":"alternate"}},"published":"2026-07-08T00:00:00+00:00","content":"<p>Automated backups that work are mind-liberating! I've shared\nin <a href=\"https:\/\/sitegui.dev\/post\/2026\/06\/how-i-back-up-my-data\">the previous post how I back up my data<\/a>. In this post\nI'll explore in detail how I handle encryption to protect my server and the insuing backup disks.<\/p>\n<h2>What does my system solve?<\/h2>\n<p>My encryption usage is meant to protect mostly against this threat:<\/p>\n<blockquote>\n<p>if a stranger has access to any of my disks while they are unenergised, they must not be able to access my data.<\/p>\n<\/blockquote>\n<p>For the backup disks this is rather simple to achieve by using full disk encryption\nwith <a href=\"https:\/\/en.wikipedia.org\/wiki\/Linux_Unified_Key_Setup\">Linux Unified Key Setup<\/a> (LUKS).<\/p>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<p>For the homelab's own disk, though, it's a different story: if I use full-disk encryption, I'll have to type in my\npasskey during boot. But this doesn't work for me: when I'm not physically home, I still want to be able to boot\nmy server. That's why I'm using two partitions on the server: one unencrypted, with the OS and some base services, and\none fully encrypted, mounted at <code>\/home\/sitegui\/protected<\/code>.<\/p>\n<p>Note that this setup does not protect against intrusion and tampering with the OS! A malicious attacker with physical\naccess to my server could modify the disk at rest to add a service that exfiltrates my passkey once entered. But I've\nruled this threat as out of scope for my use case.<\/p>\n<h2>The server<\/h2>\n<p>In my specific hardware, I have two SSDs. I've manually configured during installation that the smallest one (128 GB)\nhosts the OS and the filesystem root, while the largest one (4 TB) is fully encrypted with LUKS. But if I had only one\ndisk, I could split it during OS installation into two virtual volumes.<\/p>\n<p>To mount the protected disk, I have to run these commands:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-shell\"><span style=\"color:#d3d0c8;\">sudo cryptsetup open \/dev\/disk\/by-uuid\/b3898343-9627-4ef6-9a91-7c49d6690acb protected\n<\/span><span style=\"color:#d3d0c8;\"># Type in the passkey\n<\/span><span style=\"color:#d3d0c8;\">sudo mount \/dev\/mapper\/protected \/home\/sitegui\/protected\n<\/span><span style=\"color:#d3d0c8;\">systemctl --user start protected.target\n<\/span><\/code><\/pre>\n<p>The UUID can be obtained with <code>lsblk --fs<\/code> and the passkey can be passed as the stdin for the script. The call to\n<code>systemctl<\/code> starting the custom target &quot;protected&quot; is the one that tells all my other services that they can now start.<\/p>\n<p>This is easy to do in systemd by creating a file <code>~\/.config\/systemd\/user\/protected.target<\/code> with:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code><span style=\"color:#d3d0c8;\">[Unit]\n<\/span><span style=\"color:#d3d0c8;\">Description=Protected services\n<\/span><\/code><\/pre>\n<p>and then adding this section to every service that should not start on boot, but instead wait for the target to be\nexplicitly started:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code><span style=\"color:#d3d0c8;\">[Install]\n<\/span><span style=\"color:#d3d0c8;\">WantedBy = protected.target\n<\/span><\/code><\/pre>\n<h3>The unlock service<\/h3>\n<p>So that I can unlock my protected disk remotely, I run the OS and these services directly on the unencrypted partition:<\/p>\n<ol>\n<li>an HTTP API listening on a socket <code>~\/host-api-socket\/host-api.sock<\/code><\/li>\n<li>a push notification service (Gotify)<\/li>\n<li>a reverse proxy (Caddy)<\/li>\n<li>a web server with a UI where I can type in my passkey, with the socket above mounted in<\/li>\n<\/ol>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/07\/how-i-encrypt-my-backups-01.png\" alt=\"Flow to unlock the machine at distance\" \/><\/p>\n<p>The first process (&quot;host-api&quot;) runs directly on the host, not in a container. Mostly because I could not find a way to\ndo it... Look at the commands above that it uses and note that it needs to call <code>cryptsetup<\/code> and <code>mount<\/code> as root user,\nand also <code>systemctl --user<\/code>, which requires access to the systemd bus. So to reduce the &quot;surface&quot; of connection between\nthe web server UI and the OS, the host-api process exposes an HTTP API in a socket that is mounted in the web server\ncontainer.<\/p>\n<p>All the other 3 processes are running as rootless podman containers.<\/p>\n<p>In the diagram above, I highlight two flows:<\/p>\n<ol>\n<li>the notification flow in red: when host-api starts, it probes the system to detect if the protected disk is alreay\nmounted.\nIf not, it will send a push notification to my phone with a link to the unlock page.<\/li>\n<li>the unlock flow in blue: I got the page and enter the passkey, which gets passed to the host-api<\/li>\n<\/ol>\n<p>I've searched for a way to mount the disk without requiring root privileges, but I could not find a suitable way to do\nit... So what I do instead is: I write just the commands that require sudo on a dedicated file and add this specific\nfile to the sudoers of my user.<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code><span style=\"color:#d3d0c8;\">sitegui ALL=(ALL) NOPASSWD: \/home\/sitegui\/sudo-scripts\/mount-protected.sh\n<\/span><\/code><\/pre>\n<p>It's VERY important to protect these files with root-only privileges, otherwise a user could simply write any\ncommand to it.<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-shell\"><span style=\"color:#d3d0c8;\">sudo chown root:root \/home\/sitegui\/sudo-scripts\n<\/span><span style=\"color:#d3d0c8;\">sudo chown 700 \/home\/sitegui\/sudo-scripts\n<\/span><\/code><\/pre>\n<p>This solution ended up being very useful for all sorts of administrative tasks, you can\ncheck <a href=\"https:\/\/git.sitegui.dev\/sitegui\/home-lab\/src\/commit\/98c22b506e478b5760a8ca7e081be03d91336fd1\/config\/sudo-scripts\">in my home-lab git repo<\/a>\nall scripts that use this mechanism.<\/p>\n<p>A side-note: I also run other services on the unprotected partition. Services related to networking like Pihole and\nmonitoring like Alert manager, Grafana, Prometheus, Node exporter and Podman exporter. This way, these essentials are\nrunning even before I unlock the main services.<\/p>\n<h2>The backup disks<\/h2>\n<p>I've prepared my two backup disks to use full-disk encryption with LUKS:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-shell\"><span style=\"color:#d3d0c8;\">DEVICE=\/dev\/disk\/by-uuid\/87bbdbdf-47fd-4cc8-acae-83c97d5a6675\n<\/span><span style=\"color:#d3d0c8;\">sudo cryptsetup luksFormat &quot;$DEVICE&quot;\n<\/span><span style=\"color:#d3d0c8;\">sudo cryptsetup open &quot;$DEVICE&quot; temp\n<\/span><span style=\"color:#d3d0c8;\">sudo mkfs.ext4 \/dev\/mapper\/temp\n<\/span><span style=\"color:#d3d0c8;\">sudo cryptsetup close temp\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">DEVICE=\/dev\/disk\/by-uuid\/9c34def1-550a-4746-bcd9-bba7eee241be\n<\/span><span style=\"color:#d3d0c8;\">sudo cryptsetup luksFormat &quot;$DEVICE&quot;\n<\/span><span style=\"color:#d3d0c8;\">sudo cryptsetup open &quot;$DEVICE&quot; temp\n<\/span><span style=\"color:#d3d0c8;\">sudo mkfs.ext4 \/dev\/mapper\/temp\n<\/span><span style=\"color:#d3d0c8;\">sudo cryptsetup close temp\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">mkdir &quot;$HOME\/backup-1&quot; &quot;$HOME\/backup-2&quot;\n<\/span><\/code><\/pre>\n<p>However, note that the backup disks need to be mounted autonomously come backup time! So somehow somewhere the\nencryption key must be present. I've solved it by saving the encryption key to the disk itself, but of course in the\nprotected mount:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-shell\"><span style=\"color:#d3d0c8;\">sudo &quot;$HOME\/home-lab\/config\/mount-protected.sh&quot;\n<\/span><span style=\"color:#d3d0c8;\">PASSWORD_FILE=&quot;$HOME\/protected\/backup-password.txt&quot;\n<\/span><span style=\"color:#d3d0c8;\">nano &quot;$PASSWORD_FILE&quot; # enter password\n<\/span><span style=\"color:#d3d0c8;\">ENCRYPTION_KEY=$(cat &quot;$PASSWORD_FILE&quot;)\n<\/span><span style=\"color:#d3d0c8;\">echo -n &quot;$ENCRYPTION_KEY&quot; &gt; &quot;$PASSWORD_FILE&quot; # to remove trailing new line\n<\/span><span style=\"color:#d3d0c8;\">chmod 0600 &quot;$PASSWORD_FILE&quot;\n<\/span><\/code><\/pre>\n<p>This is a bit silly and introduces a hole in the security: if an attacker has access to the file system, they may\nread this file and grab this credential, escalating access in time, if they have future access to my disks. Under normal\noperation, all exposed services use containers with explicitly mounted directories, so it's not visible to any\ncontainer. Maybe in the future I\u2019ll remove this file and store the key only in the memory of the host-api service.<\/p>\n<p>Mounting the backup disks is then quite similar to unlocking the protected server disk:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-shell\"><span style=\"color:#d3d0c8;\">cryptsetup open \/dev\/disk\/by-uuid\/87bbdbdf-47fd-4cc8-acae-83c97d5a6675 backup-1 \\\n<\/span><span style=\"color:#d3d0c8;\">  --key-file \/home\/sitegui\/protected\/backup-password.txt\n<\/span><span style=\"color:#d3d0c8;\">mount \/dev\/mapper\/backup-1 \/home\/sitegui\/backup-1\n<\/span><\/code><\/pre>\n<p>(and similarly for backup-2)<\/p>\n"},{"title":"How I back up my data","id":"8c978f43-c9fd-4086-bcbe-f5518f3830b7","updated":"2026-06-18T00:00:00+00:00","author":{"name":"sitegui"},"category":[{"@attributes":{"term":"programming"}},{"@attributes":{"term":"homelab"}}],"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/06\/how-i-back-up-my-data","rel":"alternate"}},"published":"2026-06-18T00:00:00+00:00","content":"<p>Automated backups that work are mind-liberating! I'll share in this and the next post how I've solved it for my use case\nin my <a href=\"https:\/\/sitegui.dev\/post\/2026\/03\/my-homelab-an-old-laptop-behind-the-fridge\">homelab<\/a>.<\/p>\n<p>In this first post I want to explain my goals and my overall setup.\nIn <a href=\"https:\/\/sitegui.dev\/post\/2026\/07\/how-i-encrypt-my-backups\">the next post<\/a> I'll explore in detail how I\nhandle encryption. I'd also love to hear how <em>you<\/em> handle your backups: share in the comments.<\/p>\n<h2>What does my system solve?<\/h2>\n<p>I like to think of my backup system in terms of the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Threat_model\">thread model<\/a> against\nthe chaos and karma of the world. Which kinds of problems do I want to be protected against? After showering a couple of\ntimes, I informally had a list on my head. Here it is, transcribed:<\/p>\n<ol>\n<li>if I do something dumb, like deleting all my photos by accident, I want to recover that data (true story: when\nmigrating from Google Photos to Nextcloud, I've removed the wrong folder \ud83d\ude07).<\/li>\n<li>if my machine breaks, I want to recover my data<\/li>\n<li>if my house catches fire, is looted, or transforms itself into a pumpking (not true story), I want to recover my\ndata<\/li>\n<li>if a stranger has access to any of the disks, they must not be able to access my data<\/li>\n<li>if ransomware compromises my server, they cannot erase my backups<\/li>\n<li>if I'm not available or just lazy, the system should mostly continue to work<\/li>\n<li>if the system has problems, I should be notified<\/li>\n<li>if the system silently stops running, I should notice<\/li>\n<li>if in-disk data is inconsistent while the owner app is running, it must be stopped to flush the data<\/li>\n<li>if a copied file gets corrupted or rots, it should be identified<\/li>\n<li>if the writing patterns on the disks are similar, they should not fail at the same time due to correlated wear\nand tear<\/li>\n<\/ol>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<p>I have also set two personal challenges: I want a cheap solution, and I don't want to use no cloud!<\/p>\n<p>To reduce complexity, I've decided that the following properties are not necessary for me. But read carefully, you may\ndisagree:<\/p>\n<ol>\n<li><em>high availability<\/em>: I'm okay with shutting down my apps and databases for a while, to ensure a consistent\nstate. <a href=\"https:\/\/www.postgresql.org\/docs\/current\/backup-file.html\">Postgres' documentation<\/a> recommends doing this for\nfile-level backups.<\/li>\n<li><em>continuous backup<\/em>: I'm okay with my backup running once a day. This means that losing data created on the same day\nis possible.<\/li>\n<li><em>point in time recovery<\/em>: I'm okay with having just two backups (daily and monthly). This means that losing data if I\ndon't notice a problem in the same day is possible.<\/li>\n<\/ol>\n<h2>The system overview<\/h2>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/06\/how-i-back-up-my-data-01.png\" alt=\"the overview of my backup system\" \/><\/p>\n<p>My server is an old laptop that has two internal SSDs:<\/p>\n<ol>\n<li>a large 4 TB with most of my apps and their data. This disk is fully encrypted.<\/li>\n<li>a small 128 GB with the OS and some critical components. It's left unencrypted so that the machine can boot and\nprovide a small web UI that I use to remotely type in the password of the encrypted disk.<\/li>\n<\/ol>\n<p>Connected to it by USB, an external HDD. Every night, a systemd timer runs the backup logic that mounts, copies the\ndata, and unmounts it.<\/p>\n<p>Stored in a drawer somewhere else, another external disk, this time an SSD. Every month, I physically exchange these two\nexternal disks:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/06\/how-i-back-up-my-data-02.png\" alt=\"the exchange dance\" \/><\/p>\n<p>Note that the disks do not have the same size! That's simply because I was cheap and didn't want to buy yet another 4 TB\nSSD. In the future, I guess I'll do it, for simplicity. For now, I exclude big media files from the backup when the\nmounted disk is the one with 1 TB.<\/p>\n<h2>The daily timer<\/h2>\n<p>I'm using systemd to run most of the stuff on the server, including the backup logic. It is made of two files,\n<code>backup.timer<\/code> that declares that it should run every day at 3 AM:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code><span style=\"color:#d3d0c8;\">[Unit]\n<\/span><span style=\"color:#d3d0c8;\">Description = Backup timer\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">[Timer]\n<\/span><span style=\"color:#d3d0c8;\">OnCalendar = *-*-* 03:00:00\n<\/span><span style=\"color:#d3d0c8;\">Persistent = true\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">[Install]\n<\/span><span style=\"color:#d3d0c8;\">WantedBy = timers.target\n<\/span><\/code><\/pre>\n<p>and <code>backup.service<\/code> that describes that it should run the binary <code>home-lab<\/code> with the command &quot;backup&quot;. It should also\nretry up to three times if it fails.<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code><span style=\"color:#d3d0c8;\">[Unit]\n<\/span><span style=\"color:#d3d0c8;\">Description = Backup\n<\/span><span style=\"color:#d3d0c8;\">StartLimitIntervalSec = 12h\n<\/span><span style=\"color:#d3d0c8;\">StartLimitBurst = 3\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">[Service]\n<\/span><span style=\"color:#d3d0c8;\">WorkingDirectory = %h\/home-lab\n<\/span><span style=\"color:#d3d0c8;\">ExecStart = %h\/home-lab\/target\/release\/home-lab backup\n<\/span><span style=\"color:#d3d0c8;\">Restart = on-failure\n<\/span><span style=\"color:#d3d0c8;\">RestartSec = 60\n<\/span><\/code><\/pre>\n<p>A parenthesis about permissions: in my homelab, for simplicity, I'm running everything as the same user. So these files\nare simply put in <code>~\/.config\/systemd\/user\/<\/code> and then I run these commands to enable it:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-shell\"><span style=\"color:#d3d0c8;\">systemd --user daemon-reload\n<\/span><span style=\"color:#d3d0c8;\">systemd --user enable backup.timer\n<\/span><\/code><\/pre>\n<p>However, for the backup to be able to read the files created by the containers that I run with podman, they must be\nowned by that user. This means that I must correctly configure the user and group ids on the containers. This is still\nvery hard for me to understand. At this point, I mostly do it by trial and error. You can check in <a href=\"https:\/\/git.sitegui.dev\/sitegui\/home-lab\/src\/commit\/ec85e852b4dbc51414d7e197ed8aa768ba7f3837\/config\">the\n<code>config<\/code> folder<\/a>\nhow I've configured each app. Look for the &quot;UserNS&quot; directive on each <code>*.container<\/code> file.<\/p>\n<h2>The backup script<\/h2>\n<p>I've started with a bash script, but it got too hairy, so I've switched to Rust. You can check the <a href=\"https:\/\/git.sitegui.dev\/sitegui\/home-lab\/src\/commit\/ec85e852b4dbc51414d7e197ed8aa768ba7f3837\/home-lab\/src\/scripts\/backup.rs\">full implementation\nhere<\/a>,\nbut I'll explain the steps here as well:<\/p>\n<ol>\n<li>try to mount encrypted disk 1 in <code>~\/backup-1<\/code>. If it fails, try to mount encrypted disk 2 in <code>~\/backup-2<\/code>. I identify\nthe disks by their UUIDs.<\/li>\n<li>list all running containers with <code>podman container list<\/code>.<\/li>\n<li>for each running container, run <code>podman container inspect<\/code> to grab the label <code>PODMAN_SYSTEMD_UNIT<\/code> which represents\nthe name of the systemd service that owns it. Also, list all writable folder mounts. To illustrate, let's consider\nthe\n<a href=\"https:\/\/git.sitegui.dev\/sitegui\/home-lab\/src\/commit\/ec85e852b4dbc51414d7e197ed8aa768ba7f3837\/config\/monitoring\/prometheus.container\">prometheus service<\/a>:\n<pre style=\"background-color:#2d2d2d;\"><code><span style=\"color:#d3d0c8;\">[Unit]\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">[Container]\n<\/span><span style=\"color:#d3d0c8;\">ContainerName = prometheus\n<\/span><span style=\"color:#d3d0c8;\">Volume = %h\/home-lab\/config\/monitoring\/prometheus:\/etc\/prometheus:ro\n<\/span><span style=\"color:#d3d0c8;\">Volume = %h\/bare\/prometheus\/data:\/prometheus\n<\/span><span style=\"color:#d3d0c8;\"># ... other stuff ...\n<\/span><\/code><\/pre>\nThe container &quot;prometheus&quot; (from <code>ContainerName<\/code>) is owned by the service &quot;prometheus.service&quot; (from the file name)\nand it has one writable folder mount at <code>\/home\/sitegui\/bare\/prometheus\/data<\/code>. Note that read-only mounts are not\nconsidered here; it will become clear why later on.<\/li>\n<li>for each service, do these steps (here illustrated with the same example above):\n<ol>\n<li>stop the service with <code>systemd --user stop prometheus.service<\/code><\/li>\n<li>backup each writable mount with <code>rsync<\/code>:\n<pre style=\"background-color:#2d2d2d;\"><code><span style=\"color:#d3d0c8;\">rsync --delete --archive --verbose \\\n<\/span><span style=\"color:#d3d0c8;\">  \/home\/sitegui\/bare\/prometheus\/data\/ \\\n<\/span><span style=\"color:#d3d0c8;\">  \/home\/sitegui\/backup-1\/bare\/prometheus\/data\n<\/span><\/code><\/pre>\n<\/li>\n<li>list 1% of the files and check if the hashes of their contents match<\/li>\n<li>start the service with <code>systemd --user start prometheus.service<\/code><\/li>\n<\/ol>\n<\/li>\n<li>backup the main folders again, excluding the mounts already handled. This is were, where applicable, read-only mounts\nwill be backed up.\n<pre style=\"background-color:#2d2d2d;\"><code><span style=\"color:#d3d0c8;\">rsync --delete --archive --verbose \\\n<\/span><span style=\"color:#d3d0c8;\">  \/home\/sitegui\/bare \\\n<\/span><span style=\"color:#d3d0c8;\">  \/home\/sitegui\/protected \\\n<\/span><span style=\"color:#d3d0c8;\">  \/home\/sitegui\/backup-1 \\\n<\/span><span style=\"color:#d3d0c8;\">  --exclude \/bare\/prometheus\/data\/\n<\/span><\/code><\/pre>\n<\/li>\n<li>list 1% of the files (excluding already checked paths) and check if the hashes of their contents match<\/li>\n<li>unmount backup disk<\/li>\n<li>send a push notification using Gotify. It's a silent notification if everything went okay, or a high-priority\nnotification if some error happened<\/li>\n<\/ol>\n<h2>Final scores<\/h2>\n<ol>\n<li><em>if I do something dumb, I want to recover that data<\/em>: all data in\n<code>\/home\/sitegui\/bare<\/code> and <code>\/home\/sitegui\/protected<\/code> are copied using <code>rsync<\/code><\/li>\n<li><em>if my machine breaks, I want to recover my data<\/em>: the external disk connected to the server will have data at most\n24 hours old<\/li>\n<li><em>if my house catches fire, I want to recover my data<\/em>: the external disk stored offline in another address will have\ndata at most 1 month old.<\/li>\n<li><em>if a stranger has access, they must not be able to access my data<\/em>: (see the follow-up blog post)<\/li>\n<li><em>if ransomware compromises my server, they cannot erase my backups<\/em>: the external disk stored offline cannot be\nerased<\/li>\n<li><em>if I'm not available, the system should work<\/em>: using systemd timer and a script with robust error-handling is boring\nand reliable<\/li>\n<li><em>if the system has problems, I should be notified<\/em>: push notification with the error message<\/li>\n<li><em>if the system silently stops running, I should notice<\/em>: I'll notice that I have not received a push notification<\/li>\n<li><em>if data is inconsistent while the app is running, it must be stopped<\/em>: services are\nstopped one by one before copying their files<\/li>\n<li><em>if a copied file rots, it should be identified<\/em>: 1% of all files are checked every day<\/li>\n<li><em>if the writing patterns are similar, they should not fail at the same time<\/em>: I'm using different technologies for\nthe external disks (SSD vs HDD) and from different manufacturers, to reduce crash correlation<\/li>\n<\/ol>\n<h2>Test your backups<\/h2>\n<p>I use my PC to do a simulated worst-scenario exercise, in which I need to recover the whole system back from a backup. I\ncreate a new user in the PC and try to recreate the homelab in it. Sadly, I'm too lazy, and it's been more than a year\nsince the last time I've done it! I should totally do it again. I'll do it mum, I promise.<\/p>\n<h2>Other approaches and FAQ<\/h2>\n<p>I could have used a dedicated backup strategy for Postgres like <code>pg_dump<\/code> to avoid turning it off, but I prefer one\nsingle answer that should work for all containers and technologies.<\/p>\n<p>I could have used some dedicated backup cloud service like <a href=\"https:\/\/www.borgbase.com\/\">BorgBase<\/a> or use some cloud\nstorage like S3. For 4 TB, BorgBase costs $ 24.50 \/ month, while S3 Glacier Deep Archive costs at least $ 3.96 \/\nmonth. The actual S3 price will probably be much higher because of the caveats of glacier:<\/p>\n<blockquote>\n<p>AWS charges for 40 KB of additional metadata for each archived object, with 8 KB charged at S3\nStandard rates and 32 KB charged at (...) S3 Deep Archive rates. (...) S3 Glacier Deep Archive has a minimum storage\nduration of 180 days.<\/p>\n<\/blockquote>\n<p>However, I don't want to depend on the cloud. Besides, uploading or downloading 4 TB of data would take too long. I\ncould\nhave used another friend as &quot;the cloud&quot;. This would solve the cost issue, but I like the idea of having one offline\nbackup.<\/p>\n<p>I could have used RAID, but RAID is not backup: if you delete a file it will happily apply the destructive command to\nall copies! See goal number 1 :) Also, to reduce complexity and cost, I'm okay with the lack of high availability.<\/p>\n<p>I could have used BTRFS to do disk snapshots, but I don't know enough about it. Maybe some day I learn and change it.<\/p>\n"},{"title":"Efficiently ingesting thousands of JSON files into a Delta table","id":"2001ec46-379c-48ed-8c75-f81aa72f6138","updated":"2026-06-09T00:00:00+00:00","author":{"name":"sitegui"},"category":[{"@attributes":{"term":"programming"}},{"@attributes":{"term":"data-engineering"}},{"@attributes":{"term":"spark"}},{"@attributes":{"term":"rust"}}],"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/06\/efficiently-ingesting-thousands-of-json-files-into-a-delta-table","rel":"alternate"}},"published":"2026-06-09T00:00:00+00:00","content":"<p>In the past, I've worked with a system that produced hundreds of thousands of compressed JSON files every day, each\nweighting around 1MiB uncompressed. We wanted to ingest all that data into datalake for debug and analytics, and for\nthat we've used Spark to append to a delta table. Conceptually, the flow is very straight forward:<\/p>\n<ol>\n<li>a job runs every day<\/li>\n<li>it lists the new files that need to be ingested<\/li>\n<li>those files are read<\/li>\n<li>they are decompressed, parsed and encoded as parquet<\/li>\n<li>these new parquet files are written to the table<\/li>\n<\/ol>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/06\/efficiently-ingesting-thousands-of-json-files-into-a-delta-table-01.png\" alt=\"The basic flow of the ingestion\" \/><\/p>\n<p>In my last blog post,\nI've <a href=\"https:\/\/sitegui.dev\/post\/2026\/05\/dissecting-a-delta-table-just-a-bunch-of-JSON-and-parquet-files\">dissected the delta table format<\/a> and\nshowed\nthat it's basically just a bunch of JSON and parquet files. Part of that knowledge will be useful for this post, so\ngo take a look there if you want. I'll wait here :)<\/p>\n<p>At its core, the Spark script to do this ETL is (in Python):<\/p>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-python\"><span style=\"color:#d3d0c8;\">(\n<\/span><span style=\"color:#d3d0c8;\">    spark\n<\/span><span style=\"color:#d3d0c8;\">    .read\n<\/span><span style=\"color:#d3d0c8;\">    .<\/span><span style=\"color:#6699cc;\">option<\/span><span style=\"color:#d3d0c8;\">(&quot;<\/span><span style=\"color:#99cc99;\">pathGlobFilter<\/span><span style=\"color:#d3d0c8;\">&quot;, &quot;<\/span><span style=\"color:#99cc99;\">*.json.zst<\/span><span style=\"color:#d3d0c8;\">&quot;)\n<\/span><span style=\"color:#d3d0c8;\">    .<\/span><span style=\"color:#6699cc;\">json<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#cc99cc;\">f<\/span><span style=\"color:#d3d0c8;\">&#39;<\/span><span style=\"color:#99cc99;\">s3a:\/\/bucket-name\/service-name\/date=2026-05-16\/<\/span><span style=\"color:#d3d0c8;\">&#39;, <\/span><span style=\"color:#f2777a;\">schema<\/span><span style=\"color:#d3d0c8;\">=<\/span><span style=\"color:#6699cc;\">get_schema<\/span><span style=\"color:#d3d0c8;\">())\n<\/span><span style=\"color:#d3d0c8;\">    .write\n<\/span><span style=\"color:#d3d0c8;\">    .<\/span><span style=\"color:#6699cc;\">mode<\/span><span style=\"color:#d3d0c8;\">(&quot;<\/span><span style=\"color:#99cc99;\">append<\/span><span style=\"color:#d3d0c8;\">&quot;)\n<\/span><span style=\"color:#d3d0c8;\">    .<\/span><span style=\"color:#6699cc;\">format<\/span><span style=\"color:#d3d0c8;\">(&quot;<\/span><span style=\"color:#99cc99;\">delta<\/span><span style=\"color:#d3d0c8;\">&quot;)\n<\/span><span style=\"color:#d3d0c8;\">    .<\/span><span style=\"color:#6699cc;\">saveAsTable<\/span><span style=\"color:#d3d0c8;\">(&#39;<\/span><span style=\"color:#99cc99;\">table-name<\/span><span style=\"color:#d3d0c8;\">&#39;)\n<\/span><span style=\"color:#d3d0c8;\">)\n<\/span><\/code><\/pre>\n<p>This instructs Spark to ingest all files in the S3 bucket &quot;bucket-name&quot; at the prefix &quot;service-name\/date=2026-05-16\/&quot;\nand ending in &quot;.json.zst&quot;. The &quot;.zst&quot; extension signals to Spark that these files are compressed with z-standard.<\/p>\n<p>The <code>get_schema()<\/code> function is important to tell Spark exactly the name and types of the fields. Without this, Spark\nwill auto-detect the schema, which is both slow (Spark needs to scan the files twice) and fragile (Spark cannot\nguess all fields correctly all the time).<\/p>\n<p>And it works! End of post, <span class=\"fun\">thanks<\/span>.<\/p>\n<h2>... or is it ... ?<\/h2>\n<p>To have it run in an acceptable time (around 1 hour), we had to use a Spark cluster with more than 10 machines,\neach with 4 cores, 32 GiB and 1 TiB local SSD. We were using Databricks, so on top of the machine cost, we also paid\ntheir markup. All this to produce ~10 GiB of compressed data.<\/p>\n<p>Sometimes we get our heads stuck in the clouds for too long and forget how crazy that should sound! For a totally\nnon-scientific reference, I've searched for some rule-of-thumb speed for consumer hardware for the different\ntasks involved in the ingestion. Then I did some napkin math for the time to handle 10 GiB of compressed (100 GiB of\nuncompressed) data sequentially in a single thread:<\/p>\n<table>\n<thead>\n<tr>\n<th>Task<\/th>\n<th align=\"right\">Speed<\/th>\n<th align=\"right\">Time<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Download<\/td>\n<td align=\"right\">100 MiB\/s<\/td>\n<td align=\"right\">100 s<\/td>\n<\/tr>\n<tr>\n<td>Decompress zstd<\/td>\n<td align=\"right\">1000 MiB\/s<\/td>\n<td align=\"right\">100 s<\/td>\n<\/tr>\n<tr>\n<td>Parse JSON<\/td>\n<td align=\"right\">200 MiB\/s<\/td>\n<td align=\"right\">500 s<\/td>\n<\/tr>\n<tr>\n<td>Encode parquet<\/td>\n<td align=\"right\">50 MiB\/s<\/td>\n<td align=\"right\">2 000 s<\/td>\n<\/tr>\n<tr>\n<td>SSD write<\/td>\n<td align=\"right\">500 MiB\/s<\/td>\n<td align=\"right\">20 s<\/td>\n<\/tr>\n<tr>\n<td>Upload<\/td>\n<td align=\"right\">100 MiB\/s<\/td>\n<td align=\"right\">100 s<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>This adds up to a total of 47 minutes in a single-thread consumer hardware. Using more threads and concurrently running\nthe IO-bound steps (like download, SSD write and upload) concurrently with CPU-bound steps, we should observe much\nbetter performance.<\/p>\n<p>With the knowledge of how delta tables are organised internally as just a bunch of parquet files with the actual data\nand some JSON metadata for check pointing the versions, I got interested how it could be simpler.<\/p>\n<h2>Alternative approach<\/h2>\n<p>Instead of a Spark cluster, we can write a more focused program that reads, converts and writes the data. And for that,\nwe can use the Rust and these crates to handle each one of the steps:<\/p>\n<ul>\n<li><a href=\"https:\/\/crates.io\/crates\/tokio\">tokio<\/a>: run tasks concurrently and in-parallel<\/li>\n<li><a href=\"https:\/\/crates.io\/crates\/aws-sdk-s3\">aws-sdk-s3<\/a>: list, download and upload files to S3<\/li>\n<li><a href=\"https:\/\/crates.io\/crates\/zstd\">zstd<\/a>: decompress zstd<\/li>\n<li><a href=\"https:\/\/crates.io\/crates\/arrow-json\">arrow-json<\/a>: re-encode json into arrow<\/li>\n<li><a href=\"https:\/\/crates.io\/crates\/parquet\">parquet<\/a>: re-encode arrow into parquet<\/li>\n<li><a href=\"https:\/\/crates.io\/crates\/deltalake\">deltalake<\/a>: commit the changes to delta lake<\/li>\n<li><a href=\"https:\/\/crates.io\/crates\/flume\">flume<\/a>: implement multi-producer multi-consumer channels to handle inter-task\ncommunication<\/li>\n<\/ul>\n<p>We can use a model of &quot;pipelining&quot;, in which the whole operation is divided into tasks that can run in parallel, with\nbounded message channels connecting them:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/06\/efficiently-ingesting-thousands-of-json-files-into-a-delta-table-02.png\" alt=\"Diagram with the graph of task execution\" \/><\/p>\n<ol>\n<li><strong>list files<\/strong>: uses <code>aws-sdk-s3<\/code> to go over the listing pages of a given prefix, generating the name as <code>String<\/code> of\nthe files to download<\/li>\n<li><strong>download files<\/strong>: uses <code>aws-sdk-s3<\/code> to download the files into memory as <code>Bytes<\/code>. Note that in this model, I'm\nassuming that each file is small enough to fully fit in memory.<\/li>\n<li><strong>decompress and parse JSON<\/strong>: uses <code>zstd<\/code> and <code>arrow-json<\/code> to produce a <code>TapeDecoder<\/code> which represents the parsed\nJSON content as a flat sequence of tokens.<\/li>\n<li><strong>encode as arrow<\/strong>: uses <code>arrow-json<\/code> to batch some <code>TapeDecoder<\/code>s together and produce a <code>RecordsBatch<\/code>, which is a\nin-memory columnar representation of the data.<\/li>\n<li><strong>encode as parquet<\/strong>: uses <code>parquet<\/code> to encode the <code>RecordsBatch<\/code> as parquet row groups and write the results to\ndisk<\/li>\n<li><strong>upload parquet<\/strong>: uses <code>ask-sdk-s3<\/code> to upload the generated parquet files<\/li>\n<\/ol>\n<p>Note the use of the arrow encoding as an intermediate between JSON and parquet. This is useful because JSON is\nrow-oriented, while parquet is column-oriented. This &quot;inversion&quot; is done in-memory with the help of arrow.<\/p>\n<p>You\ncan <a href=\"https:\/\/git.sitegui.dev\/sitegui\/ingest-many-json-files\/src\/commit\/2a0e3155c25e7b14b4b98e49affb346676f28469\/rust\/src\/ingest.rs\">check the actual implementation in this repo<\/a>.\nI had to patch <code>arrow-json<\/code> and <code>deltalake<\/code> crates, so that they expose some internal logic, because their current\npublic implementation could not be used as building blocks for this custom pipeline.<\/p>\n<h2>Benchmarking and results<\/h2>\n<p>To benchmark, I've used 2 machines in the same local network:<\/p>\n<ul>\n<li>a simple S3-compatible server<\/li>\n<li>a client machine, 32 GB of RAM and AMD Ryzen 7 2700X with 16 cores. It executes the ingestion, either with:\n<ul>\n<li>Spark and\nPython - <a href=\"https:\/\/git.sitegui.dev\/sitegui\/ingest-many-json-files\/src\/commit\/c46e28ceb16191250cc504f6c280ca3a4c7ed4f0\/spark\/main.py\">see source<\/a><\/li>\n<li>Rust and the pipeline explained\nabove - <a href=\"https:\/\/git.sitegui.dev\/sitegui\/ingest-many-json-files\/src\/commit\/c46e28ceb16191250cc504f6c280ca3a4c7ed4f0\/rust\/src\/main.rs\">see source<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>I've decided to implement my own S3\nservice - <a href=\"https:\/\/git.sitegui.dev\/sitegui\/ingest-many-json-files\/src\/commit\/c46e28ceb16191250cc504f6c280ca3a4c7ed4f0\/local-s3\/src\/main.rs\">see source<\/a>,\nfor fun (I like tries!), but also to have total visibility of what Spark was doing.<\/p>\n<p>I've then generated 10 000 compressed JSON files, each containing about 1MiB of uncompressed data. The schema has around\n40 fields and nested lists of objects.<\/p>\n<p>When running Spark without any further configuration, it produced 313 small parquet files, which seems bad for future\nread performance. So I've explicitly set the number of shuffling partitions in Spark to 3 and 10. To have a fair\ncomparison, I've set the equivalent parameter in the Rust implementation to produce the same number of parquet files.<\/p>\n<h3>3 generated parquet files<\/h3>\n<table>\n<thead>\n<tr>\n<th>Metric<\/th>\n<th align=\"right\">Spark<\/th>\n<th align=\"right\">Rust<\/th>\n<th align=\"right\">Difference<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Total duration<\/td>\n<td align=\"right\">83.0 s<\/td>\n<td align=\"right\">27.2 s<\/td>\n<td align=\"right\">-67 %<\/td>\n<\/tr>\n<tr>\n<td>Peak CPU usage<\/td>\n<td align=\"right\">55.9 %<\/td>\n<td align=\"right\">36.6 %<\/td>\n<td align=\"right\">-35 %<\/td>\n<\/tr>\n<tr>\n<td>Peak memory usage<\/td>\n<td align=\"right\">3.2 GiB<\/td>\n<td align=\"right\">4.6 GiB<\/td>\n<td align=\"right\">+46 %<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>These are the CPU, memory and network usage curves (captured with <code>dstat<\/code>) through time:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/06\/efficiently-ingesting-thousands-of-json-files-into-a-delta-table-03.png\" alt=\"Performance charts\" \/><\/p>\n<p>Spark has a fundamentally different architecture from the Rust implementation: each partition runs sequentially in a\nsingle core and does one thing at a time. You can imagine that each one of the 3 partitions runs independently and does\none of these tasks:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/06\/efficiently-ingesting-thousands-of-json-files-into-a-delta-table-05.png\" alt=\"\" \/><\/p>\n<p>The problem with this architecture is that it always under-uses network <em>and<\/em> CPU: the network is idle while the\nCPU is working, and conversely, CPU is idle while the network is active. This drop is visible if we look at the &quot;network\nreceive&quot; chart: note how Rust uses up to 60 MiB\/s, while Spark stays at 20 MiB\/s.<\/p>\n<p>Another downside is that it ties the number of generated partitions with resource usage: to better use the machine\nresources, it's better to produce more parquet files. However, these extra files penalise the table read performance by\nfuture users.<\/p>\n<h3>10 generated parquet files<\/h3>\n<p>Let's look at the results with 10 partitions:<\/p>\n<table>\n<thead>\n<tr>\n<th>Metric<\/th>\n<th align=\"right\">Spark<\/th>\n<th align=\"right\">Rust<\/th>\n<th align=\"right\">Difference<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Total duration<\/td>\n<td align=\"right\">53.3 s<\/td>\n<td align=\"right\">24.8 s<\/td>\n<td align=\"right\">-54 %<\/td>\n<\/tr>\n<tr>\n<td>Peak CPU usage<\/td>\n<td align=\"right\">86.2 %<\/td>\n<td align=\"right\">33.6 %<\/td>\n<td align=\"right\">-61 %<\/td>\n<\/tr>\n<tr>\n<td>Peak memory usage<\/td>\n<td align=\"right\">6.5 GiB<\/td>\n<td align=\"right\">2.9 GiB<\/td>\n<td align=\"right\">-56 %<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/06\/efficiently-ingesting-thousands-of-json-files-into-a-delta-table-04.png\" alt=\"Performance charts\" \/><\/p>\n<p>In Spark, more partitions lead to more effetive paralellism and usage of resources. Note how network receive goes up to\nnear 50 MiB\/s. The downside is that these independent tasks take more memory in total.<\/p>\n<p>The Rust performance is clearly limited by the network: note how CPU usage is low while network receive and send fight\neach other.<\/p>\n<p>Another fun fact is that when going from 3 to 10 generated parquet files, the peak memory usage of Spark increases\n(3.2 -&gt; 6.5 GiB) while Rust's falls (4.6 -&gt; 2.9 GiB). The reason makes sense when we compare the two distinct\narchitecures: each Spark partition is independent and accumulates data in-memory: so more partitions, more usage. While\nRust operates in terms of a pipeline with a target parquet size: so smaller parquet sizes will buffer less data\nin-memory.<\/p>\n<h2>Final words<\/h2>\n<p>I had a great time hacking together my own S3 service, tweaking Spark and using Rust's arrow, parquet and delta crates.\nThe Rust datalake ecosystem is surprisingly mature and active.<\/p>\n<p>I've tried for weeks to implement a JSON-to-parquet converter faster than <code>arrow-json<\/code>'s, but I've failed! Which is\ncool, their code is really interesting to read. But for this project, I've noticed that they could better support\nparallel JSON parsing, which is something that I've implemented on my fork and hope to contribute upstream.<\/p>\n<p>I'm also satisfied to validate my gut feeling that machines are quite fast and that Spark and Databricks were\nunnecessarily bloated for our use case.<\/p>\n<p><span class=\"fun\">Won't someone think of the bytes!<\/span><\/p>\n"},{"title":"My cloud bills","id":"17987c5d-8b75-433a-bf55-dfc3571409da","updated":"2026-06-06T00:00:00+00:00","author":{"name":"sitegui"},"category":{"@attributes":{"term":"homelab"}},"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/06\/my-cloud-bills","rel":"alternate"}},"published":"2026-06-06T00:00:00+00:00","content":"<p>This is a quick post just to share how much <a href=\"https:\/\/sitegui.dev\/post\/2026\/03\/my-homelab-an-old-laptop-behind-the-fridge\">my homelab<\/a> costs me.\nSince it's an old laptop tucked near the fridge, one can guess it's not a lot. But I've got measurements!<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/06\/my-cloud-bills.jpeg\" alt=\"a wattimeter measuring consumption\" \/><\/p>\n<p>I've measured 3.382 kWh in 11 days 3 hours and 54 minutes (=267.9 hours), so an average of a bit less than <strong>13 W<\/strong>. In\na 365-day year, this adds up to 110.6 kWh.<\/p>\n<p>My current energy contract has a fixed cost and a per-kWh cost that depends on the hour of the day:<\/p>\n<table>\n<thead>\n<tr>\n<th><\/th>\n<th>Cost<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>fixed<\/td>\n<td>15,65 \u20ac<\/td>\n<\/tr>\n<tr>\n<td>03:40 - 07:40 and 12:40 - 16:40<\/td>\n<td>0.1579 \u20ac \/ kWh<\/td>\n<\/tr>\n<tr>\n<td>the rest of the day<\/td>\n<td>0.2065 \u20ac \/ kWh<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<p>Assuming a constant usage, the average unit cost is 0.1903 \u20ac \/ kWh. So I have a yearly cost of 21.04 \u20ac.<\/p>\n<p>My cloud bill is <span style=\"font-size: larger; font-weight: bold\">1.76 \u20ac \/ month<\/span>. Not bad \ud83d\udc4d\ud83c\udffc<\/p>\n"},{"title":"The bots came to hunt my forge data","id":"b5b3bb34-b1ea-46bd-9c28-92dd738f55df","updated":"2026-05-28T00:00:00+00:00","author":{"name":"sitegui"},"category":[{"@attributes":{"term":"forgejo"}},{"@attributes":{"term":"homelab"}}],"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/05\/the-bots-came-to-hunt-my-forge-data","rel":"alternate"}},"published":"2026-05-28T00:00:00+00:00","content":"<p>I'm a happy user of <a href=\"https:\/\/forgejo.org\/\">Forgejo<\/a> and I host it on my homelab\nat <a href=\"https:\/\/git.sitegui.dev\">git.sitegui.dev<\/a> to store all my open source code (including this very same page).<\/p>\n<p>However, as <a href=\"https:\/\/weirdgloop.org\/blog\/clankers\">many other hobbyists<\/a>\nand <a href=\"https:\/\/diff.wikimedia.org\/2025\/04\/01\/how-crawlers-impact-the-operations-of-the-wikimedia-projects\/\">major projects<\/a>,\nI have noticed an uptick in the number of crawling requests that my instance serves. My homelab is\nliterally <a href=\"https:\/\/sitegui.dev\/post\/2026\/03\/my-homelab-an-old-laptop-behind-the-fridge\">an old laptop close to the fridge<\/a>, so I could <strong>hear<\/strong>\nthe extra load (and it also doesn't help that Europe is\nscalding with 16 degrees above average temperatures for May).<\/p>\n<p>I saw an increase from an average of 1 000 requests a day to 200 000. The issue is not only the <em>number<\/em> of requests,\nbut their <em>nature<\/em>: these bots go over the whole git history to navigate the whole tree of files for each commit. They\nalso request a lot of repo archives that are expensive to generate.<\/p>\n<p>The <a href=\"https:\/\/git.sitegui.dev\/robots.txt\">robots.txt<\/a> clearly forbids bots to navigate to these pages:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code><span style=\"color:#d3d0c8;\">User-agent: *\n<\/span><span style=\"color:#d3d0c8;\">Disallow: \/*\/*\/src\/\n<\/span><span style=\"color:#d3d0c8;\">Disallow: \/*\/*\/archive\/\n<\/span><\/code><\/pre>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<p>Obviously, these bots do not respect it and hammer public Internet infra structure and hobbyists. The reason\nis clear:\nthey scrap to train AI models, consequences be damned. It's also very dumb: they could instead just do a <code>git clone<\/code> in\neach repo to extract all the necessary information with just a fraction of the work!<\/p>\n<h2>Checking the data with DuckDB<\/h2>\n<p>I take this opportunity to learn some <a href=\"https:\/\/duckdb.org\/\">DuckDB<\/a>, which I understand as a happy marriage between\nSpark and SQLite: query directly from disk files using SQL, but with an &quot;in-process&quot; mentality: no infrastructure to\nmanage.<\/p>\n<p>I've downloaded the server logs from Caddy and imported it into a DuckDB file to accelerate interactive querying:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-python\"><span style=\"color:#cc99cc;\">import <\/span><span style=\"color:#d3d0c8;\">duckdb\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">connection = duckdb.<\/span><span style=\"color:#6699cc;\">connect<\/span><span style=\"color:#d3d0c8;\">(&quot;<\/span><span style=\"color:#99cc99;\">data\/logs.duckdb<\/span><span style=\"color:#d3d0c8;\">&quot;)\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">connection.<\/span><span style=\"color:#6699cc;\">execute<\/span><span style=\"color:#d3d0c8;\">(&quot;&quot;&quot;\n<\/span><span style=\"color:#99cc99;\">create or replace table caddy_logs as\n<\/span><span style=\"color:#99cc99;\">select *\n<\/span><span style=\"color:#99cc99;\">from read_json_auto(\n<\/span><span style=\"color:#99cc99;\">    &#39;data\/logs\/*&#39;,\n<\/span><span style=\"color:#99cc99;\">    format = &#39;newline_delimited&#39;,\n<\/span><span style=\"color:#99cc99;\">    union_by_name = true,\n<\/span><span style=\"color:#99cc99;\">    sample_size = -1\n<\/span><span style=\"color:#99cc99;\">)\n<\/span><span style=\"color:#d3d0c8;\">&quot;&quot;&quot;)\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">connection.<\/span><span style=\"color:#6699cc;\">close<\/span><span style=\"color:#d3d0c8;\">()\n<\/span><\/code><\/pre>\n<p>Out of the 1 121 977 requests to git.sitegui.dev, I've extracted the top 5 <code>User-Agent<\/code>:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-sql\"><span style=\"color:#cc99cc;\">select<\/span><span style=\"color:#d3d0c8;\"> map_extract_value(<\/span><span style=\"color:#f99157;\">request<\/span><span style=\"color:#d3d0c8;\">.<\/span><span style=\"color:#f99157;\">headers<\/span><span style=\"color:#d3d0c8;\">, &#39;<\/span><span style=\"color:#99cc99;\">User-Agent<\/span><span style=\"color:#d3d0c8;\">&#39;),\n<\/span><span style=\"color:#d3d0c8;\">       <\/span><span style=\"color:#66cccc;\">count<\/span><span style=\"color:#d3d0c8;\">(*)\n<\/span><span style=\"color:#cc99cc;\">from<\/span><span style=\"color:#d3d0c8;\"> caddy_logs\n<\/span><span style=\"color:#cc99cc;\">where <\/span><span style=\"color:#f99157;\">request<\/span><span style=\"color:#d3d0c8;\">.<\/span><span style=\"color:#f99157;\">host <\/span><span style=\"color:#d3d0c8;\">= &#39;<\/span><span style=\"color:#99cc99;\">git.sitegui.dev<\/span><span style=\"color:#d3d0c8;\">&#39;\n<\/span><span style=\"color:#cc99cc;\">group by <\/span><span style=\"color:#f99157;\">1\n<\/span><span style=\"color:#cc99cc;\">order by <\/span><span style=\"color:#f99157;\">2 <\/span><span style=\"color:#cc99cc;\">desc limit <\/span><span style=\"color:#f99157;\">5\n<\/span><\/code><\/pre>\n<ul>\n<li>562 151: meta-externalagent\/1.1 (+https:\/\/developers.facebook.com\/docs\/sharing\/webmasters\/crawler)<\/li>\n<li>60 571: Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/121.0.0.0\nSafari\/537.36 Edg\/121.0.0.0<\/li>\n<li>35 921: Mozilla\/5.0 (Linux; Android 10; K) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/122.0.0.0 Mobile\nSafari\/537.36<\/li>\n<li>27 870: Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/122.0.0.0\nSafari\/537.36<\/li>\n<li>16 339: Mozilla\/5.0 (Windows NT 10.0; Win64; x64; rv:123.0) Gecko\/20100101 Firefox\/123.0<\/li>\n<\/ul>\n<p>The User-Agent (UA) header is present in every request, and it's how the clients announce themselves. Well-behaved bots\nmust disclose their botly presence in the UA. Well, meta is doing it, but clearly not everybody.<\/p>\n<p>So I got interested in knowing their IPs. I'm also filtering only requests to &quot;<em>\/archive\/<\/em>&quot;, which bots are explicitly\nforbidden to do, as per the robots.txt above.<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-sql\"><span style=\"color:#cc99cc;\">select<\/span><span style=\"color:#d3d0c8;\"> map_extract_value(<\/span><span style=\"color:#f99157;\">request<\/span><span style=\"color:#d3d0c8;\">.<\/span><span style=\"color:#f99157;\">headers<\/span><span style=\"color:#d3d0c8;\">, &#39;<\/span><span style=\"color:#99cc99;\">User-Agent<\/span><span style=\"color:#d3d0c8;\">&#39;),\n<\/span><span style=\"color:#d3d0c8;\">       list(distinct <\/span><span style=\"color:#f99157;\">request<\/span><span style=\"color:#d3d0c8;\">.<\/span><span style=\"color:#f99157;\">client_ip<\/span><span style=\"color:#d3d0c8;\">),\n<\/span><span style=\"color:#d3d0c8;\">       <\/span><span style=\"color:#66cccc;\">count<\/span><span style=\"color:#d3d0c8;\">(*)\n<\/span><span style=\"color:#cc99cc;\">from<\/span><span style=\"color:#d3d0c8;\"> caddy_logs\n<\/span><span style=\"color:#cc99cc;\">where <\/span><span style=\"color:#f99157;\">request<\/span><span style=\"color:#d3d0c8;\">.<\/span><span style=\"color:#f99157;\">host <\/span><span style=\"color:#d3d0c8;\">= &#39;<\/span><span style=\"color:#99cc99;\">git.sitegui.dev<\/span><span style=\"color:#d3d0c8;\">&#39;\n<\/span><span style=\"color:#d3d0c8;\">  <\/span><span style=\"color:#cc99cc;\">and <\/span><span style=\"color:#f99157;\">request<\/span><span style=\"color:#d3d0c8;\">.<\/span><span style=\"color:#f99157;\">uri <\/span><span style=\"color:#cc99cc;\">like <\/span><span style=\"color:#d3d0c8;\">&#39;<\/span><span style=\"color:#99cc99;\">%\/archive\/%<\/span><span style=\"color:#d3d0c8;\">&#39;\n<\/span><span style=\"color:#cc99cc;\">group by <\/span><span style=\"color:#f99157;\">1\n<\/span><span style=\"color:#cc99cc;\">order by <\/span><span style=\"color:#f99157;\">3 <\/span><span style=\"color:#cc99cc;\">desc limit <\/span><span style=\"color:#f99157;\">5\n<\/span><\/code><\/pre>\n<p>The top hit &quot;meta-externalagent\/1.1&quot; showed a bunch of IPv6 and IPv4 owned by meta,\nlike <a href=\"https:\/\/ipinfo.io\/2a03:2880:f814:19::\">2a03:2880:f814:19::<\/a>.\nSo fuck you Meta! You can shove your lack of politeness into your legless metaverse.<\/p>\n<p>The other top hits were all from Alibaba, like <a href=\"https:\/\/ipinfo.io\/47.82.15.152\">47.82.15.152<\/a>. Alibaba is a cloud\nprovider, so it's basically someone running a bad crawler in their servers. Their bot is &quot;smart&quot; enough to use multiple\nUser-Agent strings \ud83d\ude12.<\/p>\n<h2>My solution<\/h2>\n<p>Most of the projects that I run in my homelab are private: they are used by me, family and friends. These are protected\nby <a href=\"https:\/\/git.sitegui.dev\/sitegui\/home-lab\/src\/commit\/7a6a45db2d06d2a7bb3fc41b27561eec60aa2c6e\/knock\/README.md\">knock<\/a>\nand see little bot activity.<\/p>\n<p>However, I want forgejo to be publicly accessible. Some people are using <a href=\"https:\/\/anubis.techaro.lol\/\">Anubis<\/a>, which is\nan interesting take. I tried it for a small while, but I wanted something that:<\/p>\n<ul>\n<li>works without JavaScript support<\/li>\n<li>allows scrapping of &quot;common&quot; pages, like the list of projects and their READMEs<\/li>\n<li>doesn't sit on top of the connection, but instead &quot;by the side&quot;. That is, the bytes don't need to be proxied over\nagain<\/li>\n<\/ul>\n<p>So I've coded <a href=\"https:\/\/git.sitegui.dev\/sitegui\/forgejo-shield\">forgejo-shield<\/a> that implements a simple logic:<\/p>\n<ul>\n<li>requests to static assets, project pages and the .git protocol are always accepted<\/li>\n<li>any other request must have a cookie &quot;forgejo-shield&quot; set. If it doesn't, the user will be presented with a page with\na button that posts a form that sets the cookie<\/li>\n<\/ul>\n<p>The Caddyfile has a new <code>forward_auth<\/code> block like this:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code><span style=\"color:#d3d0c8;\">git.sitegui.dev {\n<\/span><span style=\"color:#d3d0c8;\">    forward_auth forgejo-shield:8080 {\n<\/span><span style=\"color:#d3d0c8;\">        uri \/\n<\/span><span style=\"color:#d3d0c8;\">    }\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">    reverse_proxy forgejo:3000\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<p>So potentially more expensive pages will be protected with:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/05\/the-bots-came-to-hunt-my-forge-data.png\" alt=\"the shield form with a button\" \/><\/p>\n<p>I just deployed it, and it seems to work as expected. \ud83e\udd1e\ud83c\udffd<br \/>\nThis episode got me thinking of deploying <a href=\"https:\/\/iocaine.madhouse-project.org\/\">iocaine<\/a> in the future, to create an\ninfinite maze to poison training datasets.<\/p>\n"},{"title":"Rust Week 2026","id":"83059f5a-635b-4101-a864-0fdf29cc5386","updated":"2026-05-22T00:00:00+00:00","author":{"name":"sitegui"},"category":{"@attributes":{"term":"programming"}},"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/05\/rust-week-2026","rel":"alternate"}},"published":"2026-05-22T00:00:00+00:00","content":"<p>I'm coming back home from <a href=\"https:\/\/2026.rustweek.org\/\">Rust Week 2026<\/a> in Utrecht \ud83e\udd80. It was two days of interesting and\nthought-provoking talks, followed by one day of coding together on Rust-related themes.<\/p>\n<p>The venue was intelligently chosen: a cinema! No meet up can beat these comfortable human holders:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/05\/rust-week-2026-01.jpeg\" alt=\"the cinema room\" \/><\/p>\n<p>Talking to the sponsors, they use Rust for all sorts of projects, like developing microchips (Espressif), self-hosted\nclouds (0xide), a platform for EV chargers in Holland (TandemDrive), data analysis (Polars), GPUs (Vectorware),\nnetworking infrastructure (NLNetLabs), and editor (Zed).<\/p>\n<p>I felt shy around the big crowd (I'm working on it...), but I was happy with myself because I've managed to discuss a\nlittle with people in different moments of their Rust journey.<\/p>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<p>I've met many developers using Rust: three for energy grid stability in Germany; one home-lab enthusiast\nfor their projects (no, it was not me!); one that worked on high-frequency trading; one to handle hospital data; another\nto help configure NixOS.<\/p>\n<p>I've met Denis, who maintains docs.rs infrastructure (\u2764\ufe0f). We've discussed server load, storage optimization, and\nthey teached me a nice shortcut: you can access <code>docs.rs\/some_crate::<\/code> followed by a search term, and the system\nredirects you directly to the search results.<br \/>\nFor example, to search for &quot;open&quot; in tokio, try this: <a href=\"https:\/\/docs.rs\/tokio::open\">docs.rs\/tokio::open<\/a>.<\/p>\n<p>I've also met Josh, who maintains the <a href=\"https:\/\/servo.org\/\">servo<\/a> web engine for 14 years. He works literally on the\nworld's second Rust project, the first being the compiler itself. What an inspiring figure!<\/p>\n<p>The ecosystem is very big and innovating, made out of enthusiasts and production users at the same time. And I felt\nrefreshed to hear and talk about passion projects and serious learning, far from the noise of the latest buzzwords\nmandated by corporate.<\/p>\n<h2>The talks<\/h2>\n<p>The recordings should be up in a couple of weeks\nin <a href=\"https:\/\/www.youtube.com\/@rustnederlandrustnl\">the official Youtube channel<\/a>, check if you are curious.<\/p>\n<p>There were 3 parallel tracks, so unfortunately I could not watch them all. From those that I was present, these were my\nfavourites:<\/p>\n<ul>\n<li><a href=\"https:\/\/2026.rustweek.org\/talks\/folkert\/\">Stabilizing decade-old features<\/a> by Folkert de Vries<\/li>\n<li><a href=\"https:\/\/2026.rustweek.org\/talks\/sebastian\/\">Writing GPU shaders in plain Rust<\/a> by Sebastian Sydow<\/li>\n<li><a href=\"https:\/\/2026.rustweek.org\/talks\/greg\/\">Untrusted data in Linux \u2014 How Rust is going to save us<\/a> by Greg Kroah-Hartman<\/li>\n<li><a href=\"https:\/\/2026.rustweek.org\/talks\/benno-nadri\/\">Field Projections \u2014 Making Custom Pointers feel Builtin<\/a> by Benno\nLossin and Nadri<\/li>\n<li><a href=\"https:\/\/2026.rustweek.org\/talks\/arya\/\">Obsessive Optimization with String Interning<\/a> by arya dradjica<\/li>\n<li><a href=\"https:\/\/2026.rustweek.org\/talks\/urgau\/\">Overcoming GitHub shortcomings with Triagebot<\/a> by Urgau<\/li>\n<li><a href=\"https:\/\/2026.rustweek.org\/talks\/josh\/\">Tracking down undefined behaviour in Servo<\/a> by Josh Bowman-Matthews<\/li>\n<\/ul>\n<h2>The hacking<\/h2>\n<p>On the last day, people could hack together on whatever they got nerd-snipped by.\nThe <a href=\"https:\/\/kiesraad.nl\/\">Dutch election council<\/a> was there and presented their use of Rust in an awesome project: for\nsome years they've put in place a system to help the manual counting of votes by the municipalities and also the final\naggregation and seat distribution. <a href=\"https:\/\/github.com\/kiesraad\/abacus\">Their system<\/a> is well thought of, it does not\naim to replace the manual counting, but instead enhance and verify.<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/05\/rust-week-2026-02.png\" alt=\"the abacus logo\" \/><\/p>\n<p>They use public money to create open source. I love the initiative! And it's also efficient: they told that while the\nprevious system produced the final PDF reports in 20 minutes, the new one in Rust runs in less than 1 second!<\/p>\n<p>By law, all election candidates and results must be encoded in XML using the EML-NL format (election markup language).\nI've worked to <a href=\"https:\/\/github.com\/kiesraad\/rust-eml-nl\/pull\/22\">reduce the peak memory usage<\/a> of this data by 30% \ud83e\udd1f\ud83c\udffd.<\/p>\n<p><span class=\"fun\">See you next year, I hope!<\/span><\/p>\n"},{"title":"Dissecting a delta table: just a bunch of JSON and parquet files","id":"2c989c9d-d923-4461-a4be-edb9d45c7c85","updated":"2026-05-06T00:00:00+00:00","author":{"name":"sitegui"},"category":[{"@attributes":{"term":"programming"}},{"@attributes":{"term":"data-engineering"}},{"@attributes":{"term":"spark"}}],"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/05\/dissecting-a-delta-table-just-a-bunch-of-JSON-and-parquet-files","rel":"alternate"}},"published":"2026-05-06T00:00:00+00:00","content":"<p>Today I'll share some points that I've learned while playing with Spark, Parquet and the Delta format. Even if you don't\nuse these technologies, I hope you can spot some neat ideas to reuse somewhere else later.<\/p>\n<p>I like to picture in my head that the most important architectural distinction between a datalake table and a typical\ndatabase (like Postgres) is that compute and storage are handled very separately: the table's data is stored in one\ndistributed system (typically a cloud object storage), while another distributed system (or even multiple ones) read and\nwrite to those files that compose the table.<\/p>\n<p>There are competing formats to represent these tables, with distinct trade-offs of course: Delta, Iceberg, Hudi. But\nfrom my research, I don't think there\nis <a href=\"https:\/\/datavidhya.com\/blog\/delta-lake-vs-apache-iceberg\/\">anything fundamentally different<\/a> between then. This post\nwill focus on Delta, but most of it should be easily transposable for the others.<\/p>\n<p>I like to understand technical solutions by framing the fundamental problems that they aim to solve best, so I'll\npresent it like that. Just remember that, although I have read\n<a href=\"https:\/\/github.com\/delta-io\/delta\/blob\/461fb09192cc11f4dd3a929eefc2b38830ef6e70\/PROTOCOL.md\">the specification<\/a> and\nhave used Spark with Delta tables for years, I didn't invent any of this: I'm just an outside observer who\ncan be wrong. If you spot a misconception, please tell me in the comments!<\/p>\n<h2>How it solves its main challenges<\/h2>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<h3>Very large tables: pruning files<\/h3>\n<p>The Delta format (and datalakes in general) aims to work effectively with very large tables, with petabytes\nof data over trillions of rows. To allow for this, the data is divided into multiple files which are themselves divided\nin chunks. The format then has ways to drastically reduce how many files and how much of these files need to be accessed\nin order to answer queries.<\/p>\n<p>To cut early which files are read, Delta keeps track of some simple statistics about each column in each file: number of\nnulls, maximum and minimum value. To give a concrete example, let's explore a very simple Delta table made of 3 files:<\/p>\n<ul>\n<li><code>my-table\/_delta_log\/00000000000000000000.json<\/code><\/li>\n<li><code>my-table\/part-00000-0d56d77a-f779-46e3-adf2-7eaa14656c20-c000.zstd.parquet<\/code><\/li>\n<li><code>my-table\/part-00000-a6ac1d2e-304a-465e-b549-ccc442aa3855-c000.zstd.parquet<\/code><\/li>\n<\/ul>\n<p>The <code>.parquet<\/code> files contain the actual rows' data, but put them aside for now: we'll talk about them in a minute.<\/p>\n<p>The <code>_delta_log\/00000000000000000000.json<\/code> file is a JSON-lines file that describes the table itself. It has a funny\nname (not judging!), but you can imagine that it's somehow linked to history and evolution of the table. In it, each\nparquet file that constitutes the table has a record like this:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-json\"><span style=\"color:#d3d0c8;\">{\n<\/span><span style=\"color:#d3d0c8;\">  &quot;<\/span><span style=\"color:#99cc99;\">add<\/span><span style=\"color:#d3d0c8;\">&quot;: {\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">path<\/span><span style=\"color:#d3d0c8;\">&quot;: &quot;<\/span><span style=\"color:#99cc99;\">part-00000-0d56d77a-f779-46e3-adf2-7eaa14656c20-c000.zstd.parquet<\/span><span style=\"color:#d3d0c8;\">&quot;,\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">partitionValues<\/span><span style=\"color:#d3d0c8;\">&quot;: {},\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">size<\/span><span style=\"color:#d3d0c8;\">&quot;: <\/span><span style=\"color:#f99157;\">1040477<\/span><span style=\"color:#d3d0c8;\">,\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">modificationTime<\/span><span style=\"color:#d3d0c8;\">&quot;: <\/span><span style=\"color:#f99157;\">1778102420000<\/span><span style=\"color:#d3d0c8;\">,\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">dataChange<\/span><span style=\"color:#d3d0c8;\">&quot;: <\/span><span style=\"color:#f99157;\">true<\/span><span style=\"color:#d3d0c8;\">,\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">stats<\/span><span style=\"color:#d3d0c8;\">&quot;: &quot;<\/span><span style=\"color:#99cc99;\">{<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">numRecords<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">:10,<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">minValues<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">:{<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">id<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">:0,<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">name<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">:<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">gz ovkfgoconqkwxf alpdjfuk rqvvb<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">,<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">edition<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">:12},<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">maxValues<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">:{<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">id<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">:9,<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">name<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">:<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">zulxlljs fizl qtivsko dkb<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">,<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">edition<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">:90},<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">nullCount<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">:{<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">id<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">:0,<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">name<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">:0,<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">edition<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">:0,<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">days<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">:0,<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">games<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">:0,<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">editors<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">:0,<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">visitors<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">:0}}<\/span><span style=\"color:#d3d0c8;\">&quot;\n<\/span><span style=\"color:#d3d0c8;\">  }\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<p>Look who's there: <code>stats<\/code>! It's a JSON inside a JSON, why not... Here, I'll format it so we can take a closer look:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-json\"><span style=\"color:#d3d0c8;\">{\n<\/span><span style=\"color:#d3d0c8;\">  &quot;<\/span><span style=\"color:#99cc99;\">numRecords<\/span><span style=\"color:#d3d0c8;\">&quot;: <\/span><span style=\"color:#f99157;\">10<\/span><span style=\"color:#d3d0c8;\">,\n<\/span><span style=\"color:#d3d0c8;\">  &quot;<\/span><span style=\"color:#99cc99;\">minValues<\/span><span style=\"color:#d3d0c8;\">&quot;: {\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">id<\/span><span style=\"color:#d3d0c8;\">&quot;: <\/span><span style=\"color:#f99157;\">0<\/span><span style=\"color:#d3d0c8;\">,\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">name<\/span><span style=\"color:#d3d0c8;\">&quot;: &quot;<\/span><span style=\"color:#99cc99;\">gz ovkfgoconqkwxf alpdjfuk rqvvb<\/span><span style=\"color:#d3d0c8;\">&quot;,\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">edition<\/span><span style=\"color:#d3d0c8;\">&quot;: <\/span><span style=\"color:#f99157;\">12\n<\/span><span style=\"color:#d3d0c8;\">  },\n<\/span><span style=\"color:#d3d0c8;\">  &quot;<\/span><span style=\"color:#99cc99;\">maxValues<\/span><span style=\"color:#d3d0c8;\">&quot;: {\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">id<\/span><span style=\"color:#d3d0c8;\">&quot;: <\/span><span style=\"color:#f99157;\">9<\/span><span style=\"color:#d3d0c8;\">,\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">name<\/span><span style=\"color:#d3d0c8;\">&quot;: &quot;<\/span><span style=\"color:#99cc99;\">zulxlljs fizl qtivsko dkb<\/span><span style=\"color:#d3d0c8;\">&quot;,\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">edition<\/span><span style=\"color:#d3d0c8;\">&quot;: <\/span><span style=\"color:#f99157;\">90\n<\/span><span style=\"color:#d3d0c8;\">  },\n<\/span><span style=\"color:#d3d0c8;\">  &quot;<\/span><span style=\"color:#99cc99;\">nullCount<\/span><span style=\"color:#d3d0c8;\">&quot;: {\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">id<\/span><span style=\"color:#d3d0c8;\">&quot;: <\/span><span style=\"color:#f99157;\">0<\/span><span style=\"color:#d3d0c8;\">,\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">name<\/span><span style=\"color:#d3d0c8;\">&quot;: <\/span><span style=\"color:#f99157;\">0<\/span><span style=\"color:#d3d0c8;\">,\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">edition<\/span><span style=\"color:#d3d0c8;\">&quot;: <\/span><span style=\"color:#f99157;\">0<\/span><span style=\"color:#d3d0c8;\">,\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">days<\/span><span style=\"color:#d3d0c8;\">&quot;: <\/span><span style=\"color:#f99157;\">0<\/span><span style=\"color:#d3d0c8;\">,\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">games<\/span><span style=\"color:#d3d0c8;\">&quot;: <\/span><span style=\"color:#f99157;\">0<\/span><span style=\"color:#d3d0c8;\">,\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">editors<\/span><span style=\"color:#d3d0c8;\">&quot;: <\/span><span style=\"color:#f99157;\">0<\/span><span style=\"color:#d3d0c8;\">,\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">visitors<\/span><span style=\"color:#d3d0c8;\">&quot;: <\/span><span style=\"color:#f99157;\">0\n<\/span><span style=\"color:#d3d0c8;\">  }\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<p>Cool! So it has 10 rows, <code>id<\/code> is between 0 and 9, <code>name<\/code> is between &quot;gz ovkfgoconqkwxf alpdjfuk rqvvb&quot; and &quot;zulxlljs\nfizl qtivsko dkb&quot;, no column has any null.<\/p>\n<p>This is a silly example, but in a more realistic data and query it can be very handy. Say you're doing<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-sql\"><span style=\"color:#cc99cc;\">select <\/span><span style=\"color:#d3d0c8;\">*\n<\/span><span style=\"color:#cc99cc;\">from<\/span><span style=\"color:#d3d0c8;\"> my_table\n<\/span><span style=\"color:#cc99cc;\">where<\/span><span style=\"color:#d3d0c8;\"> created_at &gt; &#39;<\/span><span style=\"color:#99cc99;\">2026-01-01<\/span><span style=\"color:#d3d0c8;\">&#39;\n<\/span><\/code><\/pre>\n<p>A Delta-compatible engine will read the <code>00000000000000000000.json<\/code> file to extract the list of all files, then use\nthe statistics to decide which files are even worth considering.<\/p>\n<p>But to be frank, I was somewhat shocked that the statistics are so simple (just max, min and null count)! If the column\nyou're searching on has naturally a similar range in all files (for example, it's a UUID v4, product name or event\nkind), it seems to me that this file pruning will be much less effective. The Delta format has no such concept of\n&quot;indexes&quot; as other databases have. Also, it surprised me that columns with array of structs don't get statistics at all!<\/p>\n<h3>Very large tables: reading less data<\/h3>\n<p>Delta uses the parquet format to store the rows. Parquet is pretty neat. If you have time to spare, go\nand <a href=\"https:\/\/github.com\/apache\/parquet-format\/blob\/96edf77704b60b6f3ca2232c218c64eff6c874d3\/README.md\">read the summary<\/a>\non the project's page. For our discussion right now, you only have to know that parquet divides the table horizontally\ninto &quot;row groups&quot; and that, in each row group, the data for each column is stored sequentially. The end of the file\ncontains a footer with the metadata and offsets for each column in each row group:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/05\/dissecting-a-delta-table-just-a-bunch-of-JSON-and-parquet-files-01.png\" alt=\"simplified-parquet-sections\" \/><\/p>\n<p>So instead of reading each file in full, the Delta engine first reads only the footer; then depending on the queried\ncolumns it reads only the segments of the file that are of interest. This is very important for datalake tables, since\nthe files are usually stored in a separate cloud service.<\/p>\n<p>Small parenthesis here: this separate cloud service is usually a &quot;dumb&quot; object storage, but it's extra cool when you\nrealise that it can be something much more refined! For example, it can be a service that implements authorisation over\nwhich columns the requesting user can access to protect sensitive information.<\/p>\n<p>Let's take a train back to the statistics of the columns. While the Delta format keeps some very simple statistics as\npart of the JSON file that links each parquet file to the table, the parquet itself is more sophisticated! First,\nbecause it does so per row group. Second, because it can store the list of unique values (if it's small) or a <a href=\"https:\/\/parquet.apache.org\/docs\/file-format\/bloomfilter\/\">bloom\nfilter<\/a> (if there are many distinct values).<\/p>\n<h3>Very large tables: distributed reading<\/h3>\n<p>That's easy: the table is split into multiple parquet files, each one may be further broken down in row groups. Each\nrow group can be handled independently, so a distributed system (say a cluster with many machines and cores) can divide\nthe work to reduce total processing time.<\/p>\n<h3>Consistent data schema and schema evolution<\/h3>\n<p>A typical use case for datalake tables is to keep a history of multiple months or even years of data. Just like a SQL\ndatabase like Postgres, each table has a schema (set of column names and types), which has to be migrated as the\nunderlying data evolves. However, unlike Postgres, the allowed changes in the schema are designed so that historical\ndata (that is, old parquet files) don't need to be rewritten.<\/p>\n<p>In practice, in the example table above the <code>00000000000000000000.json<\/code> file also contains a record like:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-json\"><span style=\"color:#d3d0c8;\">{\n<\/span><span style=\"color:#d3d0c8;\">  &quot;<\/span><span style=\"color:#99cc99;\">metaData<\/span><span style=\"color:#d3d0c8;\">&quot;: {\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">id<\/span><span style=\"color:#d3d0c8;\">&quot;: &quot;<\/span><span style=\"color:#99cc99;\">6a8b2481-493a-4796-a77d-ed5e1b70a1e1<\/span><span style=\"color:#d3d0c8;\">&quot;,\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">format<\/span><span style=\"color:#d3d0c8;\">&quot;: {\n<\/span><span style=\"color:#d3d0c8;\">      &quot;<\/span><span style=\"color:#99cc99;\">provider<\/span><span style=\"color:#d3d0c8;\">&quot;: &quot;<\/span><span style=\"color:#99cc99;\">parquet<\/span><span style=\"color:#d3d0c8;\">&quot;,\n<\/span><span style=\"color:#d3d0c8;\">      &quot;<\/span><span style=\"color:#99cc99;\">options<\/span><span style=\"color:#d3d0c8;\">&quot;: {}\n<\/span><span style=\"color:#d3d0c8;\">    },\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">schemaString<\/span><span style=\"color:#d3d0c8;\">&quot;: &quot;<\/span><span style=\"color:#99cc99;\">{<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">type<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">:<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">struct<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">,<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">fields<\/span><span style=\"color:#66cccc;\">\\&quot;<\/span><span style=\"color:#99cc99;\">:[...]}<\/span><span style=\"color:#d3d0c8;\">&quot;,\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">partitionColumns<\/span><span style=\"color:#d3d0c8;\">&quot;: [],\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">configuration<\/span><span style=\"color:#d3d0c8;\">&quot;: {},\n<\/span><span style=\"color:#d3d0c8;\">    &quot;<\/span><span style=\"color:#99cc99;\">createdTime<\/span><span style=\"color:#d3d0c8;\">&quot;: <\/span><span style=\"color:#f99157;\">1778102418300\n<\/span><span style=\"color:#d3d0c8;\">  }\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<p>Yes, another JSON in a JSON in <code>schemaString<\/code>.<\/p>\n<p>Any evolution to the schema will produce a new <code>metaData<\/code> record with the full updated schema.<\/p>\n<p>So that no previous parquet file needs to rewritten, Delta only allows:<\/p>\n<ul>\n<li>adding a new column: reading a previous parquet file will assume <code>null<\/code> for it<\/li>\n<li>removing a column: the data is left in previous parquet files, but no longer visible for the engine<\/li>\n<li>changing the order of a column: it's just superficial<\/li>\n<li>some specific change of types, called type &quot;widening&quot;: readers have to convert on the fly as needed<\/li>\n<li>renaming a column: requires &quot;column mapping&quot;<\/li>\n<\/ul>\n<p>This last one is the most complex one and has to be enabled in more recent Delta versions. With this, the name of the\ncolumn in the parquet files is just a UUID. The Delta metadata in the JSON file then maps each UUID to a user-visible\ncolumn name.<\/p>\n<p>The schema string is quite large, and it has to be repeated in its entirety every time it evolves. I guess for most\ntables that's negligible, but we can imagine a pathological case for tables with thousands of columns that evolve\nfrequently.<\/p>\n<h3>Append data<\/h3>\n<p>Many use cases of Delta tables require adding more data to it in batches, usually by some automatic ingestion pipeline\nevery hour or day. Delta deals with this by versioning the table and splitting the operation into two distinct steps:<\/p>\n<ol>\n<li>write new parquet files<\/li>\n<li>commit the new version<\/li>\n<\/ol>\n<p>The first step can take an arbitrarily long time, and multiple concurrent writers can do this at the same time.\nCrucially, these new files are &quot;dangling&quot; so far and no reader can see them.<\/p>\n<p>The second step has to be done atomically by writing a new versioned file with the next version number. In our examples\nabove, it would be <code>00000000000000000001.json<\/code>. All these JSON files have to be read and merged in sequence by future\nreaders.<\/p>\n<p>Delta uses &quot;optimistic&quot; concurrency. To illustrate, when two writers work in parallel they can do step 1 at their own\npace, but will have to serially do step 2. In the example below, writer 1 commits first, so writer 2 has to check that\nwhat writer 1 has done does not conflict with its work, then commit. For appending data that's usually fine, but for\nupdates it may require writer 2 to start over.<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/05\/dissecting-a-delta-table-just-a-bunch-of-JSON-and-parquet-files-02.png\" alt=\"a timeline of changes illustrating the optimistic concurrent model\" \/><\/p>\n<p>However, note how this architecture has one great weakness: each addition requires writing a new parquet file then\ncommiting a new version. Also, every future reader has to read all the small JSON files to glue them all together. So\nDelta is a catastrophic format if you want to do multiple updates every second!<\/p>\n<p>To mitigate the ever-increasing number of JSON files to read, the writer may decide to compact the history. For example:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code><span style=\"color:#d3d0c8;\">my-table\/_delta_log\/00000000000000000000.json\n<\/span><span style=\"color:#d3d0c8;\">my-table\/_delta_log\/00000000000000000001.json\n<\/span><span style=\"color:#d3d0c8;\">my-table\/_delta_log\/00000000000000000002.json\n<\/span><span style=\"color:#d3d0c8;\">my-table\/_delta_log\/00000000000000000003.json\n<\/span><span style=\"color:#d3d0c8;\">my-table\/_delta_log\/00000000000000000004.json\n<\/span><\/code><\/pre>\n<p>can be compacted as<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code><span style=\"color:#d3d0c8;\">my-table\/_delta_log\/00000000000000000004.json\n<\/span><span style=\"color:#d3d0c8;\">my-table\/_delta_log\/00000000000000000004.checkpoint.parquet\n<\/span><\/code><\/pre>\n<p>The <code>.checkpoint.parquet<\/code> file containing the <code>add<\/code> entries of all versions so far, and the <code>.json<\/code> file containing only\nthe last <code>metaData<\/code> entry.<\/p>\n<h3>Delete and update data<\/h3>\n<p>Delta represents &quot;removed&quot; data without actually removing them \ud83d\ude1c. It uses &quot;delete vector&quot;: a new binary file that\ndescribes which rows in the parquet files should be considered removed.<\/p>\n<p>Again, this produces a new version with a <code>delete<\/code> entry, and the same optimistic concurrency model and compaction logic\ndescribed above apply.<\/p>\n<p>Delta can update data by either producing a <code>delete<\/code> followed by an <code>add<\/code> in the same version, or using a dedicated\n<code>cdc<\/code> (which stands for change data capture). I could not learn enough about the CDC feature, so I'll not try to explain\nit here.<\/p>\n<h3>Optimisation and clean-up<\/h3>\n<p>As you may imagine, as the table evolves due to writes, the data may become too much fragmented and removed data no\nlonger accessible by readers still taking space, etc. A Delta engine implements a <code>VACCUM<\/code> and <code>OPTIMIZE<\/code> commands to\nrewrite recently added and modified parquet files.<\/p>\n<p>There's a sweet spot: too many parquet files bring a lot of metadata overhead, too few don't allow enough parallelism.<\/p>\n<p>Also, newer implementations sort and divide the rows in the row groups and parquet files to improve the\nrelevance of the column statistics for file and row-group pruning. They've named this feature &quot;liquid clustering&quot;, which\nmy brain finds too much marketing-y.<\/p>\n<hr \/>\n<p><span class=\"fun\">That's all for now, thanks for reading!<\/span><\/p>\n<p>Delta is a pretty neat format that solves some hard problems not with magic, but\nwith a bunch of JSON and parquet files. At the same time, its design also brings some major pain points... I hope that\nyou've learned something new, 'cause I certainly did.<\/p>\n"},{"title":"My homelab: an old laptop behind the fridge","id":"e4021a1e-ab78-4d0f-89bd-fb7cd3ef7101","updated":"2026-03-30T00:00:00+00:00","author":{"name":"sitegui"},"category":{"@attributes":{"term":"homelab"}},"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/03\/my-homelab-an-old-laptop-behind-the-fridge","rel":"alternate"}},"published":"2026-03-30T00:00:00+00:00","content":"<p>This very website is served from my home by an Ubuntu Server running on old hardware behind my fridge:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/03\/my-homelab-an-old-laptop-behind-the-fridge\/setup.png\" alt=\"my homelab setup\" \/><\/p>\n<p>I've talked <a href=\"https:\/\/www.meetup.com\/human-talks-angers\/\">in a local dev meetup<\/a> about my setup, you can check the slides\nhere:<\/p>\n<p><a href=\"https:\/\/sitegui.dev\/post\/2026\/03\/my-homelab-an-old-laptop-behind-the-fridge\/homelab_en.pdf\"><img src=\"https:\/\/sitegui.dev\/post\/2026\/03\/my-homelab-an-old-laptop-behind-the-fridge\/slides.png\" alt=\"my slides\" \/><\/a><\/p>\n<p>My homelab is at the same time a &quot;production&quot; environment for my digital life and &quot;staging&quot; environment for all sort of\ncrazy project that I want to play with. It's amazing what 10-year-old hardware is capable of!<\/p>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<p>Maybe someday I'll invest into another second-hand to create separate environments.<\/p>\n<p>All my homelab config is available <a href=\"https:\/\/git.sitegui.dev\/sitegui\/home-lab\">this repo in my git forge<\/a> (also itself\nhosted behind the fridge, as you may have guessed \ud83d\ude1c).<\/p>\n<p>In a future post, I want to take the layers of my setup apart and write about the different problems that I've solved\nand solutions that I've found. Until next time: <span class=\"fun\">go and play!<\/span> The world is large and there are a\nlot of very good\nself-hostable projects around. You can start simple.<\/p>\n"},{"title":"Performance advice nugget 01: use flat representations","id":"1ade03c4-f729-41e0-a0ed-623336d2be6d","updated":"2026-03-21T00:00:00+00:00","author":{"name":"sitegui"},"category":[{"@attributes":{"term":"programming"}},{"@attributes":{"term":"performance"}},{"@attributes":{"term":"PAN"}}],"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/03\/performance-advice-nugget-1-use-flat-representations","rel":"alternate"}},"published":"2026-03-21T00:00:00+00:00","content":"<p>This is my last week on my current job, and I was reflecting about some of the things that I've learned in the past 7\nyears there. There's a lot of course! As a staff data engineer, I worked with many teams and codebases and learned a\ncouple of tricks at scale. So I'm starting a new blog series &quot;performance advice nugget&quot; (or <em>PAN<\/em> for short), in which\nI'll share some insights of what worked quite well in practice.<\/p>\n<p>So welcome to <em>PAN<\/em> 01: <strong>use flat representations<\/strong>.<\/p>\n<p>I'll try to make these posts quite short. As usual with everything related to &quot;performance&quot;, you should\nalways measure and benchmark with your real workload, and always balance whether additional complexity is worth the\nperformance gains.<\/p>\n<p>Nice, forewords are said and out of the way. Let's focus on the matter: imagine that you are handling a data that has\nmultiple levels, for example, a paragraph, that is made of sentences, each made of words, each made of characters:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/03\/performance-advice-nugget-1-use-flat-representations-01.png\" alt=\"a paragraph, made of setences, words and characters\" \/><\/p>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<p>One straight way to model this is with lists of lists:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-python\"><span style=\"color:#cc99cc;\">class <\/span><span style=\"color:#ffcc66;\">Paragraph<\/span><span style=\"color:#f2f0ec;\">:\n<\/span><span style=\"color:#d3d0c8;\">    sentences: list[Sentence]\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#cc99cc;\">class <\/span><span style=\"color:#ffcc66;\">Sentence<\/span><span style=\"color:#f2f0ec;\">:\n<\/span><span style=\"color:#d3d0c8;\">    words: list[Word]\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#cc99cc;\">class <\/span><span style=\"color:#ffcc66;\">Word<\/span><span style=\"color:#f2f0ec;\">:\n<\/span><span style=\"color:#d3d0c8;\">    chars: list[str]\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">data = <\/span><span style=\"color:#6699cc;\">Paragraph<\/span><span style=\"color:#d3d0c8;\">([\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#6699cc;\">Sentence<\/span><span style=\"color:#d3d0c8;\">([\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#6699cc;\">Word<\/span><span style=\"color:#d3d0c8;\">([&quot;<\/span><span style=\"color:#99cc99;\">H<\/span><span style=\"color:#d3d0c8;\">&quot;, &quot;<\/span><span style=\"color:#99cc99;\">e<\/span><span style=\"color:#d3d0c8;\">&quot;, &quot;<\/span><span style=\"color:#99cc99;\">l<\/span><span style=\"color:#d3d0c8;\">&quot;, &quot;<\/span><span style=\"color:#99cc99;\">l<\/span><span style=\"color:#d3d0c8;\">&quot;, &quot;<\/span><span style=\"color:#99cc99;\">o<\/span><span style=\"color:#d3d0c8;\">&quot;]),\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#6699cc;\">Word<\/span><span style=\"color:#d3d0c8;\">([&quot;<\/span><span style=\"color:#99cc99;\">W<\/span><span style=\"color:#d3d0c8;\">&quot;, &quot;<\/span><span style=\"color:#99cc99;\">o<\/span><span style=\"color:#d3d0c8;\">&quot;, &quot;<\/span><span style=\"color:#99cc99;\">r<\/span><span style=\"color:#d3d0c8;\">&quot;, &quot;<\/span><span style=\"color:#99cc99;\">l<\/span><span style=\"color:#d3d0c8;\">&quot;, &quot;<\/span><span style=\"color:#99cc99;\">d<\/span><span style=\"color:#d3d0c8;\">&quot;]),\n<\/span><span style=\"color:#d3d0c8;\">    ]),\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#6699cc;\">Sentence<\/span><span style=\"color:#d3d0c8;\">([\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#6699cc;\">Word<\/span><span style=\"color:#d3d0c8;\">([&quot;<\/span><span style=\"color:#99cc99;\">B<\/span><span style=\"color:#d3d0c8;\">&quot;, &quot;<\/span><span style=\"color:#99cc99;\">y<\/span><span style=\"color:#d3d0c8;\">&quot;, &quot;<\/span><span style=\"color:#99cc99;\">e<\/span><span style=\"color:#d3d0c8;\">&quot;]),\n<\/span><span style=\"color:#d3d0c8;\">    ]),\n<\/span><span style=\"color:#d3d0c8;\">])\n<\/span><\/code><\/pre>\n<p>Note: don't overthink <code>Word<\/code> in this synthetic example! In a more realistic setup, <code>Word<\/code> could probably just be a\n<code>str<\/code>. But here I'm using it to illustrate the &quot;layered&quot; modeling.<\/p>\n<p>Today's <em>PAN<\/em> is: don't nest the lists! Instead, keep the leaf information (here the chars) flat in a single list.\nThen use other lists to index into the flat representation:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-python\"><span style=\"color:#cc99cc;\">class <\/span><span style=\"color:#ffcc66;\">FlatParagrah<\/span><span style=\"color:#f2f0ec;\">:\n<\/span><span style=\"color:#d3d0c8;\">    characters: list[str]\n<\/span><span style=\"color:#d3d0c8;\">    words: list[WordInfo]\n<\/span><span style=\"color:#d3d0c8;\">    sentences: list[SentenceInfo]\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#cc99cc;\">class <\/span><span style=\"color:#ffcc66;\">WordInfo<\/span><span style=\"color:#f2f0ec;\">:\n<\/span><span style=\"color:#d3d0c8;\">    num_chars: int\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#cc99cc;\">class <\/span><span style=\"color:#ffcc66;\">SentenceInfo<\/span><span style=\"color:#f2f0ec;\">:\n<\/span><span style=\"color:#d3d0c8;\">    num_chars: int\n<\/span><span style=\"color:#d3d0c8;\">    num_words: int\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">data = <\/span><span style=\"color:#6699cc;\">FlatParagrah<\/span><span style=\"color:#d3d0c8;\">(\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">characters<\/span><span style=\"color:#d3d0c8;\">=[&quot;<\/span><span style=\"color:#99cc99;\">H<\/span><span style=\"color:#d3d0c8;\">&quot;, &quot;<\/span><span style=\"color:#99cc99;\">e<\/span><span style=\"color:#d3d0c8;\">&quot;, &quot;<\/span><span style=\"color:#99cc99;\">l<\/span><span style=\"color:#d3d0c8;\">&quot;, &quot;<\/span><span style=\"color:#99cc99;\">l<\/span><span style=\"color:#d3d0c8;\">&quot;, &quot;<\/span><span style=\"color:#99cc99;\">o<\/span><span style=\"color:#d3d0c8;\">&quot;, &quot;<\/span><span style=\"color:#99cc99;\">W<\/span><span style=\"color:#d3d0c8;\">&quot;, &quot;<\/span><span style=\"color:#99cc99;\">o<\/span><span style=\"color:#d3d0c8;\">&quot;, &quot;<\/span><span style=\"color:#99cc99;\">r<\/span><span style=\"color:#d3d0c8;\">&quot;, &quot;<\/span><span style=\"color:#99cc99;\">l<\/span><span style=\"color:#d3d0c8;\">&quot;, &quot;<\/span><span style=\"color:#99cc99;\">d<\/span><span style=\"color:#d3d0c8;\">&quot;, &quot;<\/span><span style=\"color:#99cc99;\">B<\/span><span style=\"color:#d3d0c8;\">&quot;, &quot;<\/span><span style=\"color:#99cc99;\">y<\/span><span style=\"color:#d3d0c8;\">&quot;, &quot;<\/span><span style=\"color:#99cc99;\">e<\/span><span style=\"color:#d3d0c8;\">&quot;],\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">words<\/span><span style=\"color:#d3d0c8;\">=[\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#6699cc;\">WordInfo<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f99157;\">5<\/span><span style=\"color:#d3d0c8;\">),\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#6699cc;\">WordInfo<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f99157;\">5<\/span><span style=\"color:#d3d0c8;\">),\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#6699cc;\">WordInfo<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f99157;\">3<\/span><span style=\"color:#d3d0c8;\">),\n<\/span><span style=\"color:#d3d0c8;\">    ],\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">sentences<\/span><span style=\"color:#d3d0c8;\">=[\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#6699cc;\">SentenceInfo<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f2777a;\">num_chars<\/span><span style=\"color:#d3d0c8;\">=<\/span><span style=\"color:#f99157;\">10<\/span><span style=\"color:#d3d0c8;\">, <\/span><span style=\"color:#f2777a;\">num_words<\/span><span style=\"color:#d3d0c8;\">=<\/span><span style=\"color:#f99157;\">2<\/span><span style=\"color:#d3d0c8;\">),\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#6699cc;\">SentenceInfo<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f2777a;\">num_chars<\/span><span style=\"color:#d3d0c8;\">=<\/span><span style=\"color:#f99157;\">3<\/span><span style=\"color:#d3d0c8;\">, <\/span><span style=\"color:#f2777a;\">num_words<\/span><span style=\"color:#d3d0c8;\">=<\/span><span style=\"color:#f99157;\">1<\/span><span style=\"color:#d3d0c8;\">),\n<\/span><span style=\"color:#d3d0c8;\">    ],\n<\/span><span style=\"color:#d3d0c8;\">)\n<\/span><\/code><\/pre>\n<p>Why? Two main reasons:<\/p>\n<ol>\n<li><strong>CPU caching<\/strong>: CPUs are more efficient when accessing data that is packed closer together<\/li>\n<li><strong>less allocations<\/strong>: each list has to be allocated and deallocated individually. The flat design has a fixed and\nsmall number of lists<\/li>\n<\/ol>\n<h2>Is it true?<\/h2>\n<p>Well... as it's usual with data structures, it will highly depend on what you actually do with the data! In our case, it\nworked wonders.<\/p>\n<p>I've prepared a micro benchmark in Rust to illustrate, you can check the source code\nfor <a href=\"https:\/\/git.sitegui.dev\/sitegui\/performance-advice-nuggets\/src\/commit\/9dd505f95117336a620ff9d78c0aa561dfa834a1\/src\/paragraph.rs\">Paragraph<\/a>\nand <a href=\"https:\/\/git.sitegui.dev\/sitegui\/performance-advice-nuggets\/src\/commit\/9dd505f95117336a620ff9d78c0aa561dfa834a1\/src\/flat_paragraph.rs\">FlatParagraph<\/a>.<\/p>\n<table>\n<thead>\n<tr>\n<th>Operation<\/th>\n<th align=\"right\">Paragraph<\/th>\n<th align=\"right\">FlatParagraph<\/th>\n<th align=\"right\">Gain<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Create<\/td>\n<td align=\"right\">2100 ns<\/td>\n<td align=\"right\">370 ns<\/td>\n<td align=\"right\">5.7 x<\/td>\n<\/tr>\n<tr>\n<td>Deallocate<\/td>\n<td align=\"right\">460 ns<\/td>\n<td align=\"right\">39 ns<\/td>\n<td align=\"right\">12 x<\/td>\n<\/tr>\n<tr>\n<td>Iterate over words<\/td>\n<td align=\"right\">91 ns<\/td>\n<td align=\"right\">100 ns<\/td>\n<td align=\"right\">0.91 x<\/td>\n<\/tr>\n<tr>\n<td>Iterate over chars<\/td>\n<td align=\"right\">110 ns<\/td>\n<td align=\"right\">32 ns<\/td>\n<td align=\"right\">3.4 x<\/td>\n<\/tr>\n<tr>\n<td>Create with inserted word<\/td>\n<td align=\"right\">2400 ns<\/td>\n<td align=\"right\">350 ns<\/td>\n<td align=\"right\">6.9 x<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The last operation is the one that our codebase did the most: create a new Paragraph from an existing one by inserting a\nnew word in the middle of it. Something like:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-python\"><span style=\"color:#cc99cc;\">class <\/span><span style=\"color:#ffcc66;\">Paragraph<\/span><span style=\"color:#f2f0ec;\">:\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">def <\/span><span style=\"color:#6699cc;\">with_inserted_word<\/span><span style=\"color:#d3d0c8;\">(\n<\/span><span style=\"color:#d3d0c8;\">            <\/span><span style=\"color:#f2777a;\">self<\/span><span style=\"color:#d3d0c8;\">,\n<\/span><span style=\"color:#d3d0c8;\">            <\/span><span style=\"color:#f2777a;\">paragraph_i<\/span><span style=\"color:#d3d0c8;\">: int,\n<\/span><span style=\"color:#d3d0c8;\">            <\/span><span style=\"color:#f2777a;\">word_i<\/span><span style=\"color:#d3d0c8;\">: int,\n<\/span><span style=\"color:#d3d0c8;\">            <\/span><span style=\"color:#f2777a;\">word<\/span><span style=\"color:#d3d0c8;\">: list[int],\n<\/span><span style=\"color:#d3d0c8;\">    ) -&gt; &#39;<\/span><span style=\"color:#99cc99;\">Paragraph<\/span><span style=\"color:#d3d0c8;\">&#39;:\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#cc99cc;\">pass\n<\/span><\/code><\/pre>\n<p>And we've used a lot Rust's type system to provide an ergonomic and safe API for the rest of the codebase to handle the\ndata, allowing it to cut and slice the &quot;paragraph&quot; however necessary.<\/p>\n"},{"title":"Cooperative Buffon \u03c0","id":"d0743a38-5176-48bf-b284-aed766b4ffb4","updated":"2026-03-14T00:00:00+00:00","author":{"name":"sitegui"},"category":[{"@attributes":{"term":"maths"}},{"@attributes":{"term":"programming"}}],"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/03\/cooperative-buffon-pi","rel":"alternate"}},"published":"2026-03-14T00:00:00+00:00","content":"<p>Happy <a href=\"https:\/\/en.wikipedia.org\/wiki\/Pi_Day\">pi day<\/a>! (not to be confused with pie day, which has more pastry but is\nless sweat).<\/p>\n<p>I need your help: please refresh this page to throw more matches to the ground!<br \/>\nI'll use <a href=\"https:\/\/en.wikipedia.org\/wiki\/Buffon%27s_needle_problem\">Comte de Buffon's result<\/a> to estimate \u03c0.<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/special\/cooperative-buffon-pi.svg\" alt=\"matches on the floor\" \/><\/p>\n<p>The underlying relation is:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code><span style=\"color:#d3d0c8;\">                          2 * match_size\n<\/span><span style=\"color:#d3d0c8;\">P(match_crosses_a_line) = --------------\n<\/span><span style=\"color:#d3d0c8;\">                           \u03c0 * line_gap\n<\/span><\/code><\/pre>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<p>So \u03c0 can be estimated as:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code><span style=\"color:#d3d0c8;\">    2 * match_size\n<\/span><span style=\"color:#d3d0c8;\">\u03c0 ~ --------------\n<\/span><span style=\"color:#d3d0c8;\">     P * line_gap\n<\/span><\/code><\/pre>\n<p>In the image above <code>match_size\/line_gap<\/code> = 1\/2, so this reduces to<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code><span style=\"color:#d3d0c8;\">    1\n<\/span><span style=\"color:#d3d0c8;\">\u03c0 ~ -\n<\/span><span style=\"color:#d3d0c8;\">    P\n<\/span><\/code><\/pre>\n<p>You can\ncheck <a href=\"https:\/\/git.sitegui.dev\/sitegui\/blog\/src\/commit\/3652ce1f7f5b9293ab2cd34f0cfdf12c9ca14679\/src\/special\/cooperative_buffon_pi.rs\">the source code<\/a>\nhere.<\/p>\n<p><strong>Update<\/strong>: a friend told me that it would be nice to know which of the matches you've just thrown into the floor. So\nI've added a circle around it :)<\/p>\n"},{"title":"Understanding Bytes in Rust, one bit at a time","id":"9aaf95cc-858e-40df-bf2c-a53b8f53668f","updated":"2026-02-27T00:00:00+00:00","author":{"name":"sitegui"},"category":[{"@attributes":{"term":"programming"}},{"@attributes":{"term":"rust"}},{"@attributes":{"term":"bytes"}}],"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/02\/understanding-bytes-in-rust-one-bit-at-a-time","rel":"alternate"}},"published":"2026-02-27T00:00:00+00:00","content":"<p>In Rust, the tokio's ecosystem has a fundamental crate called <a href=\"https:\/\/crates.io\/crates\/bytes\"><code>bytes<\/code><\/a> that abstracts\nand helps dealing with bytes (you don't say!). I've indirectly used it a billion times and I thought that I had a good\nmental model of how it worked.<\/p>\n<p>So, in the spirit of the <a href=\"https:\/\/www.youtube.com\/playlist?list=PLqbS7AVVErFirH9armw8yXlE6dacF-A6z\">&quot;decrusting&quot; series<\/a>\nby the excellent Jon Gjengset, I've decided to peek behind the curtains to understand more what axum, tokio, hyper and\nthe kind do to them bytes! The code is well written, but surprisingly complex. I understand now what it does, but I\nstill don't fully grasp <em>why<\/em> it does some things in a certain way.<\/p>\n<p>I'm ready to share with you my discoveries. I hope that you are sitting, laying or squatting comfortably. This is the\nfirst post in a small series. I'm legally required by my marketing department to remind you that you can <a href=\"#end-of-page\">subscribe to\nmy low-traffic newsletter<\/a>, so that you'll know when new posts are up!<\/p>\n<p>A quick note before we start: this posts is based on the current <code>bytes<\/code> version 1.11.1.<\/p>\n<h2>What the Bytes?<\/h2>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<p>The <code>bytes<\/code> crate does many things, but its public API is quite small: it basically has two structs and two traits.\nQuoting from the documentation:<\/p>\n<ul>\n<li><code>struct Bytes<\/code>: a cheaply cloneable and sliceable chunk of contiguous memory<\/li>\n<li><code>struct BytesMut<\/code>: a unique reference to a contiguous slice of memory<\/li>\n<li><code>trait Buf<\/code>: read bytes from a buffer<\/li>\n<li><code>trait BufMut<\/code>: a trait for values that provide sequential write access to bytes<\/li>\n<\/ul>\n<p>In this series, I'll focus on the <code>Bytes<\/code> struct, because yeah, it's not as simple as one may be led to believe!<\/p>\n<h2>A compelling example<\/h2>\n<p>I would like to start concrete, with an example of what the problem that <code>Bytes<\/code> helps solve: zero-copy parsing.\nImagine that you're parsing HTTP requests and your code receives these bytes from the network:<\/p>\n<pre style=\"background-color:#111; color:#eee\">\n<span style=\"background-color:#633\">GET<\/span> <span style=\"background-color:#363\">\/post\/2026\/some-post<\/span> <span style=\"background-color:#336\">HTTP\/2<\/span>\n<span style=\"background-color:#366\">Host<\/span>: <span style=\"background-color:#636\">sitegui.dev<\/span>\n<span style=\"background-color:#366\">Accept<\/span>: <span style=\"background-color:#636\">text\/html<\/span>\n<span style=\"background-color:#366\">Referer<\/span>: <span style=\"background-color:#636\">https:\/\/sitegui.dev\/<\/span>\n<span style=\"background-color:#366\">Connection<\/span>: <span style=\"background-color:#636\">keep-alive<\/span>\n<\/pre>\n<p>You would like to efficiently parse it into a struct like this:<\/p>\n<pre style=\"background-color:#111; color:#eee\">\nstruct Request {\n    method: <span style=\"background-color:#633\">Something<\/span>,\n    path: <span style=\"background-color:#363\">Something<\/span>,\n    version: <span style=\"background-color:#336\">Something<\/span>,\n    \/\/\/ Note: headers can repeat, so I'm using a\n    \/\/\/ `Vec` not a `HashMap` to represent them\n    headers: Vec<(<span style=\"background-color:#366\">Something<\/span>, <span style=\"background-color:#636\">Something<\/span>)>,\n}\n<\/pre>\n<p>This post is not about the parsing bit, instead it's about the bytes themselves. What should we choose as <code>Something<\/code>\nabove?<\/p>\n<p>If you chose a type like <code>Vec&lt;u8&gt;<\/code>, your parser would need to allocate and copy the information into many\nmultiple instances of <code>Vec&lt;u8&gt;<\/code>. But with <code>Bytes<\/code>, all these instances are cheap to produce as they share the same\nstorage (your initial bytes are represented only once) and each instance then has a &quot;window&quot; to\nthis buffer. The underlying buffer will only be deallocated when <em>all<\/em> the instances sharing it are dropped.<\/p>\n<p>As usual in computing, using <code>Bytes<\/code> (or any &quot;shared&quot; construction) vs <code>Vec&lt;u8&gt;<\/code> (or any &quot;owned&quot;\nconstruction) is a matter of trade-offs. For example, if your parser code only produces output that references a\nsmall part of the input, it's probably better to copy over just that. Otherwise, the <code>Bytes<\/code> instances will keep the\nwhole input around in memory.<\/p>\n<h2>An initial mental model<\/h2>\n<p>As I said before, I had a useful mental model before embarking in this exploration: a <code>Bytes<\/code> instance simply has a\nreference-counted shared reference to the actual bytes and some information to where in the buffer those bytes are.\nThe fundamental operations available are:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#cc99cc;\">trait <\/span><span style=\"color:#d3d0c8;\">BytesStorage {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#747369;\">\/\/\/ Create a new instance with some data\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#747369;\">\/\/\/ It should &quot;adopt&quot; the data, not copy it\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">new<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f2777a;\">data<\/span><span style=\"color:#d3d0c8;\">: Vec&lt;<\/span><span style=\"color:#cc99cc;\">u8<\/span><span style=\"color:#d3d0c8;\">&gt;) -&gt; <\/span><span style=\"color:#cc99cc;\">Self<\/span><span style=\"color:#d3d0c8;\">;\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#747369;\">\/\/\/ Create a new instance that references some part of this data\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#747369;\">\/\/\/ Constant time, and it should not copy the actual data\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">slice<\/span><span style=\"color:#d3d0c8;\">(&amp;<\/span><span style=\"color:#f2777a;\">self<\/span><span style=\"color:#d3d0c8;\">, <\/span><span style=\"color:#f2777a;\">range<\/span><span style=\"color:#d3d0c8;\">: Range&lt;<\/span><span style=\"color:#cc99cc;\">usize<\/span><span style=\"color:#d3d0c8;\">&gt;) -&gt; <\/span><span style=\"color:#cc99cc;\">Self<\/span><span style=\"color:#d3d0c8;\">;\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#747369;\">\/\/\/ Return the bytes slice\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#747369;\">\/\/\/ Very cheap\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">as_ref<\/span><span style=\"color:#d3d0c8;\">(&amp;<\/span><span style=\"color:#f2777a;\">self<\/span><span style=\"color:#d3d0c8;\">) -&gt; &amp;[<\/span><span style=\"color:#cc99cc;\">u8<\/span><span style=\"color:#d3d0c8;\">];\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<p>We could draft a similar-in-spirit implementation using <code>Arc&lt;Vec&lt;u8&gt;&gt;<\/code>:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#cc99cc;\">pub struct <\/span><span style=\"color:#d3d0c8;\">SharedBytes {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">range<\/span><span style=\"color:#d3d0c8;\">: Range&lt;<\/span><span style=\"color:#cc99cc;\">usize<\/span><span style=\"color:#d3d0c8;\">&gt;,\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">data<\/span><span style=\"color:#d3d0c8;\">: Arc&lt;Vec&lt;<\/span><span style=\"color:#cc99cc;\">u8<\/span><span style=\"color:#d3d0c8;\">&gt;&gt;,\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#cc99cc;\">impl <\/span><span style=\"color:#d3d0c8;\">BytesStorage <\/span><span style=\"color:#cc99cc;\">for <\/span><span style=\"color:#d3d0c8;\">SharedBytes {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">new<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f2777a;\">data<\/span><span style=\"color:#d3d0c8;\">: Vec&lt;<\/span><span style=\"color:#cc99cc;\">u8<\/span><span style=\"color:#d3d0c8;\">&gt;) -&gt; <\/span><span style=\"color:#cc99cc;\">Self <\/span><span style=\"color:#d3d0c8;\">{\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#cc99cc;\">Self <\/span><span style=\"color:#d3d0c8;\">{\n<\/span><span style=\"color:#d3d0c8;\">            range: <\/span><span style=\"color:#f99157;\">0<\/span><span style=\"color:#d3d0c8;\">..data.<\/span><span style=\"color:#66cccc;\">len<\/span><span style=\"color:#d3d0c8;\">(),\n<\/span><span style=\"color:#d3d0c8;\">            data: Arc::new(data),\n<\/span><span style=\"color:#d3d0c8;\">        }\n<\/span><span style=\"color:#d3d0c8;\">    }\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">slice<\/span><span style=\"color:#d3d0c8;\">(&amp;<\/span><span style=\"color:#f2777a;\">self<\/span><span style=\"color:#d3d0c8;\">, <\/span><span style=\"color:#f2777a;\">range<\/span><span style=\"color:#d3d0c8;\">: Range&lt;<\/span><span style=\"color:#cc99cc;\">usize<\/span><span style=\"color:#d3d0c8;\">&gt;) -&gt; <\/span><span style=\"color:#cc99cc;\">Self <\/span><span style=\"color:#d3d0c8;\">{\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#747369;\">\/\/ Translate the &quot;local&quot; range to a &quot;global&quot; range\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#cc99cc;\">let<\/span><span style=\"color:#d3d0c8;\"> root_range = range.start + <\/span><span style=\"color:#f2777a;\">self<\/span><span style=\"color:#d3d0c8;\">.range.start..range.end + <\/span><span style=\"color:#f2777a;\">self<\/span><span style=\"color:#d3d0c8;\">.range.start;\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#cc99cc;\">Self <\/span><span style=\"color:#d3d0c8;\">{\n<\/span><span style=\"color:#d3d0c8;\">            range: root_range,\n<\/span><span style=\"color:#d3d0c8;\">            data: <\/span><span style=\"color:#f2777a;\">self<\/span><span style=\"color:#d3d0c8;\">.data.<\/span><span style=\"color:#66cccc;\">clone<\/span><span style=\"color:#d3d0c8;\">(),\n<\/span><span style=\"color:#d3d0c8;\">        }\n<\/span><span style=\"color:#d3d0c8;\">    }\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">as_ref<\/span><span style=\"color:#d3d0c8;\">(&amp;<\/span><span style=\"color:#f2777a;\">self<\/span><span style=\"color:#d3d0c8;\">) -&gt; &amp;[<\/span><span style=\"color:#cc99cc;\">u8<\/span><span style=\"color:#d3d0c8;\">] {\n<\/span><span style=\"color:#d3d0c8;\">        &amp;<\/span><span style=\"color:#f2777a;\">self<\/span><span style=\"color:#d3d0c8;\">.data[<\/span><span style=\"color:#f2777a;\">self<\/span><span style=\"color:#d3d0c8;\">.range.<\/span><span style=\"color:#66cccc;\">clone<\/span><span style=\"color:#d3d0c8;\">()]\n<\/span><span style=\"color:#d3d0c8;\">    }\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<p>Note how <code>new()<\/code> does not clone the data, instead the <code>Vec<\/code> is adopted into the <code>SharedBytes<\/code> instance.\nAlso, <code>slice()<\/code> clones the <code>Arc&lt;_&gt;<\/code>, which uses reference counting and avoids cloning the actual data in the <code>Vec<\/code>.<\/p>\n<p>However, <code>as_ref()<\/code> is not very good for two reasons: Rust will execute bound checks to ensure that the range is valid\nat every access, and the actual buffer is behind two memory references <code>Arc<\/code> (in <code>SharedBytes<\/code>) -&gt; <code>Vec<\/code> -&gt; buffer.<\/p>\n<p>The actual <code>Bytes<\/code> implementation avoids these two problems by storing the slice components (pointer to first byte and\nlength) as two fields directly in the struct, trading off some bloat in the struct and a small performance hit in the\n<code>slice()<\/code> operation for a simpler <code>as_slice()<\/code>:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#cc99cc;\">pub struct <\/span><span style=\"color:#d3d0c8;\">Bytes {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">ptr<\/span><span style=\"color:#d3d0c8;\">: *const <\/span><span style=\"color:#cc99cc;\">u8<\/span><span style=\"color:#d3d0c8;\">,\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#f2777a;\">len<\/span><span style=\"color:#d3d0c8;\">: <\/span><span style=\"color:#cc99cc;\">usize<\/span><span style=\"color:#d3d0c8;\">,\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#747369;\">\/\/ ... more fields ...\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#cc99cc;\">impl <\/span><span style=\"color:#d3d0c8;\">Bytes {\n<\/span><span style=\"color:#d3d0c8;\">    #[<\/span><span style=\"color:#f2777a;\">inline<\/span><span style=\"color:#d3d0c8;\">]\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">as_slice<\/span><span style=\"color:#d3d0c8;\">(&amp;<\/span><span style=\"color:#f2777a;\">self<\/span><span style=\"color:#d3d0c8;\">) -&gt; &amp;[<\/span><span style=\"color:#cc99cc;\">u8<\/span><span style=\"color:#d3d0c8;\">] {\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#cc99cc;\">unsafe <\/span><span style=\"color:#d3d0c8;\">{ slice::from_raw_parts(<\/span><span style=\"color:#f2777a;\">self<\/span><span style=\"color:#d3d0c8;\">.ptr, <\/span><span style=\"color:#f2777a;\">self<\/span><span style=\"color:#d3d0c8;\">.len) }\n<\/span><span style=\"color:#d3d0c8;\">    }\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<h2>Let's test our mental model<\/h2>\n<p>In Rust, you can set a different global memory allocator like this:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#cc99cc;\">use <\/span><span style=\"color:#d3d0c8;\">std::alloc::{GlobalAlloc, Layout, System};\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#747369;\">\/\/\/ A global allocator that can track all allocations made\n<\/span><span style=\"color:#cc99cc;\">struct <\/span><span style=\"color:#d3d0c8;\">TrackingAllocator;\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#747369;\">\/\/ Ask Rust to use our allocator as the global allocator\n<\/span><span style=\"color:#d3d0c8;\">#[<\/span><span style=\"color:#f2777a;\">global_allocator<\/span><span style=\"color:#d3d0c8;\">]\n<\/span><span style=\"color:#cc99cc;\">static <\/span><span style=\"color:#f99157;\">ALLOCATOR<\/span><span style=\"color:#d3d0c8;\">: TrackingAllocator = TrackingAllocator;\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#cc99cc;\">unsafe impl <\/span><span style=\"color:#d3d0c8;\">GlobalAlloc <\/span><span style=\"color:#cc99cc;\">for <\/span><span style=\"color:#d3d0c8;\">TrackingAllocator {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">unsafe fn <\/span><span style=\"color:#6699cc;\">alloc<\/span><span style=\"color:#d3d0c8;\">(&amp;<\/span><span style=\"color:#f2777a;\">self<\/span><span style=\"color:#d3d0c8;\">, <\/span><span style=\"color:#f2777a;\">layout<\/span><span style=\"color:#d3d0c8;\">: Layout) -&gt; *<\/span><span style=\"color:#cc99cc;\">mut u8 <\/span><span style=\"color:#d3d0c8;\">{\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#747369;\">\/\/ Do stuff\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#cc99cc;\">unsafe <\/span><span style=\"color:#d3d0c8;\">{ System.<\/span><span style=\"color:#66cccc;\">alloc<\/span><span style=\"color:#d3d0c8;\">(layout) }\n<\/span><span style=\"color:#d3d0c8;\">    }\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">unsafe fn <\/span><span style=\"color:#6699cc;\">dealloc<\/span><span style=\"color:#d3d0c8;\">(&amp;<\/span><span style=\"color:#f2777a;\">self<\/span><span style=\"color:#d3d0c8;\">, <\/span><span style=\"color:#f2777a;\">ptr<\/span><span style=\"color:#d3d0c8;\">: *<\/span><span style=\"color:#cc99cc;\">mut u8<\/span><span style=\"color:#d3d0c8;\">, <\/span><span style=\"color:#f2777a;\">layout<\/span><span style=\"color:#d3d0c8;\">: Layout) {\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#747369;\">\/\/ Do stuff\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#cc99cc;\">unsafe <\/span><span style=\"color:#d3d0c8;\">{ System.<\/span><span style=\"color:#66cccc;\">dealloc<\/span><span style=\"color:#d3d0c8;\">(ptr, layout) };\n<\/span><span style=\"color:#d3d0c8;\">    }\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<p>There\nis <a href=\"https:\/\/github.com\/rust-lang\/wg-allocators\">a Rust working group to have a more fine control of the allocator<\/a>,\nallowing us to overwrite it only for some parts of the code. But for this reduced example, it's okay to capture all\nallocations.<\/p>\n<p>I used this mechanism to track exactly which allocations and deallocations the code was doing for <code>Vec&lt;u8&gt;<\/code>, our own\n<code>SharedBytes<\/code> and <code>bytes::Bytes<\/code> and where in the source code they\nhappened. <a href=\"https:\/\/git.sitegui.dev\/sitegui\/blog-post-understanding-bytes\">You can check the full code here<\/a>,\nif you are curious or want to reproduce my work.<\/p>\n<p>To simulate the HTTP parser, I'm using this simple code:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">parse_request<\/span><span style=\"color:#d3d0c8;\">&lt;B: BytesStorage&gt;(<\/span><span style=\"color:#f2777a;\">data<\/span><span style=\"color:#d3d0c8;\">: Vec&lt;<\/span><span style=\"color:#cc99cc;\">u8<\/span><span style=\"color:#d3d0c8;\">&gt;) -&gt; Request&lt;B&gt; {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">let<\/span><span style=\"color:#d3d0c8;\"> data = B::new(data);\n<\/span><span style=\"color:#d3d0c8;\">    Request {\n<\/span><span style=\"color:#d3d0c8;\">        method: data.<\/span><span style=\"color:#66cccc;\">slice<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f99157;\">0<\/span><span style=\"color:#d3d0c8;\">..<\/span><span style=\"color:#f99157;\">3<\/span><span style=\"color:#d3d0c8;\">),\n<\/span><span style=\"color:#d3d0c8;\">        path: data.<\/span><span style=\"color:#66cccc;\">slice<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f99157;\">4<\/span><span style=\"color:#d3d0c8;\">..<\/span><span style=\"color:#f99157;\">24<\/span><span style=\"color:#d3d0c8;\">),\n<\/span><span style=\"color:#d3d0c8;\">        version: data.<\/span><span style=\"color:#66cccc;\">slice<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f99157;\">25<\/span><span style=\"color:#d3d0c8;\">..<\/span><span style=\"color:#f99157;\">31<\/span><span style=\"color:#d3d0c8;\">),\n<\/span><span style=\"color:#d3d0c8;\">        headers: vec![\n<\/span><span style=\"color:#d3d0c8;\">            (data.<\/span><span style=\"color:#66cccc;\">slice<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f99157;\">33<\/span><span style=\"color:#d3d0c8;\">..<\/span><span style=\"color:#f99157;\">37<\/span><span style=\"color:#d3d0c8;\">), data.<\/span><span style=\"color:#66cccc;\">slice<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f99157;\">39<\/span><span style=\"color:#d3d0c8;\">..<\/span><span style=\"color:#f99157;\">50<\/span><span style=\"color:#d3d0c8;\">)),\n<\/span><span style=\"color:#d3d0c8;\">            (data.<\/span><span style=\"color:#66cccc;\">slice<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f99157;\">52<\/span><span style=\"color:#d3d0c8;\">..<\/span><span style=\"color:#f99157;\">58<\/span><span style=\"color:#d3d0c8;\">), data.<\/span><span style=\"color:#66cccc;\">slice<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f99157;\">60<\/span><span style=\"color:#d3d0c8;\">..<\/span><span style=\"color:#f99157;\">69<\/span><span style=\"color:#d3d0c8;\">)),\n<\/span><span style=\"color:#d3d0c8;\">            (data.<\/span><span style=\"color:#66cccc;\">slice<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f99157;\">71<\/span><span style=\"color:#d3d0c8;\">..<\/span><span style=\"color:#f99157;\">78<\/span><span style=\"color:#d3d0c8;\">), data.<\/span><span style=\"color:#66cccc;\">slice<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f99157;\">80<\/span><span style=\"color:#d3d0c8;\">..<\/span><span style=\"color:#f99157;\">100<\/span><span style=\"color:#d3d0c8;\">)),\n<\/span><span style=\"color:#d3d0c8;\">            (data.<\/span><span style=\"color:#66cccc;\">slice<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f99157;\">102<\/span><span style=\"color:#d3d0c8;\">..<\/span><span style=\"color:#f99157;\">112<\/span><span style=\"color:#d3d0c8;\">), data.<\/span><span style=\"color:#66cccc;\">slice<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f99157;\">114<\/span><span style=\"color:#d3d0c8;\">..<\/span><span style=\"color:#f99157;\">124<\/span><span style=\"color:#d3d0c8;\">)),\n<\/span><span style=\"color:#d3d0c8;\">        ],\n<\/span><span style=\"color:#d3d0c8;\">    }\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<p>The following table shows what each line of the code above allocates for <code>Vec&lt;u8&gt;<\/code>, <code>SharedBytes<\/code> and <code>bytes::Bytes<\/code>, in\nbytes.<\/p>\n<p>But before looking at the results, take a while to run this code against your mental model and try to predict\nhow many allocations will occur for each implementation and where that will happen.<\/p>\n<table>\n<thead>\n<tr>\n<th>Code source<\/th>\n<th align=\"right\"><code>Vec&lt;u8&gt;<\/code><\/th>\n<th align=\"right\"><code>SharedBytes<\/code><\/th>\n<th align=\"right\"><code>bytes::Bytes<\/code><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>data = B::new(data)<\/code><\/td>\n<td align=\"right\"><\/td>\n<td align=\"right\">40<\/td>\n<td align=\"right\"><\/td>\n<\/tr>\n<tr>\n<td><code>method: data.slice(0..3)<\/code><\/td>\n<td align=\"right\">3<\/td>\n<td align=\"right\"><\/td>\n<td align=\"right\">24<\/td>\n<\/tr>\n<tr>\n<td><code>path: data.slice(4..24)<\/code><\/td>\n<td align=\"right\">20<\/td>\n<td align=\"right\"><\/td>\n<td align=\"right\"><\/td>\n<\/tr>\n<tr>\n<td><code>version: data.slice(25..31)<\/code><\/td>\n<td align=\"right\">6<\/td>\n<td align=\"right\"><\/td>\n<td align=\"right\"><\/td>\n<\/tr>\n<tr>\n<td><code>vec![...]<\/code><\/td>\n<td align=\"right\">192<\/td>\n<td align=\"right\">192<\/td>\n<td align=\"right\">256<\/td>\n<\/tr>\n<tr>\n<td>8x <code>data.slice(...)<\/code><\/td>\n<td align=\"right\">57<\/td>\n<td align=\"right\"><\/td>\n<td align=\"right\"><\/td>\n<\/tr>\n<tr>\n<td>Total<\/td>\n<td align=\"right\">298<\/td>\n<td align=\"right\">232<\/td>\n<td align=\"right\">280<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>As I expected, <code>Vec&lt;u8&gt;<\/code> allocates a new independent buffer in every call to <code>.slice()<\/code>, with the necessary capacity for\nthe data piece then copy it. <code>SharedBytes<\/code> allocates the <code>Arc&lt;Vec&lt;u8&gt;&gt;<\/code> once at the start, and later the\n<code>Vec&lt;(B, B)&gt;<\/code> with 8\n\u00d7 24 bytes.<\/p>\n<p>I want to explore in a separate blog post the whys and the hows <code>Arc&lt;Vec&lt;u8&gt;&gt;<\/code> has 40 bytes, and also compare it\nagainst <code>Arc&lt;[u8]&gt;<\/code>. Stay\ntuned!<\/p>\n<p>The behavior of <code>Bytes<\/code> was interesting to me for two reasons:<\/p>\n<ul>\n<li>it somehow defers the first allocation: <code>B::new()<\/code> doesn't allocate (in this case), but the first call to <code>.slice()<\/code>\ndoes. Spoiler alert: it's pretty neat but complex. I'll explore it in details later on.<\/li>\n<li>it's big! 32 bytes each instance. So <code>Vec&lt;(B, B)&gt;<\/code> has 8 \u00d7 32 bytes<\/li>\n<\/ul>\n<h2>No <code>Arc<\/code> in sight<\/h2>\n<p>Armed with the above initial mental model, it's not a surprise that the documentation and comments in the file\nimplementing <code>Bytes<\/code> mention <code>Arc<\/code> 6 times. What is a bit eyebrow-raising is that the code itself does not use <code>Arc<\/code>\nat all!<\/p>\n<p>Maybe it's related to the defered allocation we saw before? Maybe it's related to the 32 bytes needed for each instance?\nWell, kind of...<\/p>\n<p>I'll use memory diagrams like this one below to represent what the structs (in grey) are made of. Simple numerical\nfields are in yellow and pointers are in green. In parentheses, I've put the memory size in bytes, assuming a 64-bit\nsystem.<\/p>\n<p>For example, in Rust you can represent a <em>growable<\/em> owned sequence with <code>Vec&lt;T&gt;<\/code> and a <em>fixed<\/em> owned sequence\nwith <code>Box&lt;[T]&gt;<\/code>. The physical difference is that <code>Vec<\/code> may have allocated more space than it is currently using, to\namortize buffer grow on inserts, so it uses an additional <code>capacity<\/code> field. A <code>Box<\/code> uses exactly the bufffer's size, so\nlength and capacity are the same. Converting from a <code>Vec<\/code> that is &quot;full&quot; (that is, for which capacity is the same as\nlength) is cheap because the underlying buffer is reused:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/02\/understanding-bytes-in-rust-one-bit-at-a-time\/vec_to_box.png\" alt=\"Converting a Vec to Box\" \/><\/p>\n<h2>Here's a Box. Give-me Bytes<\/h2>\n<p>The implementation of <code>Bytes::from(Vec&lt;u8&gt;)<\/code> will first check if it's a &quot;full&quot; Vec to convert it into a\n<code>Box&lt;[u8]&gt;<\/code> and build a <code>Bytes<\/code> instance like below. When the <code>Vec&lt;u8&gt;<\/code> it not &quot;full&quot;, a different logic is used, but\nlet's\nput that aside for now.<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/02\/understanding-bytes-in-rust-one-bit-at-a-time\/box_to_bytes.png\" alt=\"Converting a Box to Bytes\" \/><\/p>\n<p>That's a lot to unpack. I'll explain what I've understood of the design and the trade-offs of this representation:<\/p>\n<p>First, note that the buffer is reused, as expected.<\/p>\n<p>The <code>start<\/code> and <code>length<\/code> fields simply represent the slice information. It seems a bit redundant with the <code>data<\/code>\nfield, that also points to the buffer. However, this distinction will be useful when we slice this data: the start\nof our &quot;window&quot; will be different from the buffer's start, and we need to keep the buffer's start around so that we\ncan deallocate it when the moment comes.<\/p>\n<p>The <code>vtable<\/code> field implements the &quot;dynamic dispatch&quot; pattern. Rust has &quot;trait objects&quot; to implement this pattern with\n<code>&amp;dyn SomeTrait<\/code> or <code>Box&lt;dyn SomeTrait&gt;<\/code>, but I guess the designers of the crate didn't use them because this pattern\nhas restrictions that are a deal-breaker for this use. If you know more why, please tell me!<\/p>\n<p>Note that <code>vtable<\/code> points to a statically declared instance of <code>VTable<\/code> called &quot;promotable&quot;. Aha! This name is related\nto how <code>Bytes<\/code> deferr allocation as we observed earlier.<\/p>\n<p>The <code>data<\/code> field is funny-looking (no judgements!): it's an <code>AtomicPtr&lt;_&gt;<\/code>, that unlike a normal pointer, uses atomic\noperations to read\/write to it. But why do we need atomic operations here? Spoiler alert: it's the deferred allocation\nagain. Somehow (we'll see next), this pointer will point to something else in the future.<\/p>\n<p>Continuing on the <code>data<\/code> field, it's not just a pointer. It uses the pattern of &quot;tagged pointer&quot; to store one bit of\ninformation there. I think you can already guess it: it's the deferred allocation again. This trick avoids having a\nseparate field for this, but it requires the code to ensure and check that the pointer to the buffer is always a\nmultiple of 2, so that the least bit carries no useful information and can be co-opted as a tag.<\/p>\n<h2>The other side of the river<\/h2>\n<p>Let's spring straight into action: what happens when we slice our newly-created <code>Bytes<\/code>? I've tried my best to\nconvey everything that happens in the diagram below without making it too busy. Take some time to navigate it first.<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/02\/understanding-bytes-in-rust-one-bit-at-a-time\/slice_promotable.png\" alt=\"Slicing a promotable Bytes\" \/><\/p>\n<p>First, note that calling <code>.slice(&amp;self)<\/code> the first time actually modifies the original <code>Bytes<\/code>: the previous state of\nthe instance is represented in a dashed countour, the new state uses red on the changed field: <code>data<\/code>. Naturally, only\nthis field can be modified, because <code>.slice(&amp;self)<\/code> takes a shared reference, so only internal mutation is possible,\nthrough the\n<code>AtomicPtr&lt;_&gt;<\/code>.<\/p>\n<p>As we've observed earlier, a new piece of data is allocated: an instance of the <code>Shared<\/code> struct with 24 bytes. It\ncarries information about the original buffer that will be used to deallocate (<code>buffer<\/code> and <code>capacity<\/code>). I was surprised\nto learn that in Rust the original size of the buffer is actually necessary to deallocate it. I was used to C's\n<code>free(ptr)<\/code> that clearly doesn't need it.<\/p>\n<p>The <code>Shared<\/code> struct also carries an atomic counter with the number of <code>Bytes<\/code> instances referencing this buffer. The\nimplementation doesn't use Rust's native <code>Arc<\/code>. Instead the designers preferred re-implementing it. I could not find a\ndefinitive answer to why, but I guess that's because they wanted to avoid the overhead in <code>Arc<\/code> related to the\nimplementation of &quot;weak references&quot;, that is not necessary for <code>Bytes<\/code>' use case. To implement it, <code>Arc<\/code> uses an\nadditional atomic counter and executes some extra atomic instructions when cloning and droping.<\/p>\n<p>Finally, note how the <code>vtable<\/code> pointer does not change: it's still the same &quot;promotable&quot; method list. The code uses the\ntag in the <code>data<\/code> pointer to distinguish a not-yet-promoted <code>data<\/code> (tag = 1) from a promoted <code>data<\/code> (tag = 0).<\/p>\n<p>The deferred allocation feature is a trade-off: the code is more complex because we need an <code>AtomicPtr<\/code> and a tag to\nimplement it. However, for code that creates a <code>Bytes<\/code> without actually slicing and sharing them, it avoids the <code>Shared<\/code>\nallocation entirely.<\/p>\n<h2>It's a lie<\/h2>\n<p>Let's step back a bit: the behavior above only happens when we create a <code>Bytes<\/code> from a <code>Box&lt;[u8]&gt;<\/code> or &quot;full&quot; <code>Vec&lt;u8&gt;<\/code>.\nWhen we start with a non-full <code>Vec&lt;u8&gt;<\/code>, the code will not use the deferred mechanism and instead allocates a <code>Shared<\/code>\nstruct right away. In this case, there's no tagging of the <code>data<\/code> field and a dedicated <code>vtable<\/code> is used that will not\ncheck for the tag:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/02\/understanding-bytes-in-rust-one-bit-at-a-time\/vec_to_bytes.png\" alt=\"Converting a Vec to Bytes\" \/><\/p>\n<p>This is very similar to how the simpler <code>SharedBytes<\/code> work, except that <code>Bytes<\/code> has 8 more bytes for the <code>vtable<\/code> and\n<code>Shared<\/code> has 8 fewer bytes than <code>Arc&lt;Vec&lt;u8&gt;&gt;<\/code> for the lack of weak references.<\/p>\n<h2>Bytes are versatile<\/h2>\n<p>The dynamic dispatch implemented by <code>Bytes<\/code> with the <code>vtable<\/code> and <code>data<\/code> fields effectively allows it to use custom\nimplementation for different backing storages of the data.<\/p>\n<p>We saw what happens to <code>Box<\/code> and <code>Vec<\/code>. Here's what happens when you create a <code>Bytes<\/code> from an existing statically\nallocated slice of bytes (in Rust represented as <code>&amp;'static [u8]<\/code>):<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/02\/understanding-bytes-in-rust-one-bit-at-a-time\/static_to_bytes.png\" alt=\"Converting static slice to Bytes\" \/><\/p>\n<p>Note that there is no reference counting, because in this mode, <code>Bytes<\/code> does not own the data: instead it borrows data\nthat outlives it, as indicated by the <code>'static<\/code> lifetime.<\/p>\n<p>Another interesting usage of the dynamic dispatch is to create <code>Bytes<\/code> from anything else that somehow owns a buffer.\nFor example, from a <code>memmap2::Mmap<\/code> instance that represents a memory-mapped region:<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/02\/understanding-bytes-in-rust-one-bit-at-a-time\/owned_to_bytes.png\" alt=\"Converting owned struct to Bytes\" \/><\/p>\n<p>Note that the owned struct is copied over into <code>Onwed<\/code> that, just like <code>Shared<\/code>, acts like an <code>Arc&lt;_&gt;<\/code> without the weak\nreference feature. The major distinction between <code>Owned<\/code> and <code>Shared<\/code> is that <code>Bytes<\/code> does not know how to take\nownership of the buffer, as it only requires that the given type implements <code>AsRef&lt;[u8]&gt;<\/code> to produces a\nborrowed slice of bytes.<\/p>\n<h2>Micro benchmarks<\/h2>\n<p>Never trust a benchmark you see online. So here's one more for you to ignore:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">test<\/span><span style=\"color:#d3d0c8;\">&lt;B: BytesStorage&gt;() {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">let<\/span><span style=\"color:#d3d0c8;\"> data = <\/span><span style=\"color:#66cccc;\">black_box<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#66cccc;\">example_request_package<\/span><span style=\"color:#d3d0c8;\">());\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">let<\/span><span style=\"color:#d3d0c8;\"> request = parse_request::&lt;B&gt;(data);\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#66cccc;\">assert_example_request<\/span><span style=\"color:#d3d0c8;\">(&amp;request);\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<table>\n<thead>\n<tr>\n<th><\/th>\n<th align=\"right\"><code>Vec&lt;u8&gt;<\/code><\/th>\n<th align=\"right\"><code>SharedBytes<\/code><\/th>\n<th align=\"right\"><code>bytes::Bytes<\/code><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Allocated (bytes)<\/td>\n<td align=\"right\">298<\/td>\n<td align=\"right\">232<\/td>\n<td align=\"right\">280<\/td>\n<\/tr>\n<tr>\n<td>Execution time (ns)<\/td>\n<td align=\"right\">115<\/td>\n<td align=\"right\">115<\/td>\n<td align=\"right\">150<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>So <code>Bytes<\/code> seems both chunkier and slower than my half-backed <code>SharedBytes<\/code> on this synthetic benchmark \ud83e\udee3. But of\ncourse, my implementation does not offer the same versatility.<\/p>\n<p>I searched for public benchmarks that justified some design decisions on the <code>bytes<\/code> crate, but could not find them. If\nyou know any, please tell me!<\/p>\n<p><a name=\"end-of-page\"><\/a><\/p>\n"},{"title":"Web server auto-reload in Rust","id":"76221558-958b-4072-afa3-3e602f92ac9a","updated":"2026-02-14T00:00:00+00:00","author":{"name":"sitegui"},"category":[{"@attributes":{"term":"programming"}},{"@attributes":{"term":"rust"}},{"@attributes":{"term":"axum"}}],"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/02\/web-server-auto-reload-in-rust","rel":"alternate"}},"published":"2026-02-14T00:00:00+00:00","content":"<p>This blog is written in Rust, and I wanted a way to reload the web pages automatically while I change the posts'\ncontents, styles, etc. This is common-place with JavaScript frameworks, but not automatic in the Rust land. So I've\nembarked on a side quest to achieve just that: the &quot;type and auto-reload&quot; experience. In the end, I was surprised to\nlearn a bit more about sockets and processes in Linux.<\/p>\n<p>This post is a note to myself about these nuggets that I've learned and to share the solution. It may be helpful for\nfuture me and I hope for someonelse out there.<\/p>\n<h2>TL;DR<\/h2>\n<p>You can check the solution\nhere:  <a href=\"https:\/\/git.sitegui.dev\/sitegui\/axum-web-auto-reload-example\/src\/branch\/main\/src\/main.rs\">https:\/\/git.sitegui.dev\/sitegui\/axum-web-auto-reload-example\/src\/branch\/main\/src\/main.rs<\/a>. The README in that\nrepo has some nice diagrams as well.<\/p>\n<h2>Shopping list<\/h2>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<p>To reload the browser page on a source file change, you will need:<\/p>\n<ul>\n<li>your server: I'm developing mine with <a href=\"https:\/\/docs.rs\/axum\/latest\/axum\/\">axum<\/a> in Rust<\/li>\n<li>a tool to listen to a port, pass down the socket to the server, detect file changes, and restart the server. I'm using\n<a href=\"https:\/\/github.com\/watchexec\/watchexec\"><code>watchexec<\/code><\/a><\/li>\n<li>a browser<\/li>\n<\/ul>\n<p>To run it all, I use this command:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-shell\"><span style=\"color:#d3d0c8;\">watchexec \\\n<\/span><span style=\"color:#d3d0c8;\">  --socket 8080 \\\n<\/span><span style=\"color:#d3d0c8;\">  --restart \\\n<\/span><span style=\"color:#d3d0c8;\">  --stop-signal SIGINT -- \\\n<\/span><span style=\"color:#d3d0c8;\">  cargo run\n<\/span><\/code><\/pre>\n<h2>Accepting the passed socket<\/h2>\n<p>This part is very important for smooth reloads: when the browser reloads, it will try to restablish a connection with\nyour server. However, at the same time your server is reloading and probably not yet available on the localhost\nport, causing the browser to fail immediately with a passive-aggressive message telling it was ghostest by its dearest\nfriend localhost.<\/p>\n<p><a href=\"https:\/\/github.com\/watchexec\/watchexec\/blob\/main\/doc\/socket.md\">The solution is a bit complex but quite brilliant<\/a>:\nlet's leave the socket connection to the <code>watchexec<\/code> process, which will stay alive through the session. When the\nbrowser tries to connect, the connection will not fail immediately because no process was listening. Of course,\n<code>watchexec<\/code> has no idea what to do with that incomming connection: it is <em>your<\/em> server that knows. So <code>watchexec<\/code> spawns\nyour server and pass it the socket, so that it can do its serverly stuff, like accepting connections and spitting HTML\nor whatever. To &quot;pass the socket&quot;, <code>watchexec<\/code> uses two environment variables:<\/p>\n<ul>\n<li><code>LISTEN_FDS<\/code>: the number of sockets being passed<\/li>\n<li><code>LISTEN_FDS_FIRST_FD<\/code>: the file-descriptor id of the first socket being passed<\/li>\n<\/ul>\n<p>The sockets are created with <code>ReuseAddr<\/code> and <code>ReusePort<\/code> so that the server can listen to it again.<\/p>\n<p>To make it work, your server should detect that it is called by something like <code>watchexec<\/code> and that it has received a\nsocket. I'm using the crate <a href=\"https:\/\/crates.io\/crates\/listenfd\"><code>listenfd<\/code><\/a> to help with that:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#cc99cc;\">use <\/span><span style=\"color:#d3d0c8;\">listenfd::ListenFd;\n<\/span><span style=\"color:#cc99cc;\">use <\/span><span style=\"color:#d3d0c8;\">std::error::Error;\n<\/span><span style=\"color:#cc99cc;\">use <\/span><span style=\"color:#d3d0c8;\">tokio::net::TcpListener;\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">#[<\/span><span style=\"color:#f2777a;\">tokio<\/span><span style=\"color:#d3d0c8;\">::<\/span><span style=\"color:#f2777a;\">main<\/span><span style=\"color:#d3d0c8;\">]\n<\/span><span style=\"color:#d3d0c8;\">async <\/span><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">main<\/span><span style=\"color:#d3d0c8;\">() -&gt; Result&lt;(), Box&lt;dyn Error&gt;&gt; {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#747369;\">\/\/ Use the crate `listenfd` to get the socket passed by `watchexec`\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">let<\/span><span style=\"color:#d3d0c8;\"> inherited_socket = ListenFd::from_env().<\/span><span style=\"color:#66cccc;\">take_tcp_listener<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f99157;\">0<\/span><span style=\"color:#d3d0c8;\">)?;\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">let <\/span><span style=\"color:#d3d0c8;\">(auto_refresh, listener) = <\/span><span style=\"color:#cc99cc;\">if let <\/span><span style=\"color:#d3d0c8;\">Some(inherited_socket) = inherited_socket {\n<\/span><span style=\"color:#d3d0c8;\">        println!(&quot;<\/span><span style=\"color:#99cc99;\">Listening on inherit socket<\/span><span style=\"color:#d3d0c8;\">&quot;);\n<\/span><span style=\"color:#d3d0c8;\">        inherited_socket.<\/span><span style=\"color:#66cccc;\">set_nonblocking<\/span><span style=\"color:#d3d0c8;\">(<\/span><span style=\"color:#f99157;\">true<\/span><span style=\"color:#d3d0c8;\">)?; <\/span><span style=\"color:#747369;\">\/\/ required by TcpListener::from_std()\n<\/span><span style=\"color:#d3d0c8;\">        (<\/span><span style=\"color:#f99157;\">true<\/span><span style=\"color:#d3d0c8;\">, TcpListener::from_std(inherited_socket)?)\n<\/span><span style=\"color:#d3d0c8;\">    } <\/span><span style=\"color:#cc99cc;\">else <\/span><span style=\"color:#d3d0c8;\">{\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#747369;\">\/\/ Fallback to the typical way of listening to a new port\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#cc99cc;\">let<\/span><span style=\"color:#d3d0c8;\"> listener = TcpListener::bind((&quot;<\/span><span style=\"color:#99cc99;\">127.0.0.1<\/span><span style=\"color:#d3d0c8;\">&quot;, <\/span><span style=\"color:#f99157;\">8000<\/span><span style=\"color:#d3d0c8;\">)).await?;\n<\/span><span style=\"color:#d3d0c8;\">        println!(&quot;<\/span><span style=\"color:#99cc99;\">Listening on http:\/\/<\/span><span style=\"color:#f99157;\">{}<\/span><span style=\"color:#d3d0c8;\">&quot;, listener.<\/span><span style=\"color:#66cccc;\">local_addr<\/span><span style=\"color:#d3d0c8;\">()?);\n<\/span><span style=\"color:#d3d0c8;\">        (<\/span><span style=\"color:#f99157;\">false<\/span><span style=\"color:#d3d0c8;\">, listener)\n<\/span><span style=\"color:#d3d0c8;\">    };\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<h2>Add a page script to reload<\/h2>\n<p>When <code>auto_refresh<\/code> is <code>true<\/code> in the code above, it means that we're in &quot;let's reload guys!&quot; territory.<\/p>\n<p>In my server, I'm using <a href=\"https:\/\/crates.io\/crates\/minijinja\"><code>minijinja<\/code><\/a> to render Jinja2 templates, so I do something\nlike this:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-jinja\"><span style=\"color:#d3d0c8;\">{% if AUTO_REFRESH %}\n<\/span><span style=\"color:#d3d0c8;\">&lt;script&gt;\n<\/span><span style=\"color:#d3d0c8;\">  \/\/ Connect to the server and reload when a new message is received\n<\/span><span style=\"color:#d3d0c8;\">  const eventSource = new EventSource(`\/auto_refresh`)\n<\/span><span style=\"color:#d3d0c8;\">  eventSource.addEventListener(&quot;message&quot;, () =&gt; location.reload())\n<\/span><span style=\"color:#d3d0c8;\">&lt;\/script&gt;\n<\/span><span style=\"color:#d3d0c8;\">{% endif %}\n<\/span><\/code><\/pre>\n<p>and in my main:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">main<\/span><span style=\"color:#d3d0c8;\">() {\n<\/span><span style=\"color:#d3d0c8;\">    jinja_env.<\/span><span style=\"color:#66cccc;\">add_global<\/span><span style=\"color:#d3d0c8;\">(&quot;<\/span><span style=\"color:#99cc99;\">AUTO_REFRESH<\/span><span style=\"color:#d3d0c8;\">&quot;, auto_refresh);\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<p>To tell that the browser should reload, I'm\nusing <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Server-sent_events\/Using_server-sent_events\">server-sent events (SSE)<\/a>\nwhich are pretty cool in fact! My use case here is very simple: whenever the server sends an event, reload.<\/p>\n<h2>Please call me<\/h2>\n<p>The last piece of the puzzle is to implement the <code>\/auto_refresh<\/code> SSE endpoint in the server:<\/p>\n<pre style=\"background-color:#2d2d2d;\"><code class=\"language-rust\"><span style=\"color:#cc99cc;\">use <\/span><span style=\"color:#d3d0c8;\">axum::response::sse::{Event, KeepAlive};\n<\/span><span style=\"color:#cc99cc;\">use <\/span><span style=\"color:#d3d0c8;\">axum::response::Sse;\n<\/span><span style=\"color:#cc99cc;\">use <\/span><span style=\"color:#d3d0c8;\">std::convert::Infallible;\n<\/span><span style=\"color:#cc99cc;\">use <\/span><span style=\"color:#d3d0c8;\">tokio::signal;\n<\/span><span style=\"color:#cc99cc;\">use <\/span><span style=\"color:#d3d0c8;\">tokio::sync::mpsc;\n<\/span><span style=\"color:#cc99cc;\">use <\/span><span style=\"color:#d3d0c8;\">tokio_stream::Stream;\n<\/span><span style=\"color:#cc99cc;\">use <\/span><span style=\"color:#d3d0c8;\">tokio_stream::wrappers::UnboundedReceiverStream;\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#747369;\">\/\/\/ Create a server-sent event stream that will send a single &quot;goodbye&quot; event when the server is\n<\/span><span style=\"color:#747369;\">\/\/\/ stopped.\n<\/span><span style=\"color:#cc99cc;\">pub<\/span><span style=\"color:#d3d0c8;\"> async <\/span><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">get_auto_refresh<\/span><span style=\"color:#d3d0c8;\">() -&gt; Sse&lt;impl Stream&lt;Item=Result&lt;Event, Infallible&gt;&gt;&gt; {\n<\/span><span style=\"color:#d3d0c8;\">    println!(&quot;<\/span><span style=\"color:#99cc99;\">GET \/auto_refresh<\/span><span style=\"color:#d3d0c8;\">&quot;);\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">let <\/span><span style=\"color:#d3d0c8;\">(tx, rx) = mpsc::unbounded_channel();\n<\/span><span style=\"color:#d3d0c8;\">    tokio::spawn(async <\/span><span style=\"color:#cc99cc;\">move <\/span><span style=\"color:#d3d0c8;\">{\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#66cccc;\">wait_ctrl_c<\/span><span style=\"color:#d3d0c8;\">().await;\n<\/span><span style=\"color:#d3d0c8;\">        println!(&quot;<\/span><span style=\"color:#99cc99;\">SSE: sending goodbye<\/span><span style=\"color:#d3d0c8;\">&quot;);\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#cc99cc;\">let<\/span><span style=\"color:#d3d0c8;\"> event = Event::default().<\/span><span style=\"color:#66cccc;\">data<\/span><span style=\"color:#d3d0c8;\">(&quot;<\/span><span style=\"color:#99cc99;\">goodbye<\/span><span style=\"color:#d3d0c8;\">&quot;);\n<\/span><span style=\"color:#d3d0c8;\">        <\/span><span style=\"color:#cc99cc;\">let <\/span><span style=\"color:#d3d0c8;\">_ = tx.<\/span><span style=\"color:#66cccc;\">send<\/span><span style=\"color:#d3d0c8;\">(Ok(event));\n<\/span><span style=\"color:#d3d0c8;\">    });\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">    Sse::new(UnboundedReceiverStream::new(rx)).<\/span><span style=\"color:#66cccc;\">keep_alive<\/span><span style=\"color:#d3d0c8;\">(KeepAlive::new())\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><span style=\"color:#d3d0c8;\">\n<\/span><span style=\"color:#d3d0c8;\">async <\/span><span style=\"color:#cc99cc;\">fn <\/span><span style=\"color:#6699cc;\">wait_ctrl_c<\/span><span style=\"color:#d3d0c8;\">() {\n<\/span><span style=\"color:#d3d0c8;\">    <\/span><span style=\"color:#cc99cc;\">let <\/span><span style=\"color:#d3d0c8;\">_ = signal::ctrl_c().await;\n<\/span><span style=\"color:#d3d0c8;\">}\n<\/span><\/code><\/pre>\n<p>And that's it! For sure, the JavaScript ecosystem has perfected this integration of different pieces into a better\nexperience, but now I can enjoy it also. Maybe as a next step I can package all the different pieces into a single\ncrate?<\/p>\n"},{"title":"The 15-game blew my mind","id":"7a0ba4ae-54da-4400-9940-28ff4ae721fb","updated":"2026-02-11T00:00:00+00:00","author":{"name":"sitegui"},"category":{"@attributes":{"term":"maths"}},"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/02\/the-15-game-blew-my-mind","rel":"alternate"}},"published":"2026-02-11T00:00:00+00:00","content":"<p>I've just watched the calmly titled <a href=\"https:\/\/www.youtube.com\/watch?v=UafhPUOCM1E\">&quot;The 15-game&quot;<\/a> in the Numberphile\nYoutube channel and boy oh boy, I cannot stop thinking about it!<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/02\/the-15-game-blew-my-mind.png\" alt=\"The video thumbnail\" \/><\/p>\n<p>I will not spoil the end, but if you ever got intrigued by games, maths, brain-teasers, just take a 15-minute break and\nwatch it. At school, I loved to solve the same problem from different angles, not only to increase my chances of getting\nthe good answer, but also because it was fun. This video hit right there in my heart.<\/p>\n<p>One of my first programs was a game of <span style=\"color:black;background-color:black\">tic-tac-toe<\/span>. If only\nsomeone had shown me the 15-game back then!<\/p>\n<p>I choose 5. Your turn!<\/p>\n"},{"title":"Welcome Framework Laptop","id":"23c517d2-1021-4df4-bb6a-d9ba95f8f649","updated":"2026-01-29T00:00:00+00:00","author":{"name":"sitegui"},"category":{"@attributes":{"term":"hardware"}},"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/01\/welcome-framework","rel":"alternate"}},"published":"2026-01-29T00:00:00+00:00","content":"<p>Around 3 years ago my phone's screen broke and changing the screen would cost close to a half the device's original\nprice. It was cheaper to throw away and buy a new one.<\/p>\n<p>This is clearly wasteful, but I guess this is a typical experience with well-known consumer brands.\nBut hey, I don't want to move to a new home because my sink broke!<\/p>\n<p>In Europe, new regulations\nlike <a href=\"https:\/\/commission.europa.eu\/law\/law-topic\/consumer-protection-law\/directive-repair-goods_en\">Right-to-Repair Directive<\/a>\nrepresent a smart step to force the hand of manufacturers. It requires them, for example, to keep spare parts stocked\nfor at least 10 years. In France, I always check\nthe <a href=\"https:\/\/www.ecologie.gouv.fr\/politiques-publiques\/indice-reparabilite\">repairability index<\/a> before buying. I'm more\nthan willing to pay a bonus price for good product design and engineering, that respects the resources and costumers.<\/p>\n<p>So I've begrudgingly replaced my phone. But this time I wanted to break the wasteful cycle, so I've\nbought <a href=\"https:\/\/www.fairphone.com\/\">a Fairphone<\/a>, which promises 10 year software and hardware support. Also, if\nsomething breaks I can just order a replacement and repair it myself. So far, I'm pretty happy with the experience! It\nworks, it just does. I feel soon I'll replace my battery, and that's it: it will probably stick with me a bunch more\nyears.<\/p>\n<p>Since my old laptop became my home server, I needed a new one for hacking on the loose. Following a similar strategy,\nI'm going with\n<a href=\"https:\/\/frame.work\/\">Framework<\/a> for my new rig.<\/p>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<p>They send the machine in a very simple DIY kit. It's a bit too easy for those used to tinker: snap, screw, click, boom!\nBut it's a genius brand\nmove to prove less experienced people that they can do it too.<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/01\/welcome-framework-2.jpg\" alt=\"Framework DYI laptop in fragments\" \/><\/p>\n<p>The final result is pretty good. Come back in 3 years and I'll share my thoughts.<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/01\/welcome-framework-3.jpg\" alt=\"Framework 13 laptop\" \/><\/p>\n<p><span style=\"font-size: smaller\">sent from my framework<\/span><\/p>\n"},{"title":"Nice people don't play board games for 27 straight hours","id":"6f2ab63e-a870-4a63-91f1-e79352a15c11","updated":"2026-01-22T00:00:00+00:00","author":{"name":"sitegui"},"category":{"@attributes":{"term":"boardgame"}},"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/01\/28h-board-games","rel":"alternate"}},"published":"2026-01-22T00:00:00+00:00","content":"<p>They play for <strong>28<\/strong> hours! We did it and it was pretty cool o\/<\/p>\n<p>Well... not all those hours were spent actually playing, we also had to decide what to play next! With more than 500\ngames available at the event, this sometimes took a while :)<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/01\/28h-board-games-2.jpeg\" alt=\"a display with lots of board games\" \/><\/p>\n<p>You can picture 3 rooms like the one below, full of meeples, cards, coins and fun.<\/p>\n<p><img src=\"https:\/\/sitegui.dev\/post\/2026\/01\/28h-board-games-1.jpeg\" alt=\"a room full of people playing board games\" \/><\/p>\n<p><a name=\"continue-reading\" class=\"continue-reading\"><\/a><\/p>\n<p>It was the 16th edition of the festival <em>24h Chaud les Jeux!<\/em> in <a href=\"https:\/\/www.cholet.fr\/\">Cholet<\/a>.\nThe festival name (and the association behind it) is a well-chosen self-referential pun! It translates to something like\n&quot;hot for games&quot;, and the <em>chaud les<\/em> sounds exactly the same as the city's name. French people, especially <a href=\"https:\/\/tif.hair\/\">hairdressing\nowners<\/a>, love their <em>jeu de mots<\/em>.<\/p>\n<p>The festival is made not only for hardcore gamers: it's open for anyone that passes by. I always find it heart-warming\nto see friends and family sitting around, just enjoying their company on a Sunday afternoon, while they fight furiously\nfor the control of Tokyo, or accuse each other of being a Moriarty malfeasant ready to explode the Big Ben, or just\ncasually arranging their azulejos the best they can.<\/p>\n<p>For my part, I've managed to discover 9 new games and play other 2 that I already knew. The big ones were Endeavor: age\nof sail, Coming of age, Nemesis. I like them and would happily play another time.<\/p>\n<p>Playing Nemesis for the first time at 08:00 was a difficult task hhaha! After 3 hours, we all exploded in space before\ncoming back to Earth because one of the players decided that it HAD to auto-destroy the spaceship in order to content\ntheir desire for chaos! It was a nice play though, we were all dead and happy (and ready\nfor lunch).<\/p>\n<p>We made good choices of lighter party games to cool down: Decrypto, Nosferatu and Cabanga!.<\/p>\n<p>A big Thank You! for the game club <a href=\"https:\/\/sectionjeuxasptt.wordpress.com\/\">Chaud les jeux<\/a> for all the time and effort\nyou've put into this event. I'll be happy to see you again next time.<\/p>\n"},{"title":"Hello world 2026","id":"e654d01f-b5f6-49c8-9417-f09b06d011a2","updated":"2026-01-01T00:00:00+00:00","author":{"name":"sitegui"},"link":{"@attributes":{"href":"https:\/\/sitegui.dev\/post\/2026\/01\/hello-world-2026","rel":"alternate"}},"published":"2026-01-01T00:00:00+00:00","content":"<p><a href=\"https:\/\/web.archive.org\/web\/20110715000000*\/sitegui.com.br\/blog\">I used to blog as a kid back in the 2010s<\/a>, and for a\nlong time I wanted to get back to it. Now it's the time! This is my personal space on the web, it's not the shiniest,\nnot the most visited, but it is mine :)<\/p>\n<p>For now there isn't much to see here, this first post is mostly a hack to get me going.<\/p>\n<p class=\"fun\">See you next time!<\/p>\n"}]}