Thuta Learning
System Design
BasicProgrammingintermediate

Caching အခြေခံသဘောတရား

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Caching အခြေခံသဘောတရား concept ကို နားလည်ရှင်းပြနိုင်ရန်
  • နမူနာ diagram/code ကို ကိုယ်တိုင် လေ့လာပြီး trade-off များကို ခွဲခြမ်းစိတ်ဖြာနိုင်ရန်
  • Tutorial Platform project နှင့် production scenario တွင် မှန်ကန်စွာအသုံးချနိုင်ရန်

နားလည်ထားရမယ့် အချက်

Caching ဆိုတာ database query ရှုပ်ထွေးတာ၊ computation တစ်ခု၊ rendered page တစ်ခုလိုမျိုး ထပ်ခါထပ်ခါ လုပ်ရင် ကုန်ကျစရိတ်များတဲ့ အလုပ်တွေရှိလို့ လိုအပ်တာပါ — ရလဒ်တူတူကို မကြာခင်ထပ်လိုအပ်ရင် ရှေ့ကနေပြန်လုပ်ခြင်းက underlying system ပေါ်မှာ အချိန်နဲ့ load ဖြုန်းတီးနေတာပါ။ Cache တစ်ခုက ဒီရလဒ်ကို မြန်တဲ့နေရာတစ်ခု (usually memory) မှာ သိမ်းထားပြီး နောက်တစ်ကြိမ် အလားတူ request အတွက် ချက်ချင်း ဖြေပေးနိုင်ပါတယ်။ အသုံးအများဆုံး pattern ကတော့ cache-aside ပါ — application က cache ကို အရင်စစ်ပြီး cache hit ဖြစ်ရင် သိမ်းထားတဲ့ value ကို ချက်ချင်း ပြန်ပေးပါတယ်; cache miss ဖြစ်ရင်တော့ real source (database) ဆီ ပြန်သွားပြီး fresh value ကို fetch လုပ်ပြီး cache ထဲ populate လုပ်ပြီးမှ return ပြန်ပေးပါတယ် — ဒါကြောင့် ပထမဆုံး request တစ်ခုတည်းသာ full cost ကို ပေးဆောင်ရတာပါ။ Write-through ကတော့ data ကို fresh ဖြစ်စေဖို့ alternative pattern ပါ — write တွေက cache နဲ့ source ကို တစ်ပြိုင်နက်တည်း သွားပြီး cache ဟာ ဘယ်တော့မှ နောက်ကျမနေပါဘူး။ ဒီနှစ်ခုစလုံးရဲ့ fundamental risk ကတော့ staleness ပါ — underlying data ပြောင်းလဲသွားပေမယ့် cache ကို မပြောတာဆိုရင် cache က ခေတ်နောက်ကျနေတဲ့ value ကို ယုံကြည်စိတ်ချစွာ ဆက်ပေးနေပါလိမ့်မယ်၊ response နှေးတာနဲ့ မတူဘဲ stale response တစ်ခုက မှန်ကန်ပုံပေါ်နေရင်းနဲ့ တကယ်တော့ မှားနေတာဖြစ်ပါတယ် — ဒါကြောင့် cache invalidation ကို ယူဆထားတာမျိုးမဟုတ်ဘဲ deliberate ကိုင်တွယ်ရမှာပါ။

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

Tutorial Platform ဟာ ထင်ရှားတဲ့ နေရာတွေမှာ caching ရဲ့ အကျိုးကို ရရှိနေပြီး ဖြစ်ပါတယ် — lesson တစ်ခုရဲ့ content ဟာ ရှားရှားပါးပါးပဲ ပြောင်းလဲတတ်လို့ page view တိုင်းမှာ database query လုပ်မယ့်အစား rendered lesson ကို cache-aside သုံးပြီး cache ထားနိုင်ပါတယ် — cache စစ်ပြီး miss ဖြစ်မှသာ database ကို hit လုပ်ပါ။ ဒါပေမယ့် author တစ်ယောက်က lesson ကို edit လုပ်ရင် stale cache ကြောင့် learner တွေ old wording ကိုပဲ ဆက်ပြီး endless ဖတ်နေနိုင်ပါတယ်၊ ဒါကြောင့် platform က TTL တစ်ခု ဒါမှမဟုတ် publish လုပ်ချိန် explicit invalidation တစ်ခု လိုအပ်ပြီး reader တွေ ခဏနေရင် (ဒါမှမဟုတ် ချက်ချင်း) update ကို မြင်ရအောင် လုပ်ပေးရပါတယ်။ ဒီ freshness versus ထပ်ခါထပ်ခါ ကုန်ကျတဲ့အလုပ် trade-off ဟာ search-suggest result, trending-lesson ranking, request တစ်ခုချင်းစီအတွက် ပြန်တွက်ချင်စိတ်မရှိတဲ့ data တခြားတွေမှာလည်း ထပ်ကြုံရပါတယ်။

