Thuta Learning
Cloud & Deployment
IntermediateDevOps & Toolsbeginner

CDN နှင့် Content Delivery

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

  • CDN နှင့် Content Delivery concept ကို နားလည်ရှင်းပြနိုင်ရန်
  • Diagram ကို ဖတ်ပြီး architecture ထဲမှာ request/data ဘယ်လိုစီးဆင်းသလဲ ခြေရာခံနိုင်ရန်
  • ကိုယ့် project အတွက် ဘယ်လို ဆုံးဖြတ်သင့်သလဲ ရှင်းပြနိုင်ရန်

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

CDN ဆိုတာ physical location များစွာမှာ ရှိတဲ့ server network တစ်ခုဖြစ်ပြီး တစ်ခုချင်းစီက origin server တစ်ခုတည်းကနေ အကြိမ်တိုင်း ရယူရမယ့် content ရဲ့ cached copy ကို ကိုင်ဆောင်ထားပါတယ်။

CDN မရှိရင် အဝေးက user တစ်ယောက်ဟာ request တိုင်းအတွက် network ကို origin ဆီ physically ဖြတ်ကျော်ပြီး ပြန်လာဖို့ စောင့်ရပါတယ်။ CDN ရှိနေရင်တော့ ပထမဆုံး request ပြီးနောက် content ဟာ user နားနားမှာ cache ဖြစ်သွားပြီး ဆက်လာမယ့် အနီးအနားက visitor တိုင်း local ကနေ ရယူရပါတယ်။

Cache hit ဆိုတာ အနီးအနားက location မှာ ရှိနေပြီးသား content ကို ချက်ချင်း ပြန်ပေးတာပါ။ Cache miss ဆိုတာ မရှိသေးလို့ CDN က origin ကနေ အရင်ဆွဲ၊ copy တစ်ခု cache လုပ်ပြီးမှ ပြန်ပေးလို့ hit ထက် ပိုနှေးတာပါ။

CDN တွေက image၊ stylesheet၊ script၊ video လို static asset တွေအတွက် အထူးထိရောက်ပါတယ်၊ ဒီ content တွေ ရှားရှားပါးပါး ပြောင်းလဲလို့ပါ။ Personalized dashboard လို user တစ်ယောက်ချင်းစီအလိုက် ကွဲပြားတဲ့ content ကတော့ အကျိုးအနည်းငယ်သာ ရပါတယ်။

CDN
User များနှင့်နီးသော location များတွင် content ကို cache လုပ်ပြီး serve ပေးသည့် server network တစ်ခုဖြစ်ပြီး latency နှင့် origin load ကို လျှော့ချပေးသည်။
Origin
Content ၏ authoritative copy ကို ကိုင်ဆောင်ထားသော original server ဖြစ်ပြီး cache miss ဖြစ်တိုင်း CDN က ဤနေရာသို့ ပြန်လှည့်သည်။
Cache Hit
Origin server ကို ဆက်သွယ်စရာမလိုပဲ အနီးအနားက cached copy ကနေ တိုက်ရိုက် ဖြေရှင်းပေးသော request။
Cache Miss
အနီးအနားမှာ မရှိသေးသော content အတွက် request ဖြစ်ပြီး ပြန်ပေးပြီး နောက်တစ်ကြိမ်အတွက် cache မလုပ်ခင် origin ကနေ အရင်ဆွဲရမည်။
text
ORIGIN TO CDN EDGE LOCATIONS TO NEARBY USER
-------------------------------------------
                  +--------------------+
                  |   Origin Server    |
                  +--------------------+
                    |        |        |
                  Tokyo  Frankfurt Singapore
                  Edge     Edge      Edge
                    |                  |
                 User A             User B
             (cached, fast)      (cached, fast)

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

Static site နဲ့ frontend app အတွက် modern hosting platform အများစုက content ရှေ့မှာ CDN ကို automatic ထားပေးပါတယ် — တစ်ခုခုပေါ် deploy လုပ်ရုံနဲ့ extra configuration မလိုပဲ ဒီအကျိုးရရှိပါတယ်။

Image၊ font၊ compile လုပ်ထားတဲ့ JS/CSS bundle တွေက visitor တိုင်းအတွက် အတူတူဖြစ်လို့ candidate ကောင်းတွေပါ။ User တစ်ယောက်ရဲ့ private data ပါတဲ့ response ကတော့ candidate ညံ့ပါတယ် — တခြားတစ်ယောက်ဆီ serve လုပ်ရင် real bug ဖြစ်သွားပါတယ်။

