DDIA·واژه‌هاTERMS

نقشه راهRoadmap/ واژه‌نامهGlossary/ فصل ۱۰Chapter 10

۱۰10سازگاری و اجماعConsistency and Consensus

۱۷ واژه از فصل ۱۰ — هر کدام با یک مثال روزمره، یک نمودار و تلهٔ رایج.17 terms from chapter 10, each with an everyday example, a diagram and the usual trap.

وقتی سیم‌ها پاره می‌شوند، کدام را قربانی می‌کنی؟When the wire is cut, which one do you sacrifice?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

لیست خرید مشترک با همسرت روی دو گوشی است و او در طبقهٔ منفی سوپرمارکت آنتن ندارد. حالا دو راه داری: بگذاری هر دو نفر به لیست چیز اضافه کنند و بعداً بفهمید دو نسخهٔ مختلف دارید، یا اپلیکیشن را قفل کنی تا دو باره به هم وصل شوید و تا آن موقع هیچ‌کس چیزی اضافه نکند. قضیهٔ CAP می‌گوید وقتی دو نسخه نمی‌توانند با هم حرف بزنند، یا باید جواب بدهی و بگذاری دو نسخه فرق کنند، یا باید ساکت بمانی تا درست بمانی.You share a shopping list with your partner on two phones. She is down in the supermarket basement with no signal. You can let both of you keep adding items and find out later that you have two different lists, or you can freeze the app until you reconnect and let nobody add anything. The CAP theorem says that while the two copies cannot talk, those are the only two doors.

تشبیه و تصویر ذهنیThe analogy

این موقعیت را بزرگ کن: دو شعبه از یک فروشگاه که یک انبار مشترک دارند و ارتباطشان قطع شده؛ شعبهٔ شمال می‌گوید «موجودی هست» و جنس می‌فروشد و شعبهٔ جنوب هم همان جنس را به مشتری دیگری می‌فروشد، چون خبر ندارد. هر دو مشتری راضی‌اند، ولی آخر ماه یک جنس کم می‌آید و کسی نمی‌فهمد کجا گم شد. حالا حالت دوم: وقتی ارتباط قطع شد، هر دو شعبه در را می‌بندند و می‌گویند «الان نمی‌توانیم بفروشیم»؛ هیچ چیز گم نمی‌شود، ولی فروش هم صفر است. هیچ‌کس نمی‌تواند هر دو را داشته باشد، چون هر دو شعبه نمی‌توانند همزمان بفروشند و هم مطمئن باشند یک جنس دو بار فروخته نشده. تنها کاری که می‌شود کرد انتخاب است، و انتخاب باید از قبل و با چشم باز انجام شده باشد، نه وسط حادثه.Make it bigger: two branches of a shop share one stock room and the link between them dies. The north branch says “in stock” and sells the item; the south branch sells the same item to another customer because it never heard. Both customers are happy, and at the end of the month one item is missing and nobody knows where it went. Now the other choice: when the link dies, both branches close and say “we cannot sell right now”. Nothing is lost and nothing is sold. Nobody gets both, because both branches cannot sell and also be sure the same item was not sold twice. All you can do is choose, and the choice has to be made in advance, with your eyes open, not in the middle of the incident.

دسترس‌پذیر (AP)Available (AP)جواب می‌دهد، ممکن است کهنهanswers, may be staleدو نسخه دو حرف می‌زنندtwo copies disagreeادغام بعد از وصل شدنmerge after healingسازگار (CP)Consistent (CP)منتظر می‌ماند یا خطا می‌دهدwaits or errorsیک نسخه، یک حرفone copy, one storyدسترس‌پذیری را می‌بازدavailability is lost
یک شیء، دو انتخاب در قطع: یا جواب می‌دهی و کهنه می‌شوی، یا ساکت می‌مانی و درست.One object, two choices during a partition: answer and go stale, or stay silent and stay right.

و بعدش، فنیAnd now, technically

قضیهٔ CAP دربارهٔ یک شیء و یک عملیات است، نه دربارهٔ کل پایگاه داده. P یعنی قطع شبکه: پیام‌ها بین گره‌ها گم یا بی‌نهایت کند می‌شوند. C در این قضیه معنای دقیقی دارد: linearizability، یعنی هر خواندن آخرین نوشتنِ تأییدشده پیش از خودش را می‌بیند و سیستم مثل یک نسخهٔ واحد رفتار می‌کند. A یعنی هر گرهٔ سالم بالاخره به هر درخواست جواب می‌دهد؛ «سریع» جزو تعریف نیست، ولی جوابی که هرگز نمی‌آید جواب نیست. اثبات کوتاه است: یک نوشتن روی یک طرف قطع انجام می‌شود؛ اگر خواندن از طرف دیگر باید مقدار تازه را ببیند، چاره‌ای ندارد جز اینکه منتظر بماند و در آن مدت جواب ندهد، و اگر جواب بدهد ممکن است مقدار کهنه را برگرداند. پس در طول قطع، C و A با هم جمع نمی‌شوند. بیرون از قطع، قضیه هیچ حرفی ندارد؛ «انتخاب دو تا از سه» هم گمراه‌کننده است، چون P انتخاب تو نیست، وضعیت شبکه است. خیلی از سیستم‌ها در قطع نه C دارند نه A: نوشتنی که اکثریت را نمی‌بیند نه تأیید می‌شود نه رد، و خواندنی که منتظر می‌ماند هیچ‌کدام نیست. CA هم فقط برای سیستم تک‌گره معنا دارد و برای یک سیستم توزیع‌شدهٔ واقعی یک برچسب تبلیغاتی است.The CAP theorem is about one object and one operation, not about your whole database. P means a network partition: messages between nodes are lost or delayed indefinitely. C has a precise meaning here: linearizability, meaning every read sees the most recent acknowledged write before it and the system behaves like a single copy. A means every non-failed node eventually returns a response; “fast” is not part of the definition, but an answer that never comes is not an answer. The proof is short: a write lands on one side of the partition; a read on the other side either waits, and is therefore unavailable, or answers, and may return the old value. So during a partition you cannot have both. Outside a partition the theorem says nothing, and “pick two of three” misleads, because P is not your choice, it is the network's state. Plenty of systems are neither C nor A during a partition: a write that never reaches a majority is neither acknowledged nor refused, and a read that waits is neither. CA only makes sense for a single node; for a real distributed system it is a marketing label.

1 objectقضیه دربارهٔ یک شیء و یک عملیات است، نه یک تراکنش چندشیئیthe theorem covers one object and one operation, not a multi-object transaction
N/2 + 1اکثریت، معمول‌ترین راه نگه داشتن C در طول قطعa majority is the usual way to keep C through a partition

چرا «دو تا از سه» گمراه‌کننده استWhy “two out of three” misleads

P انتخاب تو نیست؛ قطع شبکه اتفاقی است که در شبکه می‌افتد، چه تو بخواهی چه نه. پس جملهٔ درست این است: «وقتی قطع شد، چه می‌کنی؟» نه «کدام دو تا را انتخاب کردی؟». این تفاوت فقط لفظی نیست. تیمی که فکر می‌کند CA انتخاب کرده، در اولین قطع واقعی کشف می‌کند نه C داشته نه A، چون هیچ رفتاری برای آن حالت طراحی نکرده است. جملهٔ درست را از اول بنویس: در قطع، کدام عملیات باید جواب بدهد، کدام باید خطا بدهد، و چه کسی آن خطا را به کاربر توضیح می‌دهد.P is not your choice; a partition is something the network does, whether you like it or not. So the honest sentence is “what do you do when it splits?” and not “which two did you pick?”. The difference is not semantic. A team that believes it chose CA discovers, in the first real partition, that it has neither C nor A, because it designed no behaviour for that state. Write the honest sentence down up front: during a partition, which operations must answer, which must fail, and who explains that failure to the user.

انتخاب، به‌ازای هر عملیاتThe choice is per operation

یک سیستم واحد می‌تواند در قطع برای یک عملیات C باشد و برای عملیاتی دیگر A. صفحهٔ محصول می‌تواند نسخهٔ کهنه نشان بدهد؛ سبد خرید و پرداخت نباید. انبار می‌تواند شمارش تقریبی بدهد؛ رزرو صندلی نه. این تصمیم را برای هر مسیر داده جدا بگیر و در همان کد بنویس، نه در ذهن تیم. اگر فقط یک جملهٔ کلی داشته باشی، در حادثه هر کس برداشت خودش را دارد و بعضی مسیرها تصادفی انتخاب می‌کنند.One system can be C for one operation during a partition and A for another. A product page may show a stale copy; the cart and the payment must not. Inventory may report an approximate count; seat reservation may not. Make that decision per data path and write it in the code, not in the team's memory. With one general sentence, everyone improvises during the incident and some paths choose at random.

تلهٔ رایجThe usual trap

برچسب زدن «AP» روی سیستم و خیال راحت. تیم می‌گوید «ما AP هستیم»، بعد در قطع شبکه معلوم می‌شود هیچ طرحی برای ادغام دو نسخه ندارد؛ دو سبد خرید یک مشتری با هم جمع می‌شوند و یک قلم دو بار حساب می‌شود. انتخاب AP نصف کار است؛ نصف دیگرش، قاعدهٔ حل تعارض و راه بازگشت به یک حالت یکدست است، و آن نصف معمولاً نوشته نشده.Labelling the system “AP” and calling it a day. The team says “we are AP”, and then a partition reveals that nobody designed a merge: two carts of the same customer get combined and one item is charged twice. Choosing AP is half the work; the other half is the conflict rule and the road back to one consistent state.

چند ماشین، یک تصمیم. اگر یکی ساکت باشد چی؟Several machines, one decision. What if one goes silent?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

شش نفرید و باید یک رستوران انتخاب کنید؛ اگر هر کس خودش برود، نصف شب دو نفر در دو جای مختلف منتظر می‌مانند. پس قرار می‌گذارید هر کس رأی بدهد و هر رستورانی که چهار رأی آورد همان است، حتی آن دو نفری که رأی نداده‌اند. بعد از اینکه میز را رزرو کردید دیگر نمی‌شود برگشت، حتی اگر کسی پشیمان شود؛ اجماع در سیستم هم همین است: چند ماشین روی یک تصمیم توافق می‌کنند و آن تصمیم دیگر عوض نمی‌شود.Six of you need to pick a restaurant. If everyone goes alone, two people end up waiting in two different places. So you agree that whoever gets four votes wins, and even the two who voted no have to come. Once the table is booked, it cannot be unbooked, even if someone changes their mind. Consensus in a system is the same: several machines settle on one decision, and that decision is not revisited.

تشبیه و تصویر ذهنیThe analogy

چرا چهار رأی کافی است و نه شش؟ چون اگر هر شش نفر لازم بود، یک نفر که گوشی‌اش خاموش است کل تصمیم را می‌خواباند و شما هیچ‌وقت شام نمی‌خورید. با اکثریت، دو غایب مشکلی نمی‌سازند و دو گروه هم نمی‌توانند همزمان دو رستوران مختلف را برنده کنند، چون هر گروه برای برنده شدن به چهار نفر از شش نفر نیاز دارد و دو مجموعهٔ چهارنفره حتماً یک نفر مشترک دارند. بهایش این است که اگر سه نفر این طرف شهر و سه نفر آن طرف باشند و ارتباط قطع شود، هیچ طرفی اکثریت ندارد و هر دو منتظر می‌مانند. تصمیم درست همین است: وقتی نمی‌توانی مطمئن باشی، تصمیم نگیر. ولی همین «منتظر ماندن» است که در تولید دردناک می‌شود.Why four votes and not six? If all six had to agree, one friend with a dead phone would block dinner forever. With a majority, two absentees do not matter, and two groups cannot both win at once, because each needs four of six and two sets of four always share at least one person. The price is the other case: three friends on one side of town and three on the other, with the phones down. Neither side has a majority, so both wait. That is the correct answer, refusing to decide when you cannot be sure, and it is exactly the answer that hurts in production.

سه رأی نوشتنthree write votesسه رأی خواندنthree read votesگره ۱node 1گره ۲node 2گره ۳node 3گره ۴node 4گره ۵node 5
اکثریت نوشتن و اکثریت خواندن حتماً یک گره مشترک دارند؛ همان گره تصمیم را می‌رساند.A write majority and a read majority always share a node, and that node carries the decision.

و بعدش، فنیAnd now, technically

اجماع یک مسئلهٔ مشخص است: چند فرایند هر کدام یک مقدار پیشنهاد می‌دهند و باید روی یک مقدار توافق کنند، با چهار شرط. توافق: هیچ دو فرایندی دو مقدار مختلف انتخاب نمی‌کنند؛ صحت: هر فرایند حداکثر یک بار تصمیم می‌گیرد. اعتبار: مقدار تصمیم یکی از پیشنهادهاست، نه یک مقدار پیش‌فرض. پایان: هر فرایند سالمی بالاخره تصمیم می‌گیرد. سه شرط اول ایمنی‌اند و هرگز نقض نمی‌شوند؛ شرط چهارم زنده‌بودن است و همان جایی است که مشکل پیدا می‌شود. در مدل کاملاً ناهمگام، بدون هیچ فرضی دربارهٔ سرعت شبکه و ساعت، و حتی با یک خرابی crash، FLP ثابت می‌کند هیچ الگوریتم قطعی‌ای نمی‌تواند هر سه را تضمین کند. برای همین Paxos و Raft و Zab با timeout کار می‌کنند: در عمل و روی شبکه‌ای که به‌قدر کافی همگام است پایان را می‌دهند، ولی روی کاغذ تضمینی نیست. هر تصمیم به اکثریت نیاز دارد: با 2f+1 گره، f خرابی تحمل می‌شود و هر تصمیم دست‌کم یک رفت‌وبرگشت به اکثریت و نوشتن روی دیسک می‌خواهد. اجماع با خرابی بیزانسی، یعنی گرهٔ دروغگو، کاری ندارد؛ مدل خرابی‌اش crash است.Consensus is a specific problem: n processes each propose a value, and they must agree on one. Agreement: no two processes decide differently. Integrity: each process decides at most once. Validity: the decided value is one of the proposals, not a default. Termination: every correct process eventually decides. The first three are safety and must never break; the fourth is liveness, and it is where the trouble lives. In a fully asynchronous model, with no assumption about network or clock speed, FLP shows that no deterministic algorithm can guarantee all of them with even one crash. So Paxos, Raft and Zab use timeouts: in practice, on a network that is eventually timely, they terminate, but that is not a paper guarantee. Every decision needs a majority: with 2f+1 nodes you survive f failures, and each decision costs at least one round trip to a majority and a write to disk. Consensus does not deal with Byzantine faults, meaning a lying node; its failure model is crash and recovery.

