Thuta Learning
ရှာဖွေရန်
Redis
IntermediateData & Databasesbeginner

Streams နှင့် Consumer Groups

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

  • Streams နှင့် Consumer Groups concept ကို နားလည်ရှင်းပြနိုင်ရန်
  • နမူနာ Redis command/code ကို ကိုယ်တိုင် run ပြီး output စစ်နိုင်ရန်
  • Tutorial Platform project နှင့် production scenario တွင် မှန်ကန်စွာအသုံးချနိုင်ရန်

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

Redis Stream က ordered entry IDs ပါသော append-only log ဖြစ်ပါတယ်။ Consumer group က messages ကို consumers များကြားခွဲပေးပြီး delivered-but-unacknowledged entries ကို Pending Entries List တွင်ထားပါတယ်။ `XACK`၊ `XPENDING`၊ `XAUTOCLAIM` နဲ့ failure recovery လုပ်နိုင်သော်လည်း handler ကို idempotent ထားရပါတယ်။

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

Tutorial publish jobs ကို `stream:tutorial-events` ထဲ `XADD` လုပ်ပြီး notification group က `XREADGROUP` ဖြင့်ဖတ်မယ်။ Email send success ပြီးမှ `XACK` လုပ်မယ်။ Crashed consumer pending jobs ကို idle threshold နောက် `XAUTOCLAIM` လုပ်ပြီး stream ကို `MAXLEN ~` ဖြင့် bounded ထားမယ်။

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

shell
XGROUP CREATE stream:tutorial-events notifications $ MKSTREAM
XADD stream:tutorial-events MAXLEN ~ 100000 * type published tutorialId 42
XREADGROUP GROUP notifications worker-1 COUNT 10 BLOCK 5000 STREAMS stream:tutorial-events >
XPENDING stream:tutorial-events notifications
XACK stream:tutorial-events notifications 1787875200000-0
XAUTOCLAIM stream:tutorial-events notifications worker-2 60000 0-0 COUNT 10
You should see
Group workers များ job ခွဲယူ၊ acknowledge နဲ့ stale pending recovery လုပ်နိုင်မည်။

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

At-least-once email worker အတွက် idempotency key နဲ့ dead-letter policy ရေးပါ။

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

`XREADGROUP` ရပြီးချင်း ACK လုပ်ရင် processing crash အချိန် job ပျောက်သွားပါတယ်။ Side effect success ပြီးမှ ACK လုပ်ပါ။

Redis — StreamsRedis

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

  • `XREADGROUP` ရပြီးချင်း ACK လုပ်ရင် processing crash အချိန် job ပျောက်သွားပါတယ်။ Side effect success ပြီးမှ ACK လုပ်ပါ။
  • နမူနာ command ကို production Redis ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test instance နဲ့ recoverable data ပေါ်တွင် အရင်အတည်ပြုပါ။

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

At-least-once email worker အတွက် idempotency key နဲ့ dead-letter policy ရေးပါ။

You'll know it worked when: Group workers များ job ခွဲယူ၊ acknowledge နဲ့ stale pending recovery လုပ်နိုင်မည်။

Streams နှင့် Consumer Groups | Thuta Learning