Thuta Learning
System Design
IntermediateProgrammingintermediate

Database Replication

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

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

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

Database instance တစ်ခုတည်းက CPU၊ memory၊ disk I/O saturate မဖြစ်ခင် read query ဘယ်လောက်ကို ကိုင်တွယ်နိုင်တာမှာ ကန့်သတ်ချက်ရှိပါတယ် — machine ကြီးတစ်ခု ဝယ်လိုက်တာ (vertical scaling) ကလည်း ceiling ရှိပြီး ဈေးနှုန်းလည်း မြင့်တက်လာပါတယ်။ Leader-follower (primary-replica) replication ကတော့ node တစ်ခုကို leader အဖြစ် သတ်မှတ်ပြီး write အားလုံးကို လက်ခံစေကာ leader ကရတဲ့ change တိုင်းကို follower node တစ်ခု ဒါမှမဟုတ် တစ်ခုထက်ပိုပြီး stream ပို့ကာ synchronized copy တွေ ဖြစ်စေတဲ့ ဒီပြဿနာကို ဖြေရှင်းပေးပါတယ်။ ဒီနောက် read traffic ကို leader နဲ့ follower အားလုံးမှာ ခွဲပေးနိုင်ပြီး replica အရေအတွက်နဲ့ အလားတူ read capacity ကို တိုးပေးနိုင်ကာ write ကတော့ leader တစ်ခုတည်းကို ဖြတ်စေတဲ့အတွက် write consistency ကို နားလည်ရလွယ်ကူစေပါတယ်။ Catch ကတော့ replication lag ပါ — follower ကို change stream ပို့ဖို့ time ကုန်ရတဲ့အတွက် write တစ်ခုက leader မှာ ရောက်ပြီးမကြာခင် follower ကနေ stale data ကို ခဏတာ serve ပေးနေနိုင်ပါတယ် — classic symptom ကတော့ user တစ်ယောက် form တင်ပြီး reload လုပ်တာနဲ့ own change ကို မမြင်ရတာ (read ကို lagging replica ဆီ route လုပ်ခဲ့လို့) — ဒါကို 'read-your-own-writes' ပြဿနာလို့ခေါ်ပါတယ်။ ဒီလို tolerate မလုပ်နိုင်တဲ့ application တွေက read တချို့ကို leader ဆီပြန် route လုပ်ရပါတယ်။

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

Tutorial Platform ကြီးထွားလာတာနဲ့အမျှ course/lesson browse လုပ်တာက write (enrollment၊ quiz answer တင်တာ) ထက် read traffic ပိုများလာပါတယ် — ဒါကြောင့် read replica ထည့်ပါတယ်၊ leader က write ကိုင်တွယ်ပြီး lesson-page read တွေကို follower အများနဲ့ load-balance လုပ်ကာ write logic ကို မထိအောင် read capacity ကို တိုးပေးနိုင်ပါတယ်။ ဒါပေမဲ့ learner တစ်ယောက် quiz answer တင်ပြီးတာနဲ့ 'results' page ကို redirect လုပ်တဲ့အခါ lagging replica ကနေ read လုပ်ရင် ဟောင်းနေတဲ့ unanswered state ကို ခဏတာမြင်ရနိုင်ပါတယ် — ဒီအတွက် platform က ဒီ read တစ်ခုချင်းကို leader ဆီပြန် route လုပ်တာ (ဒါမှမဟုတ် replica catch-up ကို စောင့်တာ) ဖြင့် learner ကိုယ်တိုင် တင်ခဲ့တဲ့ submission ကို ချက်ချင်းမြင်ရအောင် အာမခံပေးပါတယ်။

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

text
                 WRITES
                   |
                   v
            +--------------+
            |   LEADER     |
            +--------------+
              /    |    \
     replicate  replicate  replicate (lag: ~50-200ms)
            /       |        \
           v        v         v
     +---------+ +---------+ +---------+
     |Follower1| |Follower2| |Follower3|
     +---------+ +---------+ +---------+

          READ TRAFFIC (spread across all 4 nodes)
     Leader <-- reads --> F1 <-- reads --> F2 <-- reads --> F3

  Just after a write lands on Leader, F1/F2/F3 may briefly
  return the OLD value until replication catches up ("lag").
You should see
Read traffic ကို node ၄ ခုအထိ ခွဲပေးနိုင်တာက read capacity ကို တိုးစေပေမယ့် replication lag ကြောင့် follower တွေက write ပြီးချက်ချင်းမှာ stale data ပြနိုင်ကြောင်း ပြသပါတယ်။

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

Tutorial Platform မှာ instructor တစ်ယောက် lesson content ကို edit လုပ်ပြီး save နှိပ်ပြီးတာနဲ့ page ကို ချက်ချင်း reload လုပ်ရင် edit မမြင်ရနိုင်ဘူးဆိုပါစို့။ ဒီပြဿနာကို ဘယ်လိုဖြေရှင်းမလဲ ရွေးချယ်နိုင်တဲ့ ဗျူဟာ ၂ ခု စဉ်းစားပါ။

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

Write တစ်ခု လုပ်ပြီးချက်ချင်း follower ကနေ ချက်ချင်း read ပြန်ဖတ်ရင် stale data ရနိုင်တယ်ဆိုတာကို မထည့်စဉ်းစားဘဲ 'read scale ဖြစ်ပြီ' လို့ယူဆတတ်ပါတယ်။

Follower ပေါ်မှာ write ပို့ကြည့်တာ (follower သည် read-only ဖြစ်ရမည်) ကို misconfiguration ကြောင့် ဖြစ်တတ်ပြီး data consistency ပျက်စီးစေနိုင်ပါတယ်။

Wikipedia — Replication (computing)System Design

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

  • Write တစ်ခု လုပ်ပြီးချက်ချင်း follower ကနေ ချက်ချင်း read ပြန်ဖတ်ရင် stale data ရနိုင်တယ်ဆိုတာကို မထည့်စဉ်းစားဘဲ 'read scale ဖြစ်ပြီ' လို့ယူဆတတ်ပါတယ်။
  • Follower ပေါ်မှာ write ပို့ကြည့်တာ (follower သည် read-only ဖြစ်ရမည်) ကို misconfiguration ကြောင့် ဖြစ်တတ်ပြီး data consistency ပျက်စီးစေနိုင်ပါတယ်။
  • Design decision တစ်ခုကို production system ပေါ် တိုက်ရိုက်မကျင့်သုံးမီ load/traffic assumption များကို အရင်အတည်ပြုပါ။

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

Tutorial Platform မှာ instructor တစ်ယောက် lesson content ကို edit လုပ်ပြီး save နှိပ်ပြီးတာနဲ့ page ကို ချက်ချင်း reload လုပ်ရင် edit မမြင်ရနိုင်ဘူးဆိုပါစို့။ ဒီပြဿနာကို ဘယ်လိုဖြေရှင်းမလဲ ရွေးချယ်နိုင်တဲ့ ဗျူဟာ ၂ ခု စဉ်းစားပါ။

You'll know it worked when: Read traffic ကို node ၄ ခုအထိ ခွဲပေးနိုင်တာက read capacity ကို တိုးစေပေမယ့် replication lag ကြောင့် follower တွေက write ပြီးချက်ချင်းမှာ stale data ပြနိုင်ကြောင်း ပြသပါတယ်။