<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://masonlyons.net/feed.xml" rel="self" type="application/atom+xml" /><link href="https://masonlyons.net/" rel="alternate" type="text/html" /><updated>2026-09-04T09:45:53+00:00</updated><id>https://masonlyons.net/feed.xml</id><title type="html">Mason Lyons</title><subtitle>Occasional posts about programming, data and tech.</subtitle><author><name>Mason Lyons</name></author><entry><title type="html">Fixing a caching bug in Wagtail CMS</title><link href="https://masonlyons.net/fixing-a-caching-bug-in-wagtail" rel="alternate" type="text/html" title="Fixing a caching bug in Wagtail CMS" /><published>2026-09-03T00:00:00+00:00</published><updated>2026-09-03T00:00:00+00:00</updated><id>https://masonlyons.net/fixing-a-caching-bug-in-wagtail</id><content type="html" xml:base="https://masonlyons.net/fixing-a-caching-bug-in-wagtail"><![CDATA[<p>For the nearly 10 years I’ve been using Django and the 5 years I’ve been using <a href="https://wagtail.org/">Wagtail CMS</a>, I’ve always wanted to contribute to open source. I’ve previously tried with a micro-optimisation in Django but it was rightly rejected. After first considering a monkey patch, I’ve finally made my first contribution.</p>

<p>Back in June I moved the Django caches on my employer project <a href="https://www.corsair.com/uk/en/explorer/">(Corsair Explorer)</a> from a self-hosted Redis box onto ElastiCache Serverless and pointed some <code class="language-plaintext highlighter-rouge">django.core.cache.backends.locmem.LocMemCache</code> caches at the new ElastiCache service. The initial box was a basic <code class="language-plaintext highlighter-rouge">t3a.small</code> AWS instance that was also hosting an Elasticsearch instance.</p>

<p>Response times got worse after the changeovers but the extra reliability / worker memory savings it gave us made it seem an overall positive change. My assumption was the new network latency was causing the performance regression.</p>

<p>A couple weeks ago I couldn’t shake the feeling of something being off. Even with extra network latency, the better cache hit ratio should be making up for that on the averages. Thankfully we push our ALB access logs into ClickHouse, so there were a few hundred million rows to query rather than going on vibes. Using Claude to parse and summarise the data I was left with this:</p>

<table>
  <thead>
    <tr>
      <th> </th>
      <th>p50</th>
      <th>p90</th>
      <th>p99</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>before</td>
      <td>78ms</td>
      <td>163ms</td>
      <td>288ms</td>
    </tr>
    <tr>
      <td>after</td>
      <td>95ms</td>
      <td>209ms</td>
      <td>373ms</td>
    </tr>
  </tbody>
</table>

<p>We were still serving requests quicker than our targets but it could be faster.</p>

<p>My initial guess was image renditions as I’ve seen these take up a large amount of queries in Streamfields in the past using Django Debug Toolbar. Wagtail caches them, most of our pages have a lot of images, and we’d swapped a near instant in-process cache for a TLS connection to another host. I patched the cache backend locally and saw some pages were doing 4 reads against 40 writes.</p>

<h2 id="why">Why</h2>

<p>Pointing Claude at the Wagtail <code class="language-plaintext highlighter-rouge">AbstractImage</code> source code quickly led to the issue.</p>

<p>Here is <code class="language-plaintext highlighter-rouge">get_rendition()</code> in <code class="language-plaintext highlighter-rouge">wagtail/images/models.py</code>:</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">try</span><span class="p">:</span>
    <span class="n">rendition</span> <span class="o">=</span> <span class="bp">self</span><span class="p">.</span><span class="n">find_existing_rendition</span><span class="p">(</span><span class="nb">filter</span><span class="p">)</span>
<span class="k">except</span> <span class="n">Rendition</span><span class="p">.</span><span class="n">DoesNotExist</span><span class="p">:</span>
    <span class="n">rendition</span> <span class="o">=</span> <span class="bp">self</span><span class="p">.</span><span class="n">create_rendition</span><span class="p">(</span><span class="nb">filter</span><span class="p">)</span>
    <span class="c1"># Reuse this rendition if requested again from this object
</span>    <span class="bp">self</span><span class="p">.</span><span class="n">_add_to_prefetched_renditions</span><span class="p">(</span><span class="n">rendition</span><span class="p">)</span>