2f+1برای تحمل f خرابی به این تعداد گره نیاز داریthis many nodes to survive f failures
5 → 2پنج گره، دو خرابی همزمان را تحمل می‌کندfive nodes tolerate two failures at once
1 RTTدست‌کم یک رفت‌وبرگشت به اکثریت برای هر تصمیمat least one round trip to a majority per decision

چیزی که اجماع به تو نمی‌دهدWhat consensus does not give you

اجماع فقط دربارهٔ تصمیمی است که از آن می‌پرسی. اگر همهٔ نوشتن‌ها را از لاگ رد کنی، همه‌شان مرتب و برگشت‌ناپذیر می‌شوند؛ اگر فقط بخشی را رد کنی، بقیه هر وقت بخواهند می‌نویسند و هیچ تضمینی ندارند. اجماع یک لایهٔ جادویی نیست که زیر کل پایگاه داده بگذاری. تازه، هزینه‌اش با تعداد تصمیم‌ها بالا می‌رود: هر تصمیم یک رفت‌وبرگشت به اکثریت می‌خواهد، پس قرار دادن هر نوشتن کاربر روی اجماع، توان را به چند هزار در ثانیه می‌رساند، نه چند میلیون. جای درستش تصمیم‌های کم‌تعداد و پرارزش است: رهبر کیست، قفل از کیست، ترتیب دسته‌های بعدی چیست.Consensus only covers the decisions you actually send through it. Route every write through the log and they all become ordered and irrevocable; route only some and the rest write whenever they like, with no guarantee at all. Consensus is not a magic layer you slide under the whole database. Its cost also climbs with the number of decisions: each one needs a round trip to a majority, so putting every user write through consensus caps you at a few thousand per second, not a few million. The right place for it is the small, high-value decisions: who leads, who holds the lock, what the next batch order is.

تلهٔ رایجThe usual trap

تصمیم گرفتن با تعداد زوج گره. چهار گره را برای تحمل خرابی بیشتر می‌گذاری، ولی اکثریت چهار گره سه گره است، پس فقط یک خرابی را تحمل می‌کند: همان قدر که سه گره، با یک ماشین بیشتر و یک رفت‌وبرگشت کندتر؛ برای f خرابی به 2f+1 نیاز داری، نه 2f. یک ماشین اضافه خریدی و هیچ خرابی اضافه‌ای نخریدی.Sizing the cluster with an even number of nodes. You add a fourth node for more resilience, but a majority of four is three, so it tolerates one failure: exactly what three nodes tolerate, with one more machine and a slower round trip. For f failures you need 2f+1, not 2f. You bought a machine and no extra failure.

coordination service

سرویس هماهنگی فصل ۱۰Chapter 10 · دیدن در متنsee in context

کجای سیستم می‌نویسی «این یکی رئیس است»؟Where does a system write down who is in charge?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

در ساختمان بیست‌واحدی، یک دفترچهٔ کوچک دم در گذاشته‌اید که هر کس ماشین لباسشویی را برمی‌دارد، در آن می‌نویسد «واحد ۳، چهار تا شش». خود دفترچه تقریباً هیچ اطلاعاتی ندارد، ولی کل ساختمان به آن اعتماد دارد، چون تنها جایی است که ترتیب را مشخص می‌کند و همه همان را قبول دارند؛ اگر دو نفر همزمان یک ساعت را در دو دفترچه بنویسند، دعوا می‌شود. سرویس هماهنگی همین دفترچه است، برای ماشین‌ها.In a twenty-unit building there is a small notebook by the door. Whoever takes the washing machine writes “unit 3, four to six” in it. The notebook holds almost nothing, yet the whole building trusts it, because it is the one place that fixes the order and everyone accepts that order. If two people kept two notebooks, you would have a fight. A coordination service is that notebook, for machines.

تشبیه و تصویر ذهنیThe analogy

این دفترچه برای سه چیز عالی است: چه کسی کلید را دارد، نوبت شست‌وشو با کیست، و آدرس تعمیرکار کجاست. برای سه چیز فاجعه است: نگه‌داشتن آرشیو عکس‌های ساختمان، نوشتن قبض‌های سالانه، و هر چیزی که هر روز هزار بار عوض می‌شود. دلیلش ساده است: هر نوشتن در این دفترچه باید اول بین چند نفر تأیید شود تا همه یک نسخه داشته باشند، و همین تأیید کند است. اگر عکس‌ها را هم داخلش بریزی، هر آپلود عکس باید منتظر همان تأیید بماند و کل ساختمان کند می‌شود. سرویس هماهنگی را برای تصمیم‌های کوچک و کم‌تعداد نگه دار؛ دادهٔ اصلی جای دیگری است.The notebook is excellent for three things: who has the key, whose turn the washing machine is, and where the plumber lives. It is a disaster for three others: the building's photo archive, the annual bills, and anything that changes a thousand times a day. The reason is simple: every write has to be confirmed by several people so that everyone holds the same copy, and that confirmation is slow. Put the photos in it and every upload waits for the same confirmation, and the whole building slows down. Keep the coordination service for small, infrequent decisions; the real data lives elsewhere.

درخواست کلاینتClient requestکوچک و نادرsmall and rareرأی اکثریتMajority voteRaft یا ZabRaft or Zabنوشتن در لاگAppend to logمتادادهmetadataاعلان به بقیهNotify the restwatchwatch
هر نوشتن کوچک از اکثریت می‌گذرد؛ به همین دلیل این سرویس برای دادهٔ زیاد ساخته نشده.Every small write goes through a majority, which is why this service was never built for bulk data.

و بعدش، فنیAnd now, technically

سرویس هماهنگی یک پایگاه دادهٔ کوچک است که با اجماع تکثیر می‌شود و برای متاداده ساخته شده، نه برای دادهٔ کاربر. دو نمونهٔ رایج ZooKeeper و etcd هستند. ZooKeeper با پروتکل Zab کار می‌کند: نوشتن‌ها از رهبر می‌گذرند و خطی‌پذیرند، ولی خواندن‌ها به‌طور پیش‌فرض از همانند محلی جواب می‌گیرند و ممکن است کهنه باشند؛ اگر خواندن تازه لازم داری، اول sync صدا بزن. گره‌های ZooKeeper می‌توانند موقت باشند: به نشست کلاینت بسته‌اند و با قطع نشست یا پایان timeout محو می‌شوند؛ همین مکانیزم پایهٔ اجاره‌نامه و تشخیص زنده‌بودن است. گره‌های ترتیبی یک شمارندهٔ یکتا می‌دهند و watchها یک اعلان یک‌باره می‌فرستند. etcd با Raft کار می‌کند، خواندن خطی‌پذیر را پیش‌فرض دارد، و compare-and-set چندکلیدی و شمارهٔ revision می‌دهد. کارهای معمول: انتخاب رهبر، قفل توزیع‌شده، عضویت، کشف سرویس و پیکربندی. حجم داده در حد چند مگابایت و نوشتن در حد چند هزار در ثانیه است، نه بیشتر؛ هر نوشتن یک رفت‌وبرگشت به اکثریت است.A coordination service is a small database replicated by consensus, built for metadata rather than user data. ZooKeeper and etcd are the two you will meet. ZooKeeper runs Zab: writes go through the leader and are linearizable, but by default reads are served by the local follower and may be stale, so call sync before a read that must be fresh. Its nodes can be ephemeral, tied to a client session and gone when the session dies or the timeout expires, which is the basis for leases and failure detection. Sequential nodes hand out monotonic names, and watches deliver a single notification. etcd runs Raft, serves linearizable reads by default, and gives multi-key compare-and-set and a revision number. Typical jobs: leader election, distributed locks, membership, service discovery, configuration. The data is measured in megabytes and writes in the low thousands per second, no more, because every write is a round trip to a majority.

10–30 sبازهٔ معمول session timeout؛ کوتاه‌ترش باعث churn می‌شودa typical session timeout; shorter values cause churn
≤ 1 MBاندازهٔ داده‌ای که باید در آن نگه داری، نه بیشترthe scale of data you should keep there, no more
1 RTTهر نوشتن هماهنگی، یک رفت‌وبرگشت به اکثریتevery coordination write is a round trip to a majority

وقتی خودِ سرویس هماهنگی گلوگاه می‌شودWhen the coordination service becomes the bottleneck

این سرویس کوچک است و همان کوچکی‌اش آن را شکننده می‌کند. اگر هر پروسه‌ای در خوشه یک نشست باز کند و هر ثانیه یک watch بگذارد، ترافیک هماهنگی از ترافیک کاری بیشتر می‌شود و همان سرویسی که باید خرابی را مدیریت کند، خودش می‌افتد. قاعده ساده است: پیکربندی را یک بار بخوان و با watch تازه نگه دار، نه اینکه هر درخواست یک خواندن تازه بزند؛ قفل را فقط برای کارهای واقعاً انحصاری بگیر؛ و از آن به‌عنوان صف پیام یا دفتر رویداد استفاده نکن. اگر می‌بینی نوشتن‌های هماهنگی‌ات دارد زیاد می‌شود، احتمالاً داده‌ای را واردش کرده‌ای که جای دیگری داشت.The service is small, and that smallness is exactly what makes it fragile. If every process in the cluster opens a session and sets a watch every second, coordination traffic outgrows the traffic it coordinates, and the service meant to manage failures is the one that falls over. The rule is simple: read configuration once and keep it fresh with a watch instead of a fresh read per request; take a lock only for work that is truly exclusive; and never use it as a message queue or an event log. If your coordination writes are climbing, you have probably put data in it that belonged somewhere else.

تلهٔ رایجThe usual trap

گرفتن قفل بدون fencing token: کلاینت قفل را می‌گیرد، بعد برای چند ثانیه pause می‌شود، مثلاً وسط GC، و در آن فاصله قفل منقضی می‌شود؛ وقتی برمی‌گردد هنوز فکر می‌کند قفل دارد و می‌نویسد. سرویس هماهنگی درست کار کرده و دو نویسندهٔ همزمان داریم. تا وقتی خود منبع، یعنی دیسک یا حساب یا صف، شمارهٔ ترتیب قفل را نمی‌بیند و نوشتن قدیمی را رد نمی‌کند، آن قفل فقط یک تزئین است.Taking a lock with no fencing token. The client acquires the lock, pauses for a few seconds, perhaps in a GC, and the lock expires meanwhile; when it wakes up it still believes it holds the lock and writes. The coordination service did its job and you now have two concurrent writers. Until the resource itself — the disk, the account, the queue — sees the lock's order number and rejects the stale writer, the lock is decoration.

FLP impossibility

ناامکانی FLP فصل ۱۰Chapter 10 · دیدن در متنsee in context

یا مطمئن باش، یا همیشه جواب بده — هر دو با هم نمی‌شود.Be certain, or always answer — you cannot have both.

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

دو نفر با نامه با هم هماهنگ می‌کنند. هیچ‌کس نمی‌داند نامه‌اش چند روز طول می‌کشد یا اصلاً می‌رسد. اگر یکی از آن‌ها ساکت بماند، دیگری هیچ راهی ندارد که بفهمد «مرده» یا «نامه در راه است».Two people coordinate by post. Neither knows how long a letter takes or whether it arrives at all. If one goes quiet, the other has no way to tell “gone” from “letter in transit”.

تشبیه و تصویر ذهنیThe analogy

این مسئله را می‌شود در یک جمع خانوادگی هم دید: می‌خواهید تصمیم بگیرید کجا شام بخورید و همه باید هم‌زمان به یک جواب برسند. اگر یک نفر گوشی‌اش را جواب ندهد، شما بین دو حالت گیر می‌کنید: یا رفته، یا در آسانسور است و خبر ندارد. تا وقتی نتوانی «نبودن» را از «دیر رسیدن» تشخیص بدهی، هیچ قاعده‌ای نمی‌تواند تو را مطمئن کند.You can see the same knot at a family dinner: you all want one restaurant, and everyone must reach the same answer. If one person does not pick up the phone you are stuck between two states: gone, or in the lift and unaware. Until you can tell absence from delay, no rule can make you certain.

و بعدش، فنیAnd now, technically

قضیهٔ FLP می‌گوید در مدلِ کاملاً ناهمزمان — یعنی بدون هیچ کرانِ زمانی برای رسیدن پیام — هیچ الگوریتمِ قطعی‌ای نمی‌تواند در حضور حتی یک فرآیندِ خراب، هم ایمنی را حفظ کند و هم همیشه تمام شود. توجه کن که این قضیه «هیچ اجماعی ممکن نیست» نمی‌گوید؛ می‌گوید نمی‌توانی هر دو را با هم تضمین کنی. پیاده‌سازی‌های عملی از همین شکاف استفاده می‌کنند: یک فرضِ زمانی اضافه می‌کنند (مهلت، ضربان قلب، انتخابِ رهبر با تصادفی‌سازی) تا سیستم در عمل پیش برود، و در بدترین حالت ممکن است کند شود یا دوباره انتخاب کند، ولی هرگز دو تصمیمِ متناقض نمی‌گیرد.The FLP result says that in a fully asynchronous model — no timing bound on message delivery — no deterministic algorithm can guarantee both safety and termination in the presence of even one faulty process. Note what it does not say: it does not say consensus is impossible. It says you cannot have both guarantees at once. Real systems exploit that gap by adding a timing assumption — timeouts, heartbeats, randomized leader election — so progress happens in practice and, in the worst case, the system stalls or re-elects, but never decides two contradictory things.

1 faultهمین یک فرآیندِ خراب کافی است تا تضمینِ دوگانه از بین برودone faulty process is enough to break the double guarantee

تلهٔ رایجThe usual trap

