DEV Community

Cover image for 𝐒𝐘𝐒𝐓𝐄𝐌 𝐃𝐄𝐒𝐈𝐆𝐍 & 𝐀𝐑𝐂𝐇𝐈𝐓𝐄𝐂𝐓𝐔𝐑𝐄 𝐃𝐈𝐀𝐑𝐘 - 𝐑𝐄𝐀𝐋 𝐖𝐎𝐑𝐋𝐃 𝐀𝐏𝐏𝐋𝐈𝐂𝐀𝐓𝐈𝐎𝐍 𝐆𝐎𝐈𝐍𝐆 𝐁𝐄𝐘𝐎𝐍𝐃 𝐒𝐔𝐑𝐅𝐀𝐂𝐄 𝐋𝐄𝐕𝐄𝐋
Creative Coder
Creative Coder

Posted on

𝐒𝐘𝐒𝐓𝐄𝐌 𝐃𝐄𝐒𝐈𝐆𝐍 & 𝐀𝐑𝐂𝐇𝐈𝐓𝐄𝐂𝐓𝐔𝐑𝐄 𝐃𝐈𝐀𝐑𝐘 - 𝐑𝐄𝐀𝐋 𝐖𝐎𝐑𝐋𝐃 𝐀𝐏𝐏𝐋𝐈𝐂𝐀𝐓𝐈𝐎𝐍 𝐆𝐎𝐈𝐍𝐆 𝐁𝐄𝐘𝐎𝐍𝐃 𝐒𝐔𝐑𝐅𝐀𝐂𝐄 𝐋𝐄𝐕𝐄𝐋

Day 3: 𝐘𝐨𝐮𝐫 𝐛𝐫𝐚𝐢𝐧 𝐚𝐥𝐫𝐞𝐚𝐝𝐲 𝐦𝐚𝐬𝐭𝐞𝐫𝐞𝐝 𝐜𝐚𝐜𝐡𝐢𝐧𝐠. 𝐍𝐨𝐰 𝐥𝐞𝐭'𝐬 𝐮𝐬𝐞 𝐢𝐭 𝐭𝐨 𝐛𝐮𝐢𝐥𝐝 𝐟𝐚𝐬𝐭𝐞𝐫 𝐚𝐩𝐩𝐬.

𝑬𝒗𝒆𝒓 𝒘𝒐𝒏𝒅𝒆𝒓 𝒘𝒉𝒚 𝒔𝒐𝒎𝒆 𝒂𝒑𝒑𝒔 𝒂𝒓𝒆 𝒍𝒊𝒈𝒉𝒕𝒏𝒊𝒏𝒈 𝒇𝒂𝒔𝒕 𝒘𝒉𝒊𝒍𝒆 𝒐𝒕𝒉𝒆𝒓𝒔 𝒎𝒐𝒗𝒆 𝒍𝒊𝒌𝒆 𝒂 𝒕𝒐𝒓𝒕𝒐𝒊𝒔𝒆 𝒓𝒖𝒏𝒏𝒊𝒏𝒈 𝒂 𝒔𝒑𝒓𝒊𝒏𝒕? 😂

𝗥𝗲𝗮𝗹 𝘁𝗮𝗹𝗸: Has anyone noticed in Telegram that when you clear cache, all your channel content re-downloads? That's 𝗰𝗮𝗰𝗵𝗶𝗻𝗴 in 𝗮𝗰𝘁𝗶𝗼𝗻.

Before we talk about 𝗥𝗲𝗱𝗶𝘀 𝗼𝗿 𝗖𝗗𝗡𝘀, let me show you that you already understand 𝗰𝗮𝗰𝗵𝗶𝗻𝗴 better than most developers.

𝐘𝐨𝐮'𝐫𝐞 𝐚𝐥𝐫𝐞𝐚𝐝𝐲 𝐚 𝐜𝐚𝐜𝐡𝐢𝐧𝐠 𝐞𝐱𝐩𝐞𝐫𝐭.

