DDIA·واژه‌هاTERMS

نقشه راهRoadmap/ واژه‌نامهGlossary/ فصل ۶Chapter 6

۶6تکثیر (Replication)Replication

۲۴ واژه از فصل ۶ — هر کدام با یک مثال روزمره، یک نمودار و تلهٔ رایج.24 terms from chapter 6, each with an everyday example, a diagram and the usual trap.

اگر هیچ‌کس آن کلید را نخواند، چه کسی کهنگی‌اش را می‌فهمد؟If nobody reads that key, who ever notices it is stale?

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

ماهی یک‌بار دفترچهٔ شماره‌هایت را با نسخهٔ خواهرت مقایسه می‌کنی و هر شماره‌ای را که یکی دارد و دیگری ندارد اضافه می‌کنی. هیچ تماس تلفنی و پیامی در کار نیست، فقط دو نسخه را کنار هم می‌گذاری و اختلاف را می‌بندی؛ ضدآنتروپی هم در یک خوشه همین مقایسهٔ دوره‌ای را انجام می‌دهد.Once a month you compare your address book with your sister's copy and add every number that one has and the other lacks. No calls and no messages are involved; you just put the two books side by side and close the gaps; anti-entropy runs the same periodic comparison inside a cluster.

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

این مقایسهٔ ماهانه وقتی به کار می‌آید که تغییرها از مسیر معمول جا افتاده باشند: پیامی نرسیده، گوشی خاموش بوده، یا کسی شماره‌ای را دستی عوض کرده. اگر هر شب همهٔ صفحه‌ها را بیت‌به‌بیت مقایسه کنی، وقت و ترافیک زیادی می‌سوزانی؛ اگر سالی یک‌بار این کار را بکنی، اختلاف‌ها مدت زیادی زنده می‌مانند. راه میانه این است که اول فهرست خلاصه‌ها را مقایسه کنی و فقط بخش‌های متفاوت را کامل ببینی، و فاصلهٔ این مقایسه را با تحمل کهنگی خودت تنظیم کنی.The monthly comparison earns its keep when changes missed the normal path: a message never arrived, a phone was off, or someone edited a number by hand. Compare every page byte by byte each night and you burn time and bandwidth; compare once a year and the gaps live a long time. The middle road is to compare summaries first, open only the parts that differ, and set the interval from how much staleness you can carry.

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

ضدآنتروپی یک فرایند پس‌زمینه است که نسخه‌ها را با هم مقایسه و اختلاف‌ها را ترمیم می‌کند؛ کاری که مسیر معمول خواندن و نوشتن انجامش نداده. رایج‌ترین روش، ساختن درخت هش (Merkle tree) روی بازه‌های کلید است: دو همانند ریشهٔ درخت‌ها را مقایسه می‌کنند و فقط به شاخه‌هایی می‌روند که هششان فرق دارد، پس کل داده جابه‌جا نمی‌شود. این کار کند و دوره‌ای است و برای همین نمی‌تواند تأخیر را کم کند؛ فقط تضمین می‌کند که دیر یا زود همه‌چیز به هم برسد. ضدآنتروپی مکمل read-repair و hinted-handoff است، نه جانشین آن‌ها: آن دو در لحظهٔ خواندن و نوشتن کار می‌کنند و این یکی برای چیزهایی است که از هر دو جا افتاده‌اند.Anti-entropy is a background process that compares copies and repairs the differences the normal read and write path never touched. The usual method builds a Merkle tree over key ranges: two replicas compare root hashes and descend only into branches whose hashes differ, so the whole data set never crosses the network. It is slow and periodic, which is why it cannot reduce lag; it only guarantees that everything converges eventually. Anti-entropy complements read repair and hinted handoff rather than replacing them: those work at read and write time, and this one catches what both missed.

log nبا درخت هش، برای یافتن اختلاف لازم نیست همهٔ داده خوانده شودa hash tree finds differences without reading all the data
۱ دورهر دور کامل همهٔ بازه‌ها را می‌بیند؛ فاصله‌اش را خودت انتخاب می‌کنیa full pass covers every range; you choose how often it runs

تلهٔ رایجThe usual trap

انتظار تازگی از ضدآنتروپی. این فرایند ممکن است ساعت‌ها یا روزها یک دور بزند؛ اگر روی آن حساب کنی، خواندن‌هایت تا دور بعد کهنه می‌مانند. ضدآنتروپی برای رسیدن است، نه برای تازه بودن.Expecting freshness from anti-entropy. A pass may take hours or days, so reads that rely on it stay stale until the next one. Anti-entropy is about convergence, not about being current.

conflict resolution

حل تعارض فصل ۶Chapter 6 · دیدن در متنsee in context

دو نفر هم‌زمان یک چیز را عوض کردند. کدام درست است؟Two people edited the same row at once. Who wins?

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

دو نفر از اعضای خانواده هم‌زمان فهرست خرید مشترک را باز می‌کنند. یکی «شیر» را پاک می‌کند چون خریده است و دیگری همان لحظه «شیر» را اضافه می‌کند چون تمام شده، و هر دو ذخیره می‌کنند. فهرست باید یک حرف آخر بزند؛ حل تعارض همان تصمیمی است که سیستم با یک قاعده می‌گیرد، نه با دعوا.Two family members open the shared shopping list at once. One deletes milk because she bought it while the other adds milk because it ran out, and both save. The list has to settle on one answer; conflict resolution is that settling, done by a rule instead of an argument.

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

دفتر یادداشت مشترکی را تصور کن که دو نفر با دو خودکار در دو اتاق روی آن می‌نویسند. سه راه داری. اول، هر کس دیرتر نوشته برنده است؛ ساده است، ولی کار اولی بی‌صدا پاک می‌شود. دوم، هر دو را نگه داری و بعد با یک قاعده جمع کنی، مثلاً برای فهرست خرید اجتماع دو فهرست؛ اینجا هیچ کاری گم نمی‌شود، ولی باید بدانی چطور جمع کنی و چه‌وقت یکی حذف شده است. سوم، از آدم بپرسی؛ درست‌ترین جواب و گران‌ترین. نکتهٔ اصلی این است که اگر یکی از نوشتن‌ها بعد از دیگری باشد، اصلاً تعارضی نیست؛ تعارض فقط وقتی است که هیچ‌کدام دیگری را ندیده باشد.Picture a shared notebook two people write in, with two pens, from two rooms. You have three options. First, whoever wrote later wins: simple, but the earlier work vanishes without a sound. Second, keep both and combine them under a rule, like the union of two shopping lists: nothing is lost, but you must know how to combine, and how to notice that something was deleted. Third, ask a person: the most correct answer and the most expensive. The part people skip is that if one write came after the other, there is no conflict at all; a conflict only exists when neither write has seen the other.

آخرین نوشتن برندهLast write winsیک برنده، ساده و ارزانone winner, cheapنوشتن بازنده گم می‌شودthe loser is lostبه ساعت‌ها وابستهdepends on clocksادغام نسخه‌هاMerge siblingsهمهٔ نوشتن‌ها می‌مانندevery write survivesقاعده باید قطعی باشدthe rule must be deterministicخواننده هم شاید تصمیم بگیردthe reader may decide too
دو راه برای بستن تعارض: یکی را انتخاب کن، یا هر دو را نگه دار و با یک قاعده جمع کن.Two ways to close a conflict: pick one winner, or keep both and combine them under a rule.

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

تعارض واقعی فقط بین نوشتن‌های هم‌زمان است؛ نوشتنی که نوشتن دیگری را دیده، فقط مقدار تازه‌تر است. برای همین اولین کار، تشخیص هم‌زمانی است، نه انتخاب برنده: یا با version vector می‌فهمی دو نسخه شاخه شده‌اند، یا با یک شمارندهٔ منطقی فقط ترتیب جزئی می‌سازی. با یک timestamp ساده نمی‌شود هم‌زمانی را از ترتیب تشخیص داد، چون یک عدد فقط می‌گوید کدام بزرگ‌تر است. وقتی هم‌زمانی ثابت شد، سه خانواده جواب داری: قانون آخرین نوشتن برنده، که ارزان است و نوشتن بازنده را دور می‌ریزد؛ ادغام نوع‌محور یا CRDT، که همیشه به یک مقدار می‌رسد؛ و حل در لایهٔ کاربرد، که نسخه‌ها را به کاربر نشان می‌دهد. در بعضی سیستم‌ها هر دو نسخه به‌عنوان sibling نگه داشته می‌شوند و خواندن هر دو را برمی‌گرداند، و این کار تصمیم را به خواننده می‌سپارد. قاعدهٔ ادغام باید در همهٔ نسخه‌ها و همهٔ نسخه‌های کد یکسان باشد، وگرنه دو همانند هرگز به هم نمی‌رسند. حذف هم قاعدهٔ خودش را می‌خواهد: در مجموعهٔ ادغام‌شدنی، حذف بدون tombstone به هر نوشتن دیگری باخت می‌خورد و مقدار پاک‌شده برمی‌گردد.A real conflict exists only between concurrent writes; a write that has seen another is just the newer value. So the first job is detecting concurrency, not picking a winner: a version vector tells you two versions have branched, while a logical counter only gives you a partial order. A bare timestamp cannot tell concurrency from order, because one number only says which is bigger. Once concurrency is proven, three families of answers exist: last write wins, which is cheap and throws the losing write away; type-aware merge or a CRDT, which always lands on one value; and application-level resolution, which shows the versions to a person. Some systems keep both versions as siblings and return them all on read, which hands the decision to the reader. The merge rule must be identical across replicas and across code versions, or two replicas never converge. Deletes need their own rule too: in a merged set, a delete without a tombstone loses to any other write and the removed value comes back.

2 writesدو نوشتن که همدیگر را ندیده‌اند؛ تنها حالت واقعی تعارضtwo writes that never saw each other: the only real conflict
≤ 1 valueبازندهٔ آخرین نوشتن برنده دور ریخته می‌شود و کسی خبردار نمی‌شودthe last-write-wins loser is discarded and nobody is told

تعارض را قبل از حل کردن، تشخیص بدهDetect a conflict before you resolve it

بیشتر باگ‌های تعارض از جایی می‌آید که سیستم بدون تشخیص هم‌زمانی سراغ حل می‌رود. اگر فقط یک ساعت داشته باشی، هر اختلافی شبیه ترتیب به نظر می‌رسد و هم‌زمانی از چشم می‌افتد. با version vector می‌توانی بگویی نسخهٔ الف از ب خبر دارد، ب از الف خبر ندارد، یا هیچ‌کدام از دیگری خبر ندارد؛ فقط حالت سوم تعارض است. همین تفکیک است که می‌گوید لازم نیست برای هر نوشتن هزینهٔ ادغام بدهی، و می‌گوید یک نوشتن که به‌طور علّی بعد از دیگری است هرگز نباید به آن ببازد.Most conflict bugs start where the system resolves without first detecting concurrency. With only a clock, every disagreement looks like an ordering and concurrency disappears from view. A version vector lets you say that version A has seen B, that B has not seen A, or that neither has seen the other; only the third case is a conflict. That distinction tells you not to pay a merge cost for every write, and it tells you that a write which is causally after another must never lose to it.

کجا حل شود: هنگام نوشتن یا هنگام خواندنResolve on write or on read

حل هنگام نوشتن یعنی رهبر یا هماهنگ‌کننده قبل از پاسخ دادن یک مقدار نهایی انتخاب می‌کند؛ خواندن ساده می‌شود، ولی تصمیم در جایی گرفته می‌شود که شاید زمینهٔ کسب‌وکار را نداند. حل هنگام خواندن یعنی هر دو نسخه ذخیره می‌شوند و کلاینت با دیدن siblingها تصمیم می‌گیرد؛ این انعطاف‌پذیرتر است و اجازه می‌دهد بعداً قاعده را عوض کنی، ولی هر خواننده باید بداند با چند نسخه چه کند. در عمل خیلی از سیستم‌ها ترکیب می‌کنند: نگه‌داشتن نسخه‌ها به‌عنوان sibling و یک قاعدهٔ قطعی برای انتخاب مقدار پیش‌فرض. مهم این است که تصمیم یک‌جا و مستند باشد، نه این‌که سه سرویس مختلف سه جواب بدهند.Resolving on write means the leader or coordinator picks a final value before answering; reads stay simple, but the decision is made where the business context may be missing. Resolving on read means both versions are stored and the client decides when it sees the siblings; that is more flexible and lets you change the rule later, but every reader must know what to do with several versions. Most systems combine the two: keep the versions as siblings and apply one deterministic rule for the default value. What matters is that the decision lives in one documented place, not that three different services each invent an answer.

تلهٔ رایجThe usual trap

فرض کردن این‌که یک timestamp کافی است. با یک عدد نمی‌فهمی دو نوشتن هم‌زمان بوده‌اند یا یکی بعد از دیگری، و نتیجه این است که گاهی یک مقدار کهنه روی یک مقدار تازه می‌افتد. بدتر، چون نوشتن بازنده جایی ذخیره نمی‌شود، هیچ‌وقت نمی‌فهمی چه چیزی را از دست داده‌ای.Assuming one timestamp is enough. A single number cannot tell you whether two writes were concurrent or one came after the other, so an old value sometimes lands on a fresh one. Worse, because the losing write is stored nowhere, you never learn what you lost.

consistency

سازگاری فصل ۶Chapter 6 · دیدن در متنsee in context همچنینalso in فصل ۱۰ch 10

اگر دو نفر یک چیز را بخوانند، باید یک چیز ببینند؟Must two clients reading the same key see the same value?

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

روزنامه‌فروشی سر کوچه دو بسته روزنامه دارد: یکی امروز صبح آمده، یکی دیشب مانده است. اگر از هر دکه‌ای بپرسی، ممکن است خبر امروز یا خبر دیروز را بگیری. سازگاری همان قاعده‌ای است که می‌گوید فروشنده حق دارد کدام بسته را به تو بدهد؛ و در سیستم، همان قاعده تعیین می‌کند یک خواندن از کدام نسخه چه چیزی را مجاز است ببیند.The corner newsstand has two bundles: one arrived this morning, one is left from last night. Depending on which stand you ask, you get today’s news or yesterday’s. Consistency is the rule about which bundle the seller is allowed to hand you; in a system, that same rule decides what a read from a given copy is allowed to show.

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

چهار قرارداد را روی همین دکه بچین. اول، فقط یک دکه و همیشه روزنامهٔ امروز؛ خیالت راحت است، ولی باید در صف بمانی و اگر دکه تعطیل شود دستت خالی می‌ماند. دوم، هر دکه‌ای، به شرط آن‌که وقتی امروز را دیدی دیگر دیروز به تو ندهند. سوم، هر دکه‌ای، به شرط آن‌که اشتراک خودت همیشه باشد، حتی اگر بقیهٔ خبرها عقب باشند. چهارم، هر دکه‌ای و هر نسخه‌ای، ولی تا آخر شب همه یک چیز دارند. هر پله که پایین بیایی، ارزان‌تر و در دسترس‌تر می‌شوی و در عوض مدت بیشتری ممکن است خبر کهنه بخوانی. سازگاری یک بله یا خیر نیست؛ یک نقطه روی همین نردبان است و باید بگویی کدام پله.Lay four contracts on that same stand. First, one stand only and always today’s paper: safe, but you queue, and if the stand closes you get nothing. Second, any stand, provided that once you have seen today you are never handed yesterday again. Third, any stand, provided your own subscription is always there even when other news lags. Fourth, any stand and any copy, but by the end of the night every stand agrees. Each step down is cheaper and more available, and each step down lets you read older news for longer. Consistency is not yes or no; it is a point on that ladder, and you have to name the step.

خطی‌سازی (linearizable)Linearizableترتیب علّی (causal)Causal orderخواندن نوشتهٔ خود (RYW)Read your writesخواندن یکنواMonotonic readsسازگاری نهاییEventual
هر پله پایین‌تر، خواندن ارزان‌تر و در دسترس‌تر است، ولی ممکن است کهنه‌تر باشد.Each rung down makes reads cheaper and more available, and lets them be staler.

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

در تکثیر داده، سازگاری یعنی مجموعه‌ای از قراردادها دربارهٔ این‌که یک خواندن چه نسخه‌ای را مجاز است برگرداند. بالای نردبان، خطی‌سازی است: سیستم طوری رفتار می‌کند که انگار یک نسخه دارد، و ترتیب واقعی زمان رعایت می‌شود. پایین‌تر، قراردادهای جلسه‌ای می‌آیند: خواندن نوشته‌های خود، خواندن یکنوا و ترتیب علّی، که هر کدام بخشی از تجربهٔ یک کاربر را درست می‌کنند. پایین‌ترین پله، سازگاری نهایی است: اگر نوشتن تازه‌ای نرسد، نسخه‌ها بالاخره یکی می‌شوند. مکانیزم‌ها هم متفاوتند: تک‌رهبر با تکثیر همگام خواندن تازه می‌دهد، quorum با همپوشانی، و اجماع با اکثریت. هزینه هم واقعی است: خطی‌سازی برای هر خواندن هماهنگی می‌خواهد و در برش شبکه، طرف اقلیت باید جواب را رد کند. یک هشدار اصطلاحی هم لازم است: C در ACID یعنی نبودِ وضعیت بد و حفظ invariant، ولی C در تکثیر یعنی ترتیب و دیدِ نسخه‌ها؛ این دو یکی نیستند و قاطی کردنشان خیلی رایج است.In replication, consistency is a set of contracts about which version a read is allowed to return. At the top of the ladder sits linearizability: the system behaves as if there were one copy, and real-time order is respected. Below it come the session guarantees — read your writes, monotonic reads, causal order — each fixing part of one user’s experience. The bottom rung is eventual consistency: if no new writes arrive, the copies do agree in the end. The mechanisms differ too: single-leader with synchronous replication gives fresh reads, quorums give overlap, consensus gives a majority. The cost is real: linearizability needs coordination on every read, and during a partition the minority side must refuse. One naming warning: C in ACID means no bad state and preserved invariants, while C in replication means ordering and visibility of copies. They are not the same thing, and mixing them up is very common.

5 levelsنردبان از خطی‌سازی تا سازگاری نهاییthe ladder from linearizable down to eventual
1 RTTکف هزینهٔ خواندن خطی‌سازی‌شده: حداقل یک رفت‌وبرگشتthe floor for a linearizable read: at least one round trip
n/2+1اجماع برای تصمیم‌گیری به اکثریت نیاز داردconsensus needs a majority to decide

سازگاری را باید با اسم صدا زدConsistency has to be named

جملهٔ «سیستم ما سازگار است» هیچ معنایی ندارد تا وقتی نگویی کدام قرارداد. خطی‌سازی، علّی، خواندن نوشتهٔ خود، یکنوا، نهایی؛ هر کدام یک تضمین متفاوت با یک هزینهٔ متفاوت است. بدون اسم، تست هم نمی‌شود نوشت: نمی‌دانی باید منتظر کهنه شدن بمانی یا نمانی، و در جلسهٔ حادثه هر کس برداشت خودش را دارد. کار درست این است که قرارداد را در سند معماری بنویسی و برای هر مسیر خواندن بگویی کدام پله لازم است؛ بقیهٔ مسیرها می‌توانند ارزان‌تر بمانند.The sentence “our system is consistent” means nothing until you name the contract. Linearizable, causal, read your writes, monotonic, eventual: each is a different promise at a different price. Without a name you cannot even write a test, because you do not know whether to wait for staleness, and in an incident review everyone brings their own definition. The useful move is to write the contract into the design doc and state which rung each read path needs; the other paths can stay cheap.

وقتی شبکه می‌شکندWhen the network breaks

نردبان در حالت عادی فقط تأخیر است؛ در برش شبکه تبدیل به انتخاب می‌شود. طرفی که اکثریت را دارد می‌تواند خطی‌سازی را نگه دارد و طرف اقلیت باید بنشیند و خطا بدهد؛ این همان چیزی است که CAP می‌گوید، نه این‌که سازگاری را دور بزن. اگر آن مسیر خواندن حیاتی نباشد، می‌شود نسخهٔ کهنه داد و بعداً جبران کرد. فاجعه وقتی است که تصمیم صریح نباشد: سیستم گاهی کهنه می‌دهد و گاهی خطا، و هیچ‌کس نمی‌داند کدام قرارداد در حال اجراست.The ladder is only latency in normal times; during a partition it becomes a choice. The side holding a majority can keep linearizability, and the minority side must stop and return errors. That is what CAP actually says, not that consistency is optional. If the read path is not critical, you can serve a stale copy and repair later. The disaster is an unstated decision: the system sometimes serves stale data and sometimes fails, and nobody knows which contract is running.

دو معنی کلمهٔ CTwo meanings of C

در ACIDIn ACIDدر تکثیرIn replication
سؤال اصلیMain questionآیا دادهٔ بد و ناسازگار وارد شد؟did bad data get in?خواندن چه نسخه‌ای را می‌بیند؟which version does a read see?
مثال نقضViolation looks likeموجودی منفی، مجموع نادرستnegative stock, a wrong totalخواندن نسخهٔ قدیمیa read returns an old version
ابزارToolsقید، تراکنش، invariantconstraints, transactions, invariantsتکثیر، quorum، اجماعreplication, quorum, consensus

تلهٔ رایجThe usual trap

گفتن «سازگار» بدون نام بردن قرارداد. تیم می‌گوید سیستم consistent است، بعد در تولید کاربر سفارشش را نمی‌بیند و کسی نمی‌داند این باگ است یا رفتار مورد انتظار. تا وقتی نگویی خطی‌سازی می‌خواهی یا سازگاری نهایی، جواب هر دو طرف دعوا درست است و هیچ‌کس هم مسئول نیست.Saying “consistent” without naming the contract. The team says the system is consistent, then in production a customer cannot see her order and nobody knows whether that is a bug or the expected behavior. Until you say linearizable or eventual, both sides of the argument are right and nobody is accountable.

CRDT

نوع دادهٔ مقاوم به تعارض فصل ۶Chapter 6 · دیدن در متنsee in context

دو نسخه را قاطی کن، بدون این‌که با کسی حرف بزنی.Merge two copies without asking anyone, ever.

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