خواندنِ FLP به‌عنوان «اجماع کار نمی‌کند». این قضیه دربارهٔ مدلی حرف می‌زند که در آن هیچ کرانِ زمانی وجود ندارد؛ سیستم واقعی همیشه کرانی دارد، حتی اگر بد باشد. خطر واقعی این است که آن کران را نشناسد و به آن اتکا کند.Reading FLP as “consensus does not work”. The theorem is about a model with no timing bound; a real system always has one, even a bad one. The real risk is relying on a bound you never measured.

happens-before

رخ‌دادن پیش از فصل ۱۰Chapter 10 · دیدن در متنsee in context

کدام اول بود؟ همیشه نمی‌شود گفت.Which came first? Sometimes nobody can say.

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

به برادرت پیام می‌دهی «الان راه می‌افتم» و بعد واقعاً راه می‌افتی؛ او نمی‌تواند قبل از خواندن پیام تو را دم در ببیند، چون پیام واقعاً پیش‌تر رفته است. ولی همان روز مادرت در شهر دیگری یک بسته پست می‌کند و هیچ‌کس نمی‌تواند بگوید بستهٔ او قبل از پیام تو بود یا بعدش. بعضی چیزها به هم وصل‌اند و ترتیب دارند، بعضی فقط همزمان‌اند؛ مفهوم happens-before دقیقاً همین خط کشیدن است: کجا ترتیب واقعی داریم و کجا نداریم.You text your brother “I'm leaving now” and then you actually leave. He cannot see you at the door before reading the text, because the text really did go first. The same morning your mother, in another city, posts a parcel, and nobody can say whether her parcel came before or after your text. Some events are chained and have an order; others are merely simultaneous. Happens-before is exactly that line: where a real order exists, and where it does not.

تشبیه و تصویر ذهنیThe analogy

حالا ساعت را از معادله بیرون بیاور. فرض کن ساعت گوشی تو ده دقیقه جلوتر است؛ پیام تو با زمان ۱۰:۱۰ ثبت می‌شود و بستهٔ مادرت با ۱۰:۰۵، در حالی که او بعد از پیام تو پست کرده؛ اگر به آن دو عدد اعتماد کنی، ترتیب را برعکس می‌خوانی. ترتیب علّی به ساعت کاری ندارد: فقط نگاه می‌کند که آیا زنجیره‌ای از رویدادها یکی را به دیگری وصل می‌کند یا نه. درون یک فرایند، این زنجیره همه‌چیز را مرتب می‌کند، چون هر کار بعد از کار قبلی همان فرایند است، و بین دو فرایند فقط پیام‌ها زنجیر می‌سازند. و اگر هیچ زنجیری نباشد، دو رویداد همزمان‌اند، حتی اگر ساعتشان یکی باشد یا یکی چند ثانیه دیرتر ثبت شده باشد.Now take the clock out of the equation. Suppose your phone is ten minutes fast: your text is stamped 10:10 and your mother's parcel 10:05, even though she posted it after you texted. Trust those two numbers and you read the order backwards. Causal order does not care about clocks; it only asks whether a chain of events connects one to the other. Inside one process the chain orders everything, because each step follows the previous one. Between two processes, only messages build links. And if no chain exists, the two events are concurrent, even if their timestamps are identical or seconds apart.

نوشتن AWrite Aفرایند ۱process 1ارسال پیامSend messageهمان فرایندsame processدریافت پیامReceive messageفرایند ۲process 2نوشتن BWrite Bپس از دریافتafter receive
زنجیرهٔ A تا B یک ترتیب واقعی می‌سازد؛ هر رویدادی که بیرون این زنجیره باشد، با آن همزمان است.The chain from A to B is a real order; anything outside that chain is concurrent with it.

و بعدش، فنیAnd now, technically

happens-before، که لمپورت تعریفش کرد، یک ترتیب جزئی است: a → b اگر یکی از سه حالت برقرار باشد. اول، a و b در یک فرایند باشند و a در برنامه پیش از b بیاید. دوم، a ارسال یک پیام باشد و b دریافت همان پیام. سوم، زنجیره‌ای از این دو حالت a را به b وصل کند، که همان تراگذری است. این رابطه بازتابی نیست، تراگذری است، و برای هر دو رویداد لزوماً برقرار نیست؛ اگر نه a → b و نه b → a، دو رویداد همزمان (concurrent) هستند. همین ترتیب جزئی چیزی است که مدل‌های سازگاری با آن تعریف می‌شوند: سازگاری علّی یعنی هیچ گره‌ای نوشتنی را پیش از نوشتنی که علت آن است تحویل ندهد. نکتهٔ مهم این است که happens-before زمان نیست و ترتیب کل هم نیست؛ دو ناظر مختلف می‌توانند دو رویداد همزمان را در دو ترتیب متفاوت ببینند و هر دو درست باشند. برای اینکه ترتیب کل داشته باشی، یک چیز باید آن را تحمیل کند: یک رهبر، یک شمارنده، یا یک دور اجماع. اسم این رابطه در مدل حافظهٔ جاوا هم به کار می‌رود، ولی آنجا بحث دیده‌شدن نوشتن‌ها بین threadهاست، نه علیت بین ماشین‌ها.Happens-before, as Lamport defined it, is a partial order: a → b if one of three things holds. First, a and b are in the same process and a comes before b in the program. Second, a is the sending of a message and b is the receiving of it. Third, a chain of such steps connects a to b, which is transitivity. The relation is irreflexive and transitive, and it does not relate every pair: when neither a → b nor b → a holds, the two events are concurrent. That partial order is what consistency models are written in terms of: causal consistency means no node delivers a write before a write that caused it. The important part is that happens-before is not time and not a total order; two observers can see two concurrent events in different orders and both be right. To get a total order, something has to impose it: a leader, a counter, or a consensus round. The same name appears in the Java memory model, but there it is about visibility between threads, not causality between machines.

a → bیعنی a می‌توانسته بر b اثر بگذارد، مستقیم یا با زنجیرهa could have influenced b, directly or through a chain
3 rulesسه راه ساختن رابطه: برنامه، پیام، تراگذریthree ways to build the relation: program order, a message, transitivity
∥هیچ‌کدام پیش از دیگری نیست؛ همزمان‌اندneither precedes the other; they are concurrent

این رابطه همزمانی را نشان نمی‌دهدThe relation cannot show concurrency

یک محدودیت عجیب دارد: نبودِ رابطه از بیرون دیده نمی‌شود. هر گره فقط می‌داند چه چیزهایی را دیده است؛ نمی‌داند چه چیزهایی را ندیده. پس می‌تواند بگوید «این نوشتن، آن یکی را دیده» ولی نمی‌تواند بگوید «این دو، همزمان بودند». برای همین وقتی دو نوشتن روی یک کلید به هم می‌رسند، از روی دادهٔ خودت نمی‌توانی بگویی تعارض واقعی بوده یا یکی از نظر علّی بعد از دیگری است. ساعت برداری همین کار را می‌کند: به‌جای یک عدد، برای هر گره یک خانه نگه می‌دارد و همزمانی را قابل‌دیدن می‌کند. بهایش بزرگ‌تر شدن پیام و وضعیت است، ولی بدون آن، هر داوری بین دو نوشتن یک حدس است.It has an awkward limit: the absence of a relation is not observable. Each node knows what it has seen; it cannot know what it has not. So it can say “this write saw that one”, but it can never say “these two were concurrent”. That is why, when two writes to one key meet, the data itself does not tell you whether this was a real conflict or one causally followed the other. A vector clock does exactly that: instead of one number it keeps a slot per node, which makes concurrency visible. The price is bigger messages and bigger state; without it, every judgement between two writes is a guess.

تلهٔ رایجThe usual trap

مرتب کردن لاگ بر اساس ساعت دیواری و اسم گذاشتنش روی «ترتیب رویدادها»: با انحراف ساعت، پاسخ قبل از درخواست می‌افتد و لاگ داده‌ای نشان می‌دهد که هرگز رخ نداده است. ترتیب درست را از علیت بگیر، نه از timestamp؛ و اگر timestamp تنها چیزی است که داری، حداقل بدان که ترتیبش قابل اعتماد نیست. لاگی که دروغ می‌گوید از نبود لاگ خطرناک‌تر است.Sorting the log by wall clock and calling it “the order of events”. With clock skew, a reply lands before its request and the log shows data that never existed. Take the order from causality, not from timestamps; and if a timestamp is all you have, at least know its order cannot be trusted. A log that lies is more dangerous than no log.

high-water mark

خط تراز بالا فصل ۱۰Chapter 10 · دیدن در متنsee in context

تا کجا را می‌توانی با خیال راحت بخوانی؟Up to which point is it safe to read?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

عکسی را در آلبوم مشترک خانواده می‌گذاری؛ برنامه اول آن را روی سرور آپلود می‌کند و بعد در آلبوم نشانش می‌دهد، و یک نوار پیشرفت هم دارد که می‌گوید تا کجا مطمئن است. عکس‌های زیر آن خط در آلبوم دیده می‌شوند، عکس‌های بالای خط هنوز در راه‌اند و اگر گوشی همین حالا خاموش شود ممکن است هیچ‌وقت نرسند. خط تراز بالا همان مرز است: پایینش قطعی است، بالایش هنوز نه.You drop a photo into the family shared album. The app uploads it first and only then shows it in the album; a progress bar tells you how far it is safe. Photos below that line appear in the album, photos above it are still on the way and might never arrive if the phone dies now. The high-water mark is that line: below it everything is final, above it nothing is.

تشبیه و تصویر ذهنیThe analogy

نکتهٔ اصلی این خط این است که فقط جلو می‌رود. اگر عقب برگردد، یعنی عکسی که در آلبوم بود دیگر نیست، و این بدترین خبری است که یک آلبوم می‌تواند بدهد. برای همین کسی که خط را جلو می‌برد باید مطمئن باشد؛ در سیستم، این کار وقتی انجام می‌شود که اکثریت نسخه‌ها داده را گرفته و روی دیسک نوشته باشند. بهایش این است که یک نسخهٔ کند خط را نگه می‌دارد و همه منتظر می‌مانند؛ اگر خط را زودتر جلو ببری سریع‌تر می‌شوی، ولی ممکن است چیزی را که کاربر دیده بود از دست بدهی. این همان معاملهٔ همیشگی است: کمی تأخیر بیشتر، در عوض قولی که پس گرفته نمی‌شود.The key property is that the line only moves forward. If it moved back, a photo that was in the album would no longer be there, which is the worst news an album can deliver. So whoever advances the line has to be sure, and in a system that usually means a majority of copies have received the data and written it to disk. The price is that one slow copy pins the line and everyone waits; advance it earlier and you go faster, at the risk of losing something a user already saw. It is the usual trade: a little more delay in exchange for a promise you never take back.

نوشتن رکوردWrite the recordروی رهبرon the leaderتکثیر به اکثریتCopy to a majorityهمهٔ ISRall in-syncبالا رفتن خطLine moves upحالا قابل خواندنnow readableخواندن مصرف‌کنندهConsumer readsفقط تا همین خطup to the line
تا وقتی خط بالا نرفته، رکورد بالای آن خواندنی نیست؛ شاید همان‌جا بماند و شاید برود.Until the line moves, nothing above it is readable: it may stay, or it may vanish.

و بعدش، فنیAnd now, technically

در لاگ‌های تکثیرشده، خط تراز بالا بالاترین موقعیتی است که همهٔ رکوردهای تا آن موقعیت، روی اکثریت یا روی همهٔ همانندهای همگام، پایدار شده‌اند. مصرف‌کننده فقط تا همین خط می‌تواند بخواند و بالاتر از آن هیچ قولی ندارد. در Kafka این خط کمینهٔ انتهای لاگ میان همانندهای همگام (ISR) است؛ پیامی که هنوز به همهٔ ISR نرسیده، برای مصرف‌کننده قابل خواندن نیست، و اگر رهبر بیفتد، پیام‌های بالای خط ممکن است حذف شوند تا لاگ با رهبر تازه هم‌تراز شود. در Raft همین نقش را commit index دارد: رکوردی که روی اکثریت تکثیر نشده، کامیت‌شده نیست و رهبر تازه می‌تواند آن را بازنویسی کند. مکانیزمش ساده است: هر گره موقعیت خودش را اعلام می‌کند و خط، کمینهٔ آن‌هاست، پس یک گرهٔ کند کل خط را عقب نگه می‌دارد. خط هرگز عقب نمی‌رود؛ در عوض، یک گرهٔ بسیار عقب‌مانده از مجموعهٔ همگام بیرون گذاشته می‌شود تا خط جلو برود. برای همین خواندن از بالای خط، حتی اگر داده آن‌جا روی رهبر باشد، یک قمار است: ممکن است پیش از اینکه کسی ببیندش ناپدید شود.In a replicated log, the high-water mark is the highest position such that every record up to it is durable on a majority, or on all in-sync replicas. Consumers may read up to that line and nothing above it is promised. In Kafka the high-water mark is the minimum log end offset across the in-sync replicas; a message that has not reached all of them is not readable by consumers, and if the leader fails, messages above the line may be truncated so the log matches the new leader. In Raft the same role is played by the commit index: an entry that is not on a majority is not committed, and a new leader may overwrite it. The mechanism is simple: every node reports its position and the line is the minimum, so one slow node holds the whole line back. The line never goes backwards; instead, a badly lagging node is removed from the in-sync set so the line can move. That is why reading above the line, even from the leader, is a gamble: the data may vanish before anyone else sees it.

min(ISR)خط تراز بالا در Kafka: عقب‌ترین همانند همگامthe high-water mark in Kafka: the furthest-behind in-sync replica
commit indexهمان نقش در Raft؛ بالاتر از آن کامیت‌شده نیستthe same role in Raft: above it, nothing is committed

خط تراز بالا در برابر واترمارکHigh-water mark versus watermark

