<?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: shahriar</title>
    <description>The latest articles on DEV Community by shahriar (@shuvo714).</description>
    <link>https://dev.to/shuvo714</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%2F1433385%2Fc06a1325-7e8b-4979-bd76-b202e1a468bc.jpg</url>
      <title>DEV Community: shahriar</title>
      <link>https://dev.to/shuvo714</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shuvo714"/>
    <language>en</language>
    <item>
      <title>Stop Caching Everything: A Practical Approach to Laravel and Redis</title>
      <dc:creator>shahriar</dc:creator>
      <pubDate>Sun, 29 Nov 2026 18:55:28 +0000</pubDate>
      <link>https://dev.to/shuvo714/stop-caching-everything-a-practical-approach-to-laravel-and-redis-355d</link>
      <guid>https://dev.to/shuvo714/stop-caching-everything-a-practical-approach-to-laravel-and-redis-355d</guid>
      <description>&lt;p&gt;When building an API-driven application with Laravel, caching often feels like an obvious solution to performance problems.&lt;/p&gt;

&lt;p&gt;An external API is slow? Cache the response.&lt;/p&gt;

&lt;p&gt;A database query is expensive? Cache the result.&lt;/p&gt;

&lt;p&gt;A page receives frequent requests? Add another layer of caching.&lt;/p&gt;

&lt;p&gt;But caching becomes more complicated when the information your application depends on changes frequently.&lt;/p&gt;

&lt;p&gt;The real challenge is not deciding whether to cache something. It is deciding what can safely become outdated.&lt;/p&gt;

&lt;h2&gt;
  
  
  Not Every Piece of Data Needs the Same Cache Lifetime
&lt;/h2&gt;

&lt;p&gt;Consider an API-driven marketplace that displays countries, services, pricing, and real-time availability.&lt;/p&gt;

&lt;p&gt;Country names rarely change. Service descriptions may remain unchanged for weeks. Pricing can change throughout the day, while availability might change within seconds.&lt;/p&gt;

&lt;p&gt;Applying the same cache lifetime to all this information creates unnecessary problems.&lt;/p&gt;

&lt;p&gt;A better starting point is to categorize data according to how frequently it changes and how important its accuracy is.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Country information: several hours.&lt;/li&gt;
&lt;li&gt;Service descriptions: several hours.&lt;/li&gt;
&lt;li&gt;Pricing: a few minutes.&lt;/li&gt;
&lt;li&gt;Live availability: a few seconds, if appropriate.&lt;/li&gt;
&lt;li&gt;Final order confirmation: authoritative, fresh data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are illustrative durations, not universal recommendations. The right values depend on the application and its upstream providers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keeping Cache Logic Simple in Laravel
&lt;/h2&gt;

&lt;p&gt;Laravel provides a convenient API for retrieving and storing cached data.&lt;/p&gt;

&lt;p&gt;For relatively stable public catalog data, a simple approach might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate&lt;/span&gt;\&lt;span class="nc"&gt;Support&lt;/span&gt;\&lt;span class="nc"&gt;Facades&lt;/span&gt;\&lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$services&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;store&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'redis'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;remember&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s1"&gt;'catalog:public:services:v1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="mi"&gt;3600&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$catalog&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;publicList&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="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For frequently changing availability, a separate cache key and shorter lifetime would make more sense:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$availability&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;store&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'redis'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;remember&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s2"&gt;"availability:{$countryId}:{$serviceId}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$provider&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getAvailability&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nv"&gt;$countryId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nv"&gt;$serviceId&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These examples assume that the catalog and provider services have already been implemented and that the data is not customer-specific.&lt;/p&gt;

&lt;p&gt;The important part is separating relatively stable information from frequently changing operational data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cached Availability Is Not a Reservation
&lt;/h2&gt;

&lt;p&gt;This distinction matters in applications that depend on external inventory.&lt;/p&gt;

&lt;p&gt;Imagine a user opens a page where an item is shown as available. A few seconds later, another customer purchases the last available item.&lt;/p&gt;