با همسرت یک فهرست خرید مشترک داری. تو در مترو «نان» را اضافه می‌کنی و او در خانه «شیر» را، هر دو گوشی هم آفلاین‌اند. وقتی اینترنت برگشت، هر دو فهرست هر دو قلم را دارند، بدون این‌که کسی پیامی فرستاده باشد؛ یک CRDT همان کاری است که دو نسخه را بدون پرسیدن از کسی با هم جمع می‌کند.You share a shopping list with your partner. You add bread on the subway and she adds milk at home, both phones offline. When the network returns, both lists hold both items, with no message sent; a CRDT is what joins two copies without asking anyone.

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

چند چیز را می‌شود بی‌دردسر جمع کرد و چند چیز را نه. مجموعهٔ اقلام را می‌شود با اجتماع جمع کرد: اگر هر دو طرف «نان» را اضافه کرده باشند، یکی می‌شود و اگر فقط یکی اضافه کرده باشد، همان می‌ماند. شمارنده را می‌شود با جمع دلتاها جمع کرد، چون جمع خاصیت جابه‌جایی دارد. حذف سخت‌تر است: اگر «شیر» را پاک کنی و همسرت هم‌زمان دوباره اضافه‌اش کند، نسخه‌ای که حذف را ندیده، شیر را برمی‌گرداند؛ برای همین حذف باید نشانه بگذارد. متن از همه سخت‌تر است، چون ترتیب کاراکترها مهم است و دو نفر می‌توانند وسط یک جمله بنویسند. بهای همهٔ این‌ها متادیتاست: هر قلم کمی اطلاعات اضافه دارد، هر حذف یک نشانه، و هیچ‌کس نمی‌تواند بدون هماهنگی «فقط یک نفر» را تضمین کند.Some things combine painlessly and some do not. A set of items combines by union: if both sides added bread it stays one bread, and if only one added it, it stays. A counter combines by summing deltas, because addition does not care about order. Deletes are harder: if you remove milk and your partner re-adds it at the same moment, the copy that never saw the delete brings the milk back, so the delete has to leave a marker. Text is hardest, because character order matters and two people can type in the middle of one sentence. The bill for all of it is metadata: every item carries a little extra information, every delete leaves a marker, and nobody can enforce “only one person” without coordination.

CRDT · ادغامCRDT · mergeهر ترتیبی، یک نتیجهany order, one resultبدون هماهنگیno coordinationمتادیتا و نشانهٔ حذفmetadata and tombstonesاجماع · ترتیبConsensus · orderاول ترتیب، بعد اعمالorder first, apply afterنیاز به اکثریتneeds a majorityهماهنگی، ولی سادهcoordination, but simple
دو راه برای رسیدن همه به یک مقدار: یکی ترتیب را از قبل تعیین می‌کند، دیگری ترتیب را بی‌اهمیت می‌کند.Two ways for everyone to reach one value: one fixes the order first, the other makes order irrelevant.

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

CRDT نوع داده‌ای است که یک تابع ادغام دارد با سه خاصیت: جابه‌جایی، شرکت‌پذیری و خودتوانی. همین سه خاصیت باعث می‌شود ترتیب رسیدن و تکرار شدن پیام‌ها مهم نباشد. دو خانواده وجود دارد: حالت‌محور که کل وضعیت را می‌فرستد و روی یک نیم‌شبکه ادغام می‌کند، و عمل‌محور که فقط عملیات را می‌فرستد و به تحویل علّی و بدون تکرار نیاز دارد. نمونه‌های رایج: شمارندهٔ افزایشی که جمع شمارش هر گره است، شمارندهٔ مثبت و منفی که دو شمارندهٔ افزایشی دارد، رجیستر آخرین نوشتن برنده با timestamp، و مجموعهٔ افزودن و حذف که برای هر عضو یک برچسب یکتا نگه می‌دارد. برای متن، CRDT ترتیبی مثل RGA به هر کاراکتر یک شناسهٔ موقعیت می‌دهد تا درج‌های هم‌زمان جای خودشان را پیدا کنند. ادغام باید در همهٔ نسخه‌ها و همهٔ نسخه‌های کد یکسان باشد، وگرنه نتیجه واگرا می‌شود. CRDT تضمین می‌کند نسخه‌هایی که یک مجموعه از به‌روزرسانی‌ها را دیده‌اند یکی شوند، ولی هیچ تضمینی دربارهٔ درست بودن آن مقدار از نظر کسب‌وکار نمی‌دهد.A CRDT is a data type with a merge function that is commutative, associative and idempotent. Those three properties are what make arrival order and duplicate delivery irrelevant. There are two families: state-based, which ships the whole state and merges it over a join semilattice, and operation-based, which ships operations and relies on exactly-once causal delivery. Common examples: a grow-only counter that sums per-replica counts, a positive-negative counter built from two of those, a last-write-wins register with a timestamp, and an add-remove set that keeps a unique tag per member. For text, a sequence CRDT such as RGA gives every character a position identifier so concurrent inserts find their place. The merge must be identical across replicas and across code versions, or the result diverges. A CRDT guarantees that replicas which have seen the same set of updates agree, and guarantees nothing about whether that value is right for the business.

3 lawsجابه‌جایی، شرکت‌پذیری، خودتوانی: شرط بی‌خطر بودن ادغامcommutative, associative, idempotent: what makes a merge safe
1 tombstoneهر حذف یک نشانه می‌گذارد تا مقدار پاک‌شده برنگرددevery delete leaves a marker so a removed value cannot return

چرا جمع‌کردن جواب می‌دهدWhy joining works

سه خاصیت را با یک مثال ببین. جابه‌جایی یعنی ترتیب اعمال مهم نیست: اگر من اول «نان» را اضافه کنم و تو بعد «شیر» را، یا برعکس، مجموعه یکی است. شرکت‌پذیری یعنی گروه‌بندی مهم نیست: ادغام الف با (ب و ج) همان است که (الف با ب) با ج. خودتوانی یعنی اگر یک پیام دو بار برسد، بار دوم چیزی عوض نمی‌کند؛ برای شبکه‌ای که دوباره‌تلاش می‌کند و تضمین تحویل یک‌باره ندارد، این نجات‌بخش است. نتیجه این است که هیچ گره لازم نیست منتظر دیگری بماند؛ هر گره می‌تواند آفلاین باشد و بعد با یک ادغام برگردد. همین است که CRDT را برای کلاینت‌های آفلاین و چند‌مبدأ جذاب می‌کند.Watch the three properties on one example. Commutative means the order of application does not matter: whether I add bread first and you add milk second, or the other way round, the set is the same. Associative means grouping does not matter: merging A with (B and C) equals merging (A with B) and then C. Idempotent means a message that arrives twice changes nothing the second time, which saves you on a network that retries and has no exactly-once delivery. The consequence is that no node has to wait for another; any node can go offline and come back with one merge. That is why CRDTs fit offline clients and multiple masters.

کجا CRDT جواب نمی‌دهدWhere a CRDT does not help

CRDT برای داده‌ای است که ادغامش معنا دارد: مجموعه، شمارنده، نقشه، متن. برای قاعده‌ای که به یک تصمیم جهانی نیاز دارد، جواب ندارد: یکتا بودن ایمیل، این‌که فقط یک نفر آخرین صندلی را بگیرد، یا این‌که مجموع پرداخت‌ها از بودجه بیشتر نشود. این‌ها invariantهای بین چند کلیدند و بدون هماهنگی شکسته می‌شوند. حذف هم هزینه دارد: مجموعهٔ حذف‌پذیر باید برای هر عضو حذف‌شده یک نشانه نگه دارد، پس حافظه با تعداد حذف‌ها رشد می‌کند و باید هرس شود. متن هم متادیتای زیادی می‌برد و برای سندهای بزرگ گران می‌شود. اگر این invariantها را داری، CRDT ابزار اشتباه است؛ آن‌جا اجماع یا قفل لازم داری، با همهٔ هزینه‌اش.A CRDT fits data whose merge is meaningful: a set, a counter, a map, text. It cannot help with a rule that needs one global decision: unique emails, only one person taking the last seat, or a total payment that must not exceed a budget. Those are invariants across keys, and they break without coordination. Deletes cost too: a remove-capable set must keep a marker for every removed member, so memory grows with the number of deletes and has to be pruned. Text carries a lot of metadata and gets expensive for large documents. If you have those invariants, a CRDT is the wrong tool; you need consensus or a lock, with the bill that comes with it.

تلهٔ رایجThe usual trap

فکر کردن این‌که CRDT یعنی سازگاری قوی. CRDT فقط تضمین می‌کند همه به یک مقدار می‌رسند؛ نمی‌گوید آن مقدار از نظر کسب‌وکار درست است. دو کاربر می‌توانند هم‌زمان آخرین صندلی را بگیرند و هر دو با خیال راحت به یک جواب برسند: یک صندلی و دو بلیت.Believing a CRDT means strong consistency. It only guarantees that everyone converges on one value; it says nothing about whether that value is right for the business. Two users can take the last seat at the same time and both converge happily: one seat, two tickets.

eventual consistency

سازگاری نهایی فصل ۶Chapter 6 · دیدن در متنsee in context

اگر بگویی «بالاخره درست می‌شود»، کی درست می‌شود؟“Eventually” is a promise. When does eventually arrive?

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

پیامی در گروه خانوادگی می‌فرستی؛ برای تو همان لحظه زیر انگشتت است و عمه‌ات که اینترنتش ضعیف است نیم ساعت بعد می‌بیند. هیچ‌کس نمی‌گوید پیام گم شده، فقط دیر رسیده. سازگاری نهایی همین قرار است: همه بالاخره یک چیز را می‌بینند، ولی هیچ‌کس نگفته این «بالاخره» چقدر طول می‌کشد.You send a message to the family group; for you it is under your thumb instantly, while your aunt on a weak connection sees it half an hour later. Nobody says the message was lost, only that it was late. Eventual consistency is that deal: everyone sees the same thing in the end, but nobody has said how long the end takes.

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

همین گروه خانوادگی را ادامه بده. دو پیام پشت سر هم می‌فرستی: اول «رسیدم»، بعد «کلیدم را جا گذاشتم». روی گوشی تو ترتیب درست است. روی گوشی کسی که شبکه‌اش قطعه‌قطعه است، ممکن است پیام دوم اول برسد و او فکر کند تو اول کلید را جا گذاشته‌ای و بعد رسیده‌ای. بعد یک نفر پیامی را پاک می‌کند و همان پیام روی یک دستگاه دیگر تا یک هفته می‌ماند. سازگاری نهایی آدم‌ها را وادار نمی‌کند منتظر هم بمانند؛ به همین دلیل گروه حتی وقتی شبکه بد است باز کار می‌کند و به همین دلیل گاهی ترتیبش عجیب است. اگر شبکه هیچ‌وقت درست نشود، این «بالاخره» هم هرگز نمی‌آید، و همین تفاوت بین یک وعده و یک عدد است.Stay with that family group. You send two messages in a row: “I’m here”, then “I left my keys”. On your phone the order is right. On the phone of a cousin with a patchy connection the second message may arrive first, so it looks like you lost the keys before arriving. Later someone deletes a message and it stays on another device for a week. Eventual consistency does not make people wait for each other; that is why the group still works on a bad network, and why its order sometimes looks strange. And if the network never recovers, that “eventually” never comes at all, which is the difference between a promise and a number.

نوشتنWriteنسخهٔ ۲v2رهبرLeaderذخیره شدstoredهمانند دیرSlow replicaنسخهٔ ۱v1همه یکیAll agreeنسخهٔ ۲v2
در فاصلهٔ بین نوشتن و رسیدنش، دو نسخه دو حرف مختلف می‌زنند؛ آخرش یکی می‌شوند، ولی کِی؟Between the write and its arrival the copies disagree; they do agree in the end — the question is when.

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

سازگاری نهایی یک خاصیت زنده‌بودن است، نه خاصیت ایمنی: می‌گوید اگر نوشتن تازه‌ای نرسد، نسخه‌ها بالاخره به یک مقدار می‌رسند. هیچ سقفی برای «بالاخره» نمی‌دهد، ترتیب نمی‌دهد، و یکنوایی هم نمی‌دهد. چیزی که این وضعیت را می‌سازد تکثیر ناهمگام است: نوشتن روی رهبر تأیید می‌شود و بعد در پس‌زمینه به همانندها می‌رود، و هر همانندی که عقب‌تر است، جواب کهنه می‌دهد. برای همین سیستم‌های واقعی فقط به انتظار نمی‌نشینند: read repair مقدار تازه را روی همانند عقب‌مانده می‌نویسد، hinted handoff نوشتن را موقتاً جای دیگری نگه می‌دارد، و anti-entropy در پس‌زمینه تفاوت‌ها را می‌بیند. اگر ادغام قطعی نباشد، این تلاش‌ها هم به واگرایی می‌رسند. یک سطح بالاتر هم وجود دارد: سازگاری نهایی قوی، که می‌گوید هر دو نسخه‌ای که یک مجموعه از به‌روزرسانی‌ها را دیده‌اند دقیقاً یکی هستند، و این همان چیزی است که CRDT می‌دهد. سازگاری نهایی برای فید، شمارندهٔ لایک، حضور آنلاین و کش مناسب است؛ برای موجودی و پرداخت نه.Eventual consistency is a liveness property, not a safety property: it says that absent new writes, replicas do converge on one value. It gives no bound on “eventually”, no ordering, and no monotonicity. What creates the state is asynchronous replication: the write is acknowledged at the leader and travels to followers in the background, and any follower that is behind answers with an old value. Real systems therefore do not just wait: read repair writes the fresh value back to a lagging replica, hinted handoff parks the write somewhere else for a while, and anti-entropy compares replicas in the background. If the merge is not deterministic, all that effort still ends in divergence. One level up sits strong eventual consistency, which says two replicas that have seen the same updates are identical, and that is what a CRDT gives you. Eventual consistency suits feeds, like-button counters, presence and caches; it does not suit stock levels or payments.

unbounded«بالاخره» سقف زمانی ندارد، پس SLO نیست“eventually” has no upper bound, so it is not an SLO
ms–sپنجرهٔ معمول تأخیر: از چند میلی‌ثانیه در یک مرکز تا چند صد میلی‌ثانیه بین منطقه‌هاthe usual lag window: milliseconds inside one datacenter, hundreds across regions

چرا «بالاخره» تضمین ضعیفی استWhy “eventually” is a weak promise

«بالاخره» یک وعدهٔ کیفی است و همین ضعفش است. از یک خواندن نمی‌توانی نقضش را ببینی، چون شاید هنوز وقتش نرسیده باشد؛ باید در طول زمان واگرایی را اندازه بگیری. و اگر همانندی برای همیشه از شبکه بیرون بماند، «بالاخره» هرگز نمی‌آید: دادهٔ کهنه‌اش می‌ماند تا وقتی کسی تعمیرش کند یا از مجموعه حذفش کند. برای همین سیستم‌های جدی یک کران عملی می‌گذارند: تأخیر تکثیر را مانیتور می‌کنند، برای کهنه‌بودن یک بودجه تعیین می‌کنند و همانندی‌ای که از آن بودجه بیرون بزند از سرویس خواندن کنار می‌رود. بدون این عدد، سازگاری نهایی یعنی «شاید هرگز»، و کاربر همان کسی است که کشفش می‌کند.“Eventually” is a qualitative promise, and that is its weakness. A single read cannot show you a violation, because maybe the time has not come yet; you have to measure divergence over time. And if a replica stays cut off forever, “eventually” never arrives: its stale data sits there until someone repairs it or removes it from the set. Serious systems therefore put a practical bound in place: they monitor replication lag, set a budget for staleness, and take a replica that exceeds it out of the read path. Without that number, eventual consistency means “maybe never”, and the user is the one who finds out.

چه چیزهایی را هم تضمین نمی‌کندWhat else it does not guarantee

سازگاری نهایی حتی برای یک کاربر هم کافی نیست. ممکن است نوشتهٔ خودت را نبینی، چون خواندنت به همانندی رسیده که هنوز آن را نگرفته است. ممکن است به عقب برگردی، چون این بار همانند عقب‌تری جواب داده. و ممکن است ترتیب را گم کنی: جواب یک پیام قبل از خود پیام برسد، چون از مسیر دیگری آمده. این‌ها با سازگاری نهایی نقض نمی‌شوند، چون اصلاً چیزی دربارهٔ ترتیب نگفته است. راه درست این است که روی همان زیرساخت، قراردادهای جلسه‌ای بگذاری: خواندن نوشته‌های خود و خواندن یکنوا برای همان کاربر، و اگر ترتیب مهم است، تحویل علّی. سازگاری نهایی نقطهٔ شروع است، نه پایان.Eventual consistency is not enough even for one user. You may fail to see your own write, because your read reached a replica that has not applied it. You may go backwards, because this time a more lagging replica answered. And you may lose order: a reply arrives before the message it answers, because it took a different path. None of that contradicts eventual consistency, which never said anything about order. The right move is to layer session guarantees on the same infrastructure: read your writes and monotonic reads for that user, and causal delivery when order matters. Eventual consistency is a starting point, not a finish line.

تلهٔ رایجThe usual trap

گذاشتن «بالاخره» به‌جای عدد در SLO. تیم می‌گوید سیستم سازگاری نهایی دارد و در جلسهٔ حادثه نمی‌تواند بگوید یک کاربر چقدر می‌تواند دادهٔ کهنه ببیند. اگر پنجره را اندازه نگیری و سقف نگذاری، «بالاخره» یعنی «شاید هرگز»، و کاربر همان کسی است که کشفش می‌کند.Putting “eventually” where the SLO needs a number. The team says the system is eventually consistent and then, in the incident review, cannot say how stale a user may see the data. If you do not measure the window and cap it, “eventually” means “maybe never”, and the user is the one who finds out.

hinted handoff

تحویل با یادداشت فصل ۶Chapter 6 · دیدن در متنsee in context

همسایه بسته‌ات را می‌گیرد. اگر دو هفته نباشی چه؟A neighbour holds your parcel. What if you are away two weeks?

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

وقتی بسته‌ات را می‌آورند و خانه نیستی، همسایه بسته را می‌گیرد و یک یادداشت روی در می‌گذارد. بسته گم نشده؛ فقط چند ساعت دیرتر به دستت می‌رسد. تحویل با یادداشت هم یک نوشتن را موقتاً به همین همسایه می‌سپارد.When a parcel arrives and you are out, the neighbour takes it and leaves a note on your door. The parcel is not lost; it just reaches you a few hours later. Hinted handoff leaves a write with that neighbour for a while.

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

همسایه برای چند بستهٔ دیگر هم همین کار را می‌کند و یادداشت‌ها روی در بیشتر می‌شوند. اگر سفرت دو هفته طول بکشد، همسایه انباری از بسته‌های تو دارد و ممکن است یکی از یادداشت‌ها گم شود. تحویل با یادداشت همین را می‌خرد: نوشتن معطل نمی‌شود، ولی تحویل موقتاً جای دیگری است و به یک پنجرهٔ زمانی نیاز دارد که بعد از آن، دیگر کسی مسئول آن بسته نیست.The neighbour does the same for a few more parcels and the notes on your door multiply. If your trip lasts two weeks, the neighbour is storing your post and one of the notes may go missing. Hinted handoff buys exactly this: writes do not stall, but delivery sits somewhere else for a while and needs a time window after which nobody is responsible for it any more.

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

در خوشه‌ای که همانند مقصد در دسترس نیست، نود دیگری نوشتن را با یک یادداشت ذخیره می‌کند: «این مال همانند ۳ است». وقتی همانند برگشت، یادداشت‌ها به آن تحویل داده می‌شوند. این کار نوشتن را در زمان خرابی نگه می‌دارد، ولی دو هزینه دارد: نوشتن موقتاً در جایی نشسته که خواندن‌های quorum ممکن است آن را نبینند، و اگر خرابی از پنجرهٔ نگه‌داری یادداشت‌ها طولانی‌تر شود، باید ضدآنتروپی آن اختلاف را ترمیم کند. یادداشت‌ها هم باید با نسخه یا بردار همراه باشند تا هنگام برگشت، نوشتن قدیمی روی نوشتن تازه‌تر ننشیند.When a write's home replica is unreachable, another node stores it with a note saying “this belongs to replica 3”. When that replica returns, the notes are handed over. This keeps writes flowing during a failure, at two costs: the write temporarily sits somewhere a quorum read may not look, and if the outage outlasts the hint window the gap has to be repaired by anti-entropy. Hints must also carry a version or vector so that replaying an old write does not land on top of a newer one.

۱ یادداشتبرای هر نوشتن گم‌شده، یک یادداشت روی نود جانشینone note on a stand-in node for each misplaced write
پنجرهٔ زمانیبعد از آن، یادداشت دور ریخته و ترمیم به ضدآنتروپی سپرده می‌شودafter it, the hint is dropped and repair falls to anti-entropy

تلهٔ رایجThe usual trap

تکیه کردن به تحویل با یادداشت برای خرابی‌های طولانی. یادداشت‌ها پنجرهٔ عمر دارند و بعد از آن دور ریخته می‌شوند؛ اگر همانند یک هفته غایب باشد، نوشتن‌هایی که فقط در یادداشت بودند ممکن است بی‌صدا از بین بروند.Leaning on hinted handoff for long outages. Hints have a lifetime and are discarded after it; if a replica is gone for a week, writes that lived only in a hint can disappear without a trace.

last write wins

آخرین نوشتن برنده فصل ۶Chapter 6 · دیدن در متنsee in context

ساعت دو گوشی یک دقیقه اختلاف دارد. کدام نوشتن «آخر» است؟Two phones disagree by a minute. Which write is “last”?

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

با همسرت روی یک تقویم مشترک کار می‌کنی. تو ساعت ۹:۰۵ قرار دندان‌پزشکی را می‌گذاری و او ساعت ۹:۰۴ همان روز را برای سفر می‌نویسد، و سیستم می‌گوید هر کدام زمان بزرگ‌تری دارد بماند؛ پس قرار دندان‌پزشکی می‌ماند و سفر بی‌صدا پاک می‌شود. آخرین نوشتن برنده همین است: یک برندهٔ ساده، و بازنده‌ای که هیچ‌کس خبرش را نمی‌دهد.You and your partner share a calendar. At 09:05 you add a dentist appointment and at 09:04 she blocks the same day for a trip, and the system keeps whichever has the larger timestamp; so the dentist survives and the trip is silently erased. That is last write wins: one simple winner, and a loser nobody is told about.

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