<span class="n">cache_key</span> <span class="o">=</span> <span class="n">Rendition</span><span class="p">.</span><span class="n">construct_cache_key</span><span class="p">(</span>
    <span class="bp">self</span><span class="p">,</span> <span class="nb">filter</span><span class="p">.</span><span class="n">get_cache_key</span><span class="p">(</span><span class="bp">self</span><span class="p">),</span> <span class="nb">filter</span><span class="p">.</span><span class="n">spec</span>
<span class="p">)</span>
<span class="n">Rendition</span><span class="p">.</span><span class="n">cache_backend</span><span class="p">.</span><span class="nb">set</span><span class="p">(</span><span class="n">cache_key</span><span class="p">,</span> <span class="n">rendition</span><span class="p">)</span>
</code></pre></div></div>

<p>The <code class="language-plaintext highlighter-rouge">set()</code> at the bottom isn’t conditional on anything. It runs for every image, regardless of where the rendition came from.</p>

<p><code class="language-plaintext highlighter-rouge">get_renditions()</code>, the plural version a bit further down the same file, does this instead:</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Update the cache
</span><span class="n">cache_additions</span> <span class="o">=</span> <span class="p">{</span>
    <span class="n">Rendition</span><span class="p">.</span><span class="n">construct_cache_key</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span> <span class="nb">filter</span><span class="p">.</span><span class="n">get_cache_key</span><span class="p">(</span><span class="bp">self</span><span class="p">),</span> <span class="nb">filter</span><span class="p">.</span><span class="n">spec</span>
    <span class="p">):</span> <span class="n">rendition</span>
    <span class="k">for</span> <span class="nb">filter</span><span class="p">,</span> <span class="n">rendition</span> <span class="ow">in</span> <span class="n">renditions</span><span class="p">.</span><span class="n">items</span><span class="p">()</span>
    <span class="c1"># prevent writing of cached data back to the cache
</span>    <span class="k">if</span> <span class="ow">not</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">rendition</span><span class="p">,</span> <span class="s">"_from_cache"</span><span class="p">,</span> <span class="bp">False</span><span class="p">)</span>
<span class="p">}</span>
<span class="k">if</span> <span class="n">cache_additions</span><span class="p">:</span>
    <span class="n">Rendition</span><span class="p">.</span><span class="n">cache_backend</span><span class="p">.</span><span class="n">set_many</span><span class="p">(</span><span class="n">cache_additions</span><span class="p">)</span>
</code></pre></div></div>

<p>There’s a <code class="language-plaintext highlighter-rouge">_from_cache</code> flag, and the plural method checks it before writing. The singular one, which is what the <code class="language-plaintext highlighter-rouge">{% image %}</code> tag ends up calling, doesn’t.</p>

<h2 id="the-fix">The fix</h2>

<p>A super simple change that mimics the plural version.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># prevent writing of cached data back to the cache
</span><span class="k">if</span> <span class="ow">not</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">rendition</span><span class="p">,</span> <span class="s">"_from_cache"</span><span class="p">,</span> <span class="bp">False</span><span class="p">):</span>
    <span class="n">cache_key</span> <span class="o">=</span> <span class="n">Rendition</span><span class="p">.</span><span class="n">construct_cache_key</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span> <span class="nb">filter</span><span class="p">.</span><span class="n">get_cache_key</span><span class="p">(</span><span class="bp">self</span><span class="p">),</span> <span class="nb">filter</span><span class="p">.</span><span class="n">spec</span>
    <span class="p">)</span>
    <span class="n">Rendition</span><span class="p">.</span><span class="n">cache_backend</span><span class="p">.</span><span class="nb">set</span><span class="p">(</span><span class="n">cache_key</span><span class="p">,</span> <span class="n">rendition</span><span class="p">)</span>
</code></pre></div></div>

<p>While getting Claude to write unit tests after reviewing my PR, Matt Westcott spotted another issue:</p>

<blockquote>
  <p>Thanks @MasonLyons! Getting Claude to write unit tests for this has revealed a secondary issue - the _from_cache flag was only set when the result came from a prefetch query, not when it actually came from the cache. I’ll combine that fix (+ tests) with this one and merge.</p>
</blockquote>

<p>So the check in <code class="language-plaintext highlighter-rouge">get_renditions()</code> had never worked for cache hits. Thankfully the docs tell you to do <code class="language-plaintext highlighter-rouge">prefetch_related("thumbnail__renditions")</code>, so we were safe from the issue Matt fixed. But it does explain why there was such a large imbalance between cache reads and writes. Prefetching means the renditions are already in memory so the cache never gets read, but still written to.</p>

<p>My <a href="https://github.com/wagtail/wagtail/pull/14541">PR</a>, merged as <a href="https://github.com/wagtail/wagtail/commit/c1c39fe74e339a05795763d30a6a50bb8087a107">c1c39fe</a>, should be going out in version 8.1.</p>