این دو اسم شبیه‌اند و فرقشان مهم است. خط تراز بالا دربارهٔ جای امن در لاگ است: تا کجا داده پایدار شده و خواندنش بی‌خطر است. واترمارک دربارهٔ زمان رویداد است: تا کجا مطمئنیم رویدادهای دیرتر دیگر نمی‌رسند، و معمولاً برای بستن پنجره‌های زمانی در پردازش جریانی استفاده می‌شود. یکی می‌گوید «این رکورد ممکن است گم شود»، دیگری می‌گوید «این پنجره دیگر ورودی تازه نمی‌گیرد». اگر این دو را قاطی کنی، ممکن است پنجره‌ای را با یک عقب‌ماندگی تکثیر ببندی و نتیجه‌ای بگیری که با واقعیت جور نیست.The two names look alike and the difference matters. The high-water mark is about a safe position in the log: how far data is durable, so reading up to it is safe. A watermark is about event time: how far we are sure no earlier event can still arrive, usually to close time windows in stream processing. One says “this record might still be lost”; the other says “this window will take no new input”. Mix them up and you may close a window on a replication backlog and produce a result that does not match reality.

تلهٔ رایجThe usual trap

جلو بردن خط با تأیید یک نسخه. اگر نوشتن را با acks=1 تأیید کنی، خط بالا می‌رود و مصرف‌کننده پیامی را می‌خواند که فقط روی رهبر است؛ رهبر که بیفتد، همان پیام می‌رود و مصرف‌کننده کاری را انجام داده که هیچ‌وقت ثبت نشده. خطی که با یک نسخه جلو می‌رود دیگر خط نیست، یک حدس است.Advancing the line on a single replica's acknowledgement. Acknowledge with acks=1 and the line moves while the consumer reads a message that exists only on the leader; when the leader dies the message goes, and the consumer has acted on something that was never recorded. A line advanced by one replica is not a line, it is a guess.

hybrid logical clock

ساعت منطقی ترکیبی فصل ۱۰Chapter 10 · دیدن در متنsee in context

ساعت دیواری دروغ می‌گوید؛ ساعت منطقی ترتیب ندارد. هر دو را بگیر.The wall clock lies; a logical clock has no order. Take both.

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

در یک ساختمان، ساعتِ هر واحد چند دقیقه با بقیه فرق دارد. برای اینکه بدانی کدام اتفاق اول افتاده، ساعت را تنها نگذار؛ کنارش یک شمارندهٔ کوچک هم بگذار که هر بار خبر تازه می‌رسد یکی بالا برود.In one building every flat's clock is a few minutes off. To know which event came first, do not trust the clock alone; keep a small counter beside it that ticks up whenever news arrives.

تشبیه و تصویر ذهنیThe analogy

دفترِ پذیرش یک درمانگاه را تصور کن که دو ستون دارد: ساعتِ دیواری و شمارهٔ نوبت. اگر دو بیمار ساعتِ یکسان داشته باشند، شمارهٔ نوبت می‌گوید کدام اول آمده. اگر ساعتِ پذیرش عقب باشد ولی شمارهٔ نوبت جلو رفته باشد، سیستم می‌فهمد که چیزی از یک جای دیگر رسیده و ساعتش را کمی به جلو می‌برد. این همان کاری است که یک ساعت ترکیبی می‌کند: ساعت را با واقعیت هم‌تراز نگه می‌دارد و ترتیب را با شمارنده تضمین می‌کند.Picture a clinic's intake book with two columns: the wall clock and a ticket number. If two patients show the same time, the ticket says who came first. If the reception clock lags while a ticket from elsewhere has moved ahead, the system notices something arrived from outside and nudges its clock forward. That is what a hybrid clock does: it keeps the clock aligned with reality and keeps order with the counter.

و بعدش، فنیAnd now, technically

یک ساعت ترکیبی دو مؤلفه دارد: بخش فیزیکی که از ساعت دیواری گرفته می‌شود، و بخش منطقی که شمارنده‌ای است و در هر پیام به‌روز می‌شود. هنگام ساختن یک مهر زمانی، مقدار فیزیکی و شمارنده با هم خوانده می‌شوند؛ هنگام دریافت مهر زمانی از جای دیگر، اگر مهرِ دریافتی جلوتر بود، ساعت فیزیکی به آن نزدیک می‌شود و شمارنده بالا می‌رود. نتیجه سه خصلت دارد: مهرها با زمان واقعی اختلاف محدود دارند، ترتیبِ علّی حفظ می‌شود، و برخلاف ساعت منطقیِ خالص، می‌توانی حدوداً بگویی یک رویداد چه وقت واقع شده. این همان چیزی است که سیستم‌های توزیع‌شدهٔ امروزی برای مرتب‌سازی تراکنش‌ها به کار می‌برند.A hybrid clock has two parts: a physical part taken from the wall clock and a logical part that is a counter updated on every message. When creating a timestamp, both are read together; when receiving one from elsewhere, if the incoming value is ahead, the physical part is pulled toward it and the counter is bumped. The result has three properties: timestamps stay within a bounded distance of real time, causal order is preserved, and unlike a pure logical clock you can say roughly when an event happened. That is why modern distributed databases use it to order transactions.

boundedاختلاف با زمان واقعی محدود است، نه صفرthe distance from real time is bounded, not zero
2 partsزمان فیزیکی + شمارندهٔ منطقیphysical time plus a logical counter

تلهٔ رایجThe usual trap

فکر کردن این‌که ساعت ترکیبی، ساعتِ دیواریِ غلط را درست می‌کند. اگر ساعتی چند ساعت جلو رفته باشد، ساعت ترکیبی همان جلو بودن را به بقیه منتقل می‌کند و مهرهای زمانی آینده تولید می‌شود؛ محافظِ لازم یک کرانِ سخت روی اختلاف مجاز است.Assuming a hybrid clock repairs a wrong wall clock. If one host jumps hours ahead, the hybrid clock propagates that jump and starts minting future timestamps; what you need is a hard bound on the tolerated skew.

بدون ساعت هم می‌شود ترتیب ساخت؟Can you build an order without a clock?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

تو و دوستت هر کدام یک دفترچه دارید و هر خاطره را با شماره می‌نویسید: ۱، ۲، ۳. وقتی دفترچهٔ او را می‌خوانی و می‌بینی آخرین خاطره‌اش شمارهٔ ۴۰ است، خاطرهٔ بعدی خودت را ۴۱ می‌نویسی، نه ۱۲، تا دفترچه‌ات هیچ‌وقت عقب‌تر از چیزی که خوانده‌ای نباشد. ولی اگر یک ماه همدیگر را نبینید، هر دو ممکن است به شمارهٔ ۲۰ برسید و بعد هیچ‌کس نتواند بگوید کدام ۲۰ اول بود؛ ساعت لمپورت همان شماره‌گذاری است.You and your friend each keep a diary and number the entries: 1, 2, 3. When you read her diary and see that her last entry is number 40, you number your next entry 41, not 12. That way your diary is never behind what you have read. But if you do not meet for a month, you may both reach 20, and then nobody can say whose 20 came first. A Lamport clock is that numbering.

تشبیه و تصویر ذهنیThe analogy

چرا ۴۱ و نه ۱۲؟ چون می‌خواهی شماره‌هایت با چیزی که دیده‌ای سازگار بمانند: هر چه تو بعد از خواندن آن دفترچه می‌نویسی باید شمارهٔ بزرگ‌تری داشته باشد. حالا به فاصله‌ها نگاه کن: از ۱۲ به ۴۱ پریدی، ولی هیچ‌کس نمی‌گوید ۲۹ خاطره در این فاصله رخ داده؛ شماره فقط ترتیب را نشان می‌دهد، نه مدت زمان را. دو نفر هم می‌توانند هر دو شمارهٔ ۲۰ داشته باشند، چون هیچ ارتباطی بینشان نبوده. برای اینکه از این شماره‌ها یک ترتیب کل بسازی، یک قاعدهٔ کوچک اضافه می‌کنند: شمارهٔ کوچک‌تر اول، و اگر شماره‌ها برابر بود، اسم نفر به ترتیب الفبا. این ترتیب کل با علیت سازگار است، ولی بین رویدادهای بی‌ارتباط دلبخواه است.Why 41 and not 12? Because you want your numbers to stay consistent with what you have seen: anything you write after reading her diary must carry a bigger number than what you read. Now look at the gaps. You jumped from 12 to 41, but nobody claims 29 entries happened in between; the number shows order, not duration. Two people can both hold 20, because no message ever connected them. To turn these numbers into a total order, add one small rule: smaller number first, and on a tie, the name in alphabetical order. That total order is consistent with causality, but between unrelated events it is arbitrary.

رویداد محلیLocal eventc = c + ۱c = c + 1ارسالSendc همراه پیامc on the wireدریافتReceivec = max + ۱c = max + 1
شمارنده در هر فرایند بالا می‌رود و با هر پیام، از شمارندهٔ فرستنده جلو می‌زند.The counter rises in each process and jumps past the sender's counter on every message.

و بعدش، فنیAnd now, technically

هر فرایند یک شمارندهٔ صحیح دارد: برای هر رویداد محلی یکی زیاد می‌شود، برای ارسال همراه پیام می‌رود، و برای دریافت برابر می‌شود با بیشینهٔ شمارندهٔ خودش و شمارندهٔ پیام، به‌علاوهٔ یک. تضمین اصلی این است: اگر a → b آن‌گاه L(a) < L(b). عکسش برقرار نیست؛ L(a) < L(b) فقط می‌گوید b پیش از a نبوده، نه اینکه a پیش از b بوده. یعنی دو رویداد همزمان هم می‌توانند شماره‌های مختلف داشته باشند و ترتیبشان دلبخواه باشد. زوج (شمارنده، شناسهٔ گره) یک ترتیب کل می‌سازد که با happens-before سازگار است، و همین برای خیلی چیزها کافی است: مرتب کردن رویدادها در یک لاگ توزیع‌شده، عکس گرفتن همزمان از وضعیت با الگوریتم Chandy-Lamport، و ترتیب‌دهی اولیه. هزینه‌اش یک عدد در هر پیام است، برخلاف ساعت برداری که برای هر گره یک خانه می‌خواهد. ولی نمی‌تواند همزمانی را تشخیص بدهد: با نگاه به دو شماره نمی‌توانی بگویی این دو رویداد به هم مربوط بودند یا نه. ساعت منطقی ترکیبی همین ایده را با ساعت فیزیکی ترکیب می‌کند تا هم ترتیب علّی داشته باشد هم یک زمان تقریبی.Each process keeps an integer counter. Every local event increments it. On send, the current counter travels with the message. On receive, the counter becomes the maximum of its own value and the message's, plus one. The guarantee is: if a → b then L(a) < L(b). The converse does not hold; L(a) < L(b) only says b did not happen before a, not that a happened before b. So concurrent events can carry different numbers and be ordered arbitrarily. The pair (counter, node id) gives a total order consistent with happens-before, which is enough for a lot: ordering events in a distributed log, taking a consistent snapshot with the Chandy-Lamport algorithm, and as a first ordering key. It costs one integer per message, unlike a vector clock that needs one slot per node. What it cannot do is detect concurrency: two numbers alone never tell you whether the events were related. A hybrid logical clock combines the same idea with physical time to get causal order and an approximate timestamp.

max + 1قاعدهٔ دریافت: از هر دو شمارنده جلوتر بزنthe receive rule: step past both counters
O(1)حافظهٔ هر فرایند، برخلاف ساعت برداری با O(N)state per process, against O(N) for a vector clock
1 عدددر هر پیام، نه یک خانه برای هر گرهper message, not a slot for every node

ساعت لمپورت در برابر ساعت برداریLamport clock versus vector clock

ساعت لمپورت یک عدد است و همین یک عدد باعث می‌شود ارزان باشد، ولی کور. ساعت برداری برای هر گره یک خانه دارد و برای همین می‌تواند بگوید «این دو همزمان‌اند» یا «این یکی آن یکی را دیده». این تفاوت در تصمیم‌گیری واقعی مهم است. با ساعت لمپورت، وقتی دو نوشتن به هم می‌رسند، مجبوری یکی را با یک قاعدهٔ دلبخواه انتخاب کنی، حتی اگر آن یکی اطلاعات تازه‌ای داشته باشد که دیگری ندیده. با ساعت برداری می‌فهمی تعارض واقعی است و می‌توانی هر دو را نگه داری و به کاربر نشان بدهی. بهایش این است که اندازهٔ timestamp با تعداد گره‌ها رشد می‌کند؛ برای خوشهٔ بزرگ، همان چیزی است که آدم‌ها را به سمت HLC می‌برد.A Lamport clock is one number, which is what makes it cheap and blind. A vector clock keeps a slot per node, which lets it say “these two are concurrent” or “this one saw that one”. That difference matters in real decisions. With a Lamport clock, when two writes meet, you must pick one by an arbitrary rule, even if that one carried new information the other never saw. With a vector clock you know the conflict is real, and you can keep both and show them to the user. The price is that the timestamp grows with the number of nodes; on a large cluster that is exactly what pushes people towards an HLC.

تلهٔ رایجThe usual trap

استفاده از ساعت لمپورت برای last-write-wins: چون شمارنده همیشه بالا می‌رود، هر نوشتن تازه برنده می‌شود، حتی اگر نوشتن دیگر در همان لحظه از کاربر دیگری آمده باشد و هیچ‌کدام از دیگری خبر نداشته باشد. نتیجه این است که یک به‌روزرسانی معتبر بی‌صدا حذف می‌شود و هیچ خطایی هم نمی‌بینی. برای دیدن تعارض ساعت برداری لازم است؛ برای حلش، یک قاعدهٔ کسب‌وکار.Using a Lamport clock for last-write-wins. Because the counter only climbs, every later write wins, even when the other write came from a different user at the same moment and neither had seen the other. A valid update is deleted in silence and no error is raised. Seeing the conflict needs a vector clock; resolving it needs a business rule.

leader election

انتخاب رهبر فصل ۱۰Chapter 10 · دیدن در متنsee in context

چه کسی تصمیم آخر را می‌گیرد، وقتی رئیس نیست؟Who gets the final say when the boss is gone?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

شش نفر به سفر می‌روید و یک صندوق پول مشترک دارید؛ باید یک نفر خزانه‌دار باشد، وگرنه هر کس از جیب خودش پول می‌دهد و آخر سفر حساب‌ها با هم نمی‌خواند. اگر دو نفر همزمان خودشان را خزانه‌دار بدانند، هر دو خرج می‌کنند و پول دو بار از صندوق می‌رود. اگر هم هیچ‌کس قبول نکند، هیچ‌کس نمی‌تواند بلیت بخرد؛ انتخاب رهبر همین است: یک نفر، با یک قاعدهٔ روشن، و بقیه قبول می‌کنند.Six of you go on a trip with one shared cash box. Someone has to be treasurer, or everyone pays from their own pocket and the accounts never add up. If two people both believe they are treasurer, both spend and the money leaves the box twice. If nobody takes it, nobody can buy the tickets. Leader election is that: one person, by a clear rule, and everyone else accepts it.