یک تخته وایت‌برد را تصور کن که هر کس گوشهٔ نوشته‌اش ساعت می‌زند و هر کس دیرتر نوشته، نوشتهٔ قبلی را پاک می‌کند. برای یک مقدار ساده عالی است: آخرین وضعیت چراغ اتاق، آخرین موقعیت ماشین، آخرین انتخاب از یک فهرست. هیچ هماهنگی لازم نیست، هیچ رئیسی لازم نیست، و هر گره‌ای می‌تواند خودش تصمیم بگیرد. مشکل از جایی شروع می‌شود که نوشته‌ها ارزش دارند: یک ویرایش طولانی، یک قلم سبد خرید، یک پیام. آن‌جا «دیرتر» با «درست‌تر» یکی نیست، و چون فقط برنده ذخیره می‌شود، بازنده قابل بازیابی نیست. بدتر، ساعت‌ها با هم یکی نیستند: گوشی‌ای که عقب است، نوشتهٔ تازه‌اش را به نسخهٔ کهنه می‌بازد.Picture a whiteboard where everyone timestamps their corner and the latest timestamp wipes the earlier writing. For one simple value it is excellent: the latest state of a lamp, the latest position of a car, the latest choice from a menu. No coordination, no boss, and every node can decide alone. The trouble starts when the writes have value: a long edit, a line in a shopping cart, a message. There, “later” is not “more correct”, and because only the winner is stored the loser cannot be recovered. Worse, the clocks disagree: a phone that runs behind loses its fresh write to an older value.

نوشتن AWrite Aساعت ۹:۰۵09:05نوشتن BWrite Bساعت ۹:۰۴09:04نتیجهResultفقط AA only
دو نوشتن هم‌زمان، دو ساعت؛ قاعده فقط عدد بزرگ‌تر را نگه می‌دارد، نه نوشتن درست‌تر را.Two concurrent writes, two clocks; the rule keeps the larger number, not the better answer.

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

آخرین نوشتن برنده یعنی هر مقدار یک timestamp دارد و برای هر کلید، مقداری با بزرگ‌ترین timestamp می‌ماند. این در واقع یک رجیستر CRDT است، به شرطی که ترتیب زمان‌ها کامل و یکتا باشد. منبع زمان مهم‌ترین تصمیم است: ساعت دیوار، ساعت منطقی لامپورت، یا ساعت منطقی هیبریدی. ساعت دیوار ساده است و همان چیزی است که خیلی از پایگاه‌های داده به‌طور پیش‌فرض می‌گذارند، ولی به هم‌زمانی ساعت‌ها بند است؛ یک ماشین مجازی که متوقف شده یا ساعتی که با NTP عقب کشیده، برنده را عوض می‌کند. اگر دو نوشتن timestamp یکسان بگیرند، شکستن تساوی باید قطعی باشد، مثلاً با شناسهٔ گره، وگرنه همانندها واگرا می‌شوند. حذف هم باید با همان منطق timestamp بیاید؛ در غیر این صورت یک حذف بدون مقدار به یک نوشتن قدیمی می‌بازد و مقدار پاک‌شده برمی‌گردد. و تضمینی که می‌دهد فقط همگرایی است: «آخر» یعنی آخر بر اساس ساعت، نه آخر بر اساس علّیت؛ نوشتنی که در واقعیت بعد از دیگری اتفاق افتاده، اگر ساعتش عقب باشد، می‌بازد.Last write wins attaches a timestamp to every value and, per key, keeps the value with the highest timestamp. It is in effect a CRDT register, provided the timestamp order is total and unique. The source of time is the important decision: wall clock, a Lamport clock, or a hybrid logical clock. Wall clock is simple and is what many databases do by default, but it depends on clocks agreeing; a paused virtual machine or a clock stepped backwards by NTP changes the winner. If two writes carry the same timestamp, the tie-break must be deterministic, for example by node id, or replicas diverge. Deletes must follow the same timestamp logic; otherwise a delete with no value loses to an older write and the removed value reappears. And the only guarantee it gives is convergence: “last” means last by the clock, not last by causality, so a write that really happened later loses if its clock runs behind.

1 valueهر کلید فقط یک مقدار نهایی دارد؛ نوشتن بازنده دور ریخته می‌شودone final value per key; the losing write is discarded
clock skewهر اختلاف ساعت یعنی احتمال یک برندهٔ اشتباهany clock disagreement is a chance of the wrong winner
tie → node idبرابری زمان باید با یک قاعدهٔ قطعی شکسته شودequal timestamps need a deterministic tie-break

ساعت چه بلایی سر «آخر» می‌آوردWhat the clock does to “last”

کلمهٔ «آخر» به یک ساعت تکیه دارد و ساعت‌ها دروغ می‌گویند. با NTP اختلاف معمولاً در حد ده‌ها میلی‌ثانیه است، بدون آن می‌تواند ثانیه‌ها باشد؛ و NTP می‌تواند ساعت را عقب بکشد، نه فقط جلو. یک ماشین مجازی که مکث کرده، بعد از بیدار شدن ساعتی عقب‌تر دارد. حالا دو نوشتن را در نظر بگیر که یکی واقعاً بعد از دیگری رخ داده ولی ساعتِ نویسندهٔ دوم چند ثانیه عقب است: قاعدهٔ آخرین نوشتن برنده آن را کهنه‌تر می‌بیند و نوشتن تازه را دور می‌ریزد. این تنها یک بدشانسی نیست؛ هر چه توزیع‌شده‌تر باشی بیشتر تکرار می‌شود. برای همین این قاعده در کنار version vector معنا پیدا می‌کند: اول بفهم هم‌زمانی هست یا نه، بعد اگر بود قاعده را اعمال کن.The word “last” leans on a clock, and clocks lie. With NTP the usual disagreement is tens of milliseconds; without it, seconds. NTP can step a clock backwards, not only forwards. A paused virtual machine wakes up with a clock that is behind. Now take two writes where one really happened after the other, but the second writer’s clock is a few seconds behind: last write wins sees it as the older one and discards the newer write. That is not bad luck; the more distributed you are, the more often it repeats. Which is why the rule makes sense only next to a version vector: first learn whether the writes were concurrent, and apply the rule only if they were.

کجا می‌شود از آن استفاده کردWhere it is safe to use

آخرین نوشتن برنده برای مقداری خوب است که از دست دادن نوشتن در آن قابل قبول است: آخرین موقعیت یک راننده، آخرین وضعیت آنلاین بودن، یک تنظیم که کاربر عوض می‌کند و آخرینش کافی است. برای فهرست ویرایش‌ها، سبد خرید، یا هر چیزی که کاربر انتظار دارد کارش نرود، فاجعه است. اگر مجبور به استفاده‌ای، سه کار بکن: شکستن تساوی را قطعی کن، نوشتن بازنده را جایی نگه دار تا لااقل بدانی چه از دست رفته، و برای حذف‌ها نشانه بگذار. و اگر مقدار مهم است، جای این قاعده از یک رجیستر با version vector استفاده کن و تعارض واقعی را به لایهٔ کاربرد بده؛ آن‌جا می‌شود از کاربر پرسید، ولی بعد از دور ریختن داده نمی‌شود.Last write wins suits a value where losing a write is acceptable: a driver’s last position, a last-seen online flag, a setting whose latest value is all you need. For a list of edits, a shopping cart, or anything a user expects to keep, it is a disaster. If you must use it, do three things: make the tie-break deterministic, keep the losing write somewhere so you can at least see what was lost, and leave markers for deletes. If the value matters, replace the rule with a register carrying a version vector and hand real conflicts to the application layer; there you can ask the user, which you cannot do after the data is gone.

تلهٔ رایجThe usual trap

استفاده از این قاعده برای چیزی که حذف و ویرایش در آن مهم است. یک ساعت که چند ثانیه عقب باشد کافی است تا ویرایش تازه‌تر به نسخهٔ کهنه ببازد، و چون چیزی جز مقدار برنده ذخیره نمی‌شود، هیچ‌کس نمی‌فهمد چه از دست رفته. اگر مقدار برایت مهم است، این قاعده را برای تاریخ ویرایش‌ها به کار نبر.Using it for data where edits and deletes matter. A clock a few seconds behind is enough for a newer edit to lose to an older value, and because nothing but the winner is stored, nobody learns what was lost. If the value matters, do not apply this rule to a history of edits.

چه کسی تصمیم می‌گیرد نوشتن بعدی کجا برود؟Who decides where the next write goes?

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

در نانوایی یک نفر پشت صندوق نشسته و نوبت‌ها را می‌نویسد؛ دو شاگرد فقط از روی همان فهرست نان می‌دهند. اگر شاگردها خودشان نوبت بسازند، دو نفر یک نان می‌گیرند. رهبر همان آدم پشت صندوق است: تنها جایی که ترتیب نوشتن‌ها ساخته می‌شود و بقیه همان را دنبال می‌کنند.At the bakery one person sits at the till and writes the queue; two assistants hand out bread only from that list. If the assistants invent their own order, two people get the same loaf. The leader is the person at the till: the one place where the order of writes is made, and everyone else follows it.

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

یک صندوق یعنی یک ترتیب، و یک ترتیب یعنی هیچ‌کس دوبار نمی‌فروشد. وقتی فقط یک نفر نوبت می‌دهد، لازم نیست شاگردها با هم مذاکره کنند و لازم نیست هیچ‌کس منتظر تصمیم دیگری بماند؛ همین سادگی، تمام ارزش رهبر است. بهایش سه چیز است. اگر آن یک نفر به ناهار برود و کسی جایش ننشیند، فروش می‌ایستد؛ این همان تعویض رهبر است. اگر در همان فاصله دو نفر هم‌زمان پشت صندوق بنشینند، دو نوبت موازی ساخته می‌شود و یک نان دو بار فروخته می‌شود. و اگر آن یک نفر کند باشد، همهٔ صف کند می‌شود؛ شاگردها هم همیشه چند نوبت عقب‌ترند، چون اول باید نوشته شود.One till means one order, and one order means nobody sells the same loaf twice. When a single person hands out the queue, the assistants do not have to negotiate and no one waits for someone else’s decision; that simplicity is the whole value of a leader. It costs three things. If that person goes to lunch and nobody sits down, sales stop: that is failover. If two people sit at the till for the same minute, two parallel queues appear and one loaf is sold twice. And if that person is slow, the whole queue is slow; the assistants are always a few orders behind, because the list has to be written first.

همهٔ نوشتن‌هاAll writesرهبر: نوشتن + لاگLeader: write + logهمانند ۱: خواندنReplica 1: readهمانند ۲: خواندنReplica 2: readنوشتن فقط از رهبر می‌گذرد؛ خواندن می‌تواند از هر نسخه‌ای بیایدWrites go through the leader only; reads can come from any copy
یک مسیر برای نوشتن، چند مسیر برای خواندن؛ همان تقسیم کاری که رهبر می‌سازد.One path for writes, several for reads: the division of labor a leader creates.

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

در تکثیر تک‌رهبر، یک گره رهبر همهٔ نوشتن‌ها را می‌پذیرد. رهبر نوشتن را در لاگ محلی خودش می‌نویسد و بعد آن لاگ را به همانندها می‌فرستد؛ همانندها به همان ترتیب اعمال می‌کنند و برای همین همه به یک وضعیت می‌رسند. مسیر لاگ می‌تواند انتقال WAL، تغییرهای منطقی ردیف‌ها، یا حتی دستورها باشد، ولی هر کدام محدودیت خودش را دارد. نوشتن از همانند باید به رهبر هدایت شود، وگرنه در جایی نوشته می‌شود که کسی آن را تکثیر نمی‌کند. خواندن می‌تواند از هر نسخه‌ای بیاید، و همین جاست که ظرفیت خواندن زیاد می‌شود؛ ولی ظرفیت نوشتن با رهبر بیشتر نمی‌شود. در تکثیر همگام، رهبر پیش از تأیید به کلاینت منتظر تأیید حداقل یک همانند می‌ماند، پس تأخیر بیشتری دارد و در عوض خرابی رهبر آخرین نوشتن را از بین نمی‌برد؛ تکثیر ناهمگام سریع‌تر است و در تعویض رهبر ممکن است چند نوشتن تأییدشده را از دست بدهد. تعویض رهبر خودش یک پروتکل است: تشخیص خرابی با heartbeat و timeout، انتخاب رهبر تازه با اجماع یا هماهنگ‌کنندهٔ بیرونی، هدایت کلاینت‌ها، و کنار گذاشتن رهبر قدیمی با fencing تا دو نفر هم‌زمان نوشتن نپذیرند.In single-leader replication one node, the leader, accepts all writes. It writes to its local log and ships that log to the followers, which apply it in the same order, which is why every copy reaches the same state. The log can be a WAL, logical row changes, or even statements, and each has its own limits. A write that reaches a follower must be redirected to the leader, or it lands somewhere nobody replicates. Reads can be served by any copy, and that is where read capacity grows; write capacity does not grow with a leader. Synchronous replication waits for at least one follower acknowledgement before answering the client, which costs latency but means a leader failure does not erase the latest write; asynchronous replication is faster and may lose acknowledged writes during failover. Failover is a protocol of its own: detect failure with heartbeats and timeouts, choose a new leader through consensus or an external coordinator, redirect clients, and fence the old leader so two nodes never accept writes at once.

1 writerهمهٔ نوشتن‌ها از یک مسیر می‌گذرندevery write goes through one path
secondsheartbeat و timeout معمول برای تشخیص خرابیa typical heartbeat and timeout for failure detection
2 leadersدر split-brain، دقیقاً همان چیزی که نباید بشودin split-brain, exactly what must not happen

صندلی خالی: وقتی رهبر می‌افتدThe empty chair: when the leader falls

تعویض رهبر ساده به نظر می‌رسد و پر از تله است. اول، تشخیص: با heartbeat و timeout تصمیم می‌گیری رهبر مرده است، ولی یک مکث طولانی GC یا یک شبکهٔ کند هم می‌تواند همان علائم را بدهد؛ اگر زود تصمیم بگیری، دو رهبر داری و اگر دیر، سرویس بی‌رهبر می‌ماند. دوم، دادهٔ گم‌شده: با تکثیر ناهمگام، رهبر تازه ممکن است چند نوشتن تأییدشدهٔ رهبر قبلی را نداشته باشد و همان‌ها بی‌صدا می‌روند. سوم، بازگشت: رهبر قدیمی که فقط کند بوده برمی‌گردد و اگر fence نشده باشد، همچنان نوشتن می‌پذیرد. برای همین هر تعویض رهبر باید با یک توکن حصار همراه باشد که کلاینت‌ها و ذخیره‌سازها آن را بررسی کنند.Failover looks simple and is full of traps. First, detection: heartbeats and a timeout decide the leader is dead, but a long GC pause or a slow network produces the same symptoms; decide too early and you have two leaders, decide too late and you have none. Second, lost data: with asynchronous replication the new leader may be missing writes the old leader already acknowledged, and those vanish quietly. Third, the return: the old leader that was only slow comes back, and if it was never fenced it keeps accepting writes. Every failover therefore needs a fencing token that clients and storage check.

چرا نوشتن با رهبر مقیاس نمی‌گیردWhy writes do not scale with a leader

افزودن همانند فقط خواندن را پخش می‌کند. هر نوشتن هنوز باید از رهبر بگذرد تا ترتیب ساخته شود، پس رهبر گلوگاه نوشتن است؛ اگر نوشتن گلوگاه است، همانند بیشتر فقط تأخیر تکثیر را بیشتر می‌کند. راه افزایش ظرفیت نوشتن، sharding است: تقسیم کلیدها بین چند رهبر، هر کدام مسئول بخشی از داده. البته sharding هم ترتیب جهانی نمی‌سازد و اگر تراکنش بین تیکه‌ها لازم داری، دوباره به هماهنگی برمی‌گردی. پس سؤال درست این نیست که چند همانند بسازم، این است که نوشتن را باید کجا تقسیم کنم.Adding replicas spreads reads only. Every write still has to pass through the leader to be ordered, so the leader is the write bottleneck; if writes are the bottleneck, more replicas just add replication lag. The way to add write capacity is sharding: split the keys across several leaders, each owning part of the data. Sharding does not give you a global order either, and if you need transactions across pieces you are back to coordination. So the useful question is not how many replicas to add, but where to split the writes.

تلهٔ رایجThe usual trap

تعویض رهبر بدون حصار. رهبر قدیمی که فقط کند بوده برمی‌گردد و همچنان نوشتن می‌پذیرد، در حالی که رهبر تازه هم نوشتن می‌پذیرد؛ نتیجه دو تاریخ مختلف از یک جدول است. تا وقتی کلاینت‌ها و رهبر قدیمی به‌طور قطعی از نوشتن محروم نشوند، تعویض موفق فقط یک خوش‌بینی است.Failing over without fencing. The old leader that was only slow comes back and still accepts writes while the new leader does the same, and you end up with two histories of one table. Until clients and the old leader are provably barred from writing, a successful failover is only an optimistic reading.

leaderless replication

تکثیر بی‌رهبر فصل ۶Chapter 6 · دیدن در متنsee in context

اگر رئیسی نباشد، نوشتن را چه کسی قبول می‌کند؟With no boss, who accepts the write?

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

می‌خواهی خبر تغییر ساعت کلاس را به همه برسانی و به سه همکلاسی زنگ می‌زنی. وقتی دو نفرشان جواب بدهند، خیالت راحت است که خبر پخش شده و دیگر لازم نیست کسی مسئول پخش باشد. تکثیر بی‌رهبر همین است: چند گره، و نوشتنی که وقتی قبول می‌شود که تعداد کافی از آن‌ها آن را گرفته باشند.You need to pass on a change of class time, so you call three classmates. When two of them answer, you are confident the news is out and nobody had to be the official distributor. Leaderless replication works the same way: several nodes, and a write that counts once enough of them have it.

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

همین زنجیرهٔ تلفنی را ادامه بده. اگر بعداً بپرسی «ساعت کلاس چند است؟» و فقط از یک نفر بپرسی که در آن دو نفر اول نبوده، ممکن است ساعت قدیمی را بشنوی. برای همین باید تعداد کافی بپرسی که حتماً یکی‌شان در گروه اول باشد؛ این همان قاعدهٔ همپوشانی است. بدتر، اگر دو نفر هم‌زمان دو خبر متفاوت پخش کنند، هیچ‌کدام «خبر رسمی» نیستند و باید بعداً با هم کنار بیایند. سود این ترتیب این است که اگر یکی در دسترس نباشد، کار متوقف نمی‌شود؛ هزینه‌اش این است که هیچ‌کس ترتیب نهایی را تضمین نمی‌کند و خواندن باید خودش تازه‌ترین را پیدا کند.Stay with that phone tree. If you later ask “what time is the class?” and you ask only one person who was not in the first two, you may hear the old time. So you have to ask enough people that at least one of them was in the first group; that is the overlap rule. Worse, if two people spread two different announcements at once, neither is the official one and they have to be reconciled later. The gain is that one unavailable person does not stop the work; the cost is that nobody guarantees a final order, and a reader has to work out which answer is freshest.

نوشتن: A و Bwrite: A and Bخواندن: B و Cread: B and Cگرهٔ ANode Aگرهٔ BNode Bگرهٔ CNode C
مجموعهٔ نوشتن و خواندن روی یک گره همپوشانی دارند؛ همان گره تضمین می‌کند خواندن نسخهٔ تازه را ببیند.The write set and the read set overlap on one node, and that node is what lets a read see the fresh value.

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

در تکثیر بی‌رهبر، هیچ گره‌ای رهبر نیست و هر نسخه می‌تواند نوشتن را بپذیرد. کلاینت یا یک هماهنگ‌کننده نوشتن را به همهٔ n نسخه می‌فرستد و وقتی w تأیید گرفت، نوشتن را موفق می‌داند. خواندن از r نسخه انجام می‌شود و از میان جواب‌ها تازه‌ترین انتخاب می‌شود. اگر w + r > n باشد، هر مجموعهٔ خواندن با هر مجموعهٔ نوشتن حداقل یک گره مشترک دارد، پس دست‌کم یک جواب نوشتن آخر را دیده است؛ با n برابر سه و w و r برابر دو، این شرط برقرار است و یک گرهٔ ازکارافتاده هم هنوز قابل تحمل است. برای این‌که خواندن‌های بعدی هم درست بمانند، read repair مقدار تازه را روی نسخه‌های عقب‌مانده می‌نویسد و anti-entropy در پس‌زمینه تفاوت‌ها را می‌بندد. اگر یک گره در دسترس نباشد، hinted handoff نوشتن را موقتاً روی گرهٔ دیگری نگه می‌دارد و بعد تحویل می‌دهد؛ این sloppy quorum است و دسترس‌پذیری را بالا می‌برد، ولی ممکن است نوشتن هنوز روی گرهٔ اصلی خودش نباشد. چیزی که از دست می‌دهی ترتیب کلی است: بدون یک تصمیم‌گیرندهٔ واحد، compare-and-set خطی و یکتایی سخت می‌شود و نوشتن‌های هم‌زمان باید بعداً حل شوند.In leaderless replication no node is the leader and any replica can accept a write. The client, or a coordinator, sends the write to all n replicas and counts it as successful after w acknowledgements. A read goes to r replicas and picks the freshest answer. If w + r > n, every read set intersects every write set, so at least one answer has seen the latest write; with n equal to three and w and r equal to two that holds, and one failed node is still tolerated. To keep later reads correct, read repair writes the fresh value back to lagging replicas and anti-entropy closes the gaps in the background. If a node is unavailable, hinted handoff parks the write on another node and delivers it later; that is a sloppy quorum, which buys availability at the price of a write that may not yet be on its home node. What you give up is a global order: without one decider, linearizable compare-and-set and uniqueness are hard, and concurrent writes have to be resolved afterwards.

