<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mathieu</title>
    <description>The latest articles on DEV Community by Mathieu (@mathorde).</description>
    <link>https://dev.to/mathorde</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4068452%2F3df5ce00-42af-4905-905f-e49c17b4139e.jpeg</url>
      <title>DEV Community: Mathieu</title>
      <link>https://dev.to/mathorde</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mathorde"/>
    <language>en</language>
    <item>
      <title>A missing ORDER BY cost us Thousands in API calls</title>
      <dc:creator>Mathieu</dc:creator>
      <pubDate>Tue, 18 Aug 2026 11:48:45 +0000</pubDate>
      <link>https://dev.to/mathorde/a-missing-order-by-cost-us-thousands-in-api-calls-5elg</link>
      <guid>https://dev.to/mathorde/a-missing-order-by-cost-us-thousands-in-api-calls-5elg</guid>
      <description>&lt;p&gt;In 2022, I fixed one of the most expensive one-line bugs we've shipped.&lt;/p&gt;

&lt;p&gt;The fix was this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gd"&gt;- Area.all()
&lt;/span&gt;&lt;span class="gi"&gt;+ Area.query().orderBy('id')
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it.&lt;/p&gt;

&lt;p&gt;A missing SQL ORDER BY was causing identical requests to generate different Redis cache keys.&lt;/p&gt;

&lt;p&gt;The application worked. Redis worked. MySQL worked.&lt;br&gt;
But our Google Maps API bill had climbed to around €3,500 per year.&lt;/p&gt;
&lt;h2&gt;
  
  
  The context
&lt;/h2&gt;

&lt;p&gt;I'm the co-founder and CTO of &lt;a href="https://mycater.fr" rel="noopener noreferrer"&gt;MyCater&lt;/a&gt;, a B2B catering marketplace.&lt;/p&gt;

&lt;p&gt;One of the things our backend needs to determine is which caterers can deliver to a customer's address.&lt;/p&gt;

&lt;p&gt;These checks happen several times during the ordering process, so we had a Redis cache in front of our distance calculations to avoid repeating expensive external API calls.&lt;/p&gt;

&lt;p&gt;The cache key was generated from the destination and the caterers addresses we wanted to check.&lt;/p&gt;

&lt;p&gt;The old code looked roughly like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;serializedAreas&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;method&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;areasGeocodes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;area&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;areasGeocodes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;serializedAreas&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;area&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;redisKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`distance_matrix:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nf"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;destination&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;serializedAreas&lt;/span&gt;
&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It looked deterministic.&lt;/p&gt;

&lt;p&gt;Same destination + same areas = same cache key.&lt;/p&gt;

&lt;p&gt;Except we forgot to add an ORDER BY.&lt;/p&gt;

&lt;h2&gt;
  
  
  SQL wasn't returning the areas in a guaranteed order
&lt;/h2&gt;

&lt;p&gt;The areas originally came from:&lt;br&gt;
&lt;code&gt;await Area.all()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Area 1
Area 2
Area 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Would give this hash: &lt;code&gt;destination + 1 + 2 + 3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When you test it manually, it looks like it returns them in order but without an ORDER BY, row order isn't guaranteed.&lt;/p&gt;

&lt;p&gt;So the hash for 3 Areas could look either like:&lt;br&gt;
&lt;code&gt;destination + 1 + 2 + 3&lt;/code&gt;&lt;br&gt;
&lt;code&gt;destination + 1 + 3 + 2&lt;/code&gt;&lt;br&gt;
&lt;code&gt;destination + 2 + 1 + 3&lt;/code&gt;&lt;br&gt;
&lt;code&gt;destination + 2 + 3 + 1&lt;/code&gt;&lt;br&gt;
&lt;code&gt;destination + 3 + 1 + 2&lt;/code&gt;&lt;br&gt;
&lt;code&gt;destination + 3 + 2 + 1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Giving a different string -&amp;gt; different hash -&amp;gt; different Redis key.&lt;br&gt;
Generating an extra API call.&lt;/p&gt;

&lt;p&gt;Our mistake was allowing an unordered database result to become part of something that absolutely needed to be deterministic.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix
&lt;/h2&gt;

&lt;p&gt;The production fix was incredibly small:&lt;br&gt;
&lt;code&gt;await Area.query().orderBy('id')&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why this bug is interesting
&lt;/h2&gt;

&lt;p&gt;Nothing crashed, no 500 errors.&lt;br&gt;
MySQL returned exactly what we asked for.&lt;br&gt;
Every component was working.&lt;br&gt;
That's why the bug survived long enough to become expensive. And as the number of caterers grew, the problem became more costly.&lt;/p&gt;

&lt;p&gt;At the time, our Google Maps API usage was costing us roughly €3,500 per year.&lt;/p&gt;

&lt;p&gt;Fixing this cache issue reduced unnecessary distance API calls  bringing the bill to around €100/month.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>api</category>
    </item>
  </channel>
</rss>