تشبیه و تصویر ذهنیThe analogy

قاعده را چطور می‌گذارید؟ ساده‌ترین راه رأی اکثریت است: هر کس بتواند چهار رأی از شش نفر بگیرد، خزانه‌دار است. چون هر اکثریتی با هر اکثریت دیگری حداقل یک نفر مشترک دارد، دو نفر نمی‌توانند همزمان چهار رأی داشته باشند؛ همین همپوشانی است که «هیچ‌وقت دو خزانه‌دار» را تضمین می‌کند، نه حسن نیت آدم‌ها. ولی رأی اکثریت یک روز بد هم دارد: سه نفر این طرف شهر و سه نفر آن طرف و تلفن‌ها قطع، هیچ طرفی چهار نفر ندارد و سفر معطل می‌ماند. برای همین در سیستم‌ها یک محدودیت زمانی می‌گذارند: اگر از رهبر خبری نبود، دو باره رأی‌گیری. این محدودیت هم معامله دارد: کوتاه باشد، خزانه‌دار کند را مرده حساب می‌کنند و الکی عوض می‌شود؛ بلند باشد، خرابی واقعی را دیر می‌فهمند.How do you write the rule? The simplest is a majority vote: whoever gets four of six is treasurer. Any majority overlaps any other majority in at least one person, so two people cannot both hold four votes. That overlap, not good manners, is what guarantees a single treasurer. But majority voting has a bad day: three of you on one side of town and three on the other, phones down. Neither side has four votes and the trip stalls. So systems add a clock: if the leader goes quiet, vote again. That clock is a trade too. Short, and a slow treasurer is declared dead and replaced for nothing; long, and a real failure is noticed late.

رهبر ساکتLeader goes quietheartbeat قطعno heartbeatشروع رأی‌گیریElection startsterm + ۱term + 1اکثریت رأیMajority votesرهبر تازهnew leaderرهبر قدیمیOld leaderterm بزرگ‌تر را دیدsees a higher term
رهبر ساکت می‌شود، دورهٔ تازه شروع می‌شود، اکثریت رأی می‌دهد و رهبر قدیمی با دیدن دورهٔ بزرگ‌تر کنار می‌رود.The leader goes quiet, a new term starts, a majority votes, and the old leader steps down on seeing the higher term.

و بعدش، فنیAnd now, technically

در عمل، انتخاب رهبر روی همان ابزار اجماع انجام می‌شود: نامزدها برای یک term، یعنی یک دوره، رأی می‌خواهند و هر گره در هر دوره فقط یک رأی می‌دهد؛ برنده کسی است که اکثریت را بگیرد. چون دو اکثریت همپوشانی دارند، در هر دوره حداکثر یک رهبر می‌تواند رأی کافی جمع کند، و شمارهٔ دوره باعث می‌شود رهبر قدیمی که پیام‌هایش دیر رسیده خودش کنار بکشد. رهبر با heartbeat زنده‌بودنش را اعلام می‌کند؛ اگر follower تا election timeout چیزی نشنود، خودش نامزد می‌شود، و timeoutها تصادفی‌اند تا دو نفر همزمان نامزد نشوند و رأی‌ها دو نیم نشوند. در یک دیتاسنتر این timeoutها معمولاً صدها میلی‌ثانیه‌اند؛ بین منطقه‌ها باید بزرگ‌تر باشند، چون تأخیر شبکه بیشتر است و timeout کوتاه انتخابات بی‌پایان می‌سازد. انتخاب رهبر و اجماع هم‌ارزند: با اجماع می‌توان انتخاب رهبر ساخت و با انتخاب رهبر می‌توان اجماع ساخت. قاعدهٔ ایمنی این است که رهبری بدون اکثریت وجود ندارد؛ بخشی که اکثریت را از دست داده باید از پذیرفتن نوشتن دست بکشد، حتی اگر سالم به نظر برسد.In practice leader election runs on top of consensus: candidates ask for votes for a term, each node votes at most once per term, and the winner is whoever collects a majority. Because two majorities overlap, at most one leader can gather enough votes in a term, and the term number makes a leader with delayed messages step down. The leader sends heartbeats; when a follower hears nothing for its election timeout it becomes a candidate, and the timeouts are randomized so two nodes rarely run at once and split the vote. In a datacenter those timeouts are usually hundreds of milliseconds; across regions they must be larger, because the network is slower and a short timeout produces endless elections. Leader election and consensus are equivalent: consensus can implement an election, and an election can be used to build consensus. The safety rule is that there is no leader without a majority; a minority must stop accepting writes even if it looks healthy.

2f+1اندازهٔ خوشه برای تحمل f خرابیthe cluster size that survives f failures
150–300 msبازهٔ معمول election timeout در یک دیتاسنترa typical election timeout inside one datacenter
≥ 1 termهر رهبر یک دوره دارد و دوره فقط بالا می‌رودevery leader has a term, and terms only go up

وقتی رهبر کند است، نه مردهWhen the leader is slow, not dead

تشخیص خرابی با timeout همیشه یک حدس است: گره‌ای که جواب نمی‌دهد می‌تواند مرده باشد، می‌تواند در حال GC باشد، یا می‌تواند در ترافیک شبکه گیر کرده باشد. از بیرون این سه حالت یکسان به نظر می‌رسند. اگر timeout را کوتاه کنی، رهبر کند را عوض می‌کنی و ممکن است دو رهبر داشته باشی؛ اگر بلند کنی، قطعی واقعی دقیقه‌ها طول می‌کشد. راه درست این است که ایمنی را به timeout وصل نکنی: رهبری را به رأی اکثریت و شمارهٔ دوره ببند، و منابع بیرونی را با fencing token محافظت کن. آن‌وقت timeout فقط سرعت بازیابی را تعیین می‌کند، نه درستی را.Failure detection by timeout is always a guess: a node that does not answer may be dead, may be in a GC, or may be stuck in network traffic. From outside those three look the same. Shorten the timeout and you replace a slow leader and may end up with two; lengthen it and a real outage lasts minutes. The right move is to keep safety off the timeout: tie leadership to a majority vote and a term number, and protect external resources with fencing tokens. Then the timeout only decides how fast you recover, not whether you are correct.

تلهٔ رایجThe usual trap

روشن کردن unclean leader election برای بستن یک قطعی. وقتی هیچ همانند همگامی نیست، کافکا می‌تواند یک همانند عقب‌مانده را رهبر کند تا موضوع دو باره بالا بیاید؛ سرویس برمی‌گردد، ولی پیام‌هایی که قبلاً تأیید شده بودند و فقط روی رهبر قدیمی بودند، برای همیشه می‌روند. این تنظیم یک انتخاب است، نه یک تیونینگ: دوام را می‌فروشی تا دسترس‌پذیری بخری، و باید بدانی کدام موضوع‌ها اجازهٔ این معامله را دارند.Turning on unclean leader election to end an outage. When no in-sync replica is left, Kafka can promote a lagging replica so the topic comes back; the service returns, but messages that were already acknowledged and lived only on the old leader are gone for good. That setting is a choice, not a tuning knob: you sell durability to buy availability, and you had better know which topics are allowed that trade.

دو ماشین، یک جای خالی. هر دو «یک» می‌بینند.Two people, one empty seat. Both read one.

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

پارکینگ یک جای خالی دارد و دو ورودی، و تابلوی «۱ جای خالی» بالای هر دو ورودی است. ماشین اول از ورودی شمال می‌آید و پارک می‌کند؛ از آن لحظه، تابلوی ورودی جنوب باید صفر نشان بدهد و ماشین دوم باید برگردد. اگر هر ورودی شمارش خودش را داشته باشد، هر دو «۱» می‌بینند و هر دو داخل می‌روند؛ خطی‌پذیری یعنی سیستم همان یک تابلوی مشترک است، نه دو تابلو.A car park has one free space and two entrances, each with a sign reading “1 free”. A car comes in from the north and parks; from that moment the south sign must read zero and the next car must turn around. If each entrance keeps its own count, both read 1, both drive in, and one of them backs out. Linearizability means the system is that one shared sign, not two signs.

تشبیه و تصویر ذهنیThe analogy

چرا «یک لحظه» مهم است؟ چون هر عملیات یک فاصلهٔ زمانی دارد: از وقتی درخواست می‌فرستی تا وقتی جواب می‌گیری. خطی‌پذیری می‌گوید می‌شود برای هر عملیات نقطه‌ای در همین فاصله پیدا کرد که اثرش همان‌جا گذاشته شده باشد، و ترتیب این نقطه‌ها با زمان واقعی جور باشد: اگر نوشتن تو قبل از شروع خواندن من تمام شده، خواندن من باید آن را ببیند. این تعریف به ساعت جهانی نیاز ندارد، فقط به «قبل از شروع» و «بعد از پایان» نیاز دارد. بهایش این است که برای هر عملیات باید با بقیه هماهنگ شوی و در قطع شبکه یا باید بایستی یا باید قید را بشکنی. و چیزی که خیلی‌ها اشتباه می‌گیرند: خطی‌پذیری برای هر شیء جدا معنا دارد؛ اگر هر کلید خطی‌پذیر باشد مجموعشان هم خطی‌پذیر است، ولی این هیچ ربطی به تراکنش‌های چندکلیدی ندارد.Why does “one instant” matter? Every operation occupies an interval, from the moment you send the request to the moment you get the answer. Linearizability says you can place each operation at some point inside its own interval, and that the order of those points agrees with real time: if your write finished before my read started, my read must see it. That definition needs no global clock, only “before the start” and “after the end”. The price is that every operation has to coordinate with the others, and during a partition you either stop or break the promise. And the part most people miss: linearizability is a property of each object. If every key is linearizable, the set is linearizable too, and that still says nothing about multi-key transactions.

درخواست نوشتنWrite callشروعstartتأیید نوشتنWrite returnsلحظهٔ اثرthe instantخواندن بعدیLater readباید تازه باشدmust be freshخواندن از همانندFollower readممکن است کهنهmay be stale
هر عملیات بین درخواست و پاسخ یک لحظهٔ اثر دارد؛ خوانندهٔ بعدی باید همان را ببیند.Every operation has one effective instant between call and return, and the next reader must see it.

و بعدش، فنیAnd now, technically

خطی‌پذیری یک تضمین تازگی است: هر خواندن باید آخرین نوشتنِ تأییدشده پیش از خودش را ببیند. معادلش این است که سیستم مثل یک نسخهٔ واحد با عملیات اتمی رفتار کند. دو خاصیت مهم دارد: اول، قابل ترکیب است، چون تضمین روی هر شیء مستقل است و اگر هر شیء جدا خطی‌پذیر باشد کل مجموعه هم خطی‌پذیر است. دوم، ربطی به سریال‌پذیری ندارد. سریال‌پذیری یک خاصیت ایزولاسیون تراکنش است: می‌گوید نتیجهٔ چند تراکنش همزمان معادل یک ترتیب سریال از آن‌هاست و ترتیب واقعی زمان را الزام نمی‌کند، در حالی که خطی‌پذیری یک ترتیب زمانی روی یک شیء است و تراکنش نمی‌سازد؛ سریال‌پذیری اکید هر دو را با هم می‌خواهد. در CAP، همان C خطی‌پذیری است، نه «سازگاری» در معنای ACID. پیاده‌سازی‌اش گران است: نوشتن باید از یک رهبر یا یک دور اجماع بگذرد، و خواندن از همانند هم باید مطمئن شود رهبر عوض نشده است. Spanner این را با TrueTime و صبر کردن به اندازهٔ عدم‌قطعیت ساعت می‌سازد؛ سیستم‌های دیگر با رهبر و lease یا با ReadIndex.Linearizability is a recency guarantee: every read must return the most recent acknowledged write before it. Equivalently, the system behaves like a single copy with atomic operations. Two properties matter. First, it is composable: if each object is linearizable, the whole set is, because the guarantee is per object. Second, it has nothing to do with serializability. Serializability is a transaction isolation property: the result of concurrent transactions is equivalent to some serial order, and real time is not part of it. Linearizability is a real-time order on one object and does not group operations into transactions. Strict serializability asks for both. In CAP, the C is linearizability, not consistency in the ACID sense. It is expensive: a write must go through a leader or a consensus round, and a read from a follower must first be sure the leader has not changed. Spanner builds it with TrueTime and waiting out the clock uncertainty; other systems use a leader with a lease, or ReadIndex.

1 copyرفتار سیستم مثل یک نسخهٔ واحدthe system behaves like one copy
1 RTTدست‌کم یک رفت‌وبرگشت برای هر عملیات هماهنگ‌شدهat least one round trip per coordinated operation
≈ 7 msعدم‌قطعیت TrueTime؛ Spanner تا این مدت صبر می‌کندTrueTime uncertainty; Spanner waits this long

چرا خواندن از همانند، خطی‌پذیری را می‌شکندWhy a follower read breaks linearizability

یک همانند که لاگ را اعمال می‌کند، همیشه عقب‌تر از رهبر است؛ حتی اگر فقط چند میلی‌ثانیه. خواندنی که از آن می‌آید جواب کهنه می‌دهد، و این دقیقاً همان چیزی است که خطی‌پذیری ممنوع می‌کند: خواندن بعد از یک نوشتن تأییدشده باید آن نوشتن را ببیند. مشکل بدتر هم می‌شود: رهبری که تازه کنار گذاشته شده، تا وقتی خبر عزلش را نشنیده، ممکن است به خواندن جواب بدهد و مقدار کهنه بدهد. برای همین رهبر باید قبل از جواب دادن، با یک دور heartbeat به اکثریت مطمئن شود هنوز رهبر است (ReadIndex)، یا یک lease داشته باشد که در آن بازه کسی نمی‌تواند جایش را بگیرد. هر دوی این‌ها هزینه دارند؛ همین هزینه است که خواندن خطی‌پذیر را از خواندن محلی گران‌تر می‌کند.A follower applying the log is always behind the leader, even if only by milliseconds. A read from it returns a stale answer, and that is exactly what linearizability forbids: a read after an acknowledged write must see that write. The problem gets worse: a leader that has just been deposed may keep answering reads with stale values until it learns it is out. So before answering, a leader has to confirm with a heartbeat round to a majority that it is still leader (ReadIndex), or hold a lease that keeps anyone else from taking over during that window. Both cost something, and that cost is why a linearizable read is more expensive than a local one.

