Thuta Learning
System Design
BasicProgrammingintermediate

Load Balancing

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

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

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

System တစ်ခုမှာ server အများကြီးရှိလာတဲ့အခါ (horizontal scaling ရဲ့ ရလဒ်ပါ) incoming request တစ်ခုချင်းစီကို ဘယ် server က ကိုင်တွယ်မလဲ ဆုံးဖြတ်ပေးမယ့် အရာတစ်ခု လိုအပ်ပါတယ် — ဒါက load balancer ပါ။ Load balancer တွေဟာ network stack ရဲ့ layer မတူညီတာအလိုက် အလုပ်လုပ်ကြပြီး ဒီ layer ဟာ သူတို့ရဲ့ speed နဲ့ intelligence ကို ဆုံးဖြတ်ပေးပါတယ်။ L4 (transport-layer) load balancer ဟာ request ရဲ့ actual content ကို ကြည့်ခြင်းမရှိဘဲ IP address နှင့် port ကိုသာ အခြေခံပြီး route ချပါတယ် — packet header တွေထက်ပိုပြီး parse လုပ်စရာမလိုတဲ့အတွက် CPU အားဖြင့် အလွန်မြန်ပြီး ချိုသာပါတယ်။ L7 (application-layer) load balancer ကတော့ actual HTTP request — URL path, headers, cookies — ကို inspect လုပ်ပြီး /api/search traffic အားလုံးကို search အတွက် optimize လုပ်ထားတဲ့ server တွေဆီ ပို့ခြင်းလို smarter routing decision များ ချနိုင်ပေမယ့် ဒီ inspection ဟာ request တစ်ခုချင်းစီအတွက် CPU ပိုကုန်ပါတယ်။ layer ရွေးချယ်မှု အပြင် algorithm ရွေးချယ်မှုလည်း ရှိပါသေးတယ် — round robin ဟာ server တွေကို fixed order နဲ့ အလှည့်ကျ ပတ်သွားတာဖြစ်ပြီး ရိုးရှင်းပေမယ့် server တစ်ခုချင်းစီ တကယ် busy နေလား ဆိုတာကို မသိပါဘူး၊ least connections ကတော့ new request တစ်ခုချင်းစီကို လက်ရှိ active connection အနည်းဆုံးရှိတဲ့ server ဆီ route ချပေးပြီး connection state ကို track လုပ်ရတဲ့ costs နဲ့ real load ကို adapt လုပ်ပေးပါတယ်။

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

Tutorial Platform ဟာ server တစ်ခုထက်ပိုပြီး run လာတဲ့အခါ load balancer တစ်ခုက သူတို့ရှေ့မှာ ရပ်ပြီး learner တစ်ယောက်ရဲ့ request ကို ဘယ် server က ဖြေမလဲ ဆုံးဖြတ်ပေးပါတယ်။ Round-robin ရိုးရိုးလေးကတော့ အစပိုင်းအတွက် ကောင်းပါတယ်၊ ဒါပေမယ့် request အချို့က ချိုသာ (cache ထားတဲ့ lesson page load ခြင်း) request အချို့က ကုန်ကျ (code playground execution run ခြင်း) ဖြစ်လာတဲ့အခါ round robin ဟာ heavy request တွေ ဆက်တိုက်ရလာတဲ့ server တစ်ခုကို overload ဖြစ်စေနိုင်ပါတယ် — ဒီအချိန်မှာ platform engineer တွေက least connections ဆီ ပြောင်းမှာ ဒါမှမဟုတ် playground request တွေကို ဒီ workload အတွက် CPU ပိုထားတဲ့ server တွေဆီ route ချမယ့် L7 balancer ကိုတောင် သုံးလာနိုင်ပါတယ်။

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

text
                     ROUND ROBIN                        LEAST CONNECTIONS
Client requests      (fixed rotation)                    (checks current load)
   |  |  |  |            |                                    |
   v  v  v  v             v                                    v