<h2 id="reflection">Reflection</h2>

<p>In a world full of vibe coding and AI slop it’s easy to just hand an LLM the keys and let it go to town. But while Claude very likely could have found the issue itself eventually, even with the ever increasing number of bots trying to spam PRs on open source projects and Wagtail being popular, it still needed a human touch to find it and fix it. LLMs still feel like a floor raiser and force multiplier. I had to steer it in roughly the right direction and Matt only got it to spot the issues when using it to write unit tests.</p>

<p>Thanks to Matt Westcott for being so quick with reviewing the initial PR.</p>]]></content><author><name>Mason Lyons</name></author><category term="Python" /><category term="Django" /><category term="Wagtail" /><category term="open-source" /><category term="performance" /><category term="cache" /><summary type="html"><![CDATA[Fixing a weird three year old caching bug in Wagtail only shown by a cache change.]]></summary></entry><entry><title type="html">How to add a custom language to Django</title><link href="https://masonlyons.net/how-to-add-a-new-language-to-django" rel="alternate" type="text/html" title="How to add a custom language to Django" /><published>2024-12-13T00:00:00+00:00</published><updated>2024-12-13T00:00:00+00:00</updated><id>https://masonlyons.net/how-to-add-a-new-language-to-django</id><content type="html" xml:base="https://masonlyons.net/how-to-add-a-new-language-to-django"><![CDATA[<p>When working on my employer project <a href="https://www.corsair.com/uk/en/explorer/">(Corsair Explorer)</a> we had the need to add a new language not currently supported by Django.
We’re not providing full translation this language but we do need to provide it as an option for some content.</p>

<hr />
<p>My typical layout for django projects is to have a main <code class="language-plaintext highlighter-rouge">core</code> app that houses 99% of the logic and code.
Although breaking up logic and models into apps is <em>suggested</em> it’s very, very rare i reuse an app in a different project so this gives the easiest development experience for me.
The below is part of the <code class="language-plaintext highlighter-rouge">appy.py</code> file inside the core app in the project.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># core/apps.py
</span><span class="kn">from</span> <span class="nn">django.apps</span> <span class="kn">import</span> <span class="n">AppConfig</span>
<span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span>
<span class="kn">import</span> <span class="nn">django.conf.locale</span>


<span class="k">class</span> <span class="nc">CoreConfig</span><span class="p">(</span><span class="n">AppConfig</span><span class="p">):</span>
    <span class="n">default_auto_field</span> <span class="o">=</span> <span class="s">"django.db.models.BigAutoField"</span>
    <span class="n">name</span> <span class="o">=</span> <span class="s">"core"</span>

    <span class="k">def</span> <span class="nf">ready</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">django</span><span class="p">.</span><span class="n">conf</span><span class="p">.</span><span class="n">locale</span><span class="p">.</span><span class="n">LANG_INFO</span><span class="p">[</span><span class="s">"mt"</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s">"bidi"</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
            <span class="s">"code"</span><span class="p">:</span> <span class="s">"mt"</span><span class="p">,</span>
            <span class="s">"name"</span><span class="p">:</span> <span class="s">"Maltese"</span><span class="p">,</span>
            <span class="s">"name_local"</span><span class="p">:</span> <span class="s">"Malti"</span><span class="p">,</span>
        <span class="p">}</span>
</code></pre></div></div>
<p>It’s key that the <code class="language-plaintext highlighter-rouge">django.conf.locale.LANG_INFO</code> key matches up with the <code class="language-plaintext highlighter-rouge">code</code> value, in this case <code class="language-plaintext highlighter-rouge">mt</code>.
The other keys are for the following:</p>
<ul>
  <li>bidi: languages that are essentially written and read from right-to-left.</li>
  <li>code: how the url will appear in the url. For example <code class="language-plaintext highlighter-rouge">https://example.come/mt/blog</code></li>
  <li>name: The name of the language in english</li>
  <li>name_locale: The name of the language as written in the language</li>
</ul>

<p>This can then be referenced in your <code class="language-plaintext highlighter-rouge">settings.py</code> as you’d normally do for a language</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">LANGUAGES</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">...</span>
    <span class="p">(</span><span class="s">"mt"</span><span class="p">,</span> <span class="s">"Maltese"</span><span class="p">),</span>
    <span class="p">...</span>
<span class="p">]</span>
</code></pre></div></div>]]></content><author><name>Mason Lyons</name></author><category term="Python" /><category term="Django" /><category term="Translations" /><summary type="html"><![CDATA[Learn how to seamlessly add custom languages to your Django project.]]></summary></entry></feed>