w+r>nشرط همپوشانی؛ با آن هر خواندن دست‌کم یک نسخهٔ تازه می‌بیندthe overlap rule; it keeps one fresh copy in every read
3 / 2 / 2پیکربندی کلاسیک: سه گره، دو تأیید نوشتن، دو خواندنthe classic setup: three nodes, two write acks, two reads
1 nodeدر ۳/۲/۲ یک گرهٔ ازکارافتاده هنوز تحمل می‌شودin 3/2/2 one failed node is still tolerated

همپوشانی به زبان سادهOverlap in plain words

فرمول w + r > n را می‌شود با یک مثال دید. سه گره داری و می‌خواهی یک گره ازکار بیفتد. اگر نوشتن را با دو تأیید موفق بدانی و خواندن را از دو گره انجام دهی، هر جفت نوشتن و هر جفت خواندن روی حداقل یک گره مشترک‌اند؛ پس خواندن حتماً یک نسخهٔ تازه می‌بیند. اگر r را یک بگذاری، خواندن از گره‌ای می‌آید که ممکن است در مجموعهٔ نوشتن نبوده باشد و جواب کهنه می‌گیری. اگر w را یک بگذاری، نوشتن سریع‌تر تأیید می‌شود ولی احتمال کهنگی بیشتر است. این انتخاب همان چیزی است که در پایگاه‌های داده به آن قابلیت تنظیم می‌گویند: می‌توانی برای هر کوئری w و r را عوض کنی، با علم به این‌که تنظیم ضعیف‌تر یعنی خواندن ارزان‌تر و کهنه‌تر.The formula w + r > n is easier with an example. You have three nodes and want to survive one failure. If a write counts after two acknowledgements and a read asks two nodes, every write pair and every read pair share at least one node, so the read is certain to see a fresh copy. Set r to one and the read may land on a node outside the write set and return stale data. Set w to one and the write is acknowledged sooner while staleness becomes more likely. That choice is what databases call tunability: you can change w and r per query, knowing that a weaker setting means cheaper and staler reads.

چرا نمی‌شود با آن قفل ساختWhy you cannot build a lock on it

برای قفل یا شمارندهٔ یکتا به ترتیب کلی نیاز داری: باید همه توافق کنند کدام نوشتن اول است. quorum این را نمی‌دهد، چون فقط تضمین می‌کند چیزی از قلم نیفتد، نه این‌که ترتیب چیست. دو کلاینت می‌توانند هم‌زمان از دو مجموعهٔ متفاوت دو تأیید بگیرند و هر دو فکر کنند قفل را گرفته‌اند. برای همین قفل و انتخاب رهبر و شمارندهٔ یکتا به اجماع می‌روند، که یک نفر را به‌عنوان تصمیم‌گیرنده انتخاب می‌کند و اکثریت را پشت آن می‌آورد. اگر quorum را برای کاری به کار ببری که به تصمیم واحد نیاز دارد، درست همان لحظه‌ای که همه‌چیز به هم می‌ریزد می‌فهمی ابزار اشتباه بوده است.A lock or a unique counter needs a total order: everyone must agree which write came first. A quorum does not provide that; it only promises that nothing is missed, not what the order is. Two clients can collect two acknowledgements from two different sets at the same time and both believe they hold the lock. That is why locks, leader election and unique counters go to consensus, which picks one decider and puts a majority behind it. Use a quorum for a job that needs a single decision and you discover the wrong tool at exactly the moment everything falls apart.

تلهٔ رایجThe usual trap

فکر کردن این‌که w+r>n یعنی سازگاری قوی. آن نابرابری فقط می‌گوید در جواب‌ها یک نسخهٔ تازه هست؛ نمی‌گوید کدام است، و با sloppy quorum یا خواندن از گرهٔ عقب‌مانده می‌شود نسخهٔ کهنه گرفت. اگر خواندن باید همیشه آخرین نوشتن را ببیند، quorum جواب نمی‌دهد.Reading w+r>n as strong consistency. The inequality only says a fresh copy is among the replies; it does not say which one, and a sloppy quorum or a read from a lagging node can still return stale data. If a read must always see the latest write, a quorum is not the answer.

membership

عضویت خوشه فصل ۶Chapter 6 · دیدن در متنsee in context همچنینalso in فصل ۱۰ch 10

نود عضو خوشه است یا فقط دارد جواب می‌دهد؟Who is in the cluster: whoever answers, or whoever is listed?

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

در گروه خانوادگی یک نفر لیست اعضا را دارد و بقیه هم نسخهٔ خودشان را. وقتی دایی از گروه بیرون می‌رود، تا همه لیستشان را عوض نکنند، بعضی‌ها هنوز او را عضو حساب می‌کنند و برایش پیام می‌فرستند. عضویت خوشه هم همان لیست مشترک است، با این تفاوت که باید با توافق عوض شود.In the family group one person keeps the member list and everyone else has their own copy. When your uncle leaves, until everybody updates their list some people still count him as a member and keep sending him messages. Cluster membership is that shared list, except it has to change by agreement.

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

اگر دو نفر دو لیست متفاوت داشته باشند و رأی‌گیری کنند، نتیجه به این بستگی دارد که کدام لیست را مبنا بگیری. اضافه کردن عضو هم همین‌طور است: تا وقتی بقیه خبر نداشته باشند، پیام‌های او به بعضی‌ها می‌رسد و به بعضی‌ها نه. یک لیست مشترک که همه روی آن توافق دارند کل ماجرا را ساده می‌کند، ولی خودِ توافق سخت‌ترین بخش است. تا وقتی توافق نشده، هر کس بر اساس نسخهٔ خودش حساب می‌کند و دو حساب متفاوت، دو نتیجهٔ متفاوت می‌دهد.If two people hold different lists and call a vote, the result depends on which list you count. Adding a member is the same: until the others hear about it, that person's messages reach some and not others. One shared list everyone agrees on simplifies everything, but agreeing is the hard part. Until then each person counts from their own copy, and two counts give two different answers.

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

عضویت خوشه یعنی مجموعهٔ نودهایی که رسماً بخشی از خوشه‌اند؛ نه فقط نودهایی که الآن جواب می‌دهند. تصمیم دربارهٔ این مجموعه باید با اجماع گرفته شود، وگرنه دو بخش از خوشه می‌توانند دو عضویت متفاوت بسازند و هر کدام اکثریت خودشان را داشته باشند. در الگوریتم‌هایی مثل Raft، تغییر عضویت با پروتکل مشترک (joint consensus) انجام می‌شود یا در هر بار یک نود اضافه و حذف می‌شود تا در هیچ لحظه‌ای دو اکثریت جدا وجود نداشته باشد. تشخیص خرابی با heartbeat و timeout تقریبی است؛ اگر عضویت را بر اساس همین حدس‌ها عوض کنی، یک کندی موقت می‌تواند نودی سالم را بیرون بیندازد و جابه‌جایی داده راه بیندازد.Cluster membership means the set of nodes that officially belong to the cluster, not merely those answering right now. Decisions about that set must go through consensus, or two parts of the cluster can build two different memberships and each claim its own majority. Algorithms such as Raft change membership by joint consensus, or by adding and removing one node at a time, so that two separate majorities never exist at once. Failure detection through heartbeats and timeouts is approximate; change membership on those guesses and a brief stall can eject a healthy node and start moving data.

N/2 + 1اکثریتی که برای هر تصمیم عضویت لازم استthe majority every membership decision needs
۱ تغییردر هر بار فقط یک نود اضافه یا حذف می‌شودone node is added or removed per change

تلهٔ رایجThe usual trap

عوض کردن عضویت بر اساس تشخیص خرابی محلی. هر نود با timeout خودش فکر می‌کند چه کسی زنده است؛ اگر همان را مبنای حذف و افزودن کنی، یک قطعی کوتاه می‌تواند خوشه را به دو عضویت ناسازگار بشکند و بعد از آن هیچ انتخاب رهبری قابل اعتماد نیست.Changing membership from local failure detection. Each node decides who is alive from its own timeout; base additions and removals on that and a brief partition can split the cluster into two incompatible memberships, after which no leader election is trustworthy.

monotonic reads

خواندن یکنوا فصل ۶Chapter 6 · دیدن در متنsee in context

چطور ممکن است ساعت در اپلیکیشن به عقب برگردد؟How can time run backwards inside an app?

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

در اپ پیام‌رسان، پیام را می‌خوانی و جواب می‌دهی. چند دقیقه بعد اپ را باز می‌کنی و همان پیام دوباره خوانده‌نشده است، انگار جوابت هرگز نبوده؛ چیزی عوض نشده، فقط این بار به نسخهٔ عقب‌مانده‌تری وصل شده‌ای. خواندن یکنوا همان قاعده‌ای است که نمی‌گذارد تجربه‌ات به عقب برگردد.You read a message in a chat app and reply. A few minutes later you reopen the app and the same message is unread again, as if your reply never existed; nothing changed, you just connected to a more lagging copy. Monotonic reads is the rule that stops your experience from going backwards.

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

کتابی را ورق می‌زنی؛ هیچ‌وقت قبول نمی‌کنی که شمارهٔ صفحه از ۱۲۰ به ۹۰ برگردد. حتی اگر کتاب را از اول نفهمیده باشی، این عقب‌رفتن آزارت می‌دهد، چون می‌دانی قبلاً جلوتر بوده‌ای. در سیستم هم همین حس است: می‌شود کهنه دید، ولی نباید از آنچه دیدی عقب‌تر بروی. برای همین این تضمین به ازای یک کاربر معنا دارد، نه برای کل سیستم: من ممکن است نسخهٔ ۵ را ببینم و تو نسخهٔ ۴ را، ولی هیچ‌کدام نباید بعد از دیدن نسخهٔ ۵ دوباره نسخهٔ ۳ بگیرد. بهایش این است که سیستم باید یا تو را همیشه به یک نسخه بفرستد یا قبل از جواب، نسخه‌ات را بررسی کند؛ در هر دو حالت، یک عمل اضافه در مسیر خواندن داری.You flip through a book and never accept the page number going from 120 back to 90. Even if you did not follow the story, the backwards jump bothers you, because you know you were further along. A system feels the same: stale is allowed, going backwards is not. That is why the guarantee is per user, not for the whole system: I may see version 5 while you see version 4, but neither of us should get version 3 after seeing 5. The price is that the system either pins you to one copy or checks your version before answering; either way there is one extra step on the read path.

خواندن اولFirst readنسخهٔ ۵v5همان نسخهSame copyنسخهٔ ۵v5نسخهٔ دیگرAnother copyنسخهٔ ۳v3ممنوعNot allowedعقب رفتwent back
خواندن یکنوا نمی‌گوید تازه‌ترین را می‌بینی؛ فقط می‌گوید از این به بعد عقب نمی‌روی.Monotonic reads does not promise the newest value; it promises you will not go back from what you saw.

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

خواندن یکنوا یک تضمین در سطح جلسه است: اگر خواندنی نسخهٔ v را برگرداند، هیچ خواندن بعدی در همان جلسه نسخهٔ قدیمی‌تر از v را برنمی‌گرداند. این از خواندن نوشته‌های خود ضعیف‌تر است، چون فقط می‌گوید به عقب برنمی‌گردی و کاری به این ندارد که نوشتهٔ خودت را دیده باشی یا نه. اجرایش دو راه اصلی دارد. یکی مسیر چسبنده: همان کاربر همیشه به یک همانند برود، پس ترتیب نسخه‌ها برایش عقب نمی‌رود. دیگری توکن نسخه: کلاینت آخرین نسخه‌ای را که دیده همراه می‌فرستد و همانند فقط وقتی جواب می‌دهد که نسخه‌اش از آن جلوتر یا مساوی باشد؛ اگر عقب باشد، منتظر می‌ماند، به همانند دیگری هدایت می‌کند، یا خطا می‌دهد. مشکل وقتی پیدا می‌شود که یک لودبالانسر ساده بین همانندها round-robin می‌کند: کاربر بین یک نسخهٔ تازه و یک نسخهٔ کهنه تاب می‌خورد و تجربه‌اش عقب می‌رود. همین اتفاق در جابه‌جایی بین دیتاسنترها یا بعد از تعویض رهبر هم می‌افتد، چون همانند تازه ممکن است از نسخهٔ قبلی عقب‌تر باشد. تضمین ارزان نیست: مسیر چسبنده با تغییر IP موبایل می‌شکند و بررسی نسخه یک رفت‌وبرگشت یا انتظار اضافه می‌کند.Monotonic reads is a session-level guarantee: if one read returned version v, no later read in that session returns anything older than v. It is weaker than read-your-writes, because it only forbids going backwards and does not care whether you have seen your own write. There are two main implementations. The first is sticky routing: the same user always reaches the same replica, so the version sequence cannot run backwards. The second is a version token: the client sends the last version it saw and the replica only answers if its own version is greater or equal; if it is behind, the replica waits, redirects, or errors. The problem appears when a plain load balancer round-robins across replicas: the user bounces between a fresh and a stale copy and their experience slides backwards. The same thing happens when users move between datacenters or after a failover, because the new replica may be behind the old one. The guarantee is not free: sticky routing breaks when a mobile IP changes, and version checks add a round trip or a wait.

1 sessionتضمین برای یک کلاینت است، نه برای همهٔ سیستمa guarantee for one client, not for the whole system
v5 → v3همین حرکت به عقب، همان چیزی است که این تضمین ممنوع می‌کندthat backwards move is exactly what the guarantee forbids

چرا round-robin این را می‌شکندWhy round-robin breaks it

لودبالانسر کارش پخش بار است و برای همین هر درخواست را به یک همانند می‌فرستد. اگر همانندها هم‌سرعت نباشند، یک کاربر می‌تواند در فاصلهٔ چند ثانیه دو نسخهٔ مختلف ببیند و بدتر، نسخهٔ دومش قدیمی‌تر باشد. این باگ در تست‌های ساده دیده نمی‌شود چون بار کم است و تأخیر تکثیر تقریباً صفر؛ در تولید و زیر فشار، همانندی که یک ثانیه عقب است کافی است تا کاربر صفحه‌ای را ببیند که قبلاً جلوتر دیده بود. برای همین این تضمین را باید آگاهانه انتخاب کنی و در مسیرهایی که تجربهٔ کاربر مهم است، از همان ابتدا مسیر چسبنده یا توکن نسخه بگذاری، نه بعد از شکایت.A load balancer spreads load, so it sends each request to some replica. If the replicas do not run at the same speed, one user can see two different versions within seconds, and the second may be the older one. Simple tests never show this, because the load is low and replication lag is near zero; in production, under pressure, a replica one second behind is enough for a user to see a page they had already passed. So choose the guarantee deliberately and put sticky routing or a version token on the paths where the experience matters, from the start, rather than after the complaint.

چطور اجرا می‌شود و چه بهایی داردHow it is implemented, and what it costs

ساده‌ترین راه، مسیر چسبنده است: کاربر را با یک کوکی یا کلید به همان همانند ببند. ارزان است و برای خیلی از اپلیکیشن‌ها کافی، ولی با تغییر شبکهٔ موبایل یا مقیاس‌دهی همانندها می‌شکند. راه دقیق‌تر توکن نسخه است: کلاینت نسخهٔ دیده‌شده را نگه می‌دارد و سرور قبل از جواب بررسی می‌کند. این راه درست است، ولی وقتی تأخیر تکثیر بالا می‌رود، بعضی خواندن‌ها باید صبر کنند یا خطا بدهند؛ یعنی یک برش شبکه به یک خطای قابل مشاهده تبدیل می‌شود، نه به یک جواب کهنهٔ بی‌صدا. انتخاب بین این دو، انتخاب بین گم‌شدن تجربه و سخت‌شدن دسترس‌پذیری است.The simplest route is sticky: pin the user to one replica with a cookie or a key. It is cheap and enough for many apps, but it breaks when a mobile network changes or the replicas are rescaled. The precise route is a version token: the client keeps the version it saw and the server checks before answering. That is correct, but when replication lag rises some reads must wait or fail; a network partition becomes a visible error instead of a silent stale answer. Choosing between the two is choosing between a broken experience and a harder availability story.

تلهٔ رایجThe usual trap

قاطی کردنش با خواندن نوشته‌های خود. خواندن یکنوا فقط می‌گوید به عقب برنمی‌گردی؛ تو می‌توانی از همان اول نسخهٔ کهنه را ببینی و هیچ‌وقت هم نوشتهٔ خودت را نبینی. خیلی‌ها یکی را پیاده می‌کنند و فکر می‌کنند آن یکی، دیگری را هم می‌دهد.Confusing it with read-your-writes. Monotonic reads only says you will not go backwards; you can start on a stale copy and never see your own write at all. Many teams implement one and assume it gives them the other.

multi-leader replication

تکثیر چندرهبر فصل ۶Chapter 6 · دیدن در متنsee in context

دو دیتاسنتر، هر دو می‌خواهند نوشتن بپذیرند. تعارض را چه کسی می‌بندد؟Two datacenters both want to accept writes. Who closes the conflict?

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

دو شعبه از یک فروشگاه، هر کدام انبار و دفتر خودش را دارد. شعبهٔ شمال موجودی را کم می‌کند و شعبهٔ جنوب همان ساعت زیاد، و آخر شب دفترها را با هم مقایسه می‌کنند تا تصمیم بگیرند عدد نهایی چند است. تکثیر چندرهبر همان دو دفتر است، با یک قاعدهٔ مشترک برای جمع‌کردنشان.Two branches of one shop each keep their own stock and their own ledger. The north branch counts an item down while the south counts the same item up, and at closing time the books are compared to decide the final number. Multi-leader replication is those two ledgers, plus a shared rule for combining them.

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

دو دفتر در دو شعبه یک مزیت روشن دارد: هر شعبه بدون تماس با شعبهٔ دیگر کار می‌کند. اگر خط تلفن قطع شود، هر دو باز هستند و مشتری محلی معطل نمی‌ماند؛ این همان چیزی است که چندرهبر می‌خرد. ولی دو دفتر یعنی دو حقیقت. شعبهٔ شمال و جنوب می‌توانند آخرین قلم را هم‌زمان بفروشند، و آخر شب کسی باید بگوید کدام فروش ثبت است. عدد را نمی‌شود جمع زد، چون موجودی است نه فروش؛ باید قاعده داشته باشی: یکی برنده شود، یا فروش‌ها را نگه داری و بعد جبران کنی. و هر چه شعبه بیشتر، هماهنگ‌کردن دفترها سخت‌تر و دیرتر می‌شود.Two ledgers in two branches have one clear benefit: each branch works without calling the other. If the line goes down, both stay open and the local customer is not kept waiting; that is what multi-leader buys. But two ledgers mean two truths. North and south can sell the last item at the same time, and at closing someone has to say which sale stands. You cannot just add the numbers, because it is stock, not revenue; you need a rule: let one win, or keep both sales and compensate later. The more branches, the harder and later the ledgers meet.

چندرهبرMulti-leaderنوشتن محلی، تأخیر کمlocal writes, low latencyتعارض واقعی استconflicts are realهر طرف زنده می‌ماندboth sides stay upتک‌رهبرSingle-leaderیک ترتیب، بدون تعارضone order, no conflictsنوشتن از راه دورwrites travel farقطع شدن یعنی توقفa partition stops writes
چندرهبر تأخیر نوشتن را کم می‌کند و در عوض تعارض می‌آورد؛ تک‌رهبر تعارض را حذف می‌کند و تأخیر را می‌پذیرد.Multi-leader trades write latency for conflicts; single-leader trades conflicts for latency.

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

در تکثیر چندرهبر، بیش از یک گره نوشتن را می‌پذیرد و هر رهبر نوشتن‌های خودش را به رهبرهای دیگر می‌فرستد. الگوی رایج این است که هر دیتاسنتر یک رهبر داشته باشد: نوشتن در محل انجام می‌شود، تأخیر رفت‌وبرگشت شبکهٔ گسترده حذف می‌شود، و اگر ارتباط بین دیتاسنترها قطع شود، هر طرف همچنان نوشتن محلی را می‌پذیرد. بهایش تعارض است: دو رهبر می‌توانند هم‌زمان یک رکورد را عوض کنند و هیچ‌کدام دیگری را ندیده باشد. برای همین هر طراحی چندرهبر باید از روز اول یک قاعدهٔ حل تعارض داشته باشد: آخرین نوشتن برنده، ادغام نوع‌محور، CRDT، یا نگه‌داشتن siblingها و تصمیم در لایهٔ کاربرد. توپولوژی تکثیر هم مهم است: همه‌به‌همه رایج‌ترین است، ولی حلقه و ستاره می‌توانند پیام را در مسیر گم کنند یا ترتیب را به هم بزنند. چیزهایی که در تک‌رهبر بدیهی بودند اینجا می‌شکنند: شناسهٔ خودافزا، trigger، قید یکتایی، و هر چیزی که به یک تصمیم جهانی نیاز دارد. چندرهبر برای نوشتن محلی و آفلاین عالی است، ولی راهی برای مقیاس‌دادن نوشتن در یک منطقه نیست؛ آن کار sharding است.In multi-leader replication more than one node accepts writes, and each leader ships its writes to the other leaders. The usual pattern is one leader per datacenter: the write happens locally, the wide-area round trip disappears, and if the link between datacenters breaks each side keeps accepting local writes. The price is conflict: two leaders can change the same record at once, neither having seen the other. So every multi-leader design needs a conflict rule from day one: last write wins, a type-aware merge, a CRDT, or keeping siblings and deciding in the application. Replication topology matters as well: all-to-all is the common choice, while ring and star can drop messages or reorder them. Things that were obvious under a single leader break here: auto-increment ids, triggers, uniqueness constraints, and anything needing a global decision. Multi-leader is excellent for local and offline writes, but it is not how you scale writes inside one region; that is sharding.

≥ 2حداقل دو رهبر، معمولاً یکی در هر دیتاسنترat least two leaders, usually one per datacenter
1 WAN RTTهزینه‌ای که نوشتن محلی حذف می‌کندthe cost that local writes remove
all-to-allتوپولوژی رایج؛ حلقه و ستاره می‌توانند پیام را گم کنندthe common topology; ring and star can lose messages

کجاها جواب می‌دهدWhere it earns its place