𝒀𝒐𝒖 𝒌𝒏𝒐𝒘 𝒚𝒐𝒖𝒓 𝒃𝒆𝒔𝒕 𝒇𝒓𝒊𝒆𝒏𝒅'𝒔 𝒑𝒉𝒐𝒏𝒆 𝒏𝒖𝒎𝒃𝒆𝒓 𝒃𝒚 𝒉𝒆𝒂𝒓𝒕. You don't look it up every time you want to call them. That's caching – your brain stores frequently accessed information in fast memory.

𝒀𝒐𝒖𝒓 𝒎𝒐𝒎 𝒌𝒆𝒆𝒑𝒔 𝒘𝒂𝒕𝒆𝒓 𝒃𝒐𝒕𝒕𝒍𝒆𝒔 𝒊𝒏 𝒕𝒉𝒆 𝒇𝒓𝒊𝒅𝒈𝒆 even though there's a dispenser in the kitchen. She predicted high demand and pre-stored the most requested item closer to where people need it.

𝑰𝒇 𝒚𝒐𝒖'𝒓𝒆 𝒂 𝒔𝒕𝒖𝒅𝒆𝒏𝒕, some things stay in your bag – notes, pens – because you need them accessible. Some even lay out their school uniform the night before. Caching simply means making what you need easy and more accessible.

𝒀𝒐𝒖 𝒃𝒐𝒐𝒌𝒎𝒂𝒓𝒌 𝒘𝒆𝒃𝒔𝒊𝒕𝒆𝒔 instead of googling them every time. You created a shortcut to frequently visited destinations.

These aren't just analogies. These are the exact principles powering every fast app you use.

𝐖𝐡𝐚𝐭 𝐜𝐚𝐜𝐡𝐢𝐧𝐠 𝐚𝐜𝐭𝐮𝐚𝐥𝐥𝐲 𝐦𝐞𝐚𝐧𝐬

Caching is keeping a copy of something you use frequently in a place where you can grab it faster. Every system has slow parts and fast parts. Smart systems move frequently used data from slow to fast.

𝐓𝐡𝐢𝐧𝐤 𝐚𝐛𝐨𝐮𝐭 𝐲𝐨𝐮𝐫 𝐡𝐨𝐮𝐬𝐞. Kitchen cabinets are your "𝒄𝒂𝒄𝒉𝒆" for daily food items. Rice, pasta, oil live there because you need them often. But extra bulk bags stay in the storeroom (your "database") because you don't need them immediately. You don't run to the storeroom every time you want to cook one cup of rice.

This same pattern exists at every layer of computing.

𝐖𝐡𝐞𝐫𝐞 𝐜𝐚𝐜𝐡𝐢𝐧𝐠 𝐢𝐬 𝐬𝐞𝐜𝐫𝐞𝐭𝐥𝐲 𝐰𝐨𝐫𝐤𝐢𝐧𝐠 𝐚𝐫𝐨𝐮𝐧𝐝 𝐲𝐨𝐮

𝗬𝗼𝘂𝗿 𝗽𝗵𝗼𝗻𝗲'𝘀 𝗴𝗮𝗹𝗹𝗲𝗿𝘆: Thousands of photos in the cloud, but thumbnails appear instantly. Your phone cached tiny previews locally. This happens most times in Apple iCloud.

𝗬𝗼𝘂𝗿 𝘄𝗲𝗯 𝗯𝗿𝗼𝘄𝘀𝗲𝗿: Doesn't re-download logos and images every visit. First time you visit Amazon, your browser saves their logo. Next hundred visits, it skips the download. This is why familiar sites load faster.

𝗬𝗼𝘂𝗧𝘂𝗯𝗲: Pre-downloads the next 30 seconds before you watch it. That's why you can scrub forward and it plays without buffering.

𝗪𝗵𝗮𝘁𝘀𝗔𝗽𝗽: Caches recent chats and contact photos on your device. Conversations appear instantly even before connecting to internet. Only new messages need fetching.

