Thuta Learning
System Design
ExercisesProgrammingintermediate

URL Shortener Trade-off Audit

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

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

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

ဒီ course တစ်လျှောက်လုံးရဲ့ system-design decision တိုင်းဟာ မေးခွန်းသုံးခုတည်းအထိ ကျဉ်းသွားပါတယ်၊ ပြီးတော့ decision တစ်ခုကို နောက်ကျမှ review လုပ်တယ်ဆိုတာ original ရွေးချယ်မှုကို ယုံကြည်နေမယ့်အစား ဒီမေးခွန်းတွေကို ရှင်းရှင်းလင်းလင်း ဖြေရမှာပါ။ ပထမမေးခွန်း — ဒီရွေးချယ်မှုက ဘာကုန်ကျစေလဲ (complexity, latency, durability, operational burden, ဒါမှမဟုတ် ငွေကြေး)? ဒုတိယ — ဘာရရှိစေလဲ (များသောအားဖြင့် simplicity, shipping မြန်နှုန်း, ဒါမှမဟုတ် scale နည်းတဲ့အချိန်မှာ ကုန်ကျစရိတ် သက်သာမှု)? တတိယ — အများဆုံး ကျော်သွားတတ်တဲ့ မေးခွန်း — အဖြေက ဘယ်နေရာမှာ ပြောင်းပြန်လှန်သွားလဲ — ဘယ် request volume, data size, ဒါမှမဟုတ် uptime requirement မှာ စျေးသက်သာတဲ့ ရွေးချယ်မှုက တကယ်တမ်း မှားလာလဲ? in-memory dict ဟာ free ဖြစ်ပြီး မြန်ပါတယ်၊ process restart တစ်ခု ဖန်တီးထားတဲ့ short link တိုင်းကို ဖျက်ပစ်တဲ့အထိပေါ့။ auto-incrementing counter ဟာ server များစွာက code တွေ collision မဖြစ်ဘဲ ထုတ်ပေးဖို့ လိုအပ်မချင်း trivial ပါ။ cache မရှိတာဟာ viral link တစ်ခုက read traffic ရဲ့ 40% ကို မယူချင်းအထိ ရိုးရှင်းဆုံးပါ။ single server ဟာ product တစ်ခုလုံးကို ဖျက်ချနိုင်တဲ့ single point of failure ဖြစ်မလာချင်းအထိ ဈေးအသက်သာဆုံးပါ။ discipline ဆိုတာက ဘက်တစ်ဘက်ကိုပဲ ရွေးတာမဟုတ်ဘဲ crossover point ကို သတ်မှတ်ပေးဖို့ ဖြစ်ပါတယ်။

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

ဒီ course ထဲက URL shortener project ကိုပဲ အသုံးပြုထားတဲ့ Tutorial Platform ရဲ့ 'share this lesson' short-link feature ကို review လုပ်နေတယ်လို့ ယူဆပါ။ original engineer က in-memory dict, auto-incrementing counter, cache မရှိခြင်း, single server ကို ရွေးခဲ့ပါတယ် — feature launch တုန်းက တစ်ရက်ကို link ရာနှုန်းလောက်ပဲရှိတဲ့အချိန်မှာ ဒါဟာ ကျိုးကြောင်းညီညွတ်ပါတယ်။ reviewer အနေနဲ့ သင့်ရဲ့ အလုပ်က ရွေးချယ်မှုတွေကို 'မှား' လို့ ကြေညာဖို့ မဟုတ်ဘဲ Tutorial Platform ရဲ့ လက်ရှိ traffic က trade-off ပြောင်းလဲသွားတဲ့ scale ကို ကျော်သွားပြီလားဆိုတာ စစ်ဆေးဖို့၊ ဘယ်ရွေးချယ်မှုတွေက ဆက်ခိုင်မာနေသေးလဲ ဘယ်တွေကို production မှာ feature ကျိုးပေါက်မသွားခင် ပြန်ကြည့်ဖို့လိုနေပြီလဲ ဆုံးဖြတ်ပေးဖို့ ဖြစ်ပါတယ်။

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