چندرهبر جایی معنا دارد که تأخیر نوشتن مهم است یا قطعی ارتباط بین دو محل قابل قبول نیست. در یک سیستم با کاربر در تهران و اروپا، اگر همهٔ نوشتن‌ها به یک رهبر در اروپا بروند، هر کلیک کاربر تهران یک رفت‌وبرگشت کامل شبکهٔ گسترده را تحمل می‌کند؛ با رهبر محلی، نوشتن همان‌جا تأیید می‌شود. حالت دوم، کلاینت آفلاین است: گوشی یا لپ‌تاپ مدتی بدون شبکه کار می‌کند و بعد نوشتن‌هایش را می‌فرستد؛ در واقع خودِ کلاینت یک رهبر موقت است. ولی هر دوی این‌ها تعارض می‌آورند و تعارض یعنی کد و قاعده و تست. اگر تأخیر نوشتن برایت مهم نیست، چندرهبر فقط پیچیدگی می‌خرد.Multi-leader makes sense where write latency matters or where a broken link between two sites is unacceptable. In a system with users in Tehran and Europe, if every write goes to a leader in Europe, each click in Tehran pays a full wide-area round trip; with a local leader the write is acknowledged next door. The second case is the offline client: a phone or laptop works without a network for a while and then sends its writes, which makes the client a temporary leader. Both of these bring conflicts, and conflicts mean code, rules and tests. If write latency is not a problem for you, multi-leader only buys complexity.

توپولوژی و ترتیبTopology and order

در تکثیر همه‌به‌همه، هر رهبر نوشتن‌هایش را مستقیم به بقیه می‌فرستد و پیام‌ها از مسیرهای مختلف می‌رسند. همین باعث می‌شود ترتیب علّی بشکند: جواب یک پیام ممکن است قبل از خود پیام برسد، چون از گرهٔ دیگری عبور کرده است. در حلقه، اگر یک گره بمیرد، پیام‌ها می‌توانند دور بزنند یا اصلاً نرسند؛ در ستاره، گرهٔ مرکزی گلوگاه و تک‌نقطهٔ خرابی است. راه‌حل، برچسب‌گذاری نسخه‌ها با version vector و تشخیص وابستگی‌هاست، یا استفاده از الگوریتمی که ترتیب علّی را حفظ کند. فقط «سینک کن» کافی نیست: بدون ترتیب، دو نسخه می‌توانند به هم نرسند و هیچ‌وقت یکی نشوند.In all-to-all replication each leader sends its writes directly to the rest, and messages arrive by different paths. That is what breaks causal order: a reply can arrive before the message it answers, because it went through another node. In a ring, one dead node can send messages round in circles or stop them entirely; in a star, the centre is both a bottleneck and a single point of failure. The answer is to tag versions with a version vector and track dependencies, or to use an algorithm that preserves causal order. “Just sync it” is not enough: without ordering, two versions can fail to meet and never become one.

تلهٔ رایجThe usual trap

راه‌انداختن چندرهبر بدون قاعدهٔ حل تعارض. سیستم بالا می‌آید، همه‌چیز به نظر خوب است، تا روزی که دو شعبه هم‌زمان یک رکورد را عوض می‌کنند و یکی از نوشتن‌ها بی‌صدا می‌رود. تعارض را نمی‌شود بعداً در گزارش پیدا کرد؛ باید از روز اول یک قاعدهٔ قطعی داشته باشی.Running multi-leader without a conflict rule. The system comes up, everything looks fine, until the day two sites change one record at once and one of the writes leaves without a sound. You cannot find that conflict in a report later; you need a deterministic rule from day one.

read repair

ترمیم در خواندن فصل ۶Chapter 6 · دیدن در متنsee in context

سه نسخه، سه جواب. کدام را باور می‌کنی؟Three copies, three answers. Which one do you believe?

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

ساعت شروع فیلم را از سه دوست می‌پرسی. دو نفر می‌گویند هشت، یکی می‌گوید هفت. ساعت هشت می‌روی و همان‌جا به نفر سوم پیام می‌دهی که دفترش را اصلاح کند؛ ترمیم در خواندن هم جواب تازه را می‌گیرد و همان را به نسخهٔ عقب‌مانده می‌رساند.You ask three friends when the film starts. Two say eight, one says seven. You go with eight and text the third one to correct his note; read repair does the same with the lagging copy.

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

این اصلاح تقریباً مجانی است، چون همان لحظه که جواب را می‌گیری تفاوت را هم دیده‌ای. ولی فقط برای چیزی کار می‌کند که خوانده شده باشد: هر سؤالی که کسی نپرسد، کهنه می‌ماند. کلیدهایی که زیاد خوانده می‌شوند خودشان ترمیم می‌شوند و بقیه تا مقایسهٔ دوره‌ای بعدی همان‌طور می‌مانند. برای همین ترمیم در خواندن بیشتر شبیه مرتب کردن جلوی چشم مهمان است، نه نظافت کل خانه.The fix is almost free because you saw the difference at the moment you got the answer. But it only works for what was read: any question nobody asks stays stale. Keys that are read often repair themselves; the rest wait for the next periodic comparison. Read repair is therefore more like tidying the room your guest can see than cleaning the whole house.

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

در ترمیم در خواندن، هماهنگ‌کننده درخواست خواندن را به چند همانند می‌فرستد، نسخه‌ها را با هم مقایسه می‌کند، تازه‌ترین را به کلاینت برمی‌گرداند و همان را به همانندهای عقب‌مانده هم می‌نویسد. اگر مقایسه هم‌زمان با پاسخ انجام شود، خواندن گران‌تر است؛ اگر بعد از پاسخ و در پس‌زمینه انجام شود، کلاینت معطل نمی‌شود ولی ترمیم ممکن است شکست بخورد و کسی نفهمد. ترمیم در خواندن فقط کلیدهایی را درست می‌کند که خوانده شده‌اند، پس با ضدآنتروپی جمع می‌شود، نه این‌که جایش را بگیرد. برای این‌که بفهمد کدام نسخه تازه‌تر است به شمارنده یا بردار نسخه نیاز دارد؛ با ساعت دیواری، ساعت‌های ناهماهنگ برندهٔ اشتباه را انتخاب می‌کنند.With read repair the coordinator sends the read to several replicas, compares the versions, returns the newest to the client, and writes that value back to the lagging replicas. Comparing on the request path makes the read more expensive; repairing after answering keeps the client fast but a failed repair can go unnoticed. Read repair only fixes keys that were read, so it adds to anti-entropy rather than replacing it. Knowing which version is newest requires a version number or vector; with wall clocks, unsynchronised machines pick the wrong winner.

R + W > Nبا این شرط، هر خواندن دست‌کم یک نسخهٔ تازه می‌بیندwith this, every read includes at least one fresh copy
۱ نوشتن برگشتینسخهٔ تازه فقط به همانندهای عقب‌مانده فرستاده می‌شودthe fresh value is sent only to the lagging replicas

تلهٔ رایجThe usual trap

انتخاب «تازه‌ترین» با ساعت دیواری. دو سرور که ساعتشان چند ثانیه فرق دارد، نسخهٔ قدیمی‌تر را تازه‌تر می‌بینند و همان را به بقیه برمی‌گردانند؛ ترمیم در خواندن بدون شمارندهٔ نسخه می‌تواند داده را عقب ببرد.Choosing the “newest” value by wall clock. Two servers a few seconds apart see the older version as newer and copy it back to the others; read repair without a version counter can move data backwards.

read-your-writes

خواندن نوشته‌های خود فصل ۶Chapter 6 · دیدن در متنsee in context

ثبت کردی، تأیید گرفتی، پس چرا نمی‌بینی؟You saved it and got a confirmation. Why is it not there?

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

در سایتی ثبت‌نام می‌کنی و پیام «حساب ساخته شد» می‌گیری. صفحه را رفرش می‌کنی و همان صفحه می‌گوید «چنین کاربری وجود ندارد»؛ تو یک کار را دو بار نکرده‌ای، فقط این بار پرسشت به نسخهٔ دیگری رسیده که هنوز خبر ثبت‌نام را نگرفته. خواندن نوشته‌های خود همان قاعده‌ای است که نمی‌گذارد این اتفاق بیفتد.You sign up on a site and get the message that the account was created. You refresh, and the same site says no such user exists; you did not do the work twice, your request just reached a copy that had not heard about the sign-up. Read-your-writes is the rule that stops this from happening.

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

دفتر خانه را تصور کن: کاغذی را در پوشهٔ امروز می‌گذاری و بعد می‌خواهی همان را نشان بدهی. اگر منشی تو را به بایگانی شعبه‌ای بفرستد که جعبهٔ امروز هنوز به آن نرسیده، کاغذت آن‌جا نیست و تو در نظر بقیه شبیه کسی می‌شوی که چیزی را گم کرده است. این قرار با یک شخص است، نه با همه: تضمین می‌دهد خودت کار خودت را ببینی، نه این‌که همه تازه‌ترین داده را ببینند. بهایش این است که سیستم باید بعد از نوشتن تو، برای مدتی مسیر خواندنت را عوض کند یا اولین نسخه‌ای را پیدا کند که از نوشته‌ات جلوتر است؛ یعنی یا رهبر را شلوغ می‌کند یا خواندن را کمی کند می‌کند.Think of the office archive: you file a page in today’s folder and then want to show it. If the clerk sends you to a branch archive where today’s box has not arrived, your page is not there and you look like someone who lost it. This promise is to one person, not to everyone: it guarantees you will see your own work, not that everyone sees the freshest data. The price is that after your write the system must change where your reads go for a while, or find the first copy that is at least as new as your write; that either loads the leader or makes the read a little slower.

نوشتنWriteنسخهٔ ۷v7تأیید به توAck to youاز رهبرfrom leaderخواندن از همانندRead replicaنسخهٔ ۶v6نتیجهResultنیستmissing
نوشتن روی رهبر نشست و تأیید هم آمد، ولی خواندن به همانندی رسید که هنوز نسخهٔ قبلی را دارد.The write landed on the leader and was acknowledged, but the read reached a replica still holding the previous version.

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

خواندن نوشته‌های خود یک تضمین علّی برای یک کلاینت است: بعد از این‌که نوشتنش تأیید شد، خواندن‌های بعدی همان کلاینت باید دست‌کم همان نوشتن را ببینند. ریشهٔ مشکل تکثیر ناهمگام است: رهبر نوشتن را تأیید می‌کند، همانند هنوز اعمالش نکرده، و خواندن به همان همانند می‌رسد. سه راه پیاده‌سازی دارد. اول، هدایت خواندن به رهبر برای مدتی بعد از نوشتن؛ ساده است، ولی رهبر را شلوغ می‌کند و پنجره‌اش حدسی است. دوم، حمل توکن نسخه: کلاینت LSN یا commit timestamp آخرین نوشتنش را نگه می‌دارد و سرور فقط از همانندی جواب می‌دهد که نسخه‌اش از آن جلوتر است؛ این دقیق است، ولی وقتی تأخیر بالا می‌رود خواندن باید صبر کند یا رد شود. سوم، مسیر چسبنده تا کاربر همیشه به همان همانند برود؛ ارزان است و به‌محض تغییر شبکه یا مقیاس‌دهی می‌شکند. نکتهٔ مهم این است که این تضمین فقط برای همان کاربر است: دیگران می‌توانند همان داده را کهنه ببینند و این نقض نیست. و بین چند دستگاه، توکن باید مشترک باشد، وگرنه لپ‌تاپ تو نوشتهٔ گوشی‌ات را نمی‌بیند.Read-your-writes is a causal guarantee for one client: after its write is acknowledged, that client’s later reads must reflect at least that write. The root cause is asynchronous replication: the leader acknowledges the write, a follower has not applied it yet, and the read lands on that follower. There are three implementations. First, send reads to the leader for a while after a write; simple, but it loads the leader and the window is a guess. Second, carry a version token: the client keeps the LSN or commit timestamp of its last write, and the server only answers from a replica whose version is at least that new; this is exact, but when lag is high the read must wait or be rejected. Third, sticky routing so the user always reaches the same replica; cheap, and it breaks as soon as the network changes or the replicas are rescaled. The important point is that the guarantee covers that one user: others may see the same data stale and that is not a violation. Across devices the token must be shared, or your laptop will not see what your phone wrote.

1 clientتضمین برای همان کاربری است که نوشته، نه برای بقیهa promise to the client that wrote, not to everyone else
1 versionتوکن نسخه‌ای که خواندن باید دست‌کم آن را ببیندthe version token a read must at least reach
lagهر چه تأخیر تکثیر بیشتر، پنجرهٔ این باگ بزرگ‌ترthe more replication lag, the wider the bug’s window

چرا فقط نوشتهٔ خودت مهم استWhy only your own write matters

کاربر نمی‌داند دیگران چه نوشته‌اند و اصلاً نمی‌تواند تفاوت را بفهمد، ولی نوشتهٔ خودش را می‌داند، چون خودش انجامش داده و تأییدش را دیده است. برای همین کهنه‌دیدن نوشتهٔ خود بیشترین حس خرابی را می‌سازد و بیشترین تیکت پشتیبانی را. از نظر تئوری، این تعارض با هیچ چیز جبران نمی‌شود: نه با پیام خطا، نه با رفرش. کاربر یک بار دیده که کارش انجام شده، پس انتظار دارد نتیجه‌اش را ببیند. این تضمین ارزان‌ترین راه برای جلوگیری از یک دستهٔ کامل باگ است که در غیر این صورت فقط در ترافیک واقعی و با تأخیر واقعی ظاهر می‌شود.A user does not know what other people wrote and cannot tell the difference anyway, but they do know their own write, because they made it and saw the confirmation. That is why seeing your own write go missing creates the strongest sense of breakage and the most support tickets. Theoretically the violation is unforgivable: no error message and no refresh makes it right, because the user has already seen that the work was done and expects to see the result. This guarantee is the cheapest way to prevent a whole class of bugs that would otherwise only appear under real traffic and real lag.

سه راه پیاده‌سازی و بهای هرکدامThree implementations and their bills

هدایت به رهبر برای مدت ثابت، ساده‌ترین راه است و بدترین حدس را دارد: پنجره باید از بیشترین تأخیر ممکن بزرگ‌تر باشد، پس یا خیلی کوتاه است و باگ برمی‌گردد، یا خیلی بلند است و رهبر را بی‌دلیل شلوغ می‌کند. توکن نسخه از همه دقیق‌تر است، چون شرط واقعی را چک می‌کند: آیا این همانند به نسخهٔ من رسیده است یا نه؛ ولی وقتی همانند عقب است، خواندن باید صبر کند یا از رهبر جواب بگیرد و این هزینه دارد. مسیر چسبنده ارزان است، ولی با تغییر IP و مقیاس‌دهی همانندها می‌شکند. در عمل معمولاً ترکیب می‌کنند: نوشتن‌های حساس با توکن، بقیه با مسیر چسبنده.Sending reads to the leader for a fixed window is the easiest route and the worst guess: the window must exceed the longest lag, so it is either too short and the bug returns, or too long and the leader is loaded for no reason. A version token is the most exact, because it checks the real condition: has this replica reached my version or not. But when the replica is behind, the read must wait or ask the leader, and that costs. Sticky routing is cheap but breaks on an IP change or a resize. In practice teams combine them: sensitive writes with a token, the rest sticky.

خواندن نوشتهٔ خود در برابر خواندن یکنواRead-your-writes versus monotonic reads

خواندن نوشتهٔ خودRead-your-writesخواندن یکنواMonotonic reads
تضمینPromiseنوشتهٔ خودت را می‌بینیyou see your own writeهیچ‌وقت به عقب برنمی‌گردیyou never go backwards
نقض می‌شود وقتیBreaks whenخواندن از همانند عقب‌ماندهa read hits a lagging replicaلودبالانسر بین نسخه‌ها بچرخدa load balancer rotates copies
یکی دیگری را می‌دهد؟Does one imply the other?نه، یکنوایی را تضمین نمی‌کندno, it does not give monotonicityنه، نوشتهٔ خودت را تضمین نمی‌کندno, it does not give your own write

تلهٔ رایجThe usual trap

استفاده از یک پنجرهٔ زمانی ثابت به‌جای توکن نسخه. تیم می‌گوید ده ثانیه بعد از نوشتن از رهبر بخوان، بعد در یک شب پربار تأخیر تکثیر از ده ثانیه می‌گذرد و همان باگ برمی‌گردد. راه درست این است که بپرسی نسخهٔ من روی این همانند نشسته یا نه، نه این‌که حدس بزنی چقدر صبر کافی است.Using a fixed time window instead of a version token. The team says read from the leader for ten seconds after a write; then on a busy night replication lag passes ten seconds and the bug is back. The right question is whether this replica has my version, not how long someone guessed it takes.

اگر یک نسخه بسوزد، نسخهٔ دوم کجاست؟If one copy burns, where is the second one?

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

از کلید خانه‌ات دو نسخه داری: یکی در جیبت، یکی در کشوی خانهٔ خواهرت. هر دو یک کلیدند، نه دو تیکه از یک کلید؛ هر کدام به‌تنهایی در را باز می‌کند. همانند هم همان است: یک کپی کامل از داده، نه تیکه‌ای از آن.You have two copies of your house key: one in your pocket, one in your sister’s drawer. Both are the whole key, not two halves of one key; either opens the door alone. A replica is the same idea: a complete copy of the data, not a piece of it.

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

دو نسخه از کلید سه چیز به تو می‌دهد. اگر یکی گم شود، هنوز می‌توانی وارد شوی؛ اگر مهمان داری، می‌توانی نسخهٔ دوم را به او بدهی و خودت با نسخهٔ خودت وارد شوی؛ و اگر قفل را عوض کنی، باید هر دو نسخه را عوض کنی. نکتهٔ آخر همان هزینه است: نسخهٔ دوم خودبه‌خود هم‌روز نمی‌ماند. اگر قفل را عوض کنی و کلید خواهرت را نه، او با یک کلید کهنه پشت در می‌ماند. در داده هم همین است: هر چه نسخه بیشتر، بیشتر باید نگران هم‌روز بودنشان باشی، و هر نسخه‌ای که عقب بماند می‌تواند جواب کهنه بدهد. دو نسخه فضای بیشتری را هم هدر می‌دهد؛ چیزی که می‌خری ایمنی و خواندن بیشتر است.Two keys buy you three things. If one is lost you can still get in; if you have a guest you can lend the second and still use yours; and if you change the lock you have to change both. That last point is the cost: the second copy does not keep itself up to date. Change the lock and not your sister’s key and she stands outside with an old key. Data works the same way: the more copies, the more you worry about keeping them current, and any copy that falls behind can answer with something old. Two copies do not save space, they cost it; what you are buying is safety and more reads.

یک نسخه از دادهOne copy of the dataنسخهٔ اصلی (رهبر)Primary (leader)همانند ۱Replica 1همانند ۲Replica 2هر نسخه کامل است؛ برخلاف shard که هر تیکه فقط بخشی از داده را داردEvery copy is whole; unlike a shard, which holds only a piece
یک داده، سه نسخهٔ کامل روی سه ماشین؛ خرابی یکی، دو نسخهٔ دیگر را باقی می‌گذارد.One data set, three complete copies on three machines; one failure leaves the other two.

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

همانند یک کپی کامل از دادهٔ یک گره است که روی ماشین دیگری نگه داشته می‌شود و با تکثیر به‌روز می‌ماند؛ تکثیر می‌تواند انتقال WAL، تغییرهای منطقی ردیف، دستور، یا trigger باشد. هدف‌ها چهار تاست: تحمل خرابی، چون با مردن یک ماشین ماشین دیگری جواب می‌دهد؛ مقیاس خواندن، چون خواندن را می‌شود بین نسخه‌ها پخش کرد؛ تأخیر، چون نسخه‌ای نزدیک کاربر می‌گذاری؛ و نگهداری، چون می‌شود یک نسخه را در حالی که بقیه سرویس می‌دهند ارتقا داد. همانند ظرفیت نوشتن را زیاد نمی‌کند: هر نوشتن باید همه‌جا اعمال شود، چون همان نسخهٔ داده است، نه تیکه‌ای از آن. در تکثیر همگام، رهبر پیش از تأیید به کلاینت منتظر تأیید همانند می‌ماند، پس دوام روی دو ماشین می‌خرد و تأخیر و وابستگی به کندترین همانند را می‌پردازد؛ تکثیر ناهمگام سریع است و در عوض ممکن است در تعویض رهبر چند نوشتن تأییدشده را از دست بدهد. همانند همیشه عقب نیست؛ گاهی خیلی هم عقب است: زیر بار، یک همانند می‌تواند ثانیه‌ها عقب بماند و همین منبع خواندن کهنه و باگ‌های خواندن نوشته‌های خود و یکنوایی است. همانندی که از حد زیادی عقب بماند باید از سرویس خواندن کنار برود و با لاگ یا snapshot تازه از نو ساخته شود، وگرنه هر خواندن از آن یک ریسک است.A replica is a complete copy of one node’s data kept on another machine and kept current by replication, which can be WAL shipping, logical row changes, statements, or triggers. It serves four purposes: fault tolerance, because another machine serves when one dies; read scaling, because reads can be spread across copies; latency, because you place a copy near the user; and maintenance, because you can upgrade one copy while the rest serve. A replica does not add write capacity: every write must be applied everywhere, and it is the same data, not a piece of it. Synchronous replication waits for a replica acknowledgement before answering the client, which buys durability on two machines and pays in latency and dependence on the slowest copy; asynchronous replication is fast and may lose acknowledged writes during failover. Replicas are not merely occasionally behind; sometimes they are far behind: under load one can fall seconds back, and that is the source of stale reads and of read-your-writes and monotonic-reads bugs. A replica that falls too far behind should leave the read path and be rebuilt from a log or a snapshot, or every read from it is a risk.

1 full copyهر همانند کل داده را دارد، نه بخشی از آنeach replica holds all the data, not part of it
reads ×NN همانند تقریباً N برابر ظرفیت خواندن می‌دهدN replicas give roughly N times the read capacity
1 leaderدر تکثیر تک‌رهبر، نوشتن فقط از یک جا می‌آیدunder single-leader replication, writes come from one place

همانند با shard چه فرقی داردHow a replica differs from a shard