𝗚𝗼𝗼𝗴𝗹𝗲 𝗠𝗮𝗽𝘀: Caches map tiles of routes you visit frequently. Even with weak internet, your regular route displays immediately from local storage.

𝗕𝗮𝗻𝗸𝗶𝗻𝗴 𝗮𝗽𝗽𝘀: Cache your balance for 30-60 seconds. Close and reopen immediately? You see your balance instantly without loading. The app knows it probably hasn't changed in one minute.

𝐇𝐨𝐰 𝐭𝐡𝐢𝐬 𝐰𝐨𝐫𝐤𝐬 𝐢𝐧 𝐫𝐞𝐚𝐥 𝐚𝐩𝐩𝐥𝐢𝐜𝐚𝐭𝐢𝐨𝐧𝐬

Imagine you built an online electronics store. Without caching, for every visitor:

  1. Connect to the database
  2. Fetch the latest products
  3. Calculate discounts
  4. Format data
  5. Send to browser

1,000 𝒗𝒊𝒔𝒊𝒕𝒐𝒓𝒔 = 1,000 𝒕𝒊𝒎𝒆𝒔 𝒓𝒆𝒑𝒆𝒂𝒕𝒊𝒏𝒈 𝒕𝒉𝒆 𝒔𝒂𝒎𝒆 𝒘𝒐𝒓𝒌. Wasteful.

𝗪𝗶𝘁𝗵 𝗰𝗮𝗰𝗵𝗶𝗻𝗴:

  • First visitor triggers the full process.
  • Result gets stored in cache (like Redis) for 5 minutes.
  • The next 499 visitors in those 5 minutes get the cached result instantly.
  • After 5 minutes, the cache expires and refreshes.

𝐘𝐨𝐮'𝐯𝐞 𝐫𝐞𝐝𝐮𝐜𝐞𝐝 𝟏,𝟎𝟎𝟎 𝐝𝐚𝐭𝐚𝐛𝐚𝐬𝐞 𝐪𝐮𝐞𝐫𝐢𝐞𝐬 𝐭𝐨 𝐦𝐚𝐲𝐛𝐞 𝟏𝟐 𝐩𝐞𝐫 𝐡𝐨𝐮𝐫. The database isn't overwhelmed. Pages load faster. The hosting bill drops. Everyone wins.

𝐓𝐡𝐞 𝐍𝐞𝐭𝐟𝐥𝐢𝐱 𝐜𝐨𝐧𝐧𝐞𝐜𝐭𝐢𝐨𝐧 𝐟𝐫𝐨𝐦 𝐃𝐚𝐲 𝟏

Remember Open Connect? Those physical servers Netflix puts inside ISP buildings? That's caching taken to the extreme.

Netflix realized:

  • Video files are huge (several GBs per movie)
  • Millions watch the same popular content.
  • Network bandwidth costs money
  • Long-distance transfer is slow

So they cache entire movies on servers as physically close to users as possible. When you watch Stranger Things in Lagos, you're watching a cached copy downloaded once at 3 AM, now serving thousands of local viewers. Netflix isn't streaming from California to a thousand Nigerians individually. That would be wasteful and slow.

Same principle as your mom's fridge, just scaled to millions across continents.

𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧𝐬 𝐭𝐨 𝐚𝐬𝐤 𝐰𝐡𝐞𝐧 𝐛𝐮𝐢𝐥𝐝𝐢𝐧𝐠 𝐲𝐨𝐮𝐫 𝐚𝐩𝐩

𝑨𝒎 𝑰 𝒇𝒆𝒕𝒄𝒉𝒊𝒏𝒈 𝒕𝒉𝒆 𝒔𝒂𝒎𝒆 𝒅𝒂𝒕𝒂 𝒓𝒆𝒑𝒆𝒂𝒕𝒆𝒅𝒍𝒚? Popular blog posts getting thousands of views don't need fresh database queries if the content hasn't changed.

