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)