خطی‌پذیری در برابر سریال‌پذیریLinearizability versus serializability

خطی‌پذیریLinearizabilityسریال‌پذیریSerializability
دامنهScopeیک شیء، یک عملیاتone object, one operationچند تراکنش روی چند شیءseveral transactions over objects
زمان واقعیReal timeبخشی از تعریف استpart of the definitionهیچ الزامی نداردno requirement at all
ترکیب‌پذیریComposabilityبله، هر شیء جدا کافی استyes, each object on its own is enoughنه، تراکنش‌ها به هم وابسته‌اندno, transactions depend on each other
چه می‌دهدWhat it givesتازگی خواندنfreshness of readsنتیجه‌ای معادل اجرای نوبتیa result like some serial run

تلهٔ رایجThe usual trap

فرض کردن اینکه «انبار خطی‌پذیر است، پس کد من درست است»، در حالی که دو عملیات خطی‌پذیر روی دو کلید با هم یک عملیات اتمی نمی‌سازند: موجودی را می‌خوانی، کافی است، بعد کم می‌کنی، و بین این دو نفر دیگر هم همان را خوانده و کم می‌کند. هر دو خواندن و نوشتن خطی‌پذیر بودند و باز هم دو بار فروختی؛ برای چنین کاری به compare-and-set، تراکنش، یا یک دور اجماع روی هر دو کلید نیاز داری. خطی‌پذیری تازگی می‌دهد، اتمی‌بودن نه.Assuming that “the store is linearizable, so my code is right”. Two linearizable operations on two keys do not make one atomic operation: you read the stock, it is enough, then you decrement; in between, someone else reads the same and decrements too. Both the read and the write were linearizable and you still sold twice. That job needs compare-and-set, a transaction, or a consensus round over both keys. Linearizability gives freshness, not atomicity.

بدون پارتیشن هم انتخاب داری: تأخیر یا سازگاری.Even without a partition you choose: latency or consistency.

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

دو شعبه از یک فروشگاه. وقتی خط ارتباطشان قطع شود، هر شعبه یا باید بفهمد موجودی را از دست داده (C) یا باید بفروشد و بعد اختلاف را حل کند (A). ولی در حالت عادی هم انتخاب داری: برای موجودیِ دقیق باید به مرکز زنگ بزنی و کمی صبر کنی (C)، یا از نسخهٔ محلیِ سریع استفاده کنی که ممکن است چند دقیقه کهنه باشد (L).Two branches of one shop. When the line between them fails, each branch either refuses to sell without knowing stock (C) or sells and reconciles later (A). But even in normal times you choose: call head office for exact stock and wait (C), or use the fast local copy that may be minutes old (L).

تشبیه و تصویر ذهنیThe analogy

بیشتر آدم‌ها CAP را این‌طور یاد می‌گیرند که «سه‌تا را نمی‌شود با هم داشت» و بعد فکر می‌کنند در روزهای عادی خبری از انتخاب نیست. واقعیت این است که انتخاب در هر لحظه هست؛ فقط اسمش عوض می‌شود. در نبود پارتیشن، اجباری به قربانی کردن سازگاری نداری، ولی اگر بخواهی سازگاری را حفظ کنی باید یک رفت‌وبرگشت شبکه را بپردازی، و آن یک هزینهٔ دائمی است، نه یک هزینهٔ مواقع بحران.Most people learn CAP as “pick two” and assume the choice only matters on bad days. In truth the choice is always present; only its name changes. Without a partition you are not forced to give up consistency, but if you insist on it you pay a network round trip — and that is a permanent cost, not a crisis cost.

و بعدش، فنیAnd now, technically

PACELC یک حرف به CAP اضافه می‌کند: اگر پارتیشن (P) باشد، بین دسترس‌پذیری (A) و سازگاری (C) انتخاب می‌کنی؛ در غیر این صورت (E، یعنی else) بین تأخیر (L) و سازگاری (C). این تعمیم مهم است چون بیشتر اوقاتِ عمر یک سیستم، پارتیشنی در کار نیست و انتخاب واقعی همان L در برابر C است: یک سیستمِ سازگار باید نوشتن را تا تأیید اکثریت نگه دارد، و یک سیستمِ کم‌تأخیر جواب را از نزدیک‌ترین نسخه می‌دهد حتی اگر کمی کهنه باشد. مهم‌ترین فایدهٔ PACELC این است که می‌گوید تصمیم را برای دو حالت جدا بگیر، نه یک بار برای همیشه.PACELC adds a clause to CAP: if there is a partition (P), choose between availability (A) and consistency (C); else (E) choose between latency (L) and consistency (C). The extension matters because most of a system's life has no partition, and the real choice is L versus C: a consistent system must hold a write until the majority acknowledges, while a low-latency system answers from the nearest copy even if it is slightly stale. The chief value of PACELC is that it tells you to make the decision twice, once per regime.

2 regimesبا پارتیشن و بدون آن؛ دو تصمیم جداwith and without a partition: two decisions

تلهٔ رایجThe usual trap

استفاده از CAP به‌عنوان برچسبِ بازاریابی («ما CP هستیم») و بعد شگفت‌زده شدن از تأخیر. اگر در حالت عادی هم سازگاری را انتخاب کنی، هزینه‌اش را در هر درخواست می‌دهی، نه فقط در روز خرابی.Wearing CAP as a marketing label (“we are CP”) and then being surprised by latency. If you choose consistency in normal operation too, you pay for it on every request, not only on the bad day.

چند رأی کافی است تا حرف، حرف آخر باشد؟How many votes make an answer final?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

پنج نفرید و می‌خواهید برای عروسی هدیه بخرید؛ قرار گذاشته‌اید هر تصمیمی که سه رأی بیاورد گرفته‌شده باشد، پس دو نفر می‌توانند بخوابند و تصمیم باز هم معتبر است. مهم‌تر اینکه دو تصمیم متناقض نمی‌توانند همزمان سه رأی بیاورند، چون در پنج نفر هر گروه سه‌نفره با هر گروه سه‌نفرهٔ دیگر حداقل یک نفر مشترک دارد. کوئروم در سیستم هم همین است: تعدادی گره که باید در خواندن یا نوشتن شرکت کنند تا جواب معتبر شمرده شود.A family of five is buying a wedding gift. You agree that any decision with three votes is made, so two people can sleep through it and the decision still stands. Better still: two contradictory decisions cannot both get three votes, because any three of five overlaps any other three in at least one person. A quorum in a system is the same: the number of nodes that must take part in a read or a write for the answer to count.

تشبیه و تصویر ذهنیThe analogy

حالا حساب را کامل کن. اگر نوشتن روی سه گره تأیید شود و خواندن هم از سه گره جواب بگیرد، این دو مجموعه در پنج گره حتماً به هم می‌رسند؛ پس خواندن حداقل یک نسخه دارد که نوشتن را دیده است. اگر خواندن فقط از دو گره باشد، ممکن است هر دو خارج از مجموعهٔ نوشتن باشند و مقدار کهنه برگردد. ولی این حساب یک بهای واقعی دارد: جواب خواندن وقتی آماده است که کندترین گره از آن سه جواب بدهد، پس یک گرهٔ کند کل خواندن را کند می‌کند. و اگر تعداد گره‌های سالم از حد لازم کمتر شود، سیستم باید صبر کند یا خطا بدهد؛ نمی‌شود با یک اقلیت کار کرد و همزمان ادعا کرد چیزی گم نمی‌شود.Now finish the arithmetic. If a write is acknowledged by three nodes and a read asks three nodes, the two sets must meet in a five-node cluster, so the read has at least one copy that saw the write. If the read only asks two, both may sit outside the write set and return the old value. But the arithmetic has a real price: a read is ready only when the slowest of its three answers arrives, so one slow node slows every read. And when fewer nodes are healthy than the rule needs, the system must wait or fail; you cannot work with a minority and still claim nothing is lost.

سه رأی نوشتنthree write votesسه رأی خواندنthree read votesگره ۱node 1گره ۲node 2گره ۳node 3گره ۴node 4گره ۵node 5
با پنج گره، سه رأی نوشتن و سه رأی خواندن به هم می‌رسند؛ با دو رأی خواندن، ممکن است هیچ‌کدام.With five nodes, three write votes and three read votes meet; with two read votes, they may not.

و بعدش، فنیAnd now, technically

کوئروم یک حساب ساده است: N نسخه، نوشتن باید W تأیید بگیرد، خواندن باید از R نسخه جواب بگیرد، و اگر R + W > N باشد این دو مجموعه همپوشانی دارند. با N=3 و W=2 و R=2، هر خواندن حداقل یک نسخه را می‌بیند که آخرین نوشتن را دارد. ولی این تضمین چند شرط دارد که در عمل گم می‌شوند: نوشتن‌ها باید با شمارهٔ نسخه مرتب شوند تا خواندن بتواند تازه‌ترین را انتخاب کند؛ نوشتنی که به W نرسیده باید کامل شود یا برگردد، نه اینکه نیمه‌کاره بماند؛ و هیچ نوشتنی نباید بیرون از مجموعهٔ گره‌ها تأیید شود. اگر یکی از این شرط‌ها بشکند، R + W > N فقط یک عدد است. این حساب به‌خودی‌خود خطی‌پذیری نمی‌سازد: با last-write-wins بر اساس ساعت دیواری، نوشتنی که ساعتش عقب است می‌تواند قدیمی حساب شود، و بدون compare-and-set دو کلاینت می‌توانند یک مقدار را بخوانند و هر دو بنویسند. برای همین سیستم‌های جدی برای کارهای حساس، کوئروم را با اجماع یا با یک رهبر ترکیب می‌کنند. کوئروم برای دسترس‌پذیری و تحمل خرابی خوب است، برای تصمیم‌گیری دقیق کافی نیست.A quorum is simple arithmetic: N replicas, a write must collect W acknowledgements, a read must collect R answers, and if R + W > N the two sets overlap. With N=3, W=2, R=2, every read contacts at least one replica that has the latest write. The guarantee has conditions that get lost in practice: writes need version numbers so a read can pick the newest; a write that fails to reach W must be completed or rolled back, not left half-applied; and no write may be acknowledged outside the replica set. Break one of those and R + W > N is just a number. The arithmetic does not by itself give linearizability: with last-write-wins on wall clocks, a write with a slow clock can look older, and without compare-and-set two clients can read the same value and both write. That is why serious systems combine quorums with consensus or a leader for anything that matters. Quorum buys availability and fault tolerance; it does not buy decisions.

R + W > Nشرط همپوشانی؛ بدون آن، خواندن می‌تواند کهنه باشدthe overlap condition; without it a read may be stale
3 / 2 / 2پیکربندی رایج: سه نسخه، دو تأیید نوشتن، دو جواب خواندنa common setup: three replicas, two write acks, two read replies
2 of 3تحمل یک خرابی؛ اکثریت همیشه برنده استone failure tolerated; the majority always wins

کوئروم شلخته: چطور همپوشانی را از دست می‌دهیSloppy quorum: how the overlap disappears

بعضی سیستم‌ها برای اینکه در قطع شبکه هم بنویسند، نوشتن را به گره‌های خارج از مجموعهٔ اصلی می‌سپارند و بعداً با hinted handoff به صاحبش برمی‌گردانند. این کار دسترس‌پذیری را بالا می‌برد، ولی همان قولی را که برایش کوئروم گذاشتی باطل می‌کند: نوشتن با گره‌های جانشین تأیید شده، و خواندن بعدی ممکن است هیچ‌کدام از آن‌ها را نبیند. حساب R + W > N فقط وقتی معنا دارد که مجموعهٔ گره‌ها ثابت باشد. اگر گره‌ها جابه‌جا می‌شوند، باید بدانی نوشتن کجا نشسته و خواندن چه کسانی را می‌بیند، وگرنه عددت درست است و سیستمت نه.Some systems write to nodes outside the main set during a partition, so they can keep accepting writes, and later hand the data back with hinted handoff. That raises availability and cancels the promise the quorum existed for: the write was acknowledged by stand-ins, and the next read may include none of them. R + W > N only means something when the set of nodes is stable. If nodes move around, you need to know where the write landed and whom the read sees, or your number is right and your system is not.

کوئروم در برابر اجماعQuorum versus consensus

کوئرومQuorumاجماعConsensus
چه می‌دهدWhat it givesخواندن تازه، با شرطa fresh read, under conditionsیک تصمیم برگشت‌ناپذیرone irrevocable decision
ترتیبOrderingنمی‌سازدdoes not build oneمی‌سازد؛ همان لاگbuilds one: the log
دو نوشتن همزمانTwo concurrent writesهر دو موفق، یکی برندهboth succeed, one winsیکی در لاگ می‌نشیندone enters the log
هزینهCostR + W > N، بدون دور اضافهR + W > N, no extra roundیک دور به اکثریت برای هر تصمیمone majority round per decision

تلهٔ رایجThe usual trap

فکر کردن اینکه «اکثریت نوشتند، پس تصمیم گرفته شد». کوئروم روی دو نوشتن همزمان هیچ داوری نمی‌کند: هر دو می‌توانند روی زیرمجموعه‌های مختلف تأیید شوند و هر دو موفق برگردند، بعد یک قاعدهٔ دلبخواه یکی را نگه می‌دارد. برای شمارش موجودی یا رزرو صندلی، کوئروم به‌تنهایی یعنی فروش دو باره؛ آنجا compare-and-set یا اجماع لازم است، چون کوئروم تعداد را می‌گوید، نه ترتیب را.Believing that “a majority wrote it, so the decision is made”. A quorum does not arbitrate between two concurrent writes: each can be acknowledged by a different subset, both return success, and an arbitrary rule then keeps one. For stock counts or seat reservations, a quorum alone means selling twice; that needs compare-and-set or consensus. A quorum tells you how many, never in what order.

state machine replication