𝑨𝒎 𝑰 𝒅𝒐𝒊𝒏𝒈 𝒆𝒙𝒑𝒆𝒏𝒔𝒊𝒗𝒆 𝒄𝒂𝒍𝒄𝒖𝒍𝒂𝒕𝒊𝒐𝒏𝒔 𝒐𝒗𝒆𝒓 𝒂𝒏𝒅 𝒐𝒗𝒆𝒓? Analytics reports, recommendations, search results that don't change every second? Calculate once, cache the result.

𝑨𝒎 𝑰 𝒄𝒂𝒍𝒍𝒊𝒏𝒈 𝒆𝒙𝒕𝒆𝒓𝒏𝒂𝒍 𝑨𝑷𝑰𝒔 𝒆𝒙𝒄𝒆𝒔𝒔𝒊𝒗𝒆𝒍𝒚? Weather data, exchange rates, social posts? Those providers rate-limit and charge per call. Cache their responses.

𝑪𝒂𝒏 𝑰 𝒑𝒓𝒆𝒅𝒊𝒄𝒕 𝒘𝒉𝒂𝒕 𝒖𝒔𝒆𝒓𝒔 𝒏𝒆𝒆𝒅 𝒏𝒆𝒙𝒕? Netflix downloads popular shows before anyone requests them. YouTube buffers the next portion before you watch it.

Caching exists at every level

CPU cache: Stores frequently accessed RAM data in faster memory built into your processor.

RAM: Cache for your hard drive. OS keeps frequent files in RAM because it's thousands of times faster than disk.

Browser cache: Stores website resources locally. Why clearing the cache sometimes "fixes" issues but makes sites load slower temporarily.

CDN cache (Cloudflare): Stores website files worldwide. You get files from the nearest location, not from a server on another continent.

Application cache (Redis/Memcached): Stores database results and computed values in memory so your app doesn't repeatedly hit the database.

Database query cache: Stores frequent query results inside the database itself.

The pattern repeats: identify what's frequently accessed, store it somewhere faster, reduce expensive work.

The tradeoffs you need to understand

Stale data risk: Cached data might be outdated. Cache product prices for 5 minutes and the price changes? Users see the old price until cache expires. Decide how stale your data can be.

Memory costs: Cache storage costs money and has limits. You can't cache everything forever. Need strategies for what to keep (popular) and what to evict (old/unpopular).

Cache invalidation: One of the hardest problems in computer science. Update a product in your database? How do you ensure the cached version also updates? Get this wrong and users see inconsistent data.

Added complexity: Every caching layer can break or behave unexpectedly. Sometimes bugs only appear when cache is in a certain state.

Despite these tradeoffs, caching is worth it. The performance improvements are too significant to ignore. Just implement thoughtfully.

Start simple

Don't architect elaborate multi-tier caching for your first project.

  1. Identify your slowest operations (use profiling tools)
  2. Check if those operations produce the same results repeatedly
  3. Cache those specific results with reasonable expiration times
  4. Monitor whether it actually helps

For typical web apps, caching expensive database queries with Redis can immediately make your app feel several times faster. For mobile apps, caching API responses locally makes your app usable even with poor connectivity.

The goal isn't perfect caching everywhere. The goal is strategic caching where it matters most.

Tap the link here to get hands on real world project that will solidify your understand about caching itself

Tomorrow (Day 4): When a million people try to buy concert tickets at the same time, why do some websites survive while others crash spectacularly? We're breaking down load balancing – it works exactly like how Shoprite uses multiple checkout counters to prevent chaos.

Drop a 🔥 if caching finally clicked for you and you're already thinking about where to add it in your projects.

SystemDesign #SoftwareEngineering #Caching #WebDevelopment #BackendDevelopment #PerformanceOptimization #Redis #CDN #TechArchitecture #CodingTips #DeveloperLife #SoftwareArchitecture #NigerianDevelopers #TechCommunity #LearnToCode #tcm #creativethinkers

Top comments (0)