&lt;p&gt;The first user might still see the cached availability.&lt;/p&gt;

&lt;p&gt;That does not mean the application should accept the order as successful.&lt;/p&gt;

&lt;p&gt;The final purchase operation must rely on the provider's authoritative reservation or order response.&lt;/p&gt;

&lt;p&gt;Even a fresh availability check cannot guarantee that inventory will remain available until the transaction completes.&lt;/p&gt;

&lt;p&gt;Caching should improve the browsing experience, not replace transactional validation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling External API Failures
&lt;/h2&gt;

&lt;p&gt;Another consideration is what happens when an external provider becomes temporarily unavailable.&lt;/p&gt;

&lt;p&gt;Not every failure should be interpreted as zero availability.&lt;/p&gt;

&lt;p&gt;An API timeout, a rate limit, and an actual out-of-stock response represent different situations.&lt;/p&gt;

&lt;p&gt;Treating them as identical can produce misleading information.&lt;/p&gt;

&lt;p&gt;A better approach is to distinguish between:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Confirmed availability.&lt;/li&gt;
&lt;li&gt;Confirmed unavailability.&lt;/li&gt;
&lt;li&gt;Temporarily unknown availability.&lt;/li&gt;
&lt;li&gt;Provider communication errors.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For non-critical browsing information, displaying recently cached data with an appropriate warning may be acceptable.&lt;/p&gt;

&lt;p&gt;For financial transactions and order confirmation, silently relying on outdated data is a much greater risk.&lt;/p&gt;

&lt;h2&gt;
  
  
  Preventing Cache Stampedes
&lt;/h2&gt;

&lt;p&gt;A popular cache key can receive many simultaneous requests immediately after expiration.&lt;/p&gt;

&lt;p&gt;Without coordination, those requests may all attempt to retrieve the same information from an external API.&lt;/p&gt;

&lt;p&gt;This is commonly known as a cache stampede.&lt;/p&gt;

&lt;p&gt;Laravel's atomic locks can help coordinate cache rebuilding. Background refresh jobs and appropriately designed stale-while-revalidate strategies can also reduce unnecessary upstream requests.&lt;/p&gt;

&lt;p&gt;The goal is to prevent a caching optimization from becoming another source of server load.&lt;/p&gt;

&lt;h2&gt;
  
  
  Measure Before Optimizing
&lt;/h2&gt;

&lt;p&gt;Before introducing additional caching layers, it helps to monitor a few practical metrics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cache hit and miss rates.&lt;/li&gt;
&lt;li&gt;External API response times.&lt;/li&gt;
&lt;li&gt;Request latency.&lt;/li&gt;
&lt;li&gt;Provider error rates.&lt;/li&gt;
&lt;li&gt;Queue processing time.&lt;/li&gt;
&lt;li&gt;Frequency of outdated availability information.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These measurements provide a better foundation for optimization than simply increasing cache lifetimes.&lt;/p&gt;

&lt;p&gt;A high cache hit rate is not particularly useful if customers consistently receive inaccurate information.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Laravel and Redis provide excellent tools for building responsive API-driven applications. However, the framework cannot decide which information is safe to cache.&lt;/p&gt;

&lt;p&gt;That decision depends on the business logic, the reliability of external services, and the consequences of serving outdated information.&lt;/p&gt;

&lt;p&gt;My preferred approach is to keep caching strategies simple, separate stable information from transactional data, and introduce additional complexity only when measurements justify it.&lt;/p&gt;

&lt;p&gt;If you are working on an API-driven marketplace or SaaS application, how do you manage frequently changing availability? Do you prefer short-lived caches, background refresh jobs, or a combination of both?&lt;/p&gt;

&lt;p&gt;I'd be interested to hear how other developers approach this problem.&lt;/p&gt;

&lt;p&gt;Reference: &lt;a href="https://laravel.com/docs/12.x/cache" rel="noopener noreferrer"&gt;Laravel Cache Documentation&lt;/a&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>redis</category>
    </item>
  </channel>
</rss>
