<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Abhiseka Susanto on Medium]]></title>
        <description><![CDATA[Stories by Abhiseka Susanto on Medium]]></description>
        <link>https://medium.com/@susantoabhiseka?source=rss-8969532775a6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Eg5kvVUNj_maU6jT</url>
            <title>Stories by Abhiseka Susanto on Medium</title>
            <link>https://medium.com/@susantoabhiseka?source=rss-8969532775a6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 01:12:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@susantoabhiseka/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Achieving a Fast, Cache‑Backed Endpoint]]></title>
            <link>https://medium.com/@susantoabhiseka/achieving-a-fast-cache-backed-endpoint-b84ba6166f8f?source=rss-8969532775a6------2</link>
            <guid isPermaLink="false">https://medium.com/p/b84ba6166f8f</guid>
            <dc:creator><![CDATA[Abhiseka Susanto]]></dc:creator>
            <pubDate>Tue, 19 May 2026 06:46:06 GMT</pubDate>
            <atom:updated>2026-05-19T06:46:06.377Z</atom:updated>
            <content:encoded><![CDATA[<p>I had a Django endpoint that looked <strong>harmless </strong>which returned a list of activities for a table view: GET api/kegiatan<br>Originally the list was simple and didn’t include nested relations.<br>Then the frontend needed kaprodi and guru_besarinfo inside the list rows. That change introduced a performance risk: <strong>N+1 queries</strong> if we didn’t <strong>prefetch</strong>.</p><h3>Step 1: Keep the list slim</h3><p>The list only needs summary fields which are displayed in a table, so I can reduce the response to just:</p><ul><li>periode</li><li>kaprodi_list</li><li>guru_besar_list ,</li></ul><p>while leaving out surat_pernyataan and laporan_kegiatan(the previous implementation had these in the payload). This change looks simple, but in reality, it’s something we often <strong>overlook</strong>. Because why would you need to bring a passport, clothes, snacks, and books to a trip when you can just bring passport and clothes (the necessary ones)?</p><p>In my case, doing just this removes the need to run <strong>two </strong>SQL queries, but <strong>imagine </strong>how significant the impact would be if it were a much bigger and more complex query/data.</p><h3>Step 2: Prefetching the data</h3><p>Once the list included relations, we would get N+1 if we accessed them without prefetch. So that’s what I did:</p><pre>qs = (<br>    KegiatanModel.objects.select_related(&quot;periode&quot;)<br>    .prefetch_related(<br>        Prefetch(<br>            &quot;kaprodi_relasi&quot;,<br>            KegiatanKaprodiModel.objects.select_related(&quot;kaprodi&quot;),<br>        ),<br>        Prefetch(<br>            &quot;guru_besar_relasi&quot;,<br>            KegiatanGuruBesarModel.objects.select_related(&quot;guru_besar&quot;),<br>        ),<br>    )<br>)</pre><p>Now you might be wondering, what’s the difference of select_related and prefetch_related and why use both of them when we know they both do the same thing regardless?</p><p>I’m not going to go to full detail about the difference of those two, because that’s a topic for another day, but simply put:</p><ul><li>select_related = “<strong>join it now</strong>”, Django asks the database to <strong>join </strong>the related table in the same query which makes it the best for <strong>one‑to‑one / foreign key </strong>(one object per row).</li><li>prefetch_related = “<strong>fetch it later in bulk</strong>”, Django runs a <strong>second query</strong> to get all related rows at once, then matches them in Python which makes it the best for <strong>many‑to‑many / one‑to‑many </strong>(multiple related rows per parent).</li></ul><p>Here’s a simple visualization:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*u0mDI32b8SpGkAiSWvSQuA.png" /></figure><p>Each kegiatanhas exactly one periode. So we use select_related so that JOIN gives you one clean row per kegiatan, no duplicates.</p><p>In another hand, each kegiatancan have many kaprodi / guru_besar . A JOIN would duplicate the kegiatan row for every participant and explode the result size.</p><p>Using only one method everywhere would either:</p><ul><li>waste queries (prefetch_related for FK), or</li><li>blow up the row count (select_related for many‑to‑many)</li></ul><p>So the combination gives you the best performance with the correct data shape.</p><p>I wanna do a little flexing, so here’s the comparison before vs after the prefetching:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fw6RUQEUNg7Mkf1RxAgkTA.png" /><figcaption>No prefetching</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*48frlns5O6sMG4bTCnTiNQ.png" /><figcaption>With prefetching</figcaption></figure><p>BIG changes.</p><h3>Step 3: Cache the list response</h3><p>Muscles, cars, ovens, food all need <strong>warming up</strong>, and even endpoint responses. Maybe not the same “warming up” you’d imagine, but basically: first time fetching (we will call it a “<strong>cold run</strong>”) will have a <strong>significantly longer </strong>process time and <strong>more </strong>SQL queries than the subsequent fetches (let’s call it a “<strong>warm run</strong>”).</p><p>This is done by caching:</p><ul><li>versioned key (user + role + params)</li><li>300 second TTL (time-to-live)</li><li>invalidated on create/update,</li></ul><p>which basically means that it <strong>temporarily saves the data </strong>from the first fetch for later so that the next fetching experience could be <strong>faster </strong>and much nicer.<br>Here I present to you, the cold run vs warm run comparison:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*48frlns5O6sMG4bTCnTiNQ.png" /><figcaption>Cold run</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/993/1*pfgj5SWAYGUSjPLCxHxM4Q.png" /><figcaption>Warm run</figcaption></figure><p>Cold run needs to fetch basically everything. On warm cache hits, the list endpoint does <strong>zero </strong>DB queries (outside of getting the current user). Insane improvement, no?</p><p>If you’ve made it this far, thank you for reading my mediocre writing of such a complex issue. So, here’s a reward for you:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/370/0*8AKMVHEJUmXlAHRD" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b84ba6166f8f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>