این دو کلمه را زیاد با هم قاطی می‌کنند. همانند یک کپی کامل است: هر همانند همهٔ رکوردها را دارد، پس خرابی یکی چیزی را از دست نمی‌دهد و خواندن را می‌شود پخش کرد. shard یک تیکه است: هر shard فقط بخشی از رکوردها را دارد، پس خرابی یک shard بخشی از داده را از دست می‌دهد و برای تحمل خرابی باید خودش همانند داشته باشد. همانند ظرفیت نوشتن نمی‌دهد، shard می‌دهد. یک تست ساده: اگر یک رکورد را در یک گره پیدا کردی، همانند است؛ اگر مجبوری بدانی کلید در کدام تیکه است، shard است. در عمل جدی، هر shard خودش چند همانند دارد و این دو با هم کار می‌کنند، ولی یکی نیستند.These two words get mixed up constantly. A replica is a complete copy: every replica holds all the records, so losing one loses no data and reads can be spread. A shard is a piece: each shard holds only some records, so losing one loses part of the data and the shard needs its own replicas to survive failure. Replicas do not add write capacity; shards do. A simple test: if one record is findable on one node, that is a replica; if you must know which piece holds the key, that is a shard. In real systems each shard has several replicas and the two work together, but they are not the same thing.

همانند عقب‌ماندهThe replica that falls behind

تأخیر تکثیر اجتناب‌ناپذیر است، چون همانند باید همان نوشتن‌ها را با همان ترتیب انجام دهد و شبکه و دیسک همیشه به موقع نیستند. مشکل وقتی شروع می‌شود که تأخیر اندازه‌گیری نشود. یک همانند می‌تواند از نظر تعداد بایت‌های گرفته‌شده سالم به نظر برسد ولی در اعمال تراکنش‌ها عقب باشد؛ پس معیار درست، عقب‌بودن منطقی است، نه حجم شبکه. اگر عقب‌ماندگی از حدی بگذرد، دو کار می‌شود کرد: توقف خواندن از آن تا کاربر دادهٔ کهنه نبیند، یا بیرون بردنش از مجموعه و ساختن دوباره‌اش از snapshot تازه. هر دو تصمیم باید از قبل و خودکار باشند، چون در لحظهٔ حادثه کسی وقت فکر کردن ندارد.Replication lag is unavoidable, because the replica must apply the same writes in the same order and networks and disks are not always on time. The trouble starts when lag is not measured. A replica can look healthy in bytes received while being behind in transactions applied, so the right measure is logical lag, not network volume. If the lag passes a threshold, two things are possible: stop serving reads from it so users do not see old data, or take it out of the set and rebuild it from a fresh snapshot. Both decisions must exist in advance and run automatically, because nobody has time to think during the incident.

همانند در برابر shardReplica versus shard

همانندReplicashardShard
روی گره چه هستWhat a node holdsهمهٔ داده، کاملall of the data, wholeفقط یک تیکه، با کلید مشخصone piece, chosen by key
هدفPurposeتحمل خرابی و خواندن بیشترfault tolerance and more readsظرفیت بیشتر برای داده و نوشتنmore room for data and writes
خرابی یک گرهOne node failsچیزی از دست نمی‌رودnothing is lostهمان تیکه از دست می‌رودthat piece is lost
تست تشخیصHow to tellهر گره همه‌چیز را داردevery node has everythingباید بدانی کلید کجاستyou must know where the key lives

تلهٔ رایجThe usual trap

فکر کردن این‌که سه همانند یعنی سه برابر ظرفیت نوشتن. هر سه نسخه یک نوشتن را حمل می‌کنند، پس نوشتن سه برابر نمی‌شود؛ فقط تحمل خرابی و خواندن زیاد می‌شود. اگر نوشتن گلوگاه است، همانند اضافه‌کردن فقط تأخیر تکثیر را بیشتر می‌کند.Thinking three replicas mean three times the write capacity. All three carry the same write, so writes are not tripled; only fault tolerance and reads grow. If writes are the bottleneck, adding replicas only adds replication lag.

یک نسخه کم است. چند نسخه چه دردسری می‌آورد؟One copy is not enough. What does a second copy cost you?

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

دو کلید از یک قفل داری: یکی در جیبت، یکی یدکی پیش مادرت. کلید یدکی همان در را باز می‌کند، پس اگر کلید خودت را گم کنی خانه‌ات را از دست نمی‌دهی. فقط یادت باشد اگر قفل را عوض کردی باید هر دو کلید را عوض کنی؛ تکثیر داده هم دقیقاً همین قرار را با تو می‌گذارد، نه بیشتر.You have two keys to one lock: one in your pocket, one spare at your mother's. The spare opens the same door, so losing your own key does not lose you the flat. Just remember that if you change the lock you have to change both keys; replication makes exactly that deal with you, no more.

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

حالا فرض کن سه کلید داری و هر کدام دست یک نفر است. یک نفر مریض می‌شود و دو نفر دیگر هنوز در را باز می‌کنند؛ این همان چیزی است که با نسخهٔ بیشتر می‌خری. اما سه مسئلهٔ تازه هم با آن‌ها به خانه می‌آید. اول، هر تغییری در قفل باید به هر سه کلید برسد، وگرنه کلید عقب‌مانده در را باز نمی‌کند. دوم، تا وقتی کلید سوم عوض نشده، خانه دو وضعیت دارد: در باز می‌شود یا نه، بسته به این‌که کدام کلید را می‌زنی. سوم، اگر دو نفر هم‌زمان قفل را عوض کنند، یکی از آن‌ها باید بفهمد که کلیدش دیگر کلید این در نیست. تکثیر بعضی خرابی‌ها را برمی‌دارد و در عوض، هماهنگی را به مسئلهٔ تو تبدیل می‌کند.Now suppose three people hold a key each. One falls ill and the other two still open the door; that is what extra copies buy. But three new problems move in with them. Every change to the lock has to reach all three keys, or the lagging key stops working. Until the third key is updated the flat has two truths, depending on which key you use. And if two people change the lock at once, one of them has to learn that their key is no longer the key to this door. Replication removes some failures and hands you coordination in exchange.

یک نسخه از دادهOne copy of the dataنسخهٔ رهبرLeader copyهمانند ۱Follower 1همانند ۲Follower 2نوشتن به همه می‌رسد؛ خواندن از هر نسخهٔ سالمwrites reach every copy; reads can use any healthy one
همان داده در چند نسخه می‌نشیند؛ هر نسخه یک کپی کامل است، نه یک تیکه از داده.The same data sits in several copies; each copy is the whole data set, not a slice of it.

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

تکثیر یعنی هر رکورد روی بیش از یک ماشین نگه داشته شود و نوشتن‌ها به همهٔ نسخه‌ها برسند. رایج‌ترین شکلش تکثیر مبتنی بر رهبر است: همهٔ نوشتن‌ها به یک رهبر می‌رسند، رهبر آن‌ها را به ترتیب در لاگ خود ثبت می‌کند و همانندها (replica) همان لاگ را به همان ترتیب اعمال می‌کنند. چون همه یک ترتیب را دنبال می‌کنند، در نهایت یک تاریخچه دارند؛ و چون خواندن می‌تواند از هر همانندی برود، ظرفیت خواندن با تعداد نسخه‌ها بالا می‌رود. بهایش سه چیز است: هر نوشتن باید از شبکه بگذرد و روی چند دیسک بنشیند، هر همانند می‌تواند عقب بیفتد، و روزی که رهبر بمیرد باید کسی جایش را بگیرد. تکثیر در شکل تک‌رهبر ظرفیت نوشتن را زیاد نمی‌کند و جای sharding را هم نمی‌گیرد؛ این دو مسئلهٔ جدا هستند.Replication means keeping each record on more than one machine and delivering every write to all copies. The common shape is leader-based: writes go to one leader, the leader records them in order in its log, and followers apply that log in the same order. A single order means every copy follows the same history, and because reads can go to any follower, read capacity grows with the number of copies. The bill has three lines: every write crosses the network and lands on several disks, any follower can fall behind, and one day the leader dies and someone must take over. Leader-based replication does not add write capacity, and it does not replace sharding; they are different problems.

N نسخهبرای تحمل خرابی، کمینه دو نسخه لازم استtwo copies are the minimum for tolerating a failure
۱ → Nهر نوشتن باید به همهٔ نسخه‌ها برسدevery write has to reach all copies
خواندن × Nخواندن‌ها پخش می‌شوند، نوشتن‌ها نهreads spread out; writes do not

تکثیر چه چیزی را حل می‌کند و چه چیزی را نهWhat replication buys and what it does not

تکثیر سه چیز می‌خرد: تحمل خرابی یک ماشین، ظرفیت خواندن، و نزدیکی داده به کاربر در چند منطقه. اما ظرفیت نوشتن را زیاد نمی‌کند؛ هر نوشتن باید به همهٔ نسخه‌ها برسد، پس اگر نوشتن گلوگاه باشد، نسخهٔ بیشتر فقط بار را پخش می‌کند، نه این‌که آن را کم کند. جای پشتیبان را هم نمی‌گیرد: همانند همان دستورها را اجرا می‌کند و اگر داده اشتباه پاک شود، پاک شدن هم تکثیر می‌شود. و اگر تکثیر ناهمگام باشد، تازه بودن هم تضمین نمی‌شود. قبل از افزودن نسخه باید بدانی کدام یک از این‌ها را می‌خواهی؛ وگرنه فقط هزینه‌اش را می‌پردازی.Replication buys three things: tolerance of one machine failing, read capacity, and data close to users in several regions. It does not add write capacity; every write has to reach every copy, so when writes are the bottleneck more copies spread the load rather than reduce it. It does not replace a backup either: a follower executes the same statements, so a mistaken delete replicates to every copy. And with asynchronous replication, freshness is not guaranteed at all. Before adding a copy, decide which of those three you are buying, or you will pay the bill and get none of them.

نسخه‌ها چطور از هم دور می‌افتندHow copies drift apart

سه چیز باعث می‌شود نسخه‌ها از هم فاصله بگیرند: تأخیر شبکه، بار خود همانند، و کارهای سنگینی مثل تراکنش طولانی یا مهاجرت داده. وقتی رهبر یک عملیات بزرگ را در یک تراکنش انجام می‌دهد، همانند تا پایان آن تراکنش چیزی نمی‌بیند و تأخیر یک‌دفعه بالا می‌رود. بعد از یک قطعی هم همانند باید هم عقب‌افتادگی را جبران کند و هم به نوشتن‌های تازه برسد، و در همین مرحله معمولاً چند دقیقه از رهبر عقب می‌ماند. راه‌حل جادویی وجود ندارد: یا خواندن را از رهبر می‌بری، یا نوشتن را تا رسیدن نسخه معطل می‌کنی، یا کهنگی را قبول می‌کنی و حسابش را در طراحی محصول می‌آوری.Three things push copies apart: network delay, load on the follower itself, and heavy work such as a long transaction or a data migration. When the leader runs one big operation inside a single transaction, the follower sees nothing until that transaction ends and lag jumps at once. After an outage a follower has both a backlog to catch up on and new writes arriving, which is where it typically sits minutes behind. There is no magic answer: read from the leader, hold writes until the copy catches up, or accept the staleness and budget for it in the product design.

تلهٔ رایجThe usual trap

اشتباه گرفتن همانند با نسخهٔ پشتیبان. همانند همان دستورها را اجرا می‌کند؛ اگر اشتباهی جدولی را پاک کنی، پاک شدن روی همهٔ نسخه‌ها تکثیر می‌شود و چند دقیقه بعد همه‌جا خالی است. نسخهٔ پشتیبان چیزی است که نوشتن‌های مخرب را دنبال نمی‌کند، و تکثیر این کار را نمی‌کند.Mistaking a follower for a backup. A follower executes the same statements; if you drop a table by accident the drop replicates to every copy and minutes later all of them are empty. A backup is the thing that does not follow destructive writes, and replication is not it.

replication lag

تأخیر تکثیر فصل ۶Chapter 6 · دیدن در متنsee in context

نوشتی، صفحه را تازه کردی، خبری نیست.You wrote it, refreshed the page, and it is not there.

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

پول را برای دوستت کارت‌به‌کارت می‌کنی. اپلیکیشن خودت فوراً موجودی تازه را نشان می‌دهد، ولی نیم ساعت بعد که از خودپرداز پول می‌کشی، همان موجودی دیروز را می‌بینی. آن فاصلهٔ چند دقیقه‌ای بین دو نسخه از حساب، replication lag است؛ نه گم شدن پول، فقط دیر رسیدن خبر.You send a friend money from your banking app. Your app shows the new balance at once, but half an hour later the cash machine still shows yesterday's balance. That gap of minutes between two copies of the account is replication lag: not money lost, just news arriving late.

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

دفتر شعبه را در نظر بگیر: صندوق‌دار هر فروش را همان لحظه در دفتر خودش می‌نویسد، ولی دفتر مرکزی شب‌ها یک‌بار به‌روز می‌شود. مشتری‌ای که صبح به دفتر مرکزی نگاه می‌کند، فروش دیروز را نمی‌بیند و فکر می‌کند حسابش دست‌نخورده است. این تأخیر تصادفی نیست؛ چیزی است که در ازای سرعت می‌خری. اگر بخواهی هر دو دفتر همیشه یکی باشند، هر فروش باید تا تأیید دفتر مرکزی معطل بماند و صندوق‌دار پشت صف می‌ماند. هر چه تأخیر بیشتر، هر دو طرف ارزان‌تر کار می‌کنند و فاصلهٔ حقیقت بیشتر می‌شود.Picture the branch ledger: the cashier records each sale in her own book immediately, while the head-office ledger is updated once in the evening. A customer who checks the head-office book in the morning does not see yesterday's sale and thinks the account is untouched. The delay is not an accident; it is what you buy speed with. If both books had to agree at every moment, each sale would wait for head office and the queue would grow. The longer the delay, the cheaper both sides run and the wider the gap in the truth.

کامیت روی رهبرCommit on leaderلحظهٔ صفرt = 0ارسال لاگLog shippedچند میلی‌ثانیهmillisecondsاعمال روی همانندApplied on followerلحظهٔ صفر + تأخیرt = 0 + lagخواندن کهنهStale readکاربر گیج می‌شودthe user is confused
نوشتن روی رهبر فوری است؛ رسیدنش به همانند یک فاصلهٔ زمانی دارد و همان فاصله، lag است.The write on the leader is immediate; reaching a follower takes time, and that gap is the lag.

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

در تکثیر ناهمگام، رهبر نوشتن را در لاگ خود ثبت می‌کند و همان لحظه به کلاینت جواب می‌دهد؛ همانندها همان لاگ را بعداً و با سرعت خودشان اعمال می‌کنند. تأخیر تکثیر فاصلهٔ بین این دو لحظه است و معمولاً به‌صورت مدت عقب‌ماندگی یا اختلاف موقعیت در لاگ اندازه‌گیری می‌شود. این فاصله سه ضمانت را می‌شکند: خواندن نوشتهٔ خودت، چون نوشته‌ات هنوز به همانندی که از آن می‌خوانی نرسیده؛ یکنوایی خواندن، چون دو خواندن پشت سر هم می‌توانند به دو همانند با دو تأخیر مختلف برسند؛ و ترتیب علّی، چون پاسخ یک سؤال می‌تواند قبل از خود سؤال دیده شود. تأخیر از شبکه، بار همانند، تراکنش‌های طولانی و عقب‌افتادن بعد از یک قطعی می‌آید. «تأخیر کم» تسکین‌دهنده است، نه ضمانت؛ تنها چیزی که ضمانت می‌دهد خواندن از رهبر یا منتظر ماندن برای رسیدن موقعیت لاگ است.With asynchronous replication the leader records a write in its log and answers the client right away; followers apply that log later, at their own pace. Replication lag is the gap between those two moments, usually measured as seconds behind or as a difference in log position. The gap breaks three promises: read-your-writes, because your write has not reached the follower you are reading from; monotonic reads, because two reads in a row can hit followers with different lags; and causal order, because an answer can become visible before the question. Lag comes from the network, from a loaded follower, from long transactions, and from catching up after an outage. “Low lag” is a comfort, not a guarantee; only reading from the leader, or waiting for a log position, gives you one.

چند msتأخیر سالم روی شبکهٔ محلی وقتی هیچ‌چیز شلوغ نیستhealthy lag on a local network when nothing is busy
ثانیه‌هابعد از یک قطعی کوتاه یا زیر بار سنگینafter a short outage or under heavy load
∞اگر همانند برنگردد، تأخیر تا وصل شدن دوباره ادامه داردif the follower stays away, the lag runs until it returns

تأخیر را چطور اندازه می‌گیریHow lag is measured

تأخیر یک عدد نیست که یک‌بار اندازه بگیری و تمام؛ برای هر همانند جدا اندازه‌گیری می‌شود. ساده‌ترین راه این است که موقعیت لاگ رهبر را با موقعیت اعمال‌شدهٔ همانند مقایسه کنی و اختلاف را به ثانیه ترجمه کنی. عددی که باید نگاه کنی بدترین همانند است، نه میانگین: اگر یکی از ده همانند دو دقیقه عقب باشد و بقیه صفر، میانگین شش ثانیه درمی‌آید و همان یک همانند، کاربر بدشانس را به دادهٔ کهنه می‌رساند. برای تضمین‌های خواندن هم می‌توانی یک نشانهٔ موقعیت لاگ به کلاینت بدهی و همانند را وادار کنی تا آن موقعیت صبر کند؛ این کار خواندن را گران‌تر می‌کند ولی «تأخیر کم» را به یک قرارداد تبدیل می‌کند.Lag is not one number you measure once; it is measured per follower. The simplest method compares the leader's log position with the position the follower has applied, and translates the difference into seconds. The number that matters is the worst follower, not the average: if one of ten followers is two minutes behind and the rest are current, the average is six seconds and that one follower feeds stale data to the unlucky user. To make a read promise, hand the client a log position and make the follower wait until it reaches that position; reads get more expensive, but “low lag” becomes a contract instead of a hope.

تأخیر کوتاه هم تصمیم را خراب می‌کندEven a short lag corrupts a decision

بیشتر آسیب‌ها از تأخیر چند ثانیه‌ای می‌آید، نه از تأخیر چند ساعته. کاربر پروفایلش را عوض می‌کند، صفحه دوباره باز می‌شود و نسخهٔ قبلی را می‌بیند؛ دوباره ذخیره می‌کند و تغییر خودش را باطل می‌کند. یا دو خواندن پشت سر هم انجام می‌شود، اولی از یک همانند تازه و دومی از یک همانند عقب‌مانده، و کاربر می‌بیند زمان به عقب برگشته است. خطرناک‌تر از همه جایی است که خواندن کهنه به نوشتن تبدیل می‌شود: موجودی انبار را کهنه می‌بینی، سفارش می‌گیری، و موجودی واقعی منفی می‌شود. این‌ها با «تأخیر معمولاً کم است» حل نمی‌شوند؛ باید بگویی کدام خواندن به کدام نوشتن گره می‌خورد و همان مسیر را به رهبر یا به موقعیت لاگ ببندی.Most damage comes from a lag of seconds, not hours. A user edits a profile, reloads and sees the old version, saves again and cancels their own change. Or two reads in a row hit a fresh follower and then a lagging one, and the user watches time move backwards. The dangerous version is a stale read that turns into a write: you see yesterday's stock, take the order, and the real inventory goes negative. “Lag is usually low” does not fix any of this; you have to name which read depends on which write and pin that path to the leader or to a log position.

تلهٔ رایجThe usual trap

گرفتن میانگین تأخیر به‌جای بدترین همانند. میانگین همیشه خوب به نظر می‌رسد چون یک همانند عقب‌افتاده در میان ده‌ها همانند گم می‌شود، در حالی که همان یکی است که کاربر بدشانس از آن می‌خواند. شکایت کاربر از «دادهٔ عجیب» معمولاً از همین دم توزیع می‌آید، نه از میانگین.Tracking average lag instead of the worst follower. The average always looks fine because one lagging follower disappears among a dozen, and that one is exactly where the unlucky user reads. Complaints about “weird data” usually come from that tail, not from the mean.

single-leader replication

تکثیر تک‌رهبر فصل ۶Chapter 6 · دیدن در متنsee in context

همهٔ نوشتن‌ها از یک در می‌آیند؛ اگر آن در بسته شود؟Every write goes through one door. What if the door closes?

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

یک دفترچهٔ حساب مشترک دارید و قرار گذاشته‌اید فقط خزانه‌دار در آن بنویسد؛ بقیه هر وقت بخواهند نگاهش می‌کنند، ولی هیچ‌کس جز او قلم برنمی‌دارد. برای همین هیچ‌وقت دو خط با تاریخ یکسان و مبلغ‌های مختلف در دفتر پیدا نمی‌کنی. تک‌رهبری هم همین قرار است، فقط با ماشین‌ها.You share a household account book and agree that only the treasurer writes in it; everyone else reads it whenever they like, but nobody else picks up the pen. That is why you never find two lines with the same date and different amounts. Single-leader replication is the same agreement, made with machines.

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

خزانه‌دار مریض می‌شود. تا کسی جای او را نگیرد، هیچ هزینه‌ای ثبت نمی‌شود؛ این هزینهٔ تمرکز است. وقتی هم کسی را جای او می‌گذاری، دو خطر داری: ممکن است نسخهٔ دفتر او از دفتر قدیمی عقب‌تر باشد و چند خط آخر را نداشته باشد، و ممکن است خزانه‌دار قبلی برگردد و خودش را همچنان مسئول بداند. در خانه با یک تماس تلفنی حل می‌شود؛ در یک خوشهٔ سرور، همان تماس تلفنی می‌شود انتخاب رهبر، و همان بازگشت می‌شود مغز دو نیمه.The treasurer falls ill. Until someone takes over, no expense gets recorded; that is the price of one writer. When you do appoint a replacement, two dangers appear: the new book may be behind the old one and missing the last few lines, and the old treasurer may come back still believing the job is theirs. At home a phone call settles it; in a server cluster that phone call is leader election, and that return is split brain.

درخواست نوشتنWrite requestکلاینتclientرهبرLeaderترتیب در لاگorders the logهمانندهاFollowersهمان ترتیبsame orderتأییدAckبعد از کامیتafter commit
همهٔ نوشتن‌ها از رهبر می‌گذرند و همانندها همان ترتیب لاگ را تکرار می‌کنند.Every write passes through the leader, and followers repeat the same log order.

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