Cache ဘယ်လောက်ကြာမလဲ၊ ဘယ်လို invalidate လုပ်မလဲဆိုတဲ့ တိကျတဲ့ rule တွေက cache-control header နဲ့ HTTP mechanics ပါဝင်ပါတယ် — Networking tutorial က load balancing နဲ့ proxies ကို လိုအပ်တဲ့ depth အထိ ဆွေးနွေးထားပါတယ်။

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

javascript
const cache = {};

function originFetch(path) {
  console.log(`  (origin fetch for ${path})`);
  return `content of ${path}`;
}

function getFromCdn(path) {
  if (cache[path]) {
    return { result: cache[path], status: "HIT" };
  }
  const fresh = originFetch(path);
  cache[path] = fresh;
  return { result: fresh, status: "MISS" };
}

console.log(getFromCdn("/logo.png"));
console.log(getFromCdn("/logo.png"));
console.log(getFromCdn("/style.css"));
You should see
  (origin fetch for /logo.png)
{ result: 'content of /logo.png', status: 'MISS' }
{ result: 'content of /logo.png', status: 'HIT' }
  (origin fetch for /style.css)
{ result: 'content of /style.css', status: 'MISS' }
(/logo.png ကို ဒုတိယအကြိမ် တောင်းဆိုတဲ့အခါ origin fetch ထပ်မခေါ်တော့ဘဲ HIT ရလာတာကို သတိပြုပါ)

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

Lookup sequence ထဲ တတိယ path ကွဲပြားတစ်ခု ထည့်ပြီး တစ်ကြောင်းတည်း နှစ်ကြိမ် တောင်းဆိုကြည့်ပါ။ Code ကို run မကြည့်ခင် ဘယ်ခေါ်မှု '(origin fetch...)' ကို print လုပ်မလဲဆိုတာ ခန့်မှန်းကြည့်ပါ။

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

Personalized ဒါမှမဟုတ် private response တစ်ခုကို shared content လိုမျိုး cache လုပ်မိတာ — user တစ်ယောက်ရဲ့ data ကို တခြားတစ်ယောက်ဆီ ပေါက်ကြားစေနိုင်ပါတယ်။

CDN တိုင်းနောက်ကွယ်မှာ cache miss ဆက်ရှိနေတယ်ဆိုတာ မေ့နေတာ — origin server ကို ဆက်လက် reachable ဖြစ်အောင်၊ ဒီ traffic ကို ကိုင်တွယ်နိုင်အောင် ထားရမှာပါ။

CDN — MDN Web Docs GlossaryCloud & Deployment

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

  • Personalized ဒါမှမဟုတ် private response တစ်ခုကို shared content လိုမျိုး cache လုပ်မိတာ — user တစ်ယောက်ရဲ့ data ကို တခြားတစ်ယောက်ဆီ ပေါက်ကြားစေနိုင်ပါတယ်။
  • CDN တိုင်းနောက်ကွယ်မှာ cache miss ဆက်ရှိနေတယ်ဆိုတာ မေ့နေတာ — origin server ကို ဆက်လက် reachable ဖြစ်အောင်၊ ဒီ traffic ကို ကိုင်တွယ်နိုင်အောင် ထားရမှာပါ။
  • Localhost မှာ အလုပ်လုပ်တာနဲ့ Production မှာ အလိုအလျောက်အလုပ်လုပ်မယ်လို့ မယူဆပါနှင့် — environment, network, database, security ကွာခြားချက်တွေ ရှိနိုင်ပါတယ်။

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

Lookup sequence ထဲ တတိယ path ကွဲပြားတစ်ခု ထည့်ပြီး တစ်ကြောင်းတည်း နှစ်ကြိမ် တောင်းဆိုကြည့်ပါ။ Code ကို run မကြည့်ခင် ဘယ်ခေါ်မှု '(origin fetch...)' ကို print လုပ်မလဲဆိုတာ ခန့်မှန်းကြည့်ပါ။

You'll know it worked when: (origin fetch for /logo.png) { result: 'content of /logo.png', status: 'MISS' } { result: 'content of /logo.png', status: 'HIT' } (origin fetch for /style.css) { result: 'content of /style.css', status: 'MISS' } (/logo.png ကို ဒုတိယအကြိမ် တောင်းဆိုတဲ့အခါ origin fetch ထပ်မခေါ်တော့ဘဲ HIT ရလာတာကို သတိပြုပါ)

CDN နှင့် Content Delivery | Thuta Learning