#DistributedSystems

پیاده‌سازی پروتکل اجماع Raft و چالش‌های حل پارتیشن شبکه در ذخیره‌سازهای غیرمتمرکز

۱۴ آبان ۱۴۰۳ • ۷ دقیقه

انتخاب رهبر در Raft با تایمرهای تصادفی شروع می‌شود تا چند نود هم‌زمان خود را نامزد نکنند. هر نامزد شماره دوره را بالا می‌برد و از بقیه رأی می‌خواهد. رأی فقط وقتی داده می‌شود که لاگ نامزد حداقل به اندازه لاگ رأی‌دهنده تازه باشد.

وقتی شبکه نصف می‌شود، بخش اقلیت نمی‌تواند حد نصاب جمع کند و رهبرش کنار می‌رود. بخش اکثریت رهبر جدید انتخاب می‌کند و نوشتن را ادامه می‌دهد. بعد از وصل شدن دوباره، اقلیت لاگ عقب‌مانده را از رهبر می‌گیرد و ورودی‌های ناسازگار را کنار می‌گذارد.

در ذخیره‌ساز کلید-مقدار، همین لاگ به WAL وصل می‌شود. هر نوشتن اول به لاگ می‌رود و فقط بعد از ثبت روی اکثریت نودها اعمال می‌شود. این ترتیب جلوی از دست رفتن داده را می‌گیرد، حتی اگر رهبر وسط کار قطع شود.

#DistributedSystems

Implementing the Raft Consensus Protocol and the Challenge of Resolving Network Partitions in Decentralized Stores

Nov 4, 2024 • 7 min read

Raft starts leader election with randomized timeouts so two nodes are less likely to campaign in the same term. A candidate increments the term and asks the cluster for votes. A follower grants that vote only when the candidate log is at least as fresh as its own.

A network partition leaves the minority unable to gather a quorum, so its leader steps down. The majority elects a new leader and keeps accepting writes. When the link returns, the minority copies the longer log and discards conflicting entries.

In a key-value store that log is the write-ahead log. A write is applied only after a majority has stored it. The leader can disappear mid-replication without losing committed data.