در تکثیر تک‌رهبر، کلاینت‌ها نوشتن را به رهبر می‌فرستند و رهبر با ترتیب دادن آن‌ها یک تاریخچهٔ واحد می‌سازد: هر نوشتن یک موقعیت در لاگ دارد و همانندها همان لاگ را به همان ترتیب اعمال می‌کنند. خواندن می‌تواند از رهبر یا هر همانندی برود؛ اگر از همانند برود ارزان‌تر پخش می‌شود، ولی می‌تواند کهنه باشد. وقتی رهبر از کار می‌افتد، سیستم باید شکست را تشخیص دهد، یک همانند را رهبر کند و کلاینت‌ها را به آدرس تازه بفرستد؛ به این کار failover می‌گویند. اگر همانند تازه هنوز نوشتن‌های تأییدشده را نگرفته باشد، آن‌ها با رفتن رهبر گم می‌شوند؛ اگر رهبر قدیمی برگردد و همچنان خودش را رهبر بداند، دو تاریخچهٔ موازی ساخته می‌شود. به همین دلیل تولید جدی، انتخاب رهبر را به یک سرویس اجماع می‌سپارد و رهبر قدیمی را با fencing کنار می‌گذارد.In single-leader replication clients send writes to the leader, and the leader builds one history by ordering them: each write has a log position and followers apply that log in the same order. Reads can go to the leader or to any follower; going to a follower spreads them cheaply but may return stale data. When the leader fails, the system has to detect the failure, promote a follower, and point clients at the new address; that is failover. If the new leader has not yet received an acknowledged write, that write is lost with the old leader; if the old leader comes back still believing it leads, two parallel histories exist. That is why serious deployments put leader election behind a consensus service and fence the old leader off.

۱ رهبرهمهٔ نوشتن‌ها از یک مسیر می‌گذرندevery write takes the same path
N همانندخواندن پخش می‌شود، نوشتن نهreads spread out; writes do not
ثانیه‌هازمان تشخیص خرابی و انتخاب رهبر تازهtime to notice the failure and elect a new leader

failover چه چیزی را می‌شکندWhat failover breaks

failover ساده به نظر می‌رسد: رهبر مرده، یکی دیگر را رهبر کن. در عمل سه جا می‌شکند. اول، تشخیص: اگر timeout را کوتاه بگذاری، یک کندی موقت را خرابی می‌بینی و رهبر سالم را کنار می‌گذاری؛ اگر بلند بگذاری، سیستم دقیقه‌ها بدون نوشتن می‌ماند. دوم، عقب‌ماندگی: همانندی که رهبر می‌شود ممکن است چند نوشتن تأییدشده را ندیده باشد، و آن نوشتن‌ها با ارتقا دادن او رسماً ناپدید می‌شوند. سوم، بازگشت: رهبر قدیمی که فقط کند بوده، برمی‌گردد و خودش را رهبر می‌داند؛ تا وقتی کسی او را نبندد، دو رهبر داریم. هر سه با یک عدد timeout حل نمی‌شوند؛ به quorum، شمارهٔ دورهٔ رهبری و مسیر خواندن درست نیاز دارند.Failover sounds simple: the leader is dead, promote another. In practice it breaks in three places. Detection: a short timeout turns a brief stall into a failure and demotes a healthy leader; a long one leaves the system unable to write for minutes. Lag: the follower being promoted may not have seen acknowledged writes, and those writes officially vanish when it takes over. Return: an old leader that was merely slow comes back believing it still leads, and until someone fences it there are two. None of the three is solved by picking a better timeout; they need a quorum, a leadership epoch and a correct read path.

چرا ترتیب مهم استWhy the order matters

لاگ رهبر یک ترتیب کامل می‌سازد و همانندها باید دقیقاً همان ترتیب را تکرار کنند. اگر دو نوشتن که به یک رکورد می‌رسند جابه‌جا اعمال شوند، نتیجه فرق می‌کند؛ مثلاً یکی مقدار را روی ۱۰ می‌گذارد و دیگری ۵ واحد کم می‌کند. ترتیب یکی، جواب نهایی را قابل پیش‌بینی می‌کند و بازپخش لاگ را ممکن. همین ترتیب است که به رهبر اجازه می‌دهد بدون مشورت با دیگران تصمیم بگیرد؛ چون تصمیم‌ها پشت سر هم می‌نشینند، نه هم‌زمان. بهایش این است که هر چیزی که ترتیب را بشکند — همانند دیرکرده، لاگ ناقص، رهبر تکراری — به اختلاف نسخه‌ها ترجمه می‌شود.The leader's log creates a total order, and followers must repeat exactly that order. If two writes to one record are applied out of order the result changes: one sets a value to ten, the other subtracts five. A single order makes the final answer predictable and makes replaying the log possible. That order is also what lets the leader decide without asking anyone, because decisions queue up instead of colliding. The price is that anything which breaks the order — a late follower, a truncated log, a second leader — turns into divergent copies.

تلهٔ رایجThe usual trap

فرض این‌که رهبر فقط برای نوشتن نقطهٔ شکست است. اگر همهٔ خواندن‌ها را هم به رهبر بفرستی، که برای تازه بودن وسوسه‌کننده است، ظرفیت خواندن را دور ریخته‌ای و رهبر هم برای نوشتن هم برای خواندن گلوگاه می‌شود. خواندن باید جایی برود که تازه بودنش را می‌توانی توضیح بدهی، نه جایی که فقط ساده‌تر است.Assuming the leader is a single point of failure for writes only. Send every read to it too — tempting when you want freshness — and you throw away read capacity and make the leader the bottleneck for both. Reads should go where you can explain their freshness, not where the wiring is simplest.

دو نفر هم‌زمان فکر می‌کنند رئیس‌اند. کدام درست است؟Two people both think they are in charge. Which one is right?

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

مدیر شرکت در سفر است و تلفنش جواب نمی‌دهد؛ معاون اول شروع می‌کند به تأیید فاکتورها، معاون دوم هم که خبر ندارد همان کار را می‌کند. وقتی مدیر برمی‌گردد، دو دسته فاکتور تأییدشده روی میزش است و بعضی از آن‌ها با هم جور درنمی‌آیند. دو رهبر هم‌زمان، همان مغز دو نیمه است.The boss is travelling and not answering the phone; the first deputy starts approving invoices, and the second deputy, who heard nothing, does the same. When the boss returns there are two stacks of approved invoices on the desk and some of them contradict each other. Two leaders at once is what split brain means.

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

مغازه را دو شریک اداره می‌کنند و رابطه‌شان شکرآب می‌شود؛ هر کدام برای خودش سفارش می‌گیرد. آخرین بستهٔ کالا به هر دو نفر فروخته می‌شود، و روزی که دوباره با هم حرف می‌زنند باید یکی از دو قول را بشکنند. نکتهٔ دردناک این است که مشکل از قطع رابطه نیست؛ مشکل این است که هر دو طرف در همان مدت می‌توانستند بفروشند. اگر یکی از آن‌ها اجازهٔ فروش نداشت، قطع رابطه فقط یک معطلی بود، نه یک فروش دوباره.Two partners run a shop and stop speaking to each other; each takes orders alone. The last box of stock is sold to both customers, and the day they talk again one of the two promises has to be broken. The painful part is that the broken relationship is not the problem; the problem is that both sides could sell during it. If only one of them had the authority to sell, the silence would have been a delay, not a double sale.

خوشهٔ واحدOne clusterسمت الف · رهبر خودشSide A · its own leaderسمت ب · رهبر خودشSide B · its own leaderهر دو می‌نویسند و به هم گوش نمی‌دهندboth accept writes and neither hears the other
قطع شبکه یک خوشه را دو نیم می‌کند؛ اگر هر نیم خودش را رهبر بداند، هر دو می‌نویسند.A partition cuts one cluster in two; if each half declares itself leader, both keep writing.

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

مغز دو نیمه وقتی رخ می‌دهد که یک خوشه به دلیل قطع شبکه یا تشخیص اشتباه خرابی به دو بخش تقسیم شود و هر دو بخش بتوانند رهبر بسازند. در آن حالت دو لاگ موازی نوشته می‌شود که هیچ‌کدام از دیگری خبر ندارد؛ وقتی شبکه برمی‌گردد، این دو تاریخچه باید ادغام شوند و بعضی نوشتن‌ها باید دور ریخته یا بازنویسی شوند. خطر فقط داده نیست: اگر رهبر تکراری مشغول کارهای بیرونی باشد، ممکن است یک پرداخت دو بار انجام شود. راه‌حل ساختاری این است که رهبر شدن نیاز به رأی اکثریت داشته باشد؛ بخشی که اکثریت ندارد باید از نوشتن دست بکشد، حتی اگر سالم باشد. لایهٔ ذخیره‌سازی هم باید رهبر قدیمی را با fencing token بشناسد و نوشتن‌هایش را رد کند، وگرنه صرفاً باور کردن این‌که «رهبر عوض شده» کافی نیست.Split brain happens when a partition or a false failure detection cuts a cluster in two and both halves are able to install a leader. Two parallel logs are then written, neither aware of the other; when the network returns those histories must be merged, and some writes have to be dropped or rewritten. The danger is not only data: a duplicate leader running side effects can charge a card twice. The structural fix is to require a majority vote to become leader, so the half without a majority refuses writes even though it is healthy. The storage layer must also recognise the old leader through a fencing token and reject its writes; merely believing that “the leader has changed” is not enough.

۳ از ۵بخش بدون اکثریت باید از نوشتن دست بکشدthe side without a majority must stop writing
۲ لاگدو تاریخ موازی که بعداً باید یکی شوندtwo parallel histories that must later become one
epoch +1شمارهٔ دورهٔ رهبری که لایهٔ ذخیره‌سازی چک می‌کندthe leadership epoch the storage layer checks

چرا فقط قطع شبکه فاجعه نیستWhy a partition alone is not the disaster

یک قطع شبکه به‌تنهایی فقط یک خوشهٔ دو نیمه می‌سازد؛ فاجعه وقتی شروع می‌شود که هر دو نیم بتوانند بنویسند. اگر نیم اقلیت بداند که اکثریت را ندارد و از پذیرفتن نوشتن خودداری کند، قطع شبکه فقط یک معطلی است: نیم اکثریت کار می‌کند و نیم دیگر منتظر می‌ماند. بدترین حالت آن است که هر دو نیم سالم به نظر برسند، هر دو رهبر داشته باشند و هیچ‌کدام نداند دیگری هم هست. این وضعیت می‌تواند ساعت‌ها دوام بیاورد و هیچ آلارمی هم ندهد، چون از دید هر نیم، همه‌چیز عادی است؛ فقط دنیا دو تا شده است.A partition on its own only produces two halves; the disaster starts when both halves can write. If the minority knows it lacks a majority and refuses writes, the partition is a delay: the majority side works and the rest waits. The worst case is when both sides look healthy, both have a leader, and neither knows the other exists. That state can last for hours without a single alert, because from each side everything looks normal; there are simply two worlds.

fencing دقیقاً چه می‌کندWhat fencing actually does

fencing یک شمارهٔ یکتا و صعودی به هر دورهٔ رهبری می‌دهد و لایهٔ پایین را وادار می‌کند فقط نوشتن‌های دورهٔ جاری را قبول کند. رهبر قدیمی لازم نیست بفهمد که دیگر رهبر نیست؛ کافی است ذخیره‌ساز نوشتن‌هایش را با یک خطای ساده رد کند. این کار به «رهبر قدیمی خودش کنار برود» وابسته نیست، و همین آن را قابل اعتماد می‌کند. در سیستم‌های واقعی این شماره در توکن یا در متادیتای هر درخواست می‌نشیند؛ اگر جایی این بررسی نباشد، بقیهٔ مکانیزم اجماع فقط یک قرار درون خوشه است، نه یک حفاظت واقعی.Fencing gives every leadership term a unique increasing number and makes the layer below accept writes only from the current term. The old leader does not need to learn that it lost the job; the storage simply rejects its writes with an error. This does not depend on the old leader stepping aside politely, which is what makes it trustworthy. In real systems the number rides in a token or in each request's metadata; wherever that check is missing, the rest of the consensus is an in-cluster agreement rather than real protection.

تلهٔ رایجThe usual trap

اکتفا به heartbeat و ارتقای خودکار. وقتی رهبر فقط کند است و نمرده، ارتقا دادن همانند دو رهبر زنده می‌سازد؛ و چون رهبر قدیمی هنوز به کلاینت‌ها جواب می‌دهد، مدتی هیچ‌کس نمی‌داند دو تاریخچه در جریان است. تا وقتی نوشتن‌های رهبر قدیمی جایی رد نمی‌شوند، این وضعیت با گذشت زمان خودش خوب نمی‌شود.Relying on heartbeats and automatic promotion. When the leader is merely slow rather than dead, promoting a follower creates two live leaders, and because the old one still answers clients nobody notices for a while that two histories are running. Until the old leader's writes are rejected somewhere, time does not heal it.

این عدد درست بود؛ فقط دیگر درست نیست. چرا؟That number was right. Why is it wrong now?

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

برچسب قیمت روی قفسه هنوز قیمت هفتهٔ پیش را نشان می‌دهد، ولی صندوق قیمت تازه را می‌خواند. برچسب کهنه است، نه غلط؛ فقط از قیمت‌های تازه عقب افتاده. دادهٔ کهنه هم نسخه‌ای است که از منبعش عقب افتاده.The price tag on the shelf still shows last week's price, while the till reads the new one. The tag is stale, not wrong; it has simply fallen behind the new price list. Stale data is the same: a copy that has fallen behind its source.

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

هر نسخهٔ دوم و هر کش، همین برچسب است: چیزی که در لحظه‌ای درست بود و الآن نیست. برای پوستر تبلیغاتی فرقی نمی‌کند؛ برای موجودی انبار و قیمت و نوبت پزشک فرق می‌کند. مسئله این نیست که داده کهنه بد است؛ مسئله این است که بدانی کدام خواندن‌ها می‌توانند کهنه باشند و کدام‌ها نه. وقتی این را نمی‌دانی، مجبوری یا همه‌چیز را تازه بخواهی و کند شوی، یا هیچ تازگی‌ای نخواهی و تصمیم‌هایت را روی شن بگذاری.Every second copy and every cache is that tag: something that was right at one moment and is not right now. For a poster it does not matter; for stock levels, prices and a doctor's schedule it does. The point is not that stale data is bad; the point is knowing which reads may be stale and which may not. Without that, you either demand freshness everywhere and get slow, or demand it nowhere and build decisions on sand.

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

دادهٔ کهنه، نسخه‌ای است که زمانی درست بود و از منبع عقب افتاده؛ نه داده‌ای که هرگز وجود نداشت. فاصلهٔ کهنگی را می‌توان با زمان (چند ثانیه از آخرین به‌روزرسانی)، با موقعیت لاگ، یا با TTL کش اندازه گرفت. کهنگی محدود با «حداکثر این‌قدر عقب» قابل قبول است؛ کهنگی نامحدود یعنی هر لحظه ممکن است چیزی ببینی که سال‌ها پیش درست بود. خطر اصلی جایی است که خواندن کهنه به نوشتن تبدیل می‌شود: موجودی را کهنه می‌بینی، سفارش می‌گیری و دو نفر یک صندلی را می‌فروشند. راه‌های عملی سه‌تاست: خواندن از منبع اصلی برای تصمیم‌های حساس، کوتاه کردن عمر کش، و بردن یک شمارهٔ نسخه در پاسخ تا کلاینت بتواند کهنگی را تشخیص بدهد.Stale data is a copy that was correct once and has fallen behind its source, not data that never existed. The staleness window can be measured in time (seconds since the last update), in log position, or as a cache TTL. Bounded staleness — “at most this far behind” — is acceptable; unbounded staleness means any read may show something that was true years ago. The real danger is when a stale read turns into a write: you see old stock, take the order, and two customers buy the same seat. Three practical answers exist: read the source for decisions that matter, shorten cache lifetimes, and carry a version number in the response so the client can see the staleness.

TTLسقف عمر کش را می‌گوید، نه فاصله از منبع راit caps the cache's age, not the distance to the source
چند ثانیهپنجرهٔ معمول کهنگی بین رهبر و همانندa typical staleness window between leader and follower

تلهٔ رایجThe usual trap

فرض این‌که TTL کوتاه یعنی سازگاری. TTL فقط سقف عمر کش را می‌گوید، نه فاصله از منبع را؛ یک کش با TTL یک‌ثانیه‌ای هم می‌تواند بعد از نوشتن، نسخهٔ قدیمی را برگرداند. بدتر، اگر همان خواندن کهنه ورودی یک نوشتن باشد، کهنگی به یک تصمیم غلط تبدیل می‌شود.Assuming a short TTL means consistency. A TTL caps how long a cache entry lives, not how far it sits from the source; even a one-second TTL can return the old value after a write. Worse, when the stale read feeds a write, staleness becomes a wrong decision.

sticky session

نشست چسبنده فصل ۶Chapter 6 · دیدن در متنsee in context

همیشه پیش همان آرایشگر می‌نشینی؛ اگر مرخصی برود؟You always pick the same barber. What if he takes leave?

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

همیشه پیش همان آرایشگر می‌نشینی، چون مدل مویت را یادش است. اگر یک روز نباشد و صندلی‌اش خالی بماند، مجبوری از اول توضیح بدهی. نشست چسبنده هم تو را به همان سرور می‌چسباند تا هر بار از اول شروع نکنی.You always sit with the same barber because he remembers your cut. When he is away and his chair sits empty, you have to explain it all from the start. A sticky session keeps you on the same backend so you never start over.

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

این راحتی یک شرط پنهان دارد: آن آدم باید سر کار باشد. وقتی مرخصی می‌رود، حافظه‌اش هم با او رفته؛ وقتی مغازه شلوغ می‌شود، صف پشت همان صندلی می‌ماند و صندلی‌های دیگر خالی. اگر مشتری‌ها را بر اساس «همان صندلی قبلی» بچینی، تعادل مغازه به حافظهٔ یک نفر گره می‌خورد. راه بهتر این است که هر آرایشگر بتواند پروندهٔ مشتری را از یک دفتر مشترک بخواند؛ آن‌وقت نشستن جای هر کسی اشکالی ندارد.That convenience has a hidden condition: the person has to be at work. When he takes leave his memory goes with him; when the shop is busy the queue waits behind one chair while the others stand empty. Assign customers by “your previous chair” and the shop's balance hangs on one person's memory. The better answer is a shared file each barber can read, so it no longer matters where you sit.

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

نشست چسبنده یعنی متعادل‌کنندهٔ بار همهٔ درخواست‌های یک نشست را به یک بک‌اند بفرستد، معمولاً با کوکی یا هش آدرس IP. این کار وقتی به درد می‌خورد که وضعیت نشست در حافظهٔ همان نود باشد: کاربر لازم نیست هر درخواست را دوباره معرفی کند و خواندن‌هایش هم به‌طور اتفاقی یکنوا می‌مانند. بهایش سه چیز است: توزیع بار نامتعادل می‌شود، مرگ یک نود همهٔ نشست‌های آن را می‌برد، و استقرار تازه یا مقیاس‌دهی کاربران را جابه‌جا می‌کند. راه معمول، بیرون کشیدن وضعیت نشست از نود است تا هر درخواست به هر نودی برود؛ آن‌وقت چسبندگی فقط یک بهینه‌سازی کش است، نه شرط درستی.A sticky session means the load balancer sends every request in one session to the same backend, usually by cookie or by hashing the client IP. It helps when session state lives in that node's memory: the user is not re-identified on every request and their reads stay monotonic by accident. It costs three things: load distribution becomes uneven, one node's death takes all of its sessions with it, and a deploy or scale-out shuffles users around. The usual answer is to move session state out of the node so any request can go anywhere; stickiness then becomes a cache optimisation rather than a correctness requirement.

۱ نودهمهٔ درخواست‌های یک نشست به یک بک‌اند می‌روندevery request in one session goes to one backend
hash(IP)روش رایج چسبندگی، که پشت NAT بد عمل می‌کندa common stickiness key, and a poor one behind NAT

تلهٔ رایجThe usual trap

استفاده از چسبندگی به‌جای ذخیره‌گاه نشست. با یک استقرار تازه یا ری‌استارت، همه از حساب بیرون می‌افتند؛ و اگر چسبندگی با هش IP باشد، همهٔ کاربران پشت یک NAT به یک نود می‌چسبند و بقیه بیکار می‌مانند.Using stickiness instead of a session store. One deploy or restart logs everyone out; and when stickiness is keyed on IP hash, every user behind one NAT sticks to a single node while the rest idle.

synchronous replication

تکثیر همگام فصل ۶Chapter 6 · دیدن در متنsee in context

جواب را قبل از رسیدن نسخهٔ دوم بدهیم یا بعدش؟Do we answer before the second copy arrives, or after?

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

برای اجاره‌نامه به دفترخانه می‌روی؛ کارمند نسخهٔ اول را مهر می‌کند و بعد می‌گوید «صبر کن تا نسخهٔ دوم هم مهر شود». تا آن مهر دوم نخورد، کار تو تمام نشده و کاغذت را نمی‌گیری. تکثیر همگام هم همین است: رهبر تا همانند تأیید نکند، به تو نمی‌گوید ثبت شد.You go to the notary for a lease; the clerk stamps the first copy and then says “wait until the second copy is stamped too”. Until that second stamp lands, your business is not finished and you do not get the paper. Synchronous replication is the same: the leader does not tell you it is recorded until a follower confirms.

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