تکثیر ماشین حالت فصل ۱۰Chapter 10 · دیدن در متنsee in context

چطور پنج نسخه دقیقاً یک چیز می‌مانند؟How do five copies stay exactly the same?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

در کلاس اوریگامی، معلم قدم‌به‌قدم می‌گوید: «کاغذ را از وسط تا کن، گوشهٔ راست را بیاور، حالا برگردان». همهٔ بچه‌ها همان قدم‌ها را به همان ترتیب انجام می‌دهند و آخرش همه یک شکل دارند. اگر یکی قدم‌ها را جابه‌جا بشنود، شکلش با بقیه فرق می‌کند، هرچند کاغذش همان کاغذ بوده؛ تکثیر ماشین حالت همین است: دستورها را به یک ترتیب واحد به همه بده تا همه به یک نتیجه برسند.In an origami class the teacher calls the steps one by one: fold in half, bring the right corner over, now turn it. Every child performs the same steps in the same order and ends up with the same shape. Hear the steps in a different order and your shape differs, even though the paper was identical. State machine replication is that: give every copy the same commands in the same order and they end in the same state.

تشبیه و تصویر ذهنیThe analogy

چرا ترتیب این‌قدر مهم است؟ چون بعضی کارها جابه‌جا نمی‌شوند: «۱۰۰ تا اضافه کن» و بعد «دو برابر کن» جوابی می‌دهد، برعکسش جواب دیگری؛ پس فقط یکسان بودن دستورها کافی نیست، ترتیب هم باید یکی باشد. مشکل دومی هم هست: هر قدم باید برای همه یک معنا داشته باشد. اگر دستور بگوید «یک مشت نمک بریز» یا «صبر کن تا طلایی شود»، هر کس تفسیر خودش را دارد و بچه‌ها به هم می‌رسند ولی شکل‌ها یکی نیستند. در سیستم هم همین است: تصادف، ساعت و شمارهٔ یکتا باید یک بار تصمیم گرفته شوند و داخل همان دستور بنشینند، وگرنه هر نسخه چیز دیگری می‌سازد. و کسی که دیر می‌رسد باید از اول همهٔ قدم‌ها را بزند یا از یک عکس آمادهٔ کار شروع کند.Why does order matter so much? Because some steps do not commute: add 100 then double is not double then add 100. So identical commands are not enough; the order must be identical too. There is a second problem: each step has to mean the same thing to everyone. If a step says “add a pinch of salt” or “wait until golden”, everyone interprets it differently, and the children end up with different shapes. In a system the same is true: randomness, clocks and unique IDs have to be decided once and carried inside the command, or every replica builds something else. And a child who arrives late has to replay all the steps, or start from a snapshot of the work so far.

دستور کلاینتClient commandاز کلاینتfrom clientترتیب واحدOne orderلاگ اجماعconsensus logاعمال در هر نسخهApply everywhereقطعیdeterministicوضعیت یکسانSame stateهمه یکیall alike
دستورها از یک ترتیب واحد می‌گذرند و هر نسخه همان‌ها را به همان ترتیب اعمال می‌کند.Commands pass through one order, and every copy applies them in exactly that order.

و بعدش، فنیAnd now, technically

در تکثیر ماشین حالت، چند نسخه از یک ماشین حالت قطعی، دستورهای یکسانی را به ترتیب یکسان اعمال می‌کنند، پس به وضعیت یکسان می‌رسند. چیزی که ترتیب را تأمین می‌کند یک لاگ کل‌ترتیب است که با اجماع ساخته شده؛ به همین دلیل تکثیر ماشین حالت و پخش کل‌ترتیب و اجماع در عمل یک چیزند با سه اسم. نوشتن یعنی «این دستور را در لاگ بگذار»؛ وضعیت، حاصل اعمال همهٔ دستورهای لاگ است. اگر خواندن از یک نسخهٔ محلی انجام شود ممکن است کهنه باشد؛ برای خواندن خطی‌پذیر، رهبر باید مطمئن شود هنوز رهبر است، با lease، یا از طریق ReadIndex یک دور هماهنگی بزند. هزینه‌اش این است که هر دستور باید روی اکثریت تکثیر شود و روی دیسک بنشیند، و وضعیت با گذشت زمان بزرگ می‌شود؛ برای اینکه نسخهٔ تازه‌کار بتواند عقب‌افتادگی را جبران کند باید از وضعیت snapshot گرفت و لاگ قدیمی را دور ریخت. عدم‌قطعیت دشمن این مدل است: random، زمان، و ترتیب پیمایش یک map می‌توانند دو نسخه را بی‌صدا از هم جدا کنند.In state machine replication, several copies of a deterministic state machine apply the same commands in the same order, so they reach the same state. The order comes from a totally ordered log built by consensus, which is why state machine replication, total order broadcast and consensus are the same thing under three names. A write means “put this command in the log”; the state is the result of applying every command in it. Reads served locally may be stale; for a linearizable read the leader must confirm it is still leader, with a lease, or run a ReadIndex round. The cost is that every command must be replicated to a majority and written to disk, and the state grows, so snapshots are needed to let a new replica catch up and to truncate the log. Nondeterminism is the enemy: randomness, time, and map iteration order can split two replicas apart without any error.

1 orderیک ترتیب برای همه؛ نه یک ترتیب برای هر نسخهone order for everyone, not one per replica
N × workهر دستور روی هر نسخه یک بار اعمال می‌شودevery command is applied once on every replica
snapshotبدون آن، نسخهٔ تازه‌کار باید کل لاگ را از اول بخواندwithout it, a new replica replays the whole log

خواندن هم باید از لاگ رد شودReads have to go through the log too

وسوسه‌اش زیاد است: نسخه‌ای که لاگ را اعمال می‌کند، وضعیت را دارد، پس چرا برای هر خواندن به رهبر برویم؟ چون آن نسخه ممکن است چند پیام عقب باشد و خواندن، نوشتن تأییدشده‌ای را که کاربر همین حالا انجام داده نبیند. بدتر، رهبری که دیگر رهبر نیست هم می‌تواند همین اشتباه را بکند. راه‌حل این است که خواندن هم یک نقطهٔ ترتیب بگیرد: یا از لاگ رد شود، یا رهبر با یک دور به اکثریت ثابت کند هنوز رهبر است. قیمتش یک رفت‌وبرگشت است، و همین قیمت است که تفاوت بین «خواندن مقیاس‌پذیر از هر نسخه» و «خواندن درست» را می‌سازد.The temptation is strong: a replica that applies the log already has the state, so why go to the leader for every read? Because that replica may be a few messages behind, and the read would miss a write the user just had acknowledged. Worse, a leader that is no longer leader can make the same mistake. The answer is to give reads an ordering point too: either send them through the log, or have the leader prove with a round to a majority that it is still leader. That costs a round trip, and that cost is the whole difference between “scalable reads from any replica” and “correct reads”.

تلهٔ رایجThe usual trap

داشتن عدم‌قطعیت در ماشین حالت: یک UUID تصادفی، یک now()، یا پیمایش یک hash map با ترتیب نامشخص، در هر نسخه چیز دیگری می‌دهد؛ لاگ یکی است ولی وضعیت‌ها یکی نیستند. این خرابی بی‌صدا است و معمولاً روزی کشف می‌شود که رهبر عوض شده و کاربر می‌گوید داده‌اش عوض شده. هر منبع عدم‌قطعیت را باید یک بار در لاگ تصمیم گرفت و همان مقدار را به همه داد.Letting nondeterminism into the state machine. A random UUID, a now(), or iterating a hash map in unspecified order gives a different result on every replica: one log, several states. The failure is silent, and it surfaces the day the leader changes and a user says their data moved. Every source of nondeterminism has to be decided once, in the log, and handed to everyone.

strict serializability

سریالی‌بودن اکید فصل ۱۰Chapter 10 · دیدن در متنsee in context

ترتیب تراکنش‌ها، ترتیب واقعی زمان را هم رعایت کند.Transaction order must respect real time as well as the data.

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

در یک صف نانوایی، شماره‌ها ترتیبِ واقعی را نشان می‌دهند: هر کس زودتر شماره گرفته زودتر نان می‌گیرد. حالا فرض کن کسی که شمارهٔ ۷ دارد، پیش از شمارهٔ ۴ نان بگیرد؛ همه چیز شاید «سازگار» باشد ولی ترتیبِ واقعی نقض شده.At a bakery the ticket numbers show the real order: whoever took a number earlier is served earlier. Now imagine ticket seven served before ticket four; everything might be “consistent”, yet the real order is broken.

تشبیه و تصویر ذهنیThe analogy

تفاوت سریالی‌بودن و سریالی‌بودن اکید، مثل تفاوت «ترتیب منطقی» و «ترتیب واقعی» است. یک دفتر حسابداری می‌تواند ردیف‌ها را به هر ترتیبی که دوست دارد بنویسد، به شرطی که جمع آخر درست باشد؛ این سریالی‌بودن است. ولی اگر مشتری پول را روز دوشنبه داده و رسید را روز سه‌شنبه گرفته، هیچ دفتری نمی‌تواند ادعا کند که رسید قبل از پرداخت ثبت شده. آن ادعا، سریالی‌بودن اکید را نقض می‌کند.The difference between serializability and strict serializability is the difference between logical order and real order. A ledger may write its rows in any order it likes as long as the balance is right; that is serializability. But if the customer paid on Monday and got a receipt on Tuesday, no ledger may claim the receipt came first. That claim breaks strict serializability.

و بعدش، فنیAnd now, technically

سریالی‌بودن می‌گوید نتیجهٔ اجرای هم‌زمانِ چند تراکنش، معادلِ یک ترتیبِ سریالی از همان تراکنش‌هاست؛ ولی این ترتیب می‌تواند با زمان واقعی نخواند. سریالی‌بودن اکید می‌گوید آن ترتیب باید با زمان واقعی هم سازگار باشد: اگر تراکنش A پیش از شروع B تمام شده باشد، A باید در ترتیب معادل هم قبل از B بیاید. این تعریف، سریالی‌بودن و خطی‌پذیری را در یک ضمانت جمع می‌کند و همان چیزی است که از یک پایگاه دادهٔ تراکنشیِ توزیع‌شده انتظار داری. دست‌یابی به آن، به ساعتی نیاز دارد که ترتیب زمان واقعی را قابل تشخیص کند.Serializability says the outcome of concurrent transactions equals some serial order of those transactions; that order may disagree with real time. Strict serializability says the order must also respect real time: if transaction A completed before B began, A must precede B in the equivalent order. The definition merges serializability and linearizability into one guarantee, and it is what you expect from a distributed transactional database. Reaching it requires a clock that can establish real-time order.

2 ordersترتیب معادل + ترتیب زمانی واقعیthe equivalent order plus the real-time order

تلهٔ رایجThe usual trap

اشتباه گرفتن سریالی‌بودن با سریالی‌بودن اکید در مصاحبه و در طراحی. بیشتر موتورهایی که «serializable» می‌نامند فقط سریالی‌اند؛ اگر پرس‌وجوی تو به ترتیبِ واقعی حساس است (برای مثال گزارش حسابداری)، این تفاوت دقیقاً همان جایی است که خراب می‌شود.Conflating serializability with strict serializability in an interview or a design. Most engines that advertise “serializable” are only serializable; if your query depends on real-time order — a ledger report, say — that gap is exactly where it breaks.

timestamp oracle

اوراکل زمان فصل ۱۰Chapter 10 · دیدن در متنsee in context

یک جا، شمارهٔ زمان می‌دهد؛ همه منتظر همان‌اند.One place hands out the time; everyone waits for it.

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

در یک اداره، همهٔ نامه‌ها باید شمارهٔ ثبت بگیرند و شماره‌ها نباید تکراری یا جاافتاده باشند. برای همین یک نفر پشت میز نشسته و شماره می‌دهد. اگر خودِ کارمندان شماره بدهند، دو نفر شمارهٔ یکسان می‌گیرند.In an office every letter must be registered with a number, and numbers must not repeat or skip. So one person sits at a desk and hands them out. If the clerks minted their own, two letters would share a number.

تشبیه و تصویر ذهنیThe analogy

این کارمندان در ساختمان‌های مختلف‌اند و نمی‌توانند به هم نگاه کنند؛ پس به یک میزِ مرکزی زنگ می‌زنند و شماره می‌گیرند. هزینه‌اش روشن است: هر ثبت باید یک رفت‌وبرگشت به آن میز بزند. فایده‌اش هم روشن است: هیچ‌کس نمی‌تواند ادعا کند شماره‌اش قبل از دیگری صادر شده، چون هر دو شماره از یک جا آمده‌اند.These clerks sit in different buildings and cannot see each other, so they call one central desk and take a number. The cost is clear: every registration pays a round trip to that desk. The benefit is equally clear: nobody can claim their number was issued first, because both numbers came from the same place.

و بعدش، فنیAnd now, technically

یک اوراکل زمان سرویسی است که تنها کارش این است که عددی یکنوا و بدون شکاف برگرداند: هر درخواست، عددی بزرگ‌تر از درخواست قبلی می‌گیرد. با این سرویس می‌توان ترتیبِ جهانی ساخت بدون اینکه به ساعتِ هر ماشین اعتماد کنی؛ فقط کافی است هر تراکنش هنگام شروع، یک مهر زمانی از اوراکل بگیرد. هزینهٔ اصلی، همان رفت‌وبرگشت و آن تک‌نقطهٔ مرکزی است، برای همین در عمل آن را به بازه‌ها می‌شکنند: اوراکل بازه‌ای از شماره‌ها را تحویل می‌دهد و هر سرور داخل بازهٔ خودش شماره می‌دهد. سیستم‌هایی که زمان را با ساعت اتمی و کرانِ خطا تأیید می‌کنند، همین نقش را با سخت‌افزار بازی می‌کنند.A timestamp oracle is a service whose only job is to return a monotonic, gapless number: each request receives a value greater than the previous one. With it you can build a global order without trusting any machine's clock; each transaction simply takes a timestamp at start. The main cost is the round trip and the central point, so in practice it is sharded into blocks: the oracle hands out a range and each server dispenses from its own range. Systems that validate time against atomic clocks with a bounded error play the same role in hardware.

1 round tripهر تراکنش یک رفت‌وبرگشت اضافه به اوراکل داردevery transaction pays one extra round trip to the oracle