+------------------+                              +------------------+
|   Load Balancer   |                              |   Load Balancer   |
+------------------+                              +------------------+
   |       |       |                                 |       |       |
   v       v       v                                 v       v       v
+----+  +----+  +----+                            +----+  +----+  +----+
| S1 |  | S2 |  | S3 |                             | S1 |  | S2 |  | S3 |
|req1|  |req2|  |req3|                             |4conn| |9conn| |1conn|
|req4|  |req5|  |req6|                             |     | |     | |<-new|
+----+  +----+  +----+                            +----+  +----+  +----+
(cycles blindly,                                   (new request goes to S3,
 ignores real load)                                 the least-busy server)
You should see
Diagram က round robin ဟာ server load ကို သတိမမူဘဲ ကွင်းစားပတ်သွားပုံနှင့် least connections ဟာ busy မဆုံး server ဆီ တွက်ချက်ပြီး ပို့ပုံကို ယှဉ်ပြပါတယ်။

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

Server 3 ခု (request cost မတူ) ကို round robin နဲ့ ပို့ရင် ဘာဖြစ်နိုင်လဲ ခန့်မှန်းကြည့်ပြီး least connections နဲ့ ပို့ရင် ဘယ်လို ကွာခြားမလဲ ရေးချကြည့်ပါ။

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

Request cost အများကြီးကွာနေတဲ့ workload အတွက် round robin ကို သုံးလိုက်ရင် server တစ်ခုက heavy request ဆက်တိုက်ကံဆိုးရလို့ overload ဖြစ်နိုင်ပြီး ကပ်လျက်ရှိတဲ့ server တစ်ခုက almost idle ဖြစ်နေနိုင်ပါတယ်။

Smarter routing အတွက် L7 load balancer ကို နေရာအနှံ့ရွေးချယ်ပေမယ့် request တိုင်းကို inspect လုပ်ရတဲ့ CPU cost ကို ထည့်တွက်မထားရင် request volume အလွန်မြင့်ချိန်မှာ L7 balancer ကိုယ်တိုင်က bottleneck အသစ် ဖြစ်လာနိုင်ပါတယ်။

Wikipedia — Load balancing (computing)System Design

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

  • Request cost အများကြီးကွာနေတဲ့ workload အတွက် round robin ကို သုံးလိုက်ရင် server တစ်ခုက heavy request ဆက်တိုက်ကံဆိုးရလို့ overload ဖြစ်နိုင်ပြီး ကပ်လျက်ရှိတဲ့ server တစ်ခုက almost idle ဖြစ်နေနိုင်ပါတယ်။
  • Smarter routing အတွက် L7 load balancer ကို နေရာအနှံ့ရွေးချယ်ပေမယ့် request တိုင်းကို inspect လုပ်ရတဲ့ CPU cost ကို ထည့်တွက်မထားရင် request volume အလွန်မြင့်ချိန်မှာ L7 balancer ကိုယ်တိုင်က bottleneck အသစ် ဖြစ်လာနိုင်ပါတယ်။
  • Design decision တစ်ခုကို production system ပေါ် တိုက်ရိုက်မကျင့်သုံးမီ load/traffic assumption များကို အရင်အတည်ပြုပါ။

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

Server 3 ခု (request cost မတူ) ကို round robin နဲ့ ပို့ရင် ဘာဖြစ်နိုင်လဲ ခန့်မှန်းကြည့်ပြီး least connections နဲ့ ပို့ရင် ဘယ်လို ကွာခြားမလဲ ရေးချကြည့်ပါ။

You'll know it worked when: Diagram က round robin ဟာ server load ကို သတိမမူဘဲ ကွင်းစားပတ်သွားပုံနှင့် least connections ဟာ busy မဆုံး server ဆီ တွက်ချက်ပြီး ပို့ပုံကို ယှဉ်ပြပါတယ်။

Load Balancing | Thuta Learning