အတူတူ စမ်းရေးကြည့်မယ်

text
Request for lesson "intro-to-arrays"
            |
            v
     +--------------+
     | Check cache  |
     +--------------+
        |         |
      HIT        MISS
        |         |
        v         v
  +-----------+  +------------------+
  | Return    |  | Query database    |
  | cached    |  +------------------+
  | value     |         |
  | (fast)    |         v
  +-----------+  +------------------+
                 | Populate cache    |
                 +------------------+
                         |
                         v
                 +------------------+
                 | Return value      |
                 | (slower, once)    |
                 +------------------+
You should see
Diagram က cache-aside flow ကို ပြသပြီး cache hit ဟာ ချက်ချင်းဖြစ်ကြောင်း၊ cache miss ကတော့ database query, cache populate ပြီးမှ return ဖြစ်ကြောင်း ပြသပါတယ်။

၅ မိနစ် စမ်းကြည့်

Feature တစ်ခု (ဥပမာ user profile page) ကို ရွေးပြီး cache-aside သုံးရင် ကောင်းလား write-through သုံးရင် ကောင်းလား ဆုံးဖြတ်ပြီး ဘာကြောင့်လဲ ရှင်းပြပါ။

သတိလေးတစ်ချက်

TTL မထားဘဲ invalidation plan လည်း မရှိတဲ့ data ကို cache ထားလိုက်ရင် underlying source ပြောင်းလဲသွားတာနဲ့ cache က ကိုယ့်ကိုယ်ကို ပြင်ဆင်နိုင်စွမ်းလုံးဝမရှိဘဲ ခေတ်နောက်ကျတဲ့ result ကို အကန့်အသတ်မရှိ ဆက်ပေးနေတတ်ပါတယ်။

TTL ကို တိုတိုလွန်းအောင် သတ်မှတ်လိုက်ရင် request အများစုက cache ကို miss ဖြစ်နေပြီး caching ရဲ့ ရည်ရွယ်ချက်ကို ကျော်လွန်ပျက်ပြယ်စေရင်း၊ cache run ထားရတဲ့ complexity နှင့် memory cost ကိုတော့ ဆက်ပေးဆောင်နေရတတ်ပါတယ်။

Wikipedia — Cache (computing)System Design

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • TTL မထားဘဲ invalidation plan လည်း မရှိတဲ့ data ကို cache ထားလိုက်ရင် underlying source ပြောင်းလဲသွားတာနဲ့ cache က ကိုယ့်ကိုယ်ကို ပြင်ဆင်နိုင်စွမ်းလုံးဝမရှိဘဲ ခေတ်နောက်ကျတဲ့ result ကို အကန့်အသတ်မရှိ ဆက်ပေးနေတတ်ပါတယ်။
  • TTL ကို တိုတိုလွန်းအောင် သတ်မှတ်လိုက်ရင် request အများစုက cache ကို miss ဖြစ်နေပြီး caching ရဲ့ ရည်ရွယ်ချက်ကို ကျော်လွန်ပျက်ပြယ်စေရင်း၊ cache run ထားရတဲ့ complexity နှင့် memory cost ကိုတော့ ဆက်ပေးဆောင်နေရတတ်ပါတယ်။
  • Design decision တစ်ခုကို production system ပေါ် တိုက်ရိုက်မကျင့်သုံးမီ load/traffic assumption များကို အရင်အတည်ပြုပါ။

လေ့ကျင့်ခန်း

Feature တစ်ခု (ဥပမာ user profile page) ကို ရွေးပြီး cache-aside သုံးရင် ကောင်းလား write-through သုံးရင် ကောင်းလား ဆုံးဖြတ်ပြီး ဘာကြောင့်လဲ ရှင်းပြပါ။

You'll know it worked when: Diagram က cache-aside flow ကို ပြသပြီး cache hit ဟာ ချက်ချင်းဖြစ်ကြောင်း၊ cache miss ကတော့ database query, cache populate ပြီးမှ return ဖြစ်ကြောင်း ပြသပါတယ်။

Caching အခြေခံသဘောတရား | Thuta Learning