همین انتظار هم تضمین است هم ریسک. تضمینش این است که کاغذی که در دست داری حتماً دو نسخه دارد؛ اگر فردا یکی گم شود، دیگری هست. ریسکش این است که اگر کارمند دوم مریض باشد یا دستگاهش خراب شود، کار همهٔ مراجعان می‌خوابد، نه فقط کار تو. برای همین دفاتر واقعی معمولاً یک نسخهٔ همگام و چند نسخهٔ ناهمگام دارند: فقط منتظر یک نفر می‌مانند، نه همه. این انتخاب، انتخاب بین دو خیر نیست؛ انتخاب بین دو درد است و باید بدانی کدام درد را می‌توانی تحمل کنی.That wait is both the guarantee and the risk. The guarantee is that the paper in your hand has two copies; if one is lost tomorrow, the other remains. The risk is that if the second clerk is ill or her machine is broken, everyone's business stalls, not just yours. That is why real offices keep one synchronous copy and several asynchronous ones: they wait for one person, not for everyone. The choice is not between two goods but between two pains, and you have to know which one you can carry.

همگامSynchronousرهبر منتظر تأییدleader waits for the ackتأخیر = کندترین همانندlatency = the slowest followerنوشتن تأییدشده گم نمی‌شودacked writes survive failoverناهمگامAsynchronousرهبر فوراً جواب می‌دهدleader answers at onceتأخیر کم، دسترس‌پذیری بیشترlower latency, more availableچند نوشتن آخر ممکن است برودthe last writes may be lost
دو راه برای جواب دادن به کلاینت: منتظر ماندن برای همانند، یا جواب فوری و پذیرش ریسک.Two ways to answer the client: wait for a follower, or answer at once and carry the risk.

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

در تکثیر همگام، رهبر نوشتن را روی خودش اعمال می‌کند و بعد منتظر می‌ماند تا همانند هم آن را بنویسد و تأیید کند؛ تازه بعد به کلاینت جواب می‌دهد. نتیجه این است که هر نوشتن تأییدشده روی دست‌کم دو ماشین نشسته و با مرگ رهبر گم نمی‌شود. بهایش تأخیر است: زمان پاسخ نوشتن به کندترین همانندِ حاضر گره می‌خورد، و اگر همانند از کار بیفتد نوشتن‌ها می‌ایستند تا کسی وضعیتش را روشن کند. به همین دلیل اکثر سیستم‌ها به همگام کامل نمی‌روند و semi-sync برمی‌دارند: یک همانند همگام، بقیه ناهمگام، و اگر همانند همگام کند شد موقتاً ناهمگامش می‌کنند. همان تضمین را می‌توان بدون انتظار برای یک نود خاص هم ساخت: با quorum، اگر W + R > N باشد، هر خواندن دست‌کم یک نسخهٔ تازه می‌بیند.With synchronous replication the leader applies the write locally and then waits for a follower to write and acknowledge it before answering the client. Every acknowledged write therefore sits on at least two machines and does not vanish when the leader dies. The price is latency: write response time is tied to the slowest available follower, and if that follower dies, writes stop until someone rules on its state. That is why most systems settle for semi-synchronous: one synchronous follower, the rest asynchronous, and a stuck synchronous follower is temporarily demoted. The same guarantee can be built without waiting on one particular node: with a quorum, W + R > N makes every read include at least one fresh copy.

≥ ۲ نسخههر نوشتن تأییدشده روی دست‌کم دو ماشین استevery acknowledged write sits on at least two machines
۱ تأییددر semi-sync فقط یک همانند باید جواب بدهدin semi-sync only one follower has to answer
W + R > Nراه دیگر گرفتن همان تضمین، بدون انتظار برای یک نود خاصanother way to get the guarantee without one particular node

چرا همگام کامل در تولید کم دیده می‌شودWhy full synchrony is rare in production

همگام کامل یعنی هیچ نوشتنی تأیید نمی‌شود مگر همهٔ همانندها آن را بگیرند. این تعریف در یک خوشهٔ پنج‌نودی یعنی پنج ماشین باید همیشه سالم و سریع باشند، و در عمل هیچ‌وقت نیستند: یکی برای ارتقا ریستارت می‌شود، یکی دیسکش کند شده، یکی در منطقهٔ دیگری با تأخیر شبکه‌ای زندگی می‌کند. نتیجه این است که نوشتن‌ها به کندترین و شکننده‌ترین عضو گره می‌خورند و دسترس‌پذیری عملاً از یک ماشین هم کمتر می‌شود. راه‌حل رایج semi-sync است: نوشتن باید توسط یک همانند تأیید شود، ولی بقیه می‌توانند از قافله جا بمانند. این تعریف هزینه را قابل تحمل می‌کند، ولی دیگر «همهٔ نسخه‌ها» را تضمین نمی‌کند.Full synchrony means no write is acknowledged until every follower has it. On a five-node cluster that means five machines must always be healthy and fast, which they never are: one is restarting for an upgrade, one has a slow disk, one lives in another region. Writes end up tied to the weakest member and availability is effectively worse than a single machine. The common answer is semi-synchronous: one follower must confirm, the rest may lag. That makes the cost bearable, but it no longer promises “every copy”.

همگام چه چیزی را تضمین می‌کند و چه چیزی را نهWhat synchrony does and does not promise

تکثیر همگام یک چیز را تضمین می‌کند: نوشتنی که به کلاینت تأیید شد، با از دست رفتن رهبر از بین نمی‌رود، چون قبل از تأیید روی نسخهٔ دوم هم نشسته است. اما تازه بودن خواندن را تضمین نمی‌کند: اگر خواندن‌ها از همانندهای ناهمگام بروند، همچنان می‌توانی نوشتن تأییدشده را نبینی. نوشتن اشتباه را هم جبران نمی‌کند؛ دستور غلط روی هر دو نسخه اجرا می‌شود. و اگر همانند همگام پشت یک صف طولانی باشد، تأیید گرفتن آنقدر طول می‌کشد که عملاً در دسترس نیست. همگام یک قرارداد دربارهٔ دوام است، نه دربارهٔ درستی یا تازگی.Synchronous replication promises one thing: a write that was acknowledged to the client survives the loss of the leader, because it was already on a second copy before the ack. It does not promise fresh reads: if reads go to asynchronous followers you can still miss an acknowledged write. It does not undo a bad write either; the wrong statement runs on both copies. And if the synchronous follower sits behind a long queue, getting its ack takes so long that it is effectively unavailable. Synchrony is a contract about durability, not about correctness or freshness.

تلهٔ رایجThe usual trap

گمان این‌که «ما همگام هستیم» یعنی هیچ نوشته‌ای گم نمی‌شود، در حالی که فقط یک همانند همگام است و بقیه ناهمگام‌اند؛ یا همانند همگام آن‌قدر دور یا کند است که هر نوشتن را گران می‌کند و بعد کسی بی‌صدا ناهمگامش می‌کند.Assuming “we are synchronous” means no write is ever lost, when only one follower is synchronous and the rest are not; or the synchronous follower is so slow that every write pays for it, and then somebody quietly demotes it. Either way the guarantee you think you have is gone.

کدام اتفاق اول افتاد، وقتی هیچ ساعتی قابل اعتماد نیست؟Which happened first, when no clock can be trusted?

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

تو و خواهرت هر کدام یک دفتر برای خرج‌های خانه دارید. هر تغییری که می‌نویسید، شمارهٔ آن تغییر در دفتر خودتان را کنارش می‌گذارید و آخرین شمارهٔ دفتر طرف مقابل را هم یادداشت می‌کنید. آخر هفته که دفترها را کنار هم می‌گذارید، از روی همین شماره‌ها می‌فهمید کدام خرج بعد از کدام ثبت شده و کدام‌ها هم‌زمان ثبت شده‌اند؛ ساعت برداری همین مقایسه را برای رویدادهای یک سیستم انجام می‌دهد.You and your sister each keep a notebook for household expenses. For every change you write down the count in your own book and the latest count you have seen in hers. At the weekend, with both books side by side, those numbers tell you which expense came after which, and which ones were recorded at the same time, which is the comparison a vector clock makes for events in a system.

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

مقایسه‌شان قاعدهٔ ساده‌ای دارد: اگر همهٔ شماره‌های یک دفتر کوچک‌تر یا مساوی دیگری باشند، آن یکی بعدتر است. اگر یکی در یک ستون جلو باشد و دیگری در ستون دیگر، هیچ‌کدام بعدتر نیست؛ آن‌ها هم‌زمان‌اند و باید خودت تصمیم بگیری. همین قاعده بدون ساعت دیواری کار می‌کند، چون فقط ترتیب را می‌سنجد، نه لحظه را. بهایش این است که با هر نفر تازه یک ستون به همهٔ دفترها اضافه می‌شود و مقایسه‌ها سنگین‌تر می‌شوند.The comparison follows one simple rule: if every counter in one notebook is less than or equal to the other, the first came earlier. If one book is ahead in one column and behind in another, neither came first; they are concurrent and you have to decide. The rule works without a wall clock because it measures order, not moments. The price is that every new person adds a column to every notebook and makes the comparisons heavier.

رویداد در AEvent at AA: ۱A: 1دریافت در BReceived at BA: ۱ · B: ۱A: 1 · B: 1نوشتن در AWrite at AA: ۲ · B: ۱A: 2 · B: 1هم‌زمان در BSame time at BA: ۱ · B: ۲A: 1 · B: 2
بردار سوم و چهارم هیچ‌کدام بر دیگری غالب نیست؛ پس آن دو نوشتن هم‌زمان‌اند.The third and fourth vectors dominate neither, so those two writes are concurrent.

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

ساعت برداری یک شمارنده به ازای هر نود نگه می‌دارد و هر رویداد با برداری از این شمارنده‌ها برچسب می‌خورد. نود وقتی رویدادی می‌سازد شمارندهٔ خودش را یکی زیاد می‌کند؛ وقتی پیامی از نود دیگر می‌گیرد، عنصرهای بردار را با هم مقایسه و بیشترین‌ها را برمی‌دارد، بعد شمارندهٔ خودش را یکی زیاد می‌کند. با همین قاعده می‌توان بدون ساعت هماهنگ گفت یک رویداد قبل از دیگری بوده، بعد از آن بوده، یا هیچ‌کدام قبل از دیگری نبوده. دو بردار وقتی مقایسه‌شدنی نیستند که هر کدام در جایی بزرگ‌تر باشند؛ آن‌وقت دو رویداد هم‌زمان‌اند، نه یکی مقدم. این «هم‌زمان» یعنی «بینشان رابطهٔ علّی نیست»، نه «دقیقاً در یک لحظه». بردار با تعداد نودها رشد می‌کند و همین باعث می‌شود سیستم‌های واقعی آن را کوتاه کنند، به قیمت از دست دادن بخشی از دقت ترتیب.A vector clock keeps one counter per node and labels each event with a vector of those counters. A node increments its own counter when it produces an event; when it receives a message, it takes the element-wise maximum of the two vectors and then increments its own counter. With that rule alone you can say, without synchronised clocks, that one event came before another, after it, or neither. Two vectors are incomparable when each is larger somewhere; the events are then concurrent, not ordered. “Concurrent” here means “no causal relation”, not “at the same instant”. The vector grows with the number of nodes, which is why real systems trim it and give up some of the ordering detail.

۱ شمارندهبه ازای هر نود، یک عدد که با هر رویداد بالا می‌رودone number per node, raised by each event
O(N)اندازهٔ بردار با تعداد نودها خطی رشد می‌کندthe vector's size grows linearly with the node count
A:2 B:1در برابر A:1 B:2، هیچ‌کدام بزرگ‌تر نیستagainst A:1 B:2, neither is greater

تفاوتش با ساعت دیواریHow it differs from a wall clock

ساعت دیواری لحظه را نشان می‌دهد و به همین دلیل برای ترتیب‌دادن به رویدادها بد است: دو سرور با چند ثانیه اختلاف می‌توانند رویدادی را که بعداً رخ داده، قدیمی‌تر نشان بدهند. ساعت برداری اصولاً لحظه را نمی‌داند و نمی‌خواهد بداند؛ فقط می‌گوید چه چیزی از چه چیزی خبر داشته. این تفاوت مهم است: اگر فقط ترتیب برایت مهم است، ساعت برداری کافی است و نیازی به همگام‌سازی ساعت نداری. اگر می‌خواهی بدانی «ساعت ۱۴:۰۳» اتفاق افتاده، ساعت برداری هیچ کمکی نمی‌کند و باید سراغ ساعت واقعی بروی.A wall clock shows an instant, which makes it a poor ordering device: two servers a few seconds apart can make a later event look older. A vector clock does not know the instant and does not try; it only records what each node knew about what had happened. The distinction matters: if order is all you need, a vector clock is enough and you avoid clock synchronisation. If you need to know that something happened at 14:03, a vector clock is no help and you are back to real clocks.

کوچک کردن بردار و بهایشTrimming the vector and what it costs

در یک خوشهٔ بزرگ، بردارِ هر رویداد می‌تواند ده‌ها مؤلفه داشته باشد و همراه هر پیام سفر کند. برای همین سیستم‌ها بخش‌های قدیمی را حذف می‌کنند، ولی حذف بی‌حساب خطرناک است: اگر مؤلفه‌ای را پاک کنی که هنوز به یک نود نرسیده، ممکن است دو رویداد هم‌زمان بعداً «قبل و بعد» به نظر برسند و یکی از آن‌ها بی‌صدا از دست برود. برخی سیستم‌ها برای همین از ساعت لمپورت استفاده می‌کنند که کوچک و ثابت است، ولی فقط یک ترتیب کلی می‌دهد و نمی‌تواند هم‌زمانی را تشخیص بدهد. انتخاب بین اندازهٔ پیام و دقت ترتیب است، نه بین درست و غلط.In a large cluster an event's vector can carry dozens of components and travel with every message. Systems therefore drop old entries, but careless trimming is dangerous: erase a component that has not reached a node and two concurrent events may later look ordered, with one silently lost. Some systems use a Lamport clock instead, which is small and constant but gives only a total order and cannot detect concurrency. The choice is between message size and ordering detail, not between right and wrong.

ساعت برداری در برابر بردار نسخهVector clock versus version vector

ساعت برداریVector clockبردار نسخهVersion vector
روی چه چیزی می‌نشیندAttached toهر رویدادevery eventهر نسخه از یک مقدارeach version of a value
چه می‌گویدWhat it answersقبل، بعد، یا هم‌زمانbefore, after, or concurrentغالب، مغلوب، یا خواهرdominant, dominated, or sibling
اندازه‌اشIts sizeبا تعداد نودهاone per nodeبا تعداد همانندهاone per replica

تلهٔ رایجThe usual trap

مقایسهٔ دو بردار با جمع یا طولشان. بردار بزرگ‌تر به معنی بعدتر نیست؛ فقط وقتی یکی در همهٔ مؤلفه‌ها بزرگ‌تر یا مساوی است بعدتر است. اشتباه همین یک قاعده، دو نوشتن هم‌زمان را به یک ترتیب جعلی تبدیل می‌کند و یکی را بی‌صدا دور می‌ریزد.Comparing two vectors by their sum or length. A larger vector does not mean a later event; one is later only when it is greater or equal in every component. Get that rule wrong and two concurrent writes become a fake order, with one of them quietly discarded.

دو نسخه از یک رکورد داری؛ کدام تازه‌تر است؟You hold two versions of one record. Which is newer?

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

دو صندوق‌دار در یک فروشگاه، هر کدام دفتر فاکتور جداگانه دارند و شماره‌ها را از یک شروع می‌کنند؛ یک مشتری جنس را برمی‌گرداند و فاکتورش را نشان می‌دهد. از روی شمارهٔ فاکتور نمی‌شود گفت کدام فروش بعدتر بوده، چون دو دفتر مستقل‌اند؛ باید هر دو شمارنده را با هم ببینی. بردار نسخه دقیقاً همین دو شمارنده را کنار هم می‌گذارد.Two cashiers in a shop keep separate invoice books and both start numbering at one; a customer returns an item and shows the receipt. The number alone cannot tell you which sale came later, because the books are independent; you need both counters. A version vector puts exactly those two counters side by side.

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

حالا فاکتورها را طوری بنویس که روی هر کدام شمارندهٔ هر دو صندوق‌دار چاپ شود: «الف: ۱۲، ب: ۷». اگر یک فاکتور در هر دو شمارنده بزرگ‌تر یا مساوی بود، بعدتر است. اگر یکی در الف جلو بود و دیگری در ب، آن دو فروش هم‌زمان‌اند و ترتیب ندارند. در فروشگاه واقعی این یعنی باید تصمیم بگیری: هر دو را قبول کنی، یا یکی را با یک قاعدهٔ جدا، مثلاً آخرین نوشتن، کنار بگذاری. خودِ بردار این تصمیم را نمی‌گیرد، فقط می‌گوید ترتیب طبیعی وجود ندارد.Now print both cashiers' counters on every receipt: “A: 12, B: 7”. A receipt that is greater or equal in both counters is later. If one is ahead in A and the other in B, the two sales are concurrent and have no order. In a real shop that means deciding: accept both, or set one aside by a separate rule such as last write wins. The vector itself does not decide; it only says there is no natural order.

نسخهٔ اولFirst versionA: ۱A: 1نوشتن روی AWrite on AA: ۲A: 2هم‌زمان روی BConcurrent on BA: ۱ · B: ۱A: 1 · B: 1دو خواهرTwo siblingsهیچ‌کدام غالب نیستneither dominates
آخرین وضعیت A و B هیچ‌کدام بر دیگری غالب نیست؛ دو نسخه خواهرند.The vector rides on each version; when neither dominates, the two versions are siblings.

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

بردار نسخه یک بردار از شمارنده‌هاست که همراه هر نسخه از یک مقدار ذخیره می‌شود: برای هر همانند یک عدد، که می‌گوید آن همانند چند تغییر روی این مقدار انجام داده. وقتی نوشتنی روی همانند i انجام می‌شود، مؤلفهٔ i یکی زیاد می‌شود؛ وقتی نسخه‌ای بین همانندها جابه‌جا می‌شود، بردارها هم همراه آن می‌روند. با مقایسهٔ مؤلفه‌به‌مؤلفه می‌شود فهمید یک نسخه از دیگری مشتق شده یا هر دو مستقل‌اند؛ حالت دوم را خواهر (sibling) می‌نامند و باید حل تعارض شود. بردار نسخه برای یک مقدار است، نه برای کل سیستم؛ به همین دلیل از ساعت برداری کوچک‌تر و کاربردی‌تر است. مشکل اصلی‌اش نگه‌داشتن تاریخچه است: تا وقتی مطمئن نشده‌ای همهٔ همانندها یک مؤلفه را دیده‌اند، نمی‌توانی آن را حذف کنی.A version vector is a set of counters stored with each version of a value: one number per replica, saying how many changes that replica has made to it. A write on replica i increments component i; when a version is copied between replicas the vector travels with it. Comparing component by component tells you whether one version descends from the other or whether both are independent; the second case is called a sibling and needs conflict resolution. A version vector belongs to one value rather than the whole system, which is why it is smaller and more practical than a vector clock. Its real problem is bookkeeping: you cannot drop a component until you know every replica has seen it.

A:1 · B:1در برابر A:2، هیچ‌کدام بر دیگری غالب نیستagainst A:2, neither dominates
O(replicas)یک مؤلفه به ازای هر همانند نگه داشته می‌شودone component is kept per replica
۲ خواهرنسخه‌هایی که هیچ‌کدام از دیگری مشتق نشده‌اندversions that descend from neither

غالب بودن دقیقاً یعنی چهWhat dominance means, exactly

یک بردار از بردار دیگر غالب است وقتی در همهٔ مؤلفه‌ها بزرگ‌تر یا مساوی باشد و دست‌کم در یکی بزرگ‌تر. اگر دو بردار کاملاً مساوی باشند، دو نسخه یک وضعیت را نشان می‌دهند، نه دو نسخهٔ متفاوت. اگر هر کدام در جایی بزرگ‌تر باشند، هیچ‌کدام غالب نیست و دو نسخه خواهرند. همین قاعدهٔ سه‌حالته تمام منطق را می‌سازد و هیچ‌جا به ساعت دیواری نیاز ندارد. اشتباه رایج این است که کسی «تازه‌تر» را با «عدد بزرگ‌تر» یا «تعداد مؤلفهٔ بیشتر» یکی می‌گیرد؛ آن اشتباه دقیقاً همان چیزی است که خواهرها را به یک ترتیب جعلی تبدیل می‌کند.One vector dominates another when it is greater or equal in every component and strictly greater in at least one. If the two vectors are exactly equal they describe the same state, not two versions. If each is ahead somewhere, neither dominates and the versions are siblings. That three-way rule is the whole logic and it never needs a wall clock. The usual mistake is to read “newer” as “numerically larger” or “more components”; that is exactly what turns siblings into a fake order.

چرا حذف یک مؤلفه خطرناک استWhy dropping a component is dangerous

مؤلفهٔ هر همانند تا زمانی لازم است که بدانی هنوز نسخه‌ای با آن مؤلفه در جایی زنده است. اگر بی‌دلیل حذفش کنی، دو حالت پیش می‌آید: ممکن است دو نسخهٔ هم‌زمان بعد از حذف شبیه «قبل و بعد» به نظر برسند و یکی از دست برود، یا ممکن است یک نسخهٔ قدیمی دیگر غالب به نظر برسد و روی نسخهٔ تازه‌تر بنشیند. سیستم‌های واقعی برای همین از بردارهای نقطه‌دار (dotted version vector) استفاده می‌کنند که هم جمع‌وجورتر است و هم می‌گوید کدام نوشتن هنوز تأیید نشده. بدون چنین دقتی، «تمیزکاری» بردار می‌تواند بی‌صدا داده را عقب ببرد.A replica's component is needed as long as a version carrying it may still exist somewhere. Drop it too early and one of two things happens: two concurrent versions can look ordered after the drop and one is lost, or an old version can look dominant and overwrite a newer one. Real systems use dotted version vectors for this, which are both more compact and able to say which write is still unacknowledged. Without that care, “cleaning up” a vector can quietly move data backwards.

تلهٔ رایجThe usual trap

مقایسهٔ بردارها با اندازه یا جمعشان، یا کنار گذاشتن خاموش یکی از خواهرها. وقتی سیستم خودش تصمیم بگیرد بزرگ‌تر را نگه دارد، در واقع یک last-write-wins پنهان ساخته و نوشتن دیگر را بدون این‌که کسی بفهمد دور ریخته است.Comparing vectors by size or sum, or silently dropping one sibling. When the system decides to keep the bigger one it has built a hidden last-write-wins and thrown the other write away without anyone noticing.