تلهٔ رایجThe usual trap

ساختنِ اوراکلِ زمان به‌عنوان یک شمارندهٔ سادهٔ تک‌نمونه و فراموش کردنِ اینکه خودش باید تحمل خرابی داشته باشد. اگر اوراکل از کار بیفتد، هیچ تراکنشی مهر زمانی نمی‌گیرد و کل سیستم متوقف می‌شود؛ برای همین باید یا تکثیر شود یا با بازه‌بندی، وابستگی‌اش را کم کرد.Building the oracle as a single-threaded counter and forgetting it needs fault tolerance of its own. If the oracle dies, no transaction gets a timestamp and the whole system stops; so it must be replicated, or made less essential with block allocation.

total order broadcast

پخش کل‌ترتیب فصل ۱۰Chapter 10 · دیدن در متنsee in context

همه یک چیز را می‌شنوند، ولی به یک ترتیب؟Everyone hears the same thing, but in the same order?

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

روی تلفن برای دو نفر فهرست برنامهٔ عروسی را می‌خوانی: «اول سوپ، بعد ماهی، بعد شیرینی»، و خط تلفن بد است. یکی «سوپ، ماهی، شیرینی» می‌نویسد و دیگری «سوپ، شیرینی، ماهی»؛ هر دو همان سه چیز را دارند، ولی مهمانی دو میز با دو ترتیب متفاوت غذا می‌خورد. پخش کل‌ترتیب همین است: نه فقط همان پیام‌ها، بلکه همان ترتیب برای همه.On the phone you read the wedding menu to two assistants: soup first, then fish, then dessert. The line crackles. One writes soup, fish, dessert; the other writes soup, dessert, fish. Both have the same three courses, but two tables eat in two different orders. Total order broadcast is that: not just the same messages, but the same order for everyone.

تشبیه و تصویر ذهنیThe analogy

دو چیز باید تضمین شود. اول، هیچ دو نفری ترتیب متفاوت نبینند؛ دوم، پیامی که یک نفر تحویل گرفته برای بقیه هم بالاخره بیاید. توجه کن که «همان لحظه» جزو قرار نیست: کسی که تلفنش قطع بوده بعداً نوار را گوش می‌کند و همان ترتیب را می‌شنود. این ترتیب از ترتیب علّی قوی‌تر است، چون پیام‌های بی‌ربط را هم مرتب می‌کند، و همین است که برای ساختن وضعیت یکسان لازم است. بهایش این است که ترتیب باید یک جایی ساخته شود: یا یک نفر شماره می‌زند، یا هر پیام از یک دور اجماع می‌گذرد. در هر دو حالت هر پیام یک رفت‌وبرگشت به اکثریت می‌خواهد و یک گلوگاه داری که با اضافه کردن ماشین بیشتر نمی‌شود.Two things must hold: no two people see a different order, and a message one person received must eventually reach the others. Note that “at the same moment” is not part of the deal: the assistant whose phone was down listens to the recording later and hears the same order. This order is stronger than causal order because it also orders unrelated messages, and that is exactly what building an identical state requires. The price is that the order has to be made somewhere: either one node stamps sequence numbers, or every message goes through a consensus round. Either way each message costs a round trip to a majority, and you have a bottleneck that adding machines does not remove.

پیشنهاد پیامPropose a messageهر گرهany nodeشماره‌گذاریAssign a numberرهبر یا اجماعleader or consensusپخش به همهDeliver to allبدون گم شدنnothing lostیک ترتیبOne orderبرای همه یکیsame for everyone
پیام‌ها شماره می‌گیرند و به همان ترتیب به همه می‌رسند؛ محتوا یکی است و ترتیب هم یکی.Messages get a number and reach everyone in that same order: one content, one order.

و بعدش، فنیAnd now, technically

پخش کل‌ترتیب، که به آن atomic broadcast هم می‌گویند، چهار خاصیت دارد. توافق: هیچ دو گرهٔ سالمی دو ترتیب متفاوت تحویل نمی‌گیرند؛ صحت: پیامی تکراری تحویل داده نمی‌شود و پیامی از هوا ساخته نمی‌شود. اعتبار: فقط پیام‌هایی که کسی پیشنهاد کرده تحویل می‌شوند. پایان: هر گرهٔ سالمی بالاخره هر پیام را تحویل می‌گیرد. سه خاصیت اول ایمنی‌اند و در هر شرایطی برقرارند؛ چهارمی زنده‌بودن است و به اکثریت سالم و شبکهٔ به‌قدر کافی همگام نیاز دارد. پیاده‌سازی سادهٔ آن یک sequencer است: یک گره به هر پیام شمارهٔ بعدی را می‌دهد و بقیه به همان ترتیب تحویل می‌دهند. پیاده‌سازی مقاوم، همان لاگ اجماع است که پیام‌ها را دسته‌دسته در لاگ می‌نشاند. پخش کل‌ترتیب و اجماع هم‌ارزند: با یک دور اجماع روی هر پیام، ترتیب کل می‌سازی؛ و با ترتیب کل، هر گره می‌تواند اولین پیام را بردارد و همان تصمیم اجماع است. اگر ماشین حالت قطعی داشته باشی، پخش کل‌ترتیب همان تکثیر ماشین حالت است.Total order broadcast, also called atomic broadcast, has four properties. Agreement: no two correct nodes deliver messages in different orders. Integrity: no message is delivered twice and none is invented. Validity: only messages that were proposed are delivered. Termination: every correct node eventually delivers every message. The first three are safety and hold under any condition; the fourth is liveness and needs a healthy majority and a network that is timely enough. The simple implementation is a sequencer: one node stamps each message with the next number and the rest deliver in that order. The resilient implementation is a consensus log that batches messages. Total order broadcast and consensus are equivalent: one consensus round per message builds a total order, and with a total order each node can take the first message and that is a consensus decision. Add a deterministic state machine and you have state machine replication.

1 orderیک ترتیب مشترک، نه یک ترتیب به‌ازای هر گرهone shared order, not one per node
1 partitionدر کافکا ترتیب فقط داخل هر پارتیشن استin Kafka the order exists only inside one partition
1 RTTبرای هر پیام، یک دور به اکثریتone majority round per message

ترتیب کل در برابر ترتیب علّیTotal order versus causal order

ترتیب علّی فقط پیام‌هایی را مرتب می‌کند که یکی علت دیگری است؛ پیام‌های بی‌ربط می‌توانند در هر گره‌ای در هر ترتیبی برسند. این برای خیلی کارها ارزان‌تر و کافی است. ولی برای تکثیر یک ماشین حالت کافی نیست: اگر دو دستور بی‌ربط باشند و هر گره یکی را اول بزند، وضعیت‌ها از هم جدا می‌شوند، هرچند هیچ‌کس قانون علیت را نشکسته است. ترتیب کل همین شکاف را پر می‌کند و تصمیم می‌گیرد بین دو چیز بی‌ربط هم کدام اول است. برای همین گران‌تر است: باید یک نقطهٔ تصمیم وجود داشته باشد، و آن نقطه همیشه یک گلوگاه است.Causal order orders only messages where one caused the other; unrelated messages can arrive in any order at any node. That is cheaper and enough for many things. It is not enough for replicating a state machine: if two commands are unrelated and each node applies a different one first, the states diverge, even though nobody broke causality. Total order fills that gap and decides, between two unrelated things, which came first. That is why it costs more: there has to be one decision point, and that point is always a bottleneck.

تلهٔ رایجThe usual trap

گفتن «ما کافکا داریم، پس ترتیب کل داریم». ترتیب در کافکا فقط داخل هر پارتیشن است؛ دو پارتیشن دو ترتیب مستقل دارند و مصرف‌کننده‌ای که هر دو را موازی می‌خواند هیچ ترتیب کلی نمی‌بیند. اگر وضعیت مشترکی می‌سازی، باید همهٔ رویدادهای مربوط به یک موجودیت در یک پارتیشن باشند، یا ترتیب را جای دیگری بسازی؛ ترتیب کل یک خاصیت موضوع نیست، یک خاصیت مسیر دادهٔ توست.Saying “we have Kafka, so we have total order”. Kafka orders messages only within a partition; two partitions have two independent orders, and a consumer reading both in parallel sees no overall order at all. If you are building shared state, either keep every event for one entity in one partition, or build the order somewhere else. Total order is not a property of the topic, it is a property of your data path.

unique ID generator

تولید شناسهٔ یکتا فصل ۱۰Chapter 10 · دیدن در متنsee in context

چند سرور، یک شمارهٔ یکتا.Many servers, one unique number.

مثال روزمره، بدون اصطلاحThe everyday one, no jargon

در نانوایی، نان‌ها را با شمارهٔ نوبت صدا می‌زنند. یک صندوق، یک شمارنده، هیچ اشتباهی. حالا تصور کن سه صندوق باز شود؛ اگر هر صندوق از یک شروع کند، سه نان شمارهٔ ۵ خواهند داشت و صف به هم می‌ریزد.At a bakery the bread is called by ticket number. One till, one counter, no collisions. Now imagine three tills open; if each starts at one, three loaves all get ticket five and the queue collapses.

تشبیه و تصویر ذهنیThe analogy

راه‌حل شهودی این است که هر صندوق یک بازهٔ جدا بگیرد: صندوق یک، شماره‌های ۱ تا ۱۰۰۰؛ صندوق دو، ۱۰۰۱ تا ۲۰۰۰. حالا هیچ‌کس منتظر دیگری نمی‌ماند و شماره‌ها یکتا می‌مانند. هزینه‌اش این است که شماره‌ها دیگر ترتیبِ واقعی را نشان نمی‌دهند؛ شمارهٔ ۱۵۰۰ ممکن است قبل از ۹۰۰ صادر شده باشد. هر روشِ توزیع‌شدهٔ شناسه هم دقیقاً همین معامله را می‌کند: یکتایی را می‌خری، ترتیب را می‌فروشی.The intuitive fix is to give each till its own range: till one hands out 1 to 1000, till two 1001 to 2000. Now nobody waits and the numbers stay unique. The cost is that a number no longer reflects real order; ticket 1500 may have been issued before ticket 900. Every distributed ID scheme strikes exactly this trade: you buy uniqueness and sell ordering.

یک شمارندهٔ مرکزیOne central counterبازه‌های تقسیم‌شدهPre-allocated rangesزمان + شمارهٔ سرورTime + node idشناسهٔ تصادفیRandom identifierیکتایی می‌ماند؛ ترتیب و پیوستگی می‌رودUniqueness survives; ordering and gaplessness do not
یک شمارندهٔ مرکزی به چند منبعِ شناسهٔ مستقل می‌شکند؛ هر کدام یکتایی را حفظ می‌کند.One central counter becomes several independent sources, each still unique.

و بعدش، فنیAnd now, technically

سه خانوادهٔ رایج وجود دارد. شناسهٔ تصادفی مثل UUID نسخهٔ ۴: یکتاییِ آماری، بدون هیچ هماهنگی، ولی برای پیمایش بازه‌ای بی‌فایده چون ترتیب ندارد و در درخت ایندکس بد پخش می‌شود. شناسهٔ ترکیبی مثل شمارندهٔ بازه‌ای: هر سرور بازه‌ای از قبل رزرو می‌کند و داخل بازه محلی می‌شمارد؛ یکتایی قطعی است، ولی پس از راه‌اندازی مجدد ممکن است شماره‌ها بپرند و بازه‌های رزروشده هدر بروند. شناسهٔ زمان‌محور مثل ترکیبِ زمان و شمارهٔ سرور: تقریباً مرتب است، پس برای کلید اصلی خوب است، ولی به ساعت وابسته است و باید پرشِ ساعت را تحمل کند. در همهٔ این‌ها یک نکتهٔ عملی هست: یکتایی را از سیستم بگیر، نه از اعتماد به اینکه تصادف تکرار نمی‌کند.Three families dominate. Random identifiers, such as UUID version 4: statistically unique with no coordination, useless for range scans because they carry no order and scatter through an index tree. Block-based counters: each server reserves a range and counts locally; uniqueness is certain, but restarts leave gaps and reserved ranges are wasted. Time-based identifiers, such as time plus a node number: roughly ordered, so good as a primary key, but dependent on the clock and must survive clock jumps. Across all three, the practical rule is to get uniqueness from the system, not from trusting that collisions will not happen.

128 bitاندازهٔ متعارف شناسهٔ تصادفی؛ فضای کافی برای یکتایی آماریthe usual random identifier size; enough space for statistical uniqueness
~ msدقت زمانی که شناسه‌های زمان‌محور تحمل می‌کنندthe time resolution a time-based identifier can carry

شناسهٔ مرتب، هزینهٔ پنهان داردOrdered identifiers have a hidden cost

شناسه‌ای که مرتب است، برای درخت ایندکس عالی است: درج‌ها به یک سمت می‌روند و صفحه‌ها کم می‌شکنند. ولی همان ترتیب، شناسه را قابل حدس می‌کند: اگر مشتری شناسهٔ ۱۰۰۱ را ببیند و شناسهٔ بعدی‌اش ۱۰۰۲ باشد، تعداد سفارش‌های تو را می‌فهمد. برای همین بسیاری از سیستم‌ها شناسهٔ داخلیِ مرتب را جدا از شناسهٔ عمومیِ تصادفی نگه می‌دارند. این جداسازی، هم کارایی را حفظ می‌کند و هم یک نشت اطلاعاتی را می‌بندد.An ordered identifier is excellent for a tree index: inserts go to one side and pages split rarely. But that same order makes it guessable: if a customer sees ID 1001 and their next one is 1002, they learn how many orders you have. Many systems therefore keep an ordered internal key separate from a random public identifier. The split preserves performance and closes an information leak.

تلهٔ رایجThe usual trap

استفاده از شمارندهٔ مرکزی برای همه چیز. یک شمارندهٔ مشترک ساده است و تا وقتی حجم کم است عالی کار می‌کند، ولی هر نوشتن باید برای گرفتن شماره منتظر بماند و همان شمارنده به گلوگاه و به تک‌نقطهٔ خرابی تبدیل می‌شود.Using one central counter for everything. It is simple and works well at low volume, but every write has to wait for a number, and that counter becomes both the bottleneck and the single point of failure.