python
# Scenario 1: storage - in-memory dict vs a real database
# Tutorial Platform's URL shortener started with:
#   short_to_long = {}   # short_code -> original_url, held in process memory
# It handles 200 short links/day, all created and read within the same
# process lifetime. No restarts have happened in production yet.

# Scenario 2: short code generation - auto-incrementing counter vs hash-based codes
# counter = 0
# def make_short_code():
#     global counter
#     counter += 1
#     return base62_encode(counter)
# Runs on a single web server. Traffic has grown to 50,000 new links/day
# and the team is discussing adding a second server for redundancy.

# Scenario 3: caching - no cache vs a read cache for hot short codes
# def resolve(short_code):
#     return short_to_long[short_code]   # every redirect hits the dict/DB directly
# One influencer's shared link now accounts for ~40% of all redirect
# traffic in a single afternoon.

# Scenario 4: single server vs load balancing/replication
# The entire service - API, storage, code generation - runs on one EC2
# instance. It has handled all traffic since launch with 99.9% uptime,
# but the last outage (a routine OS patch reboot) took the whole
# shortener down for 6 minutes, breaking every shared link site-wide.
You should see
ဆုံးဖြတ်ချက်တစ်ခုစီအတွက် အဖြေတစ်ခုတည်းမရှိပါဘူး — audit က correct ဖြစ်ဖို့ဆိုရင် conclusion တစ်ခုစီမှာ concrete crossover condition ကို သတ်မှတ်ပေးရပါမယ် — in-memory dict ဟာ restart မဖြစ်သေးမချင်း (သို့) multi-server မလိုအပ်မချင်းပဲ ကောင်းတယ်၊ counter ဟာ server များစွာကနေ code generate လုပ်ရန် မလိုအပ်မချင်းပဲ ကောင်းတယ်၊ no cache ဟာ link တစ်ခုက read traffic ကို လွှမ်းမိုးမလာမချင်းပဲ ကောင်းတယ်၊ single server ဟာ outage ရဲ့ ကုန်ကျစရိတ်က redundancy ထည့်တဲ့ ကုန်ကျစရိတ်ထက် မကျော်မချင်းပဲ ကောင်းတယ် — ပြီးတော့ Tutorial Platform ရဲ့ လက်ရှိ scenario တွေ (50,000 links/day, traffic ရဲ့ 40% ယူထားတဲ့ viral link, production outage) က crossover တိုင်းကို ကျော်သွားပြီးသားဖြစ်တာကို ပြသနေပါတယ်။

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

အထက်က code ထဲက scenario လေးခုစီအတွက် audit note တိုတိုလေး ရေးပါ — အပိုင်း ၃ ပိုင်းပါဝင်ရမယ် - (၁) လက်ရှိ ရွေးချယ်မှုက Tutorial Platform ကို အခုချိန်မှာ ဘာတွေကုန်ကျစေလဲ၊ (၂) feature ပိုရိုးရှင်းတုန်းက ဘာကို ဝယ်ယူခဲ့လဲ၊ (၃) ဖော်ပြထားတဲ့ traffic/scale ဂဏန်းတွေက trade-off ပြောင်းသွားပြီလားဆိုတာ — ပြောင်းသွားပြီဆိုရင် ဘယ်လို replacement (ဥပမာ database, hash-based codes, read cache, load balancer) ကို အကြံပြုချင်လဲဆိုတာ တိကျစွာ ဖော်ပြပါ။

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

in-memory dict (သို့) single server ကို 'အစကတည်းက မှားနေတယ်' လို့ blanket rule အနေနဲ့ ကောက်ချက်ချခြင်း — ဒါက feature launch လုပ်တဲ့အချိန်က scale အတွက် ဒီရွေးချယ်မှုတွေဟာ မှန်ကန်ခဲ့တာကို လျစ်လျူရှုတာဖြစ်ပြီး 'database/load balancer ကို အမြဲသုံး' ဆိုတာက cargo-culted best practice ဖြစ်ပြီး analysis မဟုတ်ပါဘူး။

