<?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 Anand Chandrabhanu on Medium]]></title>
        <description><![CDATA[Stories by Anand Chandrabhanu on Medium]]></description>
        <link>https://medium.com/@anandc999?source=rss-ffede9ebbff------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*mlMd2lusX1Pv1eD4.jpg</url>
            <title>Stories by Anand Chandrabhanu on Medium</title>
            <link>https://medium.com/@anandc999?source=rss-ffede9ebbff------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 00:20:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anandc999/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[Optimizing for speed using Cloudflare workers]]></title>
            <link>https://medium.com/ounass/optimizing-for-speed-using-cloudflare-workers-ca5ed3ad512e?source=rss-ffede9ebbff------2</link>
            <guid isPermaLink="false">https://medium.com/p/ca5ed3ad512e</guid>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[cache-control]]></category>
            <category><![CDATA[cache]]></category>
            <category><![CDATA[cloudflare-workers]]></category>
            <category><![CDATA[cloudflare]]></category>
            <dc:creator><![CDATA[Anand Chandrabhanu]]></dc:creator>
            <pubDate>Thu, 19 Mar 2020 11:59:48 GMT</pubDate>
            <atom:updated>2020-03-19T11:59:48.628Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K3LpkKQhE6pBwL1aWh1WMw.jpeg" /></figure><p>Performance is a very critical part of an e-commerce business, for example:<br>Walmart found that for every 1-second improvement in page load time, conversions increased by 2%.</p><p>This blog post tells the story of some of the optimizations we learned/applied over the years for our e-commerce solutions <a href="https://www.nisnass.ae/">Nisnass</a> and <a href="https://www.ounass.ae/">Ounass</a> respectively, starting with <a href="https://blog.cloudflare.com/introducing-cloudflare-workers/">Cloudflare worker</a>s.</p><p><strong>Problem</strong></p><p>During the sales campaign (eg: White Friday), the load on the server will be huge and we used to get <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/504">504 Gateway Timeout</a> impacting the sales. Also <a href="https://aws.amazon.com/blogs/architecture/scale-your-web-application-one-step-at-a-time/">scaling</a> up increases the cost factor as well.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/501/1*0keYf6PWh32YB2Hk6pfSpw.png" /></figure><p><strong>Solution</strong></p><p>We can either cache at the <strong>server-side</strong> or at <strong>Cloudflare</strong> itself, the latter will allow us not to worry about the load. You can read more about the worker from <a href="https://developers.cloudflare.com/workers/quickstart">Cloudflare site</a>.</p><p><strong>Server-side cache</strong></p><p>We already had a caching library in place and was working very well for us. The below diagram depicts how server-side cache works.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/514/1*djxmNICJNukqb8_seygrtg.png" /></figure><p><strong>Caching using Cloudflare worker</strong></p><p>We were already using the Cloudflare enterprise plan and it had the worker feature enabled, so we decided to experiment with Cloudflare workers.</p><p>It is better to read about <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control">Cache-control</a>, as Cloudflare respects this header for deciding the cache and we make use of that to cache whatever we want.</p><p>First basic configuration in the Cloudflare fetch method</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/35ff904a7e04762d240988f63199b15b/href">https://medium.com/media/35ff904a7e04762d240988f63199b15b/href</a></iframe><blockquote>There is another useful option resolveOverride, which we are using, but am skipping as that is not related to caching.</blockquote><p>In order for Cloudflare to cache, the backend must set the cache-control header in the response. So we created a function which when invoked will set the header. The below code sets the response header:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f4b364a7a259e7fcf63663e6bcb262e/href">https://medium.com/media/0f4b364a7a259e7fcf63663e6bcb262e/href</a></iframe><p>I am not explaining any of the above because you can read from <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control">Cache-control</a></p><pre>res.removeHeader(&#39;set-cookie&#39;)</pre><p>This remove header is added because Cloudflare does not cache when the set-cookie header is present in the response. Read the specification below to understand why</p><p><a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie">Set-Cookie - HTTP | MDN</a></p><p>Now we got the page without any customer data, but we need to show the customer data as well. We make an <a href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest">XHR</a> call that gets the customer data and renders after the page load.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/465ac78f5936908612b34d952f193b6b/href">https://medium.com/media/465ac78f5936908612b34d952f193b6b/href</a></iframe><p>After getting the response we update the state and since we use react and redux, the page will automatically re-render.</p><p>The below diagram depicts how caching is done.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/541/1*PQqB6DsEM6Naq52xe9bMjA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/748/1*RPs8ytBeYmfYvb-pSNr-4Q.png" /><figcaption>Before Caching response time - taken by disabling the worker cache</figcaption></figure><p>The response time after caching</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*nCFF5guYR-xyOwvaeTBKJQ.png" /><figcaption>After cache response time</figcaption></figure><p><strong>Do we need to handle any other use case?</strong></p><p>Yes, we need to handle device type, <strong>marketing campaigns</strong> which appends parameters like (sub_id, cake_o, _ga, clickref, gclid, gclsrc, utm_) with unique value to the URL, filters which users apply, redirects. So now our worker code looks like this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/280e102156b5a816bacec43cfba10262/href">https://medium.com/media/280e102156b5a816bacec43cfba10262/href</a></iframe><p><strong>How long we should cache?</strong></p><p>The cache duration depends on how frequent the update happens and how critical are those changes generally. So it is up to the business requirements and for us 10 mins for product details page and 3hrs for product listing page worked well.</p><p><strong>Pagination</strong></p><p>When a cached page supports infinite scroll, then all pages must expire at the same time. For this, we need to modify the backend logic where we set the cache-control header.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/456e38a524ffc0acbb56ac83acc69f9c/href">https://medium.com/media/456e38a524ffc0acbb56ac83acc69f9c/href</a></iframe><p><strong>Conclusion</strong></p><p>Kindly note that the post intentionally omitted much complexity of the worker code to keep it brief. Thank you for reading and stay tuned!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ca5ed3ad512e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ounass/optimizing-for-speed-using-cloudflare-workers-ca5ed3ad512e">Optimizing for speed using Cloudflare workers</a> was originally published in <a href="https://medium.com/ounass">Ounass</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>