Database Scaling၊ Replication နဲ့ Sharding
Server တစ်လုံးကို စွမ်းရည်တိုးတာ၊ data ကို servers အများကြီးဆီ ကူးတာနဲ့ dataset ကို အပိုင်းခွဲပြီး servers အများကြီးမှာ ထားတာတို့ကို နှိုင်းယှဉ်ပါ။
Code စမ်းလို့ရတဲ့ သင်ခန်းစာ ဖွင့်ရန်
ဒီစာမျက်နှာမှာ
ဘယ်အပိုင်းမှာ စွမ်းရည် ကန့်သတ်လာလဲ စစ်ခြင်း
Query ကြာချိန်၊ CPU၊ memory၊ disk သုံးစွဲမှုနဲ့ အဖတ်အရေးပမာဏကို ကြည့်ပါ။ Server တစ်လုံးကို CPU၊ memory သို့မဟုတ် storage တိုးပေးတာက vertical scaling ပါ။ အလုပ်မျှဝေဖို့ server တွေ ထပ်သုံးတာက horizontal scaling ပါ။
-- Find the queries that use the most shared block reads.
-- Requires pg_stat_statements to be enabled.
SELECT calls, total_exec_time, shared_blks_read, query
FROM pg_stat_statements
ORDER BY shared_blks_read DESC
LIMIT 5;
Replication- Primary server က အပြောင်းအလဲတွေကို တခြား servers ဆီ ကူးပေးတယ်။ Read replica က ဖတ်တဲ့ request တွေကို ကိုင်တွယ်နိုင်ပြီး standby ကို failover အတွက် သုံးနိုင်တယ်။ Asynchronous replication မှာ နောက်ဆုံးရေးထားတာ replica မှာ ပေါ်လာဖို့ အချိန်နည်းနည်း ကြာနိုင်တယ်။
Table partitioning- Table တစ်ခုကို လအလိုက်လိုမျိုး အပိုင်းခွဲထားရင် သင့်တော်တဲ့ query တွေမှာ မဆိုင်တဲ့အပိုင်းကို engine က ကျော်နိုင်တယ်။ ဒီ partitions တွေကို server တစ်လုံးတည်းမှာလည်း ထားနိုင်တယ်။
Sharding- Dataset တစ်ခုတည်းကို အပိုင်းခွဲပြီး မတူတဲ့ servers တွေပေါ် ဖြန့်ထားတယ်။ Shard key က ဖတ်ရမယ့်၊ ရေးရမယ့် shard ကို ရွေးဖို့ ကူညီတယ်။ Shard အများကြီးက data လိုတဲ့ query ဆိုရင် ရလဒ်တွေ ပြန်စုဖို့ အလုပ်ပိုလာနိုင်တယ်။
Inventory App မှာ Scaling ရွေးပုံ
Inventory app မှာ report ဖတ်တဲ့ အလုပ်ကို replica ဆီ ခွဲပေးနိုင်သလို သိမ်းရမယ့် မှတ်တမ်းတွေကိုလည်း ဂိုဒေါင်အလိုက် shard ခွဲနိုင်ပါတယ်။ Report ဖတ်တာ များပြီး data နောက်ကျနည်းနည်းကို လက်ခံနိုင်ရင် read replica သုံးလို့ အဆင်ပြေမပြေ စမ်းကြည့်ပါ။ Warehouse ID နဲ့ shard ခွဲထားရင် ဂိုဒေါင်တစ်ခုတည်းကို ဖတ်တဲ့ query က shard နည်းနည်းပဲ ဖတ်ရနိုင်ပြီး ဂိုဒေါင်အားလုံးပါတဲ့ report က shard အများကြီး ဖတ်ရနိုင်တယ်။ ဂိုဒေါင်တစ်ခုတည်းက traffic အများကြီးကို ကိုင်နေရလားဆိုတာလည်း တိုင်းပါ။
-- The backend can route this report to a read replica if
-- it can tolerate replication lag.
SELECT SUM(quantity) AS total_units
FROM stock_balances
WHERE warehouse_id = 3;
-- In a sharded design, warehouse_id = 3 also tells the
-- shard router which part of the inventory to query.