ဒီ fix (cache, database, replication) ကို ခိုင်လုံစေတဲ့ တိကျတဲ့ traffic ဂဏန်းကို မထောက်ပြဘဲ recommend လုပ်ခြင်း — crossover condition ကို မသတ်မှတ်ပေးတဲ့ trade-off audit ဟာ preference တစ်ခုသက်သက်ပဲဖြစ်ပြီး traffic ထပ်ပြောင်းသွားရင် ဒီ fix အတူတူပဲ မှန်နေဦးမလားဆိုတာ ခွဲခြားလို့ မရနိုင်ပါဘူး။

Wikipedia — Non-functional requirementSystem Design

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

  • in-memory dict (သို့) single server ကို 'အစကတည်းက မှားနေတယ်' လို့ blanket rule အနေနဲ့ ကောက်ချက်ချခြင်း — ဒါက feature launch လုပ်တဲ့အချိန်က scale အတွက် ဒီရွေးချယ်မှုတွေဟာ မှန်ကန်ခဲ့တာကို လျစ်လျူရှုတာဖြစ်ပြီး 'database/load balancer ကို အမြဲသုံး' ဆိုတာက cargo-culted best practice ဖြစ်ပြီး analysis မဟုတ်ပါဘူး။
  • ဒီ fix (cache, database, replication) ကို ခိုင်လုံစေတဲ့ တိကျတဲ့ traffic ဂဏန်းကို မထောက်ပြဘဲ recommend လုပ်ခြင်း — crossover condition ကို မသတ်မှတ်ပေးတဲ့ trade-off audit ဟာ preference တစ်ခုသက်သက်ပဲဖြစ်ပြီး traffic ထပ်ပြောင်းသွားရင် ဒီ fix အတူတူပဲ မှန်နေဦးမလားဆိုတာ ခွဲခြားလို့ မရနိုင်ပါဘူး။
  • Design decision တစ်ခုကို production system ပေါ် တိုက်ရိုက်မကျင့်သုံးမီ load/traffic assumption များကို အရင်အတည်ပြုပါ။

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

အထက်က code ထဲက scenario လေးခုစီအတွက် audit note တိုတိုလေး ရေးပါ — အပိုင်း ၃ ပိုင်းပါဝင်ရမယ် - (၁) လက်ရှိ ရွေးချယ်မှုက Tutorial Platform ကို အခုချိန်မှာ ဘာတွေကုန်ကျစေလဲ၊ (၂) feature ပိုရိုးရှင်းတုန်းက ဘာကို ဝယ်ယူခဲ့လဲ၊ (၃) ဖော်ပြထားတဲ့ traffic/scale ဂဏန်းတွေက trade-off ပြောင်းသွားပြီလားဆိုတာ — ပြောင်းသွားပြီဆိုရင် ဘယ်လို replacement (ဥပမာ database, hash-based codes, read cache, load balancer) ကို အကြံပြုချင်လဲဆိုတာ တိကျစွာ ဖော်ပြပါ။

You'll know it worked when: ဆုံးဖြတ်ချက်တစ်ခုစီအတွက် အဖြေတစ်ခုတည်းမရှိပါဘူး — audit က correct ဖြစ်ဖို့ဆိုရင် conclusion တစ်ခုစီမှာ concrete crossover condition ကို သတ်မှတ်ပေးရပါမယ် — in-memory dict ဟာ restart မဖြစ်သေးမချင်း (သို့) multi-server မလိုအပ်မချင်းပဲ ကောင်းတယ်၊ counter ဟာ server များစွာကနေ code generate လုပ်ရန် မလိုအပ်မချင်းပဲ ကောင်းတယ်၊ no cache ဟာ link တစ်ခုက read traffic ကို လွှမ်းမိုးမလာမချင်းပဲ ကောင်းတယ်၊ single server ဟာ outage ရဲ့ ကုန်ကျစရိတ်က redundancy ထည့်တဲ့ ကုန်ကျစရိတ်ထက် မကျော်မချင်းပဲ ကောင်းတယ် — ပြီးတော့ Tutorial Platform ရဲ့ လက်ရှိ scenario တွေ (50,000 links/day, traffic ရဲ့ 40% ယူထားတဲ့ viral link, production outage) က crossover တိုင်းကို ကျော်သွားပြီးသားဖြစ်တာကို ပြသနေပါတယ်။

URL Shortener Trade-off Audit | Thuta Learning