۹9دردسرهای سیستمهای توزیعشدهThe Trouble with Distributed Systems
۲۱ واژه از فصل ۹ — هر کدام با یک مثال روزمره، یک نمودار و تلهٔ رایج.21 terms from chapter 9, each with an everyday example, a diagram and the usual trap.
اگر گره فقط ساکت نشود، دروغ بگوید چه؟What if a node doesn’t just go quiet — it lies?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
چهار نفری میخواهید تصمیم بگیرید کجا شام بخورید. یکی از دوستان به تو میگوید «هر جا بخواهی»، به نفر سوم میگوید «من فقط سوشی»، و به نفر چهارم میگوید «من مخالفم». وقتی همه جمع میشوید سه روایت مختلف روی میز است و هیچکس هم دروغش لو نرفته. خرابی بیزانسی یعنی عضوی که نه قطع میشود و نه ساکت میماند؛ فقط روایتش با هر کس فرق دارد.Four of you are deciding where to eat. One friend tells you he is easy, tells the third person he only wants sushi, and tells the fourth he objects to everything. When you all sit down there are three different accounts on the table, and nobody has been caught lying. A Byzantine fault is a member who neither goes offline nor goes quiet — his story is just different for each of you.
تشبیه و تصویر ذهنیThe analogy
در کار معمولی، خرابی را «غیبت» میفهمی: کسی نمیآید و جایش خالی میشود. برای همین با یک تماس تلفنی میفهمی چه کسی نیست. خرابی بیزانسی جنس دیگری دارد: عضو حاضر است، پرانرژی هم هست، ولی چیزی میگوید که با واقعیت نمیخواند و ممکن است به هر کس چیز دیگری بگوید. حالا برای تصمیم گرفتن نمیتوانی فقط بشمری که «چند نفر موافقاند»؛ باید بتوانی حرفها را با هم بسنجی و کسی را که همه جا روایت عوض میکند کنار بگذاری. این کار رأی بیشتری میخواهد: با دروغگو کنار آمدن گرانتر از با غایب کنار آمدن است.In ordinary work you understand failure as absence: someone does not show up and leaves a gap, so one phone call tells you who is missing. A Byzantine fault is a different species: the member is present, energetic even, but says something that does not match reality and may say something different to each of you. Now you cannot decide by counting who agrees; you have to cross-check the accounts and set aside whoever keeps changing his story. That needs a bigger majority — living with liars costs more than living with absentees.
در خرابی توقف گره غایب است؛ در خرابی بیزانسی حاضر است و دروغ میگوید — و همین اکثریت بزرگتری میخواهد.A crash fault is absent; a Byzantine one is present and lying — which is why it needs a larger majority.
و بعدش، فنیAnd now, technically
در مدل خرابی بیزانسی، یک گره معیوب میتواند هر رفتاری داشته باشد: به هر همتا مقدار دیگری بدهد، پیام را حذف یا دیر کند، پیام جعل کند اگر امضا نباشد، و با گرههای معیوب دیگر همدست شود. مدل فقط همین را فرض میکند که «تا f گره ممکن است هر کاری بکنند»، و بقیه درستکار و پیرو پروتکلاند؛ همچنین فرض نمیکند که بتوانی معیوب را تشخیص بدهی — ممکن است فقط بدانی حداکثر f تا هستند. برای رسیدن به توافق در چنین مدلی، با پیامهای شفاهی به n ≥ 3f+1 گره نیاز داری، چون باید هم دروغها را با اکثریت بخوانی و هم جلوی گرهای را که به هر کس چیز دیگری میگوید بگیری. نکتهٔ مهم این است که بیشتر سیستمهای داده در دیتاسنتر این مدل را انتخاب نمیکنند: سرورهای خودت دشمن نیستند، خراب میشوند و دروغ نمیگویند؛ خرابی بیزانسی جای خودش را در سیستمهای باز دارد که شرکتکنندهها به هم اعتماد ندارند.Under the Byzantine fault model a faulty node may do anything: give each peer a different value, drop or delay messages, forge them when there are no signatures, and collude with other faulty nodes. The model assumes only that up to f nodes may behave arbitrarily while the rest are correct and follow the protocol; it does not assume you can identify the faulty ones — you may only know there are at most f. Reaching agreement under that model with oral messages needs n ≥ 3f+1 nodes, because you have to outvote the lies and also stop a node that tells every peer something different. The important part is that most data systems inside a datacenter do not choose this model: your own servers are not adversaries, they fail rather than lie. Byzantine faults earn their place in open systems where the participants do not trust each other.
تلهٔ رایجThe usual trap
برچسب بیزانسی زدن به هر خرابی عجیب. بیتِ خرابِ دیسک با checksum گرفته میشود و پاسخ دیررس با مهلت؛ خرابی بیزانسی یعنی کل گره بیرون از قرارداد رفتار میکند، نه اینکه یک بایت غلط باشد. برعکسش هم اشتباه است: اگر همهٔ گرهها همان کد باگدار را اجرا کنند، هیچ رأیگیریای نجاتت نمیدهد، چون همه با هم اشتباه میکنند.Slapping the Byzantine label on every odd failure. A corrupted disk bit is caught by a checksum and a late reply by a deadline; a Byzantine fault means the whole node steps outside the protocol, not that one byte is wrong. The mirror error is just as bad: if every node runs the same buggy code, no voting scheme saves you, because they all get it wrong together.
دو سرور، یک لحظه، دو زمان متفاوت. کدام درست است؟Two servers, one moment, two different times. Which one is right?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
ساعت دیواری آشپزخانه را با گوشیات مقایسه میکنی: چهار دقیقه جلوتر است. هر ماه چند ثانیه بیشتر جلو میرود و هیچوقت خودش را با گوشی جور نمیکند، چون کسی کوکش نمیکند. ساعت سرورها هم همین قصه را دارند: هر ماشین عدد خودش را میگوید و هیچ دو تایی در یک لحظه با هم یکی نیستند.You compare the kitchen wall clock with your phone: it is four minutes ahead. Every month it drifts a little further and never corrects itself, because nobody winds it back. Server clocks tell the same story: each machine reports its own number, and no two agree at the same instant.
تشبیه و تصویر ذهنیThe analogy
یک ساختمان اداری را تصور کن که در هر اتاق یک ساعت دیواری دارد. منشی هر صبح همه را با ساعت رادیویی کوک میکند، ولی خودِ کوک کردن چند ثانیه طول میکشد و در همان چند ثانیه یکی جلو میافتد و یکی عقب میماند. حالا دو نفر در دو اتاق یادداشت میکنند که قرار ساعت ده است؛ از روی کاغذها نمیتوانی بگویی کدام اول بوده. اگر منشی هم مریض شود، ساعتها کمکم از هم دور میشوند و هیچکس خبر ندارد. راهحل سادهٔ «همه را کوک کن» هست، ولی کوک کردن خودش خطا دارد و گاهی ساعت را به عقب میبرد؛ همانجا هم ترتیب رویدادها خراب میشود.Picture an office where every room has its own wall clock. Each morning the receptionist sets them all from the radio time signal, but the setting itself takes a few seconds, and in those seconds one clock gains and another falls behind. Now two people in two rooms write down that a meeting starts at ten; from the paper alone you cannot say which note came first. If the receptionist is off sick, the clocks quietly drift apart and nobody notices. The simple answer — “just set them all” — has its own error, and sometimes the correction moves a clock backwards, which is exactly when the order of events breaks.
دو ساعت هیچوقت یکی نیستند: معمولاً میلیثانیهها فاصله دارند و وقتی NTP قطع شود به ثانیهها میرسد.Two clocks never agree: usually the gap is milliseconds, and once NTP is gone it reaches seconds.
و بعدش، فنیAnd now, technically
هر ماشین یک نوسانساز کوارتز دارد که با دما و عمر، فرکانسش کمی تغییر میکند؛ همین میشود drift، در حد چند ده قسمت در میلیون، که هر ۱۰ ppm نزدیک یک ثانیه در روز است؛ پس یک روز بدون تصحیح، چند ثانیه انحراف میسازد. NTP این انحراف را با گرفتن زمان از چند سرور مرجع و محاسبهٔ offset و زمان رفتوبرگشت تصحیح میکند و در دیتاسنتر معمولاً اختلاف را به چند میلیثانیه میرساند، ولی هیچوقت صفر نمیکند. تصحیح دو شکل دارد: step که ساعت را یکباره جلو یا عقب میبرد، و slew که سرعت ساعت را مدتی کم یا زیاد میکند؛ step میتواند زمان را به عقب ببرد و همین برای ترتیب رویدادها کشنده است. نتیجه این است که مهر زمانی دو گرهٔ مختلف هیچ ترتیب قطعی نمیدهد: رویدادی که در گرهٔ A بعد از رویداد B رخ داده، ممکن است عدد کوچکتری گرفته باشد. اگر روی همین مهر زمانی last-write-wins بگذاری، نوشتن تازهتر میتواند بیصدا حذف شود؛ و اگر TTL یا lease را با ساعت دیواری بسنجی، یک گره میتواند زودتر از موعد خودش را بیرون ببیند. برای همین مقایسهٔ ساعت دو ماشین فقط یک حدس است، نه یک حقیقت، و هر تصمیمی که به آن تکیه کند باید یک حاشیهٔ خطا برای خودش نگه دارد.Every machine has a quartz oscillator whose frequency shifts slightly with temperature and age; that is drift, a few tens of parts per million, and every 10 ppm is close to a second a day; a day without correction therefore builds up several seconds. NTP corrects it by sampling several reference servers and computing an offset plus a round-trip time, and in a datacenter it usually brings the gap down to a few milliseconds, but it never makes it zero. Corrections come in two forms: a step, which jumps the clock forwards or backwards at once, and slew, which temporarily speeds the clock up or slows it down; a step can move time backwards, and that is what kills event ordering. The consequence is that timestamps from two different nodes establish no definite order: an event that happened after another on node B may carry a smaller number on node A. Put last-write-wins on those numbers and a fresh write can be silently dropped; measure a TTL or a lease with the wall clock and a slow node can consider itself out of time too early. Comparing two machines' clocks is therefore a guess, not a fact, and any decision built on it needs an error margin of its own.
≈ 10–100 ppmانحراف معمول کوارتز؛ هر ۱۰ ppm نزدیک یک ثانیه در روزtypical quartz drift; every 10 ppm is about a second a day
≈ 1–10 msاختلاف معمول دو ماشین در دیتاسنتر با NTPthe usual gap between two datacenter machines on NTP
step / slewدو نوع تصحیح NTP؛ step میتواند ساعت را عقب ببردthe two NTP corrections; a step can move a clock backwards
چرا مهر زمانی ترتیب را ثابت نمیکندWhy a timestamp cannot order events
فرض کن یک تراکنش در گرهٔ A ساعت ۱۰:۰۰:۰۰٫۱۰۰ ثبت شده و رویدادی که آن را خوانده در گرهٔ B ساعت ۱۰:۰۰:۰۰٫۰۵۰. از روی این دو عدد میشود گفت B اول بوده، ولی اگر ساعت B سیصد میلیثانیه عقب باشد، ترتیب واقعی برعکس است. مسئله این نیست که ساعتها «دقیق نیستند»؛ مسئله این است که هیچکس از بیرون نمیداند خطای هر کدام چقدر است. راه درست، جدا کردن زمان از ترتیب است: برای ترتیب از شمارندههای منطقی، شناسهٔ نسخه یا بازهٔ عدمقطعیت استفاده کن، و مهر زمانی را فقط برای نمایش و تقریب نگه دار. هر جا ترتیب را از ساعت خواستهای، یک فرض پنهان گذاشتهای که روزی میشکند.Suppose a transaction on node A is stamped 10:00:00.100 and the event that read it on node B is stamped 10:00:00.050. From those two numbers B came first — unless B's clock is three hundred milliseconds behind, in which case the real order is reversed. The problem is not that clocks are “imprecise”; it is that nobody outside knows how wrong each one is. The right move is to separate time from order: get order from logical counters, version numbers or an uncertainty interval, and keep timestamps for display and approximation only. Every time you ask a clock for order you have planted a hidden assumption that will break on some day.
حاشیهٔ خطا را چطور نگه داریمKeeping an uncertainty window
در سیستمهایی که زمان واقعاً بخشی از منطق است، مثل Spanner، هر ماشین عدمقطعیت ساعت خودش را میداند و تراکنشها صبر میکنند تا مطمئن شوند هیچ مهر زمانی متناقضی وجود ندارد. این کار امن است، ولی به سختافزار زمانسنج و چند میلیثانیه تأخیر اضافه هزینه دارد. اگر چنین زیرساختی نداری، سادهترین راه این است که ترتیب را از ساعت نخواهی: هر نوشتن را با شناسهٔ یکتا و نسخه بفرست، در برخورد از قانونی قطعی استفاده کن، و برای نمایش به کاربر مهر زمانی را با برچسب «تقریبی» قبول کن.In systems where time genuinely participates in the logic, such as Spanner, each machine knows its own clock uncertainty and transactions wait until no contradictory timestamps can exist. That is safe, and it costs special time hardware plus a few milliseconds of extra delay. Without such infrastructure the cheapest route is to stop asking the clock for order: send every write with a unique id and a version, resolve conflicts with a deterministic rule, and accept timestamps as “approximately” when you show them to a human.
تلهٔ رایجThe usual trap
تکیه کردن به NTP بهعنوان ضمانت. NTP معمولاً کار میکند، ولی وقتی سرور مرجع از دسترس خارج شود یا کسی ساعت ماشین را دستی عوض کند، ساعت میتواند یکباره به عقب بپرد. در آن لحظه TTLها زودتر منقضی میشوند، کارهای زمانبندیشده دوباره اجرا میشوند و رکوردی که تازه نوشته شده «قدیمیتر» از رکورد قبلی به نظر میرسد — و اگر هیچکس متوجه نشود، داده بیصدا گم میشود.Treating NTP as a guarantee. NTP usually works, but when the reference servers become unreachable or someone sets a machine's clock by hand, the clock can jump backwards in one go. In that moment TTLs expire early, scheduled jobs run twice, and a record written just now looks older than the one before it — and if nobody notices, data disappears silently.
هر بار که شکست میخوری، دیرتر دوباره امتحان کن. چرا؟Every time you fail, wait longer before trying again. Why?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
پشت تلفن یک تعمیرکار هستی و خط اشغال است. اگر بیوقفه دوباره و دوباره زنگ بزنی، هم خط را اشغال کردهای هم او را از جواب دادن انداختهای. عاقلانهتر این است که اول ده ثانیه صبر کنی، بعد نیم دقیقه، بعد دو دقیقه — و همین فاصلهٔ بزرگتر با هر تلاش دوباره، همان چیزی است که در شبکه exponential backoff مینامیم.You are calling a repairman and the line is busy. Ringing again and again without pause keeps the line tied up and keeps him from answering anyone. The sane move is to wait ten seconds, then half a minute, then two minutes — and each retry after a longer gap is exactly what a network calls exponential backoff.
تشبیه و تصویر ذهنیThe analogy
حالا تصور کن ده نفر همزمان همین شماره را میگیرند. اگر همه دقیقاً ده ثانیه صبر کنند، سر ثانیهٔ دهم همه با هم میزنند و خط دوباره اشغال میشود؛ پس صبرِ یکسان کافی نیست و باید هر کس کمی متفاوت صبر کند. این همان jitter است: یک عدد تصادفی کوچک روی فاصله اضافه کن تا تلاشها پخش شوند. طرف دیگر هم مهم است: اگر فاصله را بینهایت بزرگ کنی، مشتری که منتظر است رفته و کاربرد عملاً شکست خورده. پس هم سقف لازم است، هم یک لحظه که بپذیری دیگر نباید تلاش کرد.Now imagine ten people calling that same number. If they all wait exactly ten seconds, all ten ring at the tenth second and the line is jammed again; identical waiting is not enough, so each has to wait a little differently. That is jitter: add a small random amount to the gap so the attempts scatter. The other end matters just as much: make the gap enormous and the waiting customer has left, so the feature has failed anyway. You need a cap, and a moment where you admit the retry is not coming.
هر شکست فاصله را دو برابر میکند تا سقف برسد؛ jitter همان فاصله را کمی جابهجا میکند تا همه همزمان نکوبند.Each failure doubles the gap until a cap; jitter nudges the gap so everyone does not knock at once.
و بعدش، فنیAnd now, technically
الگوریتم ساده است: بعد از خطای nام، پیش از تلاش بعدی به مدت base × 2^n صبر کن و این عدد را با یک سقف ببند. base معمولاً چند صد میلیثانیه است و سقف چند ده ثانیه، وگرنه تأخیر به کاربر میرسد. jitter را به چند شکل میشود اضافه کرد: نصف فاصله را تصادفی انتخاب کن، یا کل بازه را تصادفی بزن؛ دومی همزمانی را بهتر میشکند. این کار از یک پدیدهٔ مشخص جلوگیری میکند: وقتی سیستمی از ظرفیتش گذشته، retryها بار را بیشتر میکنند و صف بلندتر میشود، و همان صف تأخیر را بالا میبرد و تایماوتهای بیشتری میسازد؛ این حلقه تا حد فروپاشی میتواند ادامه پیدا کند. تایماوت همیشه مبهم است: ممکن است درخواست انجام شده باشد و فقط جواب گم شده باشد، پس تلاش دوباره بدون idempotency میتواند دو بار اثر بگذارد. در عمل یک retry budget هم میگذارند: مثلاً تلاشهای دوباره حداکثر ده درصد کل درخواستها باشند، و retry فقط وقتی انجام شود که هنوز مهلت کلی درخواست باقی است.The algorithm is simple: after the nth failure, wait base × 2^n before the next attempt, and clamp that number with a cap. The base is usually a few hundred milliseconds and the cap a few tens of seconds, otherwise the delay reaches the user. Jitter comes in several shapes: pick half the gap at random, or pick anywhere in the whole interval; the second breaks synchronisation better. This prevents a specific failure: once a service is past capacity, retries add load, the queue grows, the queue raises latency, and higher latency produces more timeouts; the loop can run all the way to collapse. A timeout is always ambiguous — the request may have been processed and only the reply lost — so retrying without idempotency can apply the effect twice. Production also uses a retry budget: retries at most ten percent of all requests, and only while the request's overall deadline still has room.
×2هر شکست، فاصلهٔ تلاش بعدی را دو برابر میکندeach failure doubles the next delay
≈ 30 sسقف معمول؛ بعد از آن بهتر است شکست را به کاربر بگوییa common cap; past it, tell the user it failed
±50%jitter معمول؛ فاصله را تصادفی جابهجا میکندtypical jitter; scatters the delay
≈ 10%سهم معمول retry از کل درخواستها در یک retry budgeta typical retry share of total requests in a retry budget
چرا retry سیستم را میکشدWhy retries can kill a system
وقتی سرویسی به مرز ظرفیتش رسیده، هر درخواست اضافه در صف مینشیند و صف یعنی تأخیر. تأخیر یعنی تایماوت، و تایماوت یعنی درخواست دوباره. پس همان لحظهای که سرویس بیشترین فشار را دارد، بیشترین درخواست اضافه را هم میگیرد. بدترین شکلش این است که بعد از یک قطعی کوتاه، همهٔ کلاینتها با هم برمیگردند و سرویسی که تازه بالا آمده زیر همان موج اول زمین میخورد؛ این حالت میتواند بعد از برطرف شدن علت اصلی هم ادامه پیدا کند تا وقتی کسی retryها را کم کند. backoff با jitter و یک retry budget همان کسی است که جلوی این حلقه را میگیرد.When a service sits at its capacity limit, every extra request waits in a queue, and a queue means latency. Latency means timeouts, and timeouts mean more requests. So at the exact moment the service is most loaded it also receives the most extra traffic. The worst shape is a short outage followed by every client returning at once, knocking over the service that just came back; this can outlive the original cause until somebody sheds the retries. Backoff with jitter and a retry budget is what breaks that loop.
کدام خطا را retry کنیمWhich errors deserve a retry
همهٔ خطاها ارزش تلاش دوباره ندارند. خطای گذرا مثل تایماوت، قطع اتصال یا 503 را میشود دوباره فرستاد؛ خطای 400 یا 422 با همان بدنه دوباره هم همان جواب را میدهد و فقط وقت تلف میکند. اگر سرور Retry-After داده، همان را رعایت کن، نه عدد خودت را. و مهمتر: retry را فقط در یک لایه بگذار. اگر هم gateway، هم SDK و هم کد اپلیکیشن هر سه retry کنند، یک درخواست میتواند به بیستوهفت درخواست تبدیل شود و هیچکس هم در داشبورد این را نمیبیند.Not every error deserves another try. A transient failure such as a timeout, a reset connection or a 503 can be resent; a 400 or 422 will answer the same way with the same body and only wastes time. If the server sent Retry-After, honour that instead of your own number. More important: retry in one layer only. If the gateway, the SDK and the application code all retry, a single request can become twenty-seven requests, and no dashboard will show you that.
تلهٔ رایجThe usual trap
retry کردن بدون idempotency و بدون jitter. کد کلاینت تایماوت میگیرد، دوباره میفرستد، و چون سرور درخواست اول را انجام داده بود، مشتری دو بار شارژ میشود. بدترش این است که همهٔ کلاینتها با همان تأخیر ثابت retry میکنند و بعد از یک قطعی کوتاه، موجی همزمان به سروری که تازه بالا آمده میزنند و دوباره زمینش میزنند.Retrying without idempotency and without jitter. The client times out, sends again, and because the server had already completed the first request the customer is charged twice. Worse, every client retries on the same fixed delay, so after a short outage a synchronised wave hits the server that just came back and puts it down again.
ساکت شد. مرده یا فقط کند؟It went quiet. Dead, or just slow?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
قرار است دوستت ساعت هفت برسد؛ هفت میشود، هفت و ربع، و هیچ پیامی نمیآید. هفت و نیم بلند میشوی و میروی، چون تصمیم گرفتهای «نمیآید»؛ ولی او ممکن است در ترافیک باشد یا باتری گوشیاش مرده باشد. تو فقط سکوت را دیدهای، نه نبودنش را؛ آشکارساز خرابی هم از همین سکوت حکم میسازد.A friend is supposed to meet you at seven; seven comes, quarter past, and no message arrives. At half past you get up and leave, because you have decided he is not coming — though he may be stuck in traffic or his phone may be dead. You saw silence, not absence; a failure detector builds its verdict out of exactly that silence.
تشبیه و تصویر ذهنیThe analogy
عددِ انتظار اینجا همهچیز را عوض میکند. اگر ده دقیقه صبر کنی، ممکن است دوست خوبی را جا بگذاری که فقط ترافیک بدی داشت؛ اگر یک ساعت صبر کنی، کل غروب را تنها روی صندلی کافه نشستهای. هیچ عددی هر دو را حل نمیکند، چون «دیر رسیدن» و «نیامدن» از جایی که تو نشستهای دقیقاً یک شکلاند. میتوانی به کافه زنگ بزنی یا از دوست مشترک بپرسی، ولی این کارها هم وقت میبرند و خودشان ممکن است جواب ندهند. هر آشکارساز خرابی همین معامله را دارد: زود حکم دادن یعنی زنده نگه داشتن سیستم، و دیر حکم دادن یعنی کمتر اشتباه کردن.The waiting number changes everything here. Wait ten minutes and you may walk out on a good friend who merely hit bad traffic; wait an hour and you have spent the evening alone on a café chair. No number solves both, because “late” and “not coming” look identical from where you sit. You can call the café or ask a mutual friend, but those take time and may not answer either. Every failure detector makes this trade: decide early and the system stays alive, decide late and you make fewer mistakes.
ضربان میرسد، چند ضربان گم میشود، تایماوت حکم میدهد — ولی حکم دربارهٔ واقعیت نیست، دربارهٔ سکوت است.A heartbeat arrives, several go missing, a timeout delivers a verdict — a verdict about silence, not about reality.
و بعدش، فنیAnd now, technically
آشکارساز خرابی معمولاً با ضربان ساده کار میکند: هر گره هر ثانیه یا چند صد میلیثانیه یک پیام «من زندهام» میفرستد، و اگر چند ضربان پشت سر هم نرسد، گره مشکوک یا مرده اعلام میشود. دو نوع خطا دارد: مثبت کاذب، یعنی گرهای زنده است ولی بهخاطر تأخیر شبکه یا توقف فرآیند مرده اعلام میشود؛ و منفی کاذب، یعنی گرهای مرده است و هنوز کسی خبر ندارد. در یک شبکهٔ ناهمگام نمیشود این دو را قطعی از هم تشخیص داد، چون تأخیر پیام کران ندارد و «کند» با «مرده» یکی به نظر میرسد؛ پس هر آشکارساز فقط یک حدس است، با احتمال خطا. به همین دلیل معیارها نسبیاند: کامل بودن یعنی هر گرهٔ مردهای بالأخره مشکوک شود، و دقت یعنی گرهٔ زندهای بهاشتباه مشکوک نشود؛ هیچ الگوریتمی هر دو را همزمان کامل نمیکند. در عمل تایماوت را از توزیع تأخیر مشاهدهشده میگیرند، نه از یک عدد ثابت، و بهجای یک ناظر مرکزی از شایعهپراکنی استفاده میکنند تا یک نقطهٔ شکست نداشته باشند. مهمترین نکته این است که حکم detector فقط یک اشاره است، نه یک مجوز: اگر بر اساس آن رهبر عوض کنی، باید توکن حصار هم داشته باشی، وگرنه گرهٔ قدیمی که فقط کند بوده برمیگردد و با رهبر جدید همزمان مینویسد.A failure detector usually runs on heartbeats: every node sends “I am alive” every few hundred milliseconds, and if several beats in a row do not arrive the node is marked suspect or dead. It makes two kinds of error: a false positive, where a live node is declared dead because of network delay or a process pause, and a false negative, where a dead node goes unnoticed. In an asynchronous network the two cannot be told apart with certainty, because message delay has no bound and “slow” looks exactly like “dead”; every detector is therefore a guess with an error rate. That is why its measures are relative: completeness means every dead node is eventually suspected, accuracy means live nodes are not suspected by mistake, and no algorithm maximises both. In practice the timeout comes from the observed latency distribution rather than a constant, and gossip replaces a central observer so there is no single point of failure. The crucial point is that the verdict is a hint, never a licence: act on it to change leaders and you also need fencing tokens, or the old node that was merely slow wakes up and writes alongside the new one.
≈ 1 sفاصلهٔ معمول ضربان بین گرههاa common heartbeat interval
۳–۵تعداد ضربان گمشده تا اعلام مشکوکmissed beats before a node is suspect
FP / FNدو خطا: زنده را مرده دیدن، مرده را زنده پنداشتنtwo errors: a live node seen as dead, a dead one unseen
چرا نمیشود مطمئن شدWhy certainty is unavailable
هر گره فقط میتواند چیزی را مشاهده کند که به او رسیده است؛ پیام نرسیده، هیچ اطلاعاتی حمل نمیکند. ممکن است بسته در مسیر گم شده باشد، ممکن است سوئیچ شلوغ باشد، ممکن است فرآیند مقصد در GC باشد و بعد جواب بدهد. حتی اگر ده مسیر موازی هم امتحان کنی، باز هم «هیچ جوابی در این بازه نرسید» را نمیتوانی به «آن طرف مرده است» ترجمه کنی. تنها کاری که میشود کرد این است که احتمال خطا را کم کنیم و هزینهٔ هر نوع خطا را جدا بسنجیم.A node can only observe what reaches it; a message that never arrives carries no information. The packet may have been dropped, the switch may be congested, the destination may be sitting in a garbage collection pause and answer afterwards. Even probing ten parallel paths leaves you unable to translate “no reply within this window” into “the other side is dead”. All you can do is lower the error probability and price each kind of error separately.
حکم را کجا خرج میکنیWhere the verdict gets spent
هر اقدامی که بعد از حکم آشکارساز انجام میشود، تحمل خطای متفاوتی دارد. فرستادن درخواست به replica دیگر ارزان است و اگر اشتباه باشد فقط کمی کار اضافه شده؛ عوض کردن رهبر گران است و اگر اشتباه باشد دو رهبر میسازی. به همین دلیل تایماوت را بر اساس هزینهٔ اقدام انتخاب کن، نه بر اساس حسِّ «سرعت». برای اقدامهای برگشتپذیر تایماوت کوتاه بگذار، و برای اقدامهای خطرناک حاشیهٔ بزرگتر و یک لایهٔ حصار اضافه کن.Every action taken after the verdict has a different tolerance. Sending the request to another replica is cheap and a mistake only adds a little work; changing the leader is expensive, and a mistake creates two leaders. Choose the timeout from the cost of the action, not from a feeling about speed. Keep it short for reversible actions, and give dangerous ones a wider margin plus a fencing layer.
تلهٔ رایجThe usual trap
کوچک کردن تایماوت برای «سریعتر شدن» تشخیص. یک توقف GC چهلثانیهای یا یک ازدحام شبکه کافی است تا گرهٔ سالم مرده اعلام شود؛ رهبر جدید انتخاب میشود، گرهٔ قدیمی برمیگردد و چون هیچکس توکنش را چک نکرده، هر دو خودشان را رهبر میدانند. تایماوت کوتاهتر سیستم را سریعتر نمیکند؛ فقط اشتباه بیشتری تولید میکند.Shrinking the timeout to “detect faster”. A forty-second GC pause or a congested switch is enough to declare a healthy node dead; a new leader is elected, the old node comes back, and because nobody checked its token both believe they lead. A shorter timeout does not make the system faster; it only manufactures more mistakes.
صاحب قبلی برگشت. چه چیزی جلویش را میگیرد؟The old owner is back and still thinks it owns the place. What stops it?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
در هتل کارت کلید میگیری و وارد اتاق میشوی؛ فردا اتاق به مهمان بعدی داده میشود و قفل با کارت جدید او هماهنگ میشود. کارت تو هنوز سالم در جیبت است، ولی در را باز نمیکند، چون قفل فقط آخرین کارت را میشناسد. توکن حصار همان نسخهٔ کارت است و منبع داده همان قفل که کارت قدیمی را رد میکند.At a hotel you are given a key card and let yourself into the room; next day the room goes to another guest and the lock is paired with their new card. Your card is still intact in your pocket, but it no longer opens the door, because the lock only knows the latest one. A fencing token is that card version, and the resource is the lock that rejects the old card.
تشبیه و تصویر ذهنیThe analogy
چرا باید قفل چک کند و نه خودِ مهمان؟ مهمان قبلی ممکن است واقعاً باور داشته باشد که اتاق مال اوست؛ او کارتش را در جیب دارد و هیچ پیامی نگرفته که اتاق را به کس دیگری دادهاند. اگر فقط از خودش بپرسی، جوابش صادقانه «من اتاق دارم» است. کارتها باید از یک جا صادر شوند، نسخهها باید همیشه بالا بروند و هیچ نسخهای دوبار صادر نشود، وگرنه قفل نمیتواند مقایسه کند. و قفل باید بالاترین نسخهای را که دیده یادش بماند، حتی وقتی باتریاش عوض میشود؛ وگرنه با یک ریاستارت، کارت کهنه دوباره قبول میشود.Why must the lock check, and not the guest? The previous guest may genuinely believe the room is his; he still has the card in his pocket and received no message that the room went to someone else. Ask him and he will honestly say “this is my room”. The cards must be issued in one place, the versions must always rise, and no version may be issued twice, or the lock has nothing to compare. And the lock must remember the highest version it has seen even when its battery is replaced; otherwise a restart brings the old card back to life.
سرویس قفل توکن ۳۴ میدهد؛ کلاینت آن را با نوشتن میفرستد و منبع، هر توکن پایینتر از ۳۴ را رد میکند.The lock service issues token 34; the client sends it with the write, and the resource rejects anything below 34.
و بعدش، فنیAnd now, technically
در عمل، سرویس قفل با هر بار دادن lease یک توکن هم صادر میکند: عددی که با هر واگذاری بزرگتر میشود، مثل epoch یا شمارهٔ تراکنش در یک سرویس هماهنگ. کلاینت این توکن را با هر درخواست نوشتن به منبع داده میفرستد؛ منبع داده بالاترین توکنی را که تا حالا دیده نگه میدارد و هر درخواستی با توکن کمتر را رد میکند. این کار حتی وقتی ساعت کلاینت غلط است یا فرآیندش چهل ثانیه متوقف شده هم جواب میدهد، چون تصمیم به زمان وابسته نیست؛ به ترتیب اعداد وابسته است. توکن باید از یک جای واحد و پایدار صادر شود؛ اگر سرویس قفل خودش failover کند و شمارندهاش به عقب برگردد، کل حصار بیفایده میشود. همچنین چک باید در منبع داده باشد، نه در کلاینت: کلاینتی که خودش را بررسی میکند، همان کلاینتی است که خوابیده و بیدار شده و هنوز فکر میکند صاحب است. محدودیت اصلی این است که منبع داده باید این چک را بلد باشد؛ اگر مقصد یک سرویس بیرونی است که توکن را نمیفهمد، fencing هیچ کمکی نمیکند و باید سراغ راه دیگری مثل تضمین تکنویسنده یا partition کردن رفت.In practice the lock service issues a token with every lease: a number that grows on each grant, such as an epoch or a transaction id from a coordinating service. The client sends that token with every write to the resource; the resource remembers the highest token it has seen and rejects any request carrying a lower one. This works even when the client's clock is wrong or its process was frozen for forty seconds, because the decision does not depend on time; it depends on the order of numbers. The token must come from one durable issuer; if the lock service fails over and its counter goes backwards, the whole fence is gone. The check must also live at the resource, not in the client: a client that checks itself is the very one that slept, woke up, and still believes it holds the lease. The main limitation is that the resource has to understand the check; if the destination is an external service that ignores tokens, fencing cannot help and you need a different answer, such as a single-writer guarantee or partitioning.
max(token)تنها چیزی که منبع داده لازم دارد به یاد بسپاردthe only thing the resource needs to remember
+1هر واگذاری اجاره، توکن را بزرگتر میکند؛ هرگز برابر یا کمتر نهeach lease grant raises the token; never equal or lower
1 issuerتوکن باید از یک جای واحد صادر شود، وگرنه مقایسه بیمعناستone issuer; two counters make comparison meaningless
چرا lease بهتنهایی کافی نیستWhy a lease alone is not enough
صحنهای را دنبال کن: گرهٔ A اجارهٔ دهثانیهای میگیرد و شروع میکند. در ثانیهٔ سوم برای چهل ثانیه در GC میرود. سرویس قفل در ثانیهٔ دهم اجاره را به B میدهد و B شروع میکند. در ثانیهٔ چهلوسوم A بیدار میشود و از دید خودش هنوز وسط کار است؛ چیزی هم به او نگفتهاند. اگر فقط lease داشته باشی، A همان نوشتنهای قبلی را ادامه میدهد و B را خراب میکند. اگر منبع داده بالاترین توکن را نگه دارد، نوشتن A با توکن قدیمی رد میشود و مشکل همانجا تمام است. تفاوت این دو حالت فقط یک عدد است.Follow the scene: node A takes a ten-second lease and starts working. In the third second it enters a forty-second garbage collection pause. At second ten the lock service grants the lease to B, and B starts. At second forty-three A wakes up and, from where it stands, is still mid-work; nobody told it anything. With a lease alone, A continues its old writes and corrupts B's work. With the resource remembering the highest token, A's write is rejected on the old number and the problem ends there. The difference between the two worlds is one integer.
حصار فقط جایی کار میکند که لحظهٔ نوشتن از یک دروازه بگذرد. اگر صاحب قدیمی فایلی را روی یک اشتراک شبکهای بازنویسی کند، یا ایمیل بفرستد، یا به یک API بیرونی که توکن را نمیشناسد وصل شود، هیچ عددی جلویش را نمیگیرد. راهحل این است که آن اثر را هم از پشت یک دروازه رد کنی، یا آن را به مرحلهای منتقل کنی که خودش نسخهدار است. راهحل دوم این است که صاحب قبلی وقتی تمدیدش شکست خورد خودش کنار بکشد؛ ولی چون ممکن است در همان لحظه متوقف باشد، این فقط یک لایهٔ کمکی است، نه ضمانت.A fence only works where the moment of writing passes through a gate. If the old owner overwrites a file on a network share, sends an email, or calls an external API that does not understand tokens, no number will stop it. Either route that effect through a gate as well, or move it into a stage that is itself versioned. The other answer is for the old owner to stand down when its renewal fails; but since it may be paused at that exact moment, that is a helper layer, not a guarantee.
تلهٔ رایجThe usual trap
چک کردن توکن در کلاینت، یا صادر کردن آن از دو جا. یک تیم توکن را میگیرد و همانجا در کد کلاینت مقایسه میکند که «آیا من هنوز صاحبم؟»؛ فرآیندی که چهل ثانیه در GC بوده، دقیقاً همان لحظه بیدار میشود و جواب این سؤال را از حافظهٔ خودش میگیرد: بله. تنها جایی که این سؤال جواب درست دارد، همان جایی است که داده را مینویسد.Checking the token in the client, or issuing it from two places. A team takes the token and compares it right there in client code — “am I still the owner?” — and the process that spent forty seconds in garbage collection wakes up and answers from its own memory: yes. The only place where that question has a correct answer is the place that writes the data.
همان درخواست دو بار رسید. دو بار هم اثر گذاشت؟The same request arrived twice. Did it take effect twice?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
دکمهٔ طبقهٔ هفت در آسانسور را دو بار میزنی؛ آسانسور یک بار به هفت میرود و تمام، ولی دکمهٔ چراغ را دو بار بزنی، چراغ دوباره خاموش میشود. تفاوت در خودِ کار است، نه در انگشت تو: یکی با تکرار عوض نمیشود، دیگری هر بار برمیگردد. idempotency همین تفاوت را نام میگذارد.You press the seventh-floor button in the lift twice; the lift goes to seven once and that is that, but press the light switch twice and the light goes off again. The difference is in the work, not in your finger: one does not change on a repeat, the other flips every time. Idempotency is the name for that difference.
تشبیه و تصویر ذهنیThe analogy
حالا این را به پرداخت وصل کن. کارت میکشی، ترمینال پیام «در حال پردازش» میدهد و تو نمیدانی پول کم شده یا نه. اگر دوباره بکشی، ممکن است دو بار کم شود؛ اگر نکشی، ممکن است خرید انجام نشده باشد. راهی که بانکها میروند این است که هر درخواست یک شناسهٔ یکتا داشته باشد و سرور آن شناسه را کنار اثرش ذخیره کند: بار دوم که همان شناسه برسد، اثر دوباره اجرا نمیشود و فقط جواب قبلی برگردانده میشود. هزینهاش این است که باید آن شناسهها را نگه داری، تصمیم بگیری چقدر نگه داری، و برای دو درخواست همزمان هم فکری بکنی.Now wire that to a payment. You tap your card, the terminal says “processing”, and you cannot tell whether the money left. Tap again and you may be charged twice; walk away and the purchase may not have happened. What banks do is give every request a unique id and have the server store that id next to its effect: when the same id arrives again the effect is not replayed, only the earlier answer is returned. The cost is that you must keep those ids, decide for how long, and think about two identical requests arriving at the same moment.
همان درخواست، دو بار: یکی همان نتیجه را میدهد، دیگری دو بار اثر میگذارد.The same request, twice: one lands the same result, the other lands two effects.
و بعدش، فنیAnd now, technically
در ریاضیات، idempotent یعنی f(f(x)) = f(x)؛ در سیستمهای توزیعشده، یعنی اثر مشاهدهپذیر تکرار، همان اثر یک اجراست. بعضی عملیات ذاتاً اینطورند: خواندن، «این مقدار را بنویس»، و حذف؛ بعضی نیستند: «یکی اضافه کن»، «این مبلغ را برداشت کن»، «یک ایمیل بفرست». چون شبکه میتواند پیام را گم کند و تایماوت همیشه مبهم است، تحویل at-least-once عملاً تنها گزینه است؛ کاری که آن را قابل تحمل میکند، idempotent کردن گیرنده است، نه اصرار بر تحویل exactly-once. الگوی عملی این است: کلاینت یک idempotency key میسازد، سرور در همان تراکنشِ اثر یک ردیف با آن کلید درج میکند، و اگر کلید تکراری بود اثر را اجرا نمیکند و جواب ذخیرهشده را برمیگرداند. کلید باید بهقدری نگه داشته شود که از طولانیترین پنجرهٔ retry بیشتر باشد، و برای دو درخواست همزمان باید قید یکتا یا قفل داشته باشی، وگرنه هیچکدام ردیف دیگری را نمیبیند و هر دو اثر میگذارند. جواب هم مهم است: درخواست تکراری میتواند ۲۰۰ با همان بدنهٔ قبلی بگیرد یا ۴۰۹ بگیرد، ولی نباید اثر تازه بسازد. و هر side effect بیرونی، مثل ایمیل یا پیامک، هم باید همین شناسه را حمل کند.In mathematics idempotent means f(f(x)) = f(x); in a distributed system it means the observable effect of a repeat equals the effect of one run. Some operations are naturally like that: reads, “write this value”, deletes; some are not: “add one”, “withdraw this amount”, “send an email”. Because the network can lose messages and a timeout is always ambiguous, at-least-once delivery is effectively the only option; what makes it bearable is making the receiver idempotent, not insisting on exactly-once delivery. The practical pattern: the client makes an idempotency key, the server inserts a row with that key in the same transaction as the effect, and on a duplicate it skips the effect and returns the stored answer. The key must be kept longer than the longest retry window, and two concurrent duplicates need a unique constraint or a lock, or neither sees the other's row and both apply. The response matters too: a duplicate may get 200 with the original body or a 409, but it must not create a fresh effect. And every external side effect, email or SMS, has to carry the same id.
f(f(x)) = f(x)تعریف ریاضی؛ اثر تکرار همان اثر یک بار استthe definition: repeating changes nothing
≥ 1 deliveryتحویل at-least-once یعنی ممکن است دو بار برسدat-least-once means it may arrive twice
1 rowردیف کلید یکتا، کنار اثر و در همان تراکنشone unique-key row, written with the effect in one transaction
کلید idempotency در عملThe idempotency key in practice
کلید را کلاینت میسازد، نه سرور، چون فقط کلاینت میداند که دو درخواست «همان کار» هستند یا دو کار جدا. برای پرداخت، کلید معمولاً از شناسهٔ سبد خرید یا شناسهٔ کلیک ساخته میشود، نه از یک شمارندهٔ داخلی سرور. سرور کلید را با نتیجهاش ذخیره میکند و در برخورد، همان نتیجه را برمیگرداند. اگر کلید را بعد از اثر بنویسد، بین این دو یک پنجرهٔ کرش دارد که در آن پول کم شده و کلید نیست. اگر کلید را قبل از اثر بنویسد، پنجرهای دارد که کلید هست و اثر نیست. هر دو باید در یک تراکنش بنشینند یا یک مرحلهٔ جبران صریح داشته باشند.The client makes the key, not the server, because only the client knows whether two requests are the same job or two different jobs. For a payment the key usually comes from a cart id or a click id, not from a server-side counter. The server stores the key with its result and returns that result on a collision. Write the key after the effect and there is a crash window where the money moved and the key is missing. Write the key before the effect and there is a window where the key exists and the effect does not. Both belong in one transaction, or need an explicit compensation step.
چرا exactly-once وجود نداردWhy exactly-once does not exist
تحویل exactly-once در یک شبکهٔ غیرقابلاعتماد به این معناست که فرستنده بداند پیام رسیده و گیرنده بداند تکراری نیست، و این دو خبر باید همزمان درست باشند در حالی که تأییدیه خودش میتواند گم شود. کاری که واقعاً انجام میشود این است: تحویل at-least-once بهعلاوهٔ حذف تکراری، که نتیجهٔ نهاییاش یک اثر است و به آن effectively-once میگویند. این تفاوت لفظی نیست: اگر حالت حذف تکراری از دست برود، یا کلیدها قبل از پایان retry پاک شوند، همان دقیقاً دو بار انجام میشود. exactly-once در عمل یعنی «بهقدر کافی خوب»، نه یک ضمانت ریاضی.Exactly-once delivery over an unreliable network would mean the sender knows the message arrived and the receiver knows it is not a duplicate, and both facts would have to be true at the same instant while the acknowledgement itself can be lost. What actually gets built is at-least-once delivery plus deduplication, whose net result is one effect, called effectively-once. The distinction is not cosmetic: lose the dedup state, or expire keys before the retries end, and it happens exactly twice. Exactly-once in practice means “good enough”, not a mathematical guarantee.
تلهٔ رایجThe usual trap
ذخیرهٔ کلید در یک قدم و اثر در قدمی دیگر. اول ردیف کلید را مینویسی، بعد مبلغ را جابهجا میکنی؛ اگر بین این دو کرش کنی، کلید هست و اثر نیست، و درخواست بعدی «تکراری» رد میشود در حالی که هیچ پولی جابهجا نشده. برعکسش هم بد است — اثر اول، کلید بعد — و هر دو باید در یک تراکنش یا با یک قانون جبران صریح بنشینند.Storing the key in one step and applying the effect in another. Write the key row first, move the money second, and a crash in between leaves the key without the effect — so the next request is rejected as a duplicate while no money ever moved. The reverse is just as bad — effect first, key second — and both belong in one transaction, or behind an explicit compensation rule.
چه چیزی باید همیشه درست بماند؟What has to stay true, always?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
صورتحساب رستوران را بین پنج نفر تقسیم میکنید. هر کس سهم خودش را میدهد، یکی بیشتر و یکی کمتر. مهم نیست چطور تقسیم کنید؛ یک چیز نباید عوض شود: جمع پول روی میز باید دقیقاً اندازهٔ صورتحساب باشد. این جمله همان invariant است — هر ترتیبی بدهی، تا وقتی جمع برابر است حساب درست است.You split a restaurant bill five ways. Everyone puts in their share, one more and one less. How you divide it is your business; one thing must not change: what is on the table has to equal the bill exactly. That sentence is an invariant — shuffle the amounts however you like, the arithmetic still has to close.
تشبیه و تصویر ذهنیThe analogy
نکتهٔ ظریف این است که وقتی پول از دست یکی به دست دیگری در راه است، جمع روی میز لحظهای درست نیست. برای همین نابردار را روی وضعیتهای «نشسته» میسنجند، نه روی لحظهای که تراکنش نصفه است. اگر کسی از وسط کار به میز نگاه کند و جمع بزند، ممکن است چیز عجیبی ببیند، و همینجاست که ایزوله بودن تراکنشها معنا پیدا میکند: ناظر یا وضعیت قبل را میبیند یا وضعیت بعد را، نه حالت نصفهکاره را. هر نابرداری که خودش را «هر لحظه، حتی وسط کار» تعریف کند، عملاً به یک قفل بسیار سنگین نیاز دارد.The subtle part is that while money is travelling from one hand to another, the sum on the table is briefly wrong. That is why invariants are checked on settled states, not on the instant a transfer is half done. A bystander who adds the pile mid-move may see something strange, and this is where isolation earns its meaning: the observer sees either the state before or the state after, never the half-finished one. An invariant that insists on holding at every instant, including mid-flight, effectively demands a very heavy lock.
و بعدش، فنیAnd now, technically
نابردار یک گزاره روی وضعیت پایگاه داده است که باید پیش و پس از هر تراکنش درست باشد؛ در عمل، همان شکلِ قابلسنجشِ یک خاصیت ایمنی است. نمونههای آشنا: «جمع موجودی همهٔ حسابها ثابت است»، «موجودی هیچ کالایی منفی نیست»، «هر سفارش یک مشتری دارد»، «هیچ دو کاربری یک نام ندارند». راه تضمینش سهتاست: قید در خود پایگاه داده (یکتا، کلید خارجی، check)، تراکنشی که سطح ایزولهاش واقعاً جلوی آن anomaly را میگیرد، یا یک کوئری بازرسی که هر شب اجرا میشود و باید صفر ردیف برگرداند. دام اصلی این است که یک نابردار میتواند برای هر تراکنش بهتنهایی درست باشد و بین دو تراکنش همزمان بشکند — همان چیزی که در write skew میبینی. پس نقطهٔ تضمین، سطح ایزوله است نه بازبینی کد.An invariant is a predicate over database state that must hold before and after every transaction; in practice it is the checkable form of a safety property. Familiar examples: the sum of all balances is constant, no stock level is negative, every order has a customer, no two users share a name. There are three ways to guarantee one: a constraint in the database itself (unique, foreign key, check), a transaction whose isolation level actually blocks the anomaly, or an audit query that runs nightly and must return zero rows. The main trap is that an invariant can hold for each transaction alone and still break between two concurrent ones — the write skew shape. So the enforcement point is the isolation level, not a code review.
تلهٔ رایجThe usual trap
چک کردن نابردار در کد، بین دو کوئری. «اول بشمار، اگر موجودی بود کم کن» در سطح ایزولهٔ read committed دو درخواست همزمان را از هم جدا نمیکند و آخرین قطعه دو بار فروخته میشود. نابردار باید همانجایی سنجیده شود که همهٔ نوشتنها از آن رد میشوند — قید، تراکنش سریال، یا قفل — نه در یک if که به نظرت اتمیک است.Checking the invariant in application code, between two queries. “Count first, decrement if there is stock” does not separate two concurrent requests at read committed, and the last unit sells twice. The invariant has to be checked where every write must pass — a constraint, a serializable transaction, or a lock — not in an if that merely looks atomic.
چرا همه همزمان شروع میکنند؟Why does everyone start at once?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
ساعت زنگ همهٔ اعضای خانه روی ۶:۳۰ کوک است. همان لحظه همه از جا میپرند، همه با هم سر دستشویی صف میبندند و آشپزخانه قفل میشود. حالا همان ۶:۳۰ را نگه دار ولی به هر گوشی بگو بین صفر تا ده دقیقه تصادفی دیرتر زنگ بزند: همان کارها انجام میشود، بدون آن گرهِ اول صبح. این «تصادفی» همان jitter است.Every phone in the house is set for 6:30. Everyone jumps up at the same second, queues for the bathroom together, and the kitchen locks up. Keep 6:30 but tell each phone to ring at a random offset of up to ten minutes: the same chores get done, without the first-hour traffic jam. That randomness is jitter.
تشبیه و تصویر ذهنیThe analogy
چرا تصادف کار میکند؟ چون اگر همه دقیقاً دو ثانیه بعد دوباره تلاش کنند، دوباره با هم میکوبند. تصادفی کردن، همان تلاشها را از یک تپه به یک دامنهٔ پهن پخش میکند؛ مجموع کار عوض نمیشود، فقط توزیعش. بهایش این است که بعضی درخواستها کمی دیرتر شروع میشوند، و در عوض هیچ لحظهای نیست که همه با هم به در بکوبند. بدون این، هر بار که سرویس بالا میآید یا شبکه یک تکان میخورد، همان تپه دوباره ساخته میشود.Why does randomness work? Because if everyone retries exactly two seconds later, they all knock together again. Randomizing spreads the same attempts from one spike into a wide slope; the total work is unchanged, only its shape. The price is that some requests start a little later, and in exchange there is no instant when everyone hammers the door at once. Without it, every service restart or network hiccup rebuilds the same spike.
و بعدش، فنیAnd now, technically
jitter یعنی یک تأخیر تصادفی به زمانبندی اضافه کنی تا مجموعهای از کلاینتها که یک حادثه را با هم دیدهاند، با هم واکنش نشان ندهند. سه جای اصلی دارد. اول، retry: بهجای اینکه همه دقیقاً دو ثانیه صبر کنند، هر کس بین صفر و همان سقف یک عدد تصادفی برمیدارد؛ این full jitter است و مکمل فاصلهٔ نمایی است، نه جایگزینش. دوم، کارهای زمانبندیشده: اگر هزار ماشین ساعت سه بامداد یک کار را شروع کنند، پایگاه داده همان ثانیه میخوابد؛ یک آفست تصادفی داخل یک پنجره، همان کار را روی نیم ساعت پخش میکند. سوم، چیزهایی که خودشان با هم منقضی میشوند: TTL کش را چند درصد بالا و پایین ببر تا کلیدها یکجا نپرند. هزینهاش این است که بدترین حالت تأخیر به اندازهٔ کل پنجره بدتر میشود، پس جایی که ترتیب یا زمان دقیق مهم است jitter را نگذار. یک کاربرد دیگر هم دارد: در اندازهگیری شبکه، jitter به تغییر تأخیر بین بستهها میگوید، نه به تأخیر متوسط.Jitter means adding a random delay to a schedule so that a set of clients who all saw the same event do not all react at the same instant. It has three main homes. First, retries: instead of everyone waiting exactly two seconds, each waits a random amount up to the cap — that is full jitter, and it complements exponential backoff rather than replacing it. Second, scheduled work: if a thousand machines start the same job at three in the morning, the database dies that second, while a random offset inside a window spreads the job across half an hour. Third, anything that expires in unison: spread cache TTLs by a few percent so the keys do not drop together. The cost is that your worst-case delay grows by up to the whole window, so do not add jitter where order or exact timing matters. It has one other meaning too: in network measurement, jitter is the variation in delay between packets, not the average delay.
0 … capfull jitter: هر تلاش یک عدد تصادفی تا سقفfull jitter: a random delay up to the cap
± 10%پخش معمول روی TTL کش تا کلیدها با هم نپرندa typical spread on cache TTLs so keys do not expire together
تلهٔ رایجThe usual trap
تصادفی که واقعاً تصادفی نیست. اگر همهٔ نمونهها با یک seed یکسان قرعه بکشند، یا کد یک عدد تصادفی را یک بار در بالا بگیرد و برای همهٔ تلاشها استفاده کند، همان عدد در همه درمیآید و صفِ همزمان سر جایش میماند. jitter باید در هر تلاش و در هر فرآیند تازه کشیده شود.Randomness that is not random. If every instance draws from the same seed, or the code picks one random number at startup and reuses it for every attempt, they all get the same offset and the synchronized queue remains. Jitter has to be drawn fresh per attempt and per process.
قفل داری، ولی تا کی؟You hold the lock. Until when?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
دوچرخهٔ اشتراکی را با اپ باز میکنی و سوار میشوی. اجارهنامهات سی دقیقه اعتبار دارد؛ اگر در این سی دقیقه تمدید نکنی، اپ دیگر تو را صاحب دوچرخه نمیداند و نفر بعدی میتواند سوارش شود — حتی اگر تو هنوز روی زین نشسته باشی. lease همان اجارهنامهٔ زماندار است: حق تو تا وقتی ادامه دارد که تمدیدش کنی.You unlock a shared bike with the app and ride off. Your rental is good for thirty minutes; fail to renew within those thirty and the app no longer considers you the rider, and the next person can take the bike — even if you are still sitting on it. A lease is that timed rental: your claim lasts only as long as you keep renewing it.
تشبیه و تصویر ذهنیThe analogy
چرا اصلاً قفل را زماندار کنند؟ چون صاحب قفل ممکن است بمیرد. اگر قفل بیزمان باشد و گرهٔ صاحبش از کار بیفتد، آن قفل تا ابد دست یک مرده میماند و کل سیستم میایستد. با انقضا، سیستم میتواند بعد از یک وقفه دوباره راه بیفتد. ولی حالا دو سر طیف داری: اجارهٔ کوتاه یعنی تمدیدهای زیاد و این خطر که یک توقف کوتاه، کارِ نیمهتمام را از دستت بیرون بیاورد؛ اجارهٔ بلند یعنی بعد از خرابی، مدتی طولانی هیچکس نمیتواند کار کند. و بدترین قسمت این است که صاحب اجاره نمیتواند از ساعت خودش بفهمد آیا هنوز آن را دارد یا نه.Why put a clock on a lock at all? Because the holder may die. A lock with no expiry, held by a node that failed, stays with a corpse forever and the whole system stops. An expiry lets the system recover after a gap. But now you have two ends of a spectrum: a short lease means constant renewals and the risk that a brief pause yanks unfinished work away; a long lease means a long stretch after a crash when nobody can work. And the worst part is that the holder cannot use its own clock to know whether it still has the lease.
اجاره داده میشود، در نیمهٔ عمر تمدید میشود، و اگر تمدید نیاید منقضی میشود و صاحب بعدی با توکن بالاتر میآید.A lease is granted, renewed at half its term, and if renewal stops it expires and the next holder arrives with a higher token.
و بعدش، فنیAnd now, technically
lease یک قفل زماندار است که یک سرویس قفل مرکزی یا یک کوئروم میدهد و صاحبش باید پیش از انقضا تمدیدش کند. در انتخاب رهبر، رهبر یک lease میگیرد تا در بازهٔ مشخصی هیچکس دیگری رهبر نشود، و این بازه باید از بدترین تأخیر شبکه و بدترین توقف فرآیند بزرگتر باشد. صاحب اجاره معمولاً در نیمهٔ عمر آن تمدید میکند، تا یک تمدید گمشده هنوز فرصت جبران داشته باشد. مشکل بنیادی این است که هیچ ساعتی مشترک نیست: گرهٔ A ممکن است با ساعت خودش مطمئن باشد اجارهاش معتبر است، در حالی که سرویس قفل بهخاطر تأخیر پیام همان لحظه اجاره را به B داده است. پس lease بهتنهایی انحصار نمیسازد؛ چیزی که آن را امن میکند این است که هر واگذاری یک توکن صعودی بگیرد و منبع داده توکنهای قدیمی را رد کند. صاحب اجاره هم باید وقتی تمدید شکست خورد فوراً دست از کار بکشد؛ ولی چون ممکن است در همان لحظه متوقف باشد، این «خودحصاری» فقط یک لایهٔ کمکی است، نه ضمانت.A lease is a timed lock handed out by a central lock service or a quorum, and the holder must renew it before it expires. In leader election the leader takes a lease so that nobody else becomes leader for a set window, and that window has to exceed the worst network delay plus the worst process pause. The holder usually renews at half the term, so that one lost renewal still leaves room to recover. The fundamental problem is that there is no shared clock: node A may be certain, by its own clock, that its lease is valid while the lock service has already granted it to B because a message was delayed. So a lease alone does not create mutual exclusion; what makes it safe is that every grant carries a rising token and the resource rejects older ones. The holder must also stop working the moment renewal fails; but since it may be paused at that very moment, this “self-fencing” is a helper layer, not a guarantee.
½ TTLزمان معمول تمدید؛ یک تمدید گمشده را جبران میکندthe usual renewal point; survives one lost renewal
TTL > pauseعمر اجاره باید از بدترین توقف و تأخیر شبکه بزرگتر باشدthe term must exceed the worst pause plus network delay
2 holdersدر بازهٔ همپوشانی ممکن است دو گره خود را صاحب بدانندduring the overlap two nodes can both believe they hold it
چرا lease بدون توکن امن نیستWhy a lease without a token is unsafe
گرهٔ A اجاره میگیرد و شروع میکند. وسط کار، فرآیندش برای چهل ثانیه متوقف میشود؛ نه کرش، فقط توقف. سرویس قفل بعد از انقضا اجاره را به B میدهد و B کار را از سر میگیرد. A بیدار میشود و چون هیچ پیامی در آن مدت نگرفته، از دید خودش هنوز صاحب است و همان نوشتنها را ادامه میدهد. اگر منبع داده فقط «آخرین نویسنده» را قبول کند، نوشتهٔ A روی نوشتهٔ B میافتد. تنها چیزی که این را میبندد این است که A توکن قدیمیتری داشته باشد و منبع داده آن را رد کند. پس lease مسئلهٔ «چه کسی صاحب است» را حل نمیکند؛ فقط آن را زماندار میکند.Node A takes the lease and starts. Mid-work its process freezes for forty seconds — not a crash, just a pause. The lock service, after expiry, grants the lease to B, and B takes over. A wakes up, received nothing during that time, still believes it owns the lease, and carries on writing. If the resource simply accepts the latest writer, A's write lands on top of B's. The only thing that closes this is A carrying an older token and the resource rejecting it. A lease does not answer “who owns this”; it only puts a clock on the question.
عمر اجاره را چطور انتخاب کنیمChoosing the lease term
عمر اجاره را از توزیع واقعی توقف و تأخیر بگیر، نه از حسِّ شهودی. توقفهای GC با heap بزرگ میتوانند صدها میلیثانیه یا چند ثانیه باشند، و مهاجرت زندهٔ ماشین مجازی چند ثانیه. تأخیر شبکه در دیتاسنتر معمولاً کمتر از یک میلیثانیه است، ولی در لحظهٔ ازدحام یا خطای مسیر میتواند دهها برابر شود. یک قاعدهٔ عملی این است که عمر اجاره را چند برابر بدترین چیزی بگذاری که اندازه گرفتهای، و بعد هزینهٔ تمدیدهای اضافی را بپذیری. اگر عمر را کوتاه کنی، تعداد تمدیدها و شانس از دست دادن اجاره بالا میرود؛ اگر بلند کنی، بعد از خرابی مدت زیادی طول میکشد تا کسی جایش را بگیرد.Take the term from the measured distribution of pauses and delays, not from intuition. Garbage collection on a large heap can stop for hundreds of milliseconds or several seconds, and a live VM migration for seconds. Datacenter network delay is usually under a millisecond, but congestion or a bad route can multiply it many times. A workable rule is to set the term at several times the worst value you have measured, then accept the cost of the extra renewals. Shorten it and you get more renewals and a higher chance of losing the lease; lengthen it and a failure leaves the system waiting a long time for a replacement.
تلهٔ رایجThe usual trap
انتخاب TTL بر اساس «معمولاً سریع است». یک heap بزرگ با GC چندثانیهای، یا یک ماشین مجازی که مهاجرت میکند، TTL را در یک چشم به هم زدن میبلعد؛ صاحب قبلی بیدار میشود، هنوز خودش را رهبر میداند، و اگر منبع داده توکن را چک نکند، نوشتههای رهبر جدید را بازنویسی میکند.Picking the TTL on “it is usually fast”. A large heap with a multi-second garbage collection, or a VM that migrates, swallows the TTL in one gulp; the old holder wakes up still believing it leads, and if the resource never checks a token it overwrites the new leader's writes.
هیچچیز بدی رخ نداد. هیچچیز هم پیش نرفت.Nothing bad happened. Nothing moved, either.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
پشت چراغ راهنمای پیادهرو ایستادهای: چراغ سبز نمیشود، ولی هیچ ماشینی هم رد نمیشود و تصادفی رخ نمیدهد. سیستم از نظر «بد نشدن» کامل سالم است؛ فقط تو هیچوقت به آن طرف خیابان نمیرسی. زندهمانی همین است: نه نبودِ خرابی، بلکه وجودِ پیشرفت.You are waiting at a pedestrian light: it never turns green, but no car passes and nothing crashes. By the “nothing bad” measure the system is perfectly healthy; you simply never reach the other side. That is liveness: not the absence of failure, but the presence of progress.
تشبیه و تصویر ذهنیThe analogy
دو قول را از هم جدا کن. «هیچوقت دو ماشین همزمان در تقاطع نباشند» یک قول ایمنی است؛ «بالأخره یک نفر رد شود» یک قول زندهمانی. چراغی که همیشه قرمز بماند، قول اول را بینقص نگه میدارد و قول دوم را میکشد. حالا برعکسش: اگر برای رد شدن مردم، مدت قرمز را به نصف ثانیه برسانی، پیشرفت میگیری و خطر تصادف را بالا میبری. در سیستم توزیعشده هم دقیقاً همین معامله هست: یک آشکارساز خرابی که تا ابد صبر میکند امن است و مرده؛ یکی که سریع حکم میدهد زنده است و ممکن است دو رهبر بسازد.Separate the two promises. “Never two cars in the junction at once” is a safety promise; “someone eventually gets across” is a liveness promise. A light that stays red forever keeps the first promise perfectly and kills the second. Now the reverse: shorten the red phase so people get across, and you gain progress while raising the chance of a collision. A distributed system makes the same trade: a failure detector that waits forever is safe and dead; one that decides quickly is live and may create two leaders.
ایمنی میگوید هیچ اتفاق بدی نیفتد؛ زندهمانی میگوید بالأخره یک اتفاق خوب بیفتد.Safety says nothing bad ever happens; liveness says something good eventually does.
و بعدش، فنیAnd now, technically
در سیستمهای توزیعشده، safety یعنی «هیچ اتفاق بدی نمیافتد»: هیچوقت دو رهبر همزمان نیست، هیچ نوشتنِ commitشدهای گم نمیشود، هیچ پیامی دوبار اثر نمیگذارد. liveness یعنی «یک اتفاق خوب بالأخره میافتد»: هر درخواست بالأخره جواب میگیرد، سیستم بالأخره پیشرفت میکند، رهبر جدید بالأخره انتخاب میشود. تفاوت مهم این است که نقض safety برگشتناپذیر است — اگر دو رهبر همزمان نوشتند، بعداً نمیشود گفت کدام درست بود — ولی نقض liveness اغلب بعداً جبران میشود: سیستم یک ساعت گیر کرده و بعد راه افتاده. چون شبکه میتواند هرقدر بخواهد کند باشد، هر الگوریتمی که بخواهد زنده بماند باید در نقطهای با تایماوت تصمیم بگیرد، و هر تصمیمی احتمال خطا دارد. نتیجهٔ معروف FLP همین را میگوید: در شبکهٔ کاملاً ناهمگام با یک گرهٔ خراب، هیچ الگوریتم قطعیای نمیتواند تضمین کند که در همان حال که امن میماند، کارش هم بالأخره تمام میشود. به همین دلیل سیستمهای واقعی زمان را «بهاندازهٔ کافی همگام» فرض میکنند، از اکثریت استفاده میکنند، و سمت اقلیت را امن اما بیپاسخ نگه میدارند.In distributed systems, safety means “nothing bad happens”: never two leaders at once, no committed write lost, no message applied twice. Liveness means “something good eventually happens”: every request eventually gets an answer, the system eventually makes progress, a new leader is eventually chosen. The important asymmetry is that a safety violation is irreversible — once two leaders have both written, there is no later way to say which was right — while a liveness violation is often repaired later: the system stalled for an hour and then resumed. Because the network can be arbitrarily slow, any algorithm that wants to stay live must at some point decide on a timeout, and every such decision can be wrong. The well-known FLP result says the same: in a fully asynchronous network with one faulty node, no deterministic algorithm can be both always safe and always terminating. Real systems therefore assume the network is “synchronous enough”, use majorities, and keep the minority side safe but unresponsive.
at most 1نمونهٔ safety: هیچوقت بیشتر از یک رهبرa safety example: never more than one leader
eventuallyکلمهٔ کلیدی liveness؛ زمانش مهم نیست، وقوعش مهم استthe keyword of liveness; not when, but whether
quorumبیشتر سیستمها liveness را به اکثریت گرهها میدهندmost systems grant liveness to the majority
چرا نمیشود هر دو را کامل داشتWhy you cannot have both, whole
فرض کن شبکه دو نیمه شده و هر نیمه چند گره دارد. اگر هر دو نیمه اجازهٔ نوشتن داشته باشند، سیستم زنده میماند ولی ممکن است دو نسخهٔ متناقض بسازد؛ اگر فقط نیمهٔ اکثریت اجازهٔ نوشتن داشته باشد، سازگاری حفظ میشود ولی نیمهٔ دیگر تا وصل شدن شبکه بیفایده است. این همان انتخاب معروف CAP است، با این تصحیح مهم که «C» در آن به معنای سازگاری خطی است، نه سازگاری ACID. هیچ تنظیمی این معامله را حذف نمیکند؛ فقط جای آن را عوض میکند. کاری که میشود کرد این است که انتخاب را آگاهانه و برای هر عملیات جدا انجام دهی.Suppose the network has split and each side holds some nodes. If both sides may write, the system stays live and may produce two contradictory versions; if only the majority side may write, consistency holds and the other side is useless until the link returns. That is the familiar CAP choice, with the important correction that its C means linearizability, not ACID consistency. No configuration removes the trade; it only moves it. What you can do is make the choice deliberately, per operation.
انتخاب مهندسی: کدام را قربانی میکنیThe engineering choice: which one you sacrifice
برای یک عملیات بانکی، جواب روشن است: تا وقتی مطمئن نیستی دو نسخه نمیسازی، نوشتن را قبول نکن؛ کاربر چند ثانیه پشت صفحهٔ خطا میماند و این بهتر از دو موجودی متناقض است. برای یک فید اجتماعی یا شمارندهٔ لایک، جواب برعکس است: بهتر است کاربر بتواند لایک کند و بعداً دو شمارش با هم جمع شوند، تا اینکه صفحه خطا ببیند. خیلی از خرابیهای واقعی از این میآیند که یک تیم برای همهٔ عملیاتها یک انتخاب کرده است. انتخاب درست، بهازای هر عملیات، با پرسیدن «اگر دو نسخه بسازم، چه چیزی میشکند؟» به دست میآید.For a banking operation the answer is clear: do not accept the write until you are sure you will not create two versions; a user staring at an error for a few seconds beats two contradictory balances. For a social feed or a like counter the answer flips: better to let the user like now and merge two counts later than to show an error. Many real outages come from a team making one choice for every operation. The right choice is per operation, and it comes from asking “if I create two versions, what breaks?”
تلهٔ رایجThe usual trap
بهینهسازی برای liveness به قیمت safety. برای اینکه کاربر پشت صفحهٔ قرمز نماند، سمت اقلیت هم نوشتن را قبول میکند؛ شبکه که برمیگردد، دو تاریخ متفاوت داری و هیچ راه قطعی برای انتخاب نداری. برعکسش هم هست: تایماوتی که برای «زنده ماندن» کوتاه شده، رهبر دوم میسازد و همان safety را میشکند.Optimising for liveness at the cost of safety. So that nobody stares at a red page, the minority side accepts writes too; when the network returns you hold two histories and no deterministic way to pick one. The mirror error exists as well: a timeout shortened “to stay live” elects a second leader and breaks the very safety you needed.
برای اندازه گرفتن مدت، به ساعت دیواری اعتماد نکن.To measure a duration, do not trust the wall clock.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
برای اینکه بفهمی چای چقدر دم کشیده، کرنومتر گوشی را روشن میکنی، نه اینکه به ساعت دیواری نگاه کنی. ساعت دیواری ممکن است سر تغییر ساعت تابستانی جابهجا شود یا کسی درستش کند؛ کرنومتر فقط میشمارد و کاری به تاریخ ندارد. ساعت یکنوا همان کرنومتر است: ابزاری برای مدت، نه برای تاریخ.To see how long the tea has been steeping you start the stopwatch on your phone, not glance at the wall clock. The wall clock may jump at a daylight-saving change or be corrected by someone; the stopwatch just counts, and does not care what date it is. A monotonic clock is that stopwatch: a tool for durations, not for dates.
تشبیه و تصویر ذهنیThe analogy
دو نفر با دو کرنومتر مسابقهٔ دوی صد متر را زمان میگیرند. هر دو عدد درست است، ولی نمیشود آنها را با هم مقایسه کرد، چون هر کرنومتر از لحظهای متفاوت شروع شده. بدتر: اگر کرنومتر یکی وسط کار باتریاش تمام شود و از صفر شروع کند، عددش بیمعنا میشود. ساعت یکنوای هر ماشین هم همینطور است: مبدأاش معمولاً لحظهٔ بوت است، بین ماشینها مشترک نیست، و بعد از ریاستارت میتواند از صفر شروع کند. پس برای مدت عالی است و برای «کی» بیفایده.Two people time a hundred-metre race with two stopwatches. Both numbers are correct, but they cannot be compared, because each watch started at a different moment. Worse: if one watch runs out of battery mid-race and restarts from zero, its number becomes meaningless. Each machine's monotonic clock is like that: its origin is usually boot time, it is not shared between machines, and after a restart it can begin at zero again. Excellent for duration, useless for “when”.
یکی برای مدت ساخته شده و یکی برای تاریخ؛ عددشان را نمیشود جای هم گذاشت.One is built for duration, the other for dates; their numbers are not interchangeable.
و بعدش، فنیAnd now, technically
سیستمعامل دو خانوادهٔ ساعت میدهد: ساعت دیواری (مثل CLOCK_REALTIME) که زمان تقویمی را نشان میدهد و NTP میتواند آن را جلو یا عقب ببرد، و ساعت یکنوا (مثل CLOCK_MONOTONIC) که فقط برای اندازهگیری فاصله است. ساعت یکنوا مبدأ مشخصی ندارد؛ معمولاً از لحظهٔ بوت میشمارد، پس عددش بین دو ماشین یا بین دو بوت قابل مقایسه نیست. دقتش به سختافزار بستگی دارد و میتواند در حد میلیثانیه یا بدتر باشد. روی لینوکس، CLOCK_MONOTONIC با تنظیم فرکانس NTP کمی کند یا تند میشود ولی با step عقب نمیرود؛ CLOCK_MONOTONIC_RAW اصلاً دست نمیخورد. یک نکتهٔ عملی: بعضی ساعتهای یکنوا وقتی ماشین به خواب میرود نمیشمارند، پس مدت واقعی یک وقفهٔ طولانی را کم نشان میدهند؛ اگر این مهم است، از ساعتی استفاده کن که خواب را هم حساب میکند. قاعدهٔ طلایی این است: مدت را با یکنوا اندازه بگیر، تاریخ را با دیواری، و هرگز عدد یکنوای دو ماشین را با هم مقایسه نکن.An operating system exposes two clock families: the wall clock (CLOCK_REALTIME) showing calendar time, which NTP may step forwards or backwards, and the monotonic clock (CLOCK_MONOTONIC) built only to measure intervals. The monotonic clock has no fixed origin; it usually counts from boot, so its value cannot be compared across machines or across reboots. Its resolution depends on hardware and may be milliseconds or worse. On Linux, CLOCK_MONOTONIC is slightly sped up or slowed by NTP frequency adjustments but never stepped backwards; CLOCK_MONOTONIC_RAW is left alone entirely. One practical warning: some monotonic clocks stop counting while the machine is suspended, so a long sleep is under-reported; if that matters, use a clock that includes suspend. The golden rule: measure durations with the monotonic clock, dates with the wall clock, and never compare two machines' monotonic numbers.
CLOCK_MONOTONICنمونهٔ لینوکسی؛ فقط جلو میرود، مبدأ بوتthe Linux one; forward only, epoch at boot
µs–msدقت معمول؛ به سختافزار و مجازیسازی وابستهtypical resolution; depends on hardware and virtualisation
boot epochمبدأ ساعت یکنوا؛ بین ماشینها و بین بوتها مشترک نیستthe monotonic epoch; not shared across machines or reboots
چرا مبدأ مهم نیستWhy the origin does not matter
برای اندازهگیری مدت، فقط تفاضل دو خوانش مهم است، نه خودِ عدد. اگر ساعت یکنوا ۱۲۳۴۵۶۷۸ ثانیه از بوت گذشته باشد، تفاضل دو خوانش همچنان درست است. برای همین میتوانی بدون هیچ هماهنگی با دنیای بیرون، تأخیر یک درخواست، طول یک تایماوت یا فاصلهٔ دو رویداد محلی را اندازه بگیری. تنها شرط این است که هر دو خوانش از یک ماشین و یک بوت باشند؛ اگر بین آن دو، ماشین ریاستارت شود، تفاضل ممکن است منفی یا بیمعنا شود.To measure a duration only the difference between two readings matters, not the readings themselves. If the monotonic clock starts at 12,345,678 seconds since boot, the difference is still correct. That is why you can measure a request's latency, a timeout's length or the gap between two local events without any coordination with the outside world. The only condition is that both readings come from one machine and one boot; restart in between and the difference can go negative or become meaningless.
کجا گول میزندWhere it misleads
سه جا بیشتر از همه دردسر میسازد. اول، خواب و مهاجرت: ماشین مجازی که زنده منتقل میشود ممکن است ساعت یکنوایش بپرد، و لپتاپی که به خواب رفته ممکن است اصلاً نشمارد. دوم، دقت: یک تایماوت ۱۰ میلیثانیهای روی ساعتی با دقت ۱۵ میلیثانیه معنا ندارد. سوم، مقایسهٔ بین ماشینها: عدد یکنوای گرهٔ A هیچ ارتباطی با عدد گرهٔ B ندارد، حتی اگر هر دو از یک نوع سختافزار باشند. اگر چیزی مثل «کدام درخواست اول رسید» لازم داری، باید از شناسهٔ منطقی یا از قرارداد بیرونی استفاده کنی.Three places cause most of the trouble. First, sleep and migration: a live-migrated VM may jump its monotonic clock, and a suspended laptop may not count at all. Second, resolution: a ten-millisecond timeout is meaningless on a clock with fifteen-millisecond granularity. Third, cross-machine comparison: node A's monotonic number has no relation to node B's, even on identical hardware. If you need “which request arrived first”, get it from a logical id or an external agreement.
تلهٔ رایجThe usual trap
مهر زمانی رویدادها را از ساعت یکنوا گرفتن و بعد لاگهای چند سرور را با هم مرتب کردن. هر سرور از بوت خودش شمرده و مبدأها یکی نیستند، پس ترتیبی که به دست میآید بیمعناست — گاهی حتی مدت یک span منفی میشود. برای ترتیب بین ماشینها باید از شناسهٔ منطقی یا ساعت دیواری با حاشیهٔ خطا استفاده کنی، نه از یکنوا.Taking event timestamps from the monotonic clock and then sorting logs from several servers. Each server counted from its own boot, the origins differ, so the resulting order is nonsense — sometimes a span even reports a negative duration. For cross-machine order use a logical id or a wall clock with an error margin, not the monotonic clock.
هر دو طرف زندهاند و همدیگر را نمیبینند.Both sides are alive and cannot see each other.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
آیفون ساختمان خراب میشود؛ هر واحد میتواند با داخل خودش حرف بزند، ولی زنگ واحد دو در واحد یک شنیده نمیشود و برعکس. هیچکس از کار نیفتاده و هیچ دیواری فرو نریخته؛ فقط مسیر بین دو نیمه قطع شده. تقسیم شبکه همین است: هر طرف زنده، ولی پیام از مرز رد نمیشود.The building intercom breaks; each flat can still talk to its own rooms, but a ring from flat two never reaches flat one, and the other way round. Nobody lost their job, no wall fell down; only the path between the two halves is gone. A network partition is that: each side alive, and no message crossing the line.
تشبیه و تصویر ذهنیThe analogy
حالا ببین چرا این وضعیت بدتر از خرابی ساده است. اگر کل ساختمان برق نداشته باشد، همه میدانند که خراب است و کاری نمیشود کرد. ولی وقتی فقط آیفون خراب است، هر واحد فکر میکند حالش خوب است و دیگری مشکل دارد. ممکن است مسیر فقط یکطرفه قطع باشد: زنگ از واحد یک به دو میرسد و جواب برنمیگردد؛ ممکن است فقط بعضی واحدها قطع باشند، نه همه؛ و ممکن است آیفون سالم باشد ولی کسی آنقدر دیر جواب دهد که تو فکر کنی قطع است. در همهٔ این حالتها از جایی که ایستادهای، «قطع» و «کند» و «مرده» یک شکلاند.Now see why this is worse than a plain failure. If the whole building loses power, everyone knows it is broken and nothing can be done. When only the intercom dies, each flat believes it is fine and the other one has the problem. The path can fail in one direction only: a ring reaches flat two and the answer never comes back; only some flats may be cut off, not all; and the intercom may be healthy while someone answers so slowly that you conclude it is broken. In every one of these cases, from where you stand, “cut”, “slow” and “dead” look the same.
یک شبکه به دو نیمهٔ زنده میشکند؛ هر نیمه درون خودش کار میکند، ولی پیام از مرز رد نمیشود.One network splits into two live halves; each works internally, and no message crosses the line.
و بعدش، فنیAnd now, technically
پارتیشن یعنی گروهی از گرهها نمیتوانند به گروه دیگر پیام برسانند، در حالی که هر گروه درون خودش سالم است. علت میتواند خرابی سوئیچ، یک ACL اشتباه، مسیر نامتقارن، ازدحام، یا حتی توقف یک فرآیند باشد که ضربانهایش را قطع میکند؛ از بیرون همهٔ اینها یکسان دیده میشوند. تشخیص قطعی ممکن نیست، چون شبکهٔ ناهمگام تأخیر کراندار ندارد و «کند» با «جدا» فرق قابلاندازهگیری ندارد. وقتی پارتیشن رخ میدهد، سیستم مجبور است انتخاب کند: یا سمت اقلیت نوشتن را رد کند و سازگاری را نگه دارد، یا هر دو طرف بنویسند و بعداً سازگار کنند و خطر واگرایی را بپذیرد. در سیستمهای مبتنی بر کوئروم، سمت اکثریت ادامه میدهد و سمت اقلیت فقط خواندن کهنه سرو میکند یا اصلاً جواب نمیدهد. نکتهٔ مهم این است که پارتیشن با مرگ گره یکی نیست: گرهها زندهاند و کار میکنند، و بعد از وصل شدن، دو تاریخِ جدا ممکن است به هم برسند. همینجاست که split-brain و نیاز به توکن حصار و قواعد حل تعارض جدی میشوند.A partition means one group of nodes cannot deliver messages to another, while each group is healthy internally. The cause can be a failed switch, a wrong ACL, an asymmetric route, congestion, or even a process pause that silences its heartbeats; from outside they all look alike. Certain detection is impossible, because an asynchronous network has no delay bound and “slow” is not measurably different from “cut off”. When a partition happens the system must choose: have the minority side reject writes and keep consistency, or let both sides write and reconcile later, accepting divergence. In quorum systems the majority side continues and the minority serves stale reads or nothing at all. The crucial point is that a partition is not a node failure: the nodes are alive and working, and when the link returns two separate histories may meet. That is where split-brain, fencing tokens and conflict resolution become serious.
2 sidesحداقل دو گروه که درون خودشان ارتباط دارندat least two groups that still talk internally
quorumسمت اکثریت ادامه میدهد، اقلیت متوقف میشودthe majority side continues; the minority stops
CAPهنگام پارتیشن، انتخاب بین سازگاری و دسترسپذیریduring a partition, a choice between consistency and availability
چرا تشخیصش سخت استWhy detection is hard
هر گره فقط میتواند بگوید «از فلان گره خبری نگرفتم»، و این جمله دربارهٔ شبکه است، نه دربارهٔ آن گره. پارتیشن میتواند نامتقارن باشد: بستهها در یک جهت میروند و برنمیگردند. میتواند جزئی باشد: فقط چند میزبان از یک رک به رک دیگر نمیرسند. میتواند موقتی باشد: یک مسیر BGP چند ثانیه اشتباه اعلام شود و برگردد. سیستم پایش هم از همان شبکه استفاده میکند، پس وقتی شبکه میشکند، ابزار تو هم کور میشود و ممکن است هر دو طرف را سالم نشان دهد یا هر دو را مرده.All a node can say is “I heard nothing from that node”, and that sentence is about the network, not about the node. A partition can be asymmetric: packets go one way and never return. It can be partial: only a few hosts cannot reach the next rack. It can be brief: a BGP route is misannounced for a few seconds and comes back. Your monitoring uses the same network, so when the network breaks your tools go blind too, and may show both sides healthy or both sides dead.
بعد از وصل شدن چه میشودWhat happens after the link returns
پارتیشن مثل یک زخم است: وقتی شبکه برمیگردد، دو طرف دوباره همدیگر را میبینند و حالا باید بفهمند چه چیزی را از دست دادهاند. اگر هر دو طرف نوشته باشند، رکوردهایی با نسخههای متناقض داری و باید تصمیم بگیری کدام برنده است. last-write-wins ساده است، ولی بر اساس ساعت است و میتواند نوشتن درست را دور بریزد. ساختارهایی مثل CRDT و شمارندههای توزیعشده میتوانند بدون تصمیم انسانی ادغام شوند، به قیمت محدود شدن به عملیات خاص. برای پول و موجودی، هیچ ادغام خودکاری وجود ندارد و باید یا از اول نوشتن سمت اقلیت را بسته باشی، یا بعداً با یک فرایند انسانی و دفتر تراکنشها تطبیق بدهی.A partition leaves a wound: when the link returns the two sides see each other again and must work out what they missed. If both sides wrote, you hold records with contradictory versions and need a rule for which wins. Last-write-wins is simple but relies on clocks and can discard the correct write. Structures such as CRDTs and distributed counters can merge without a human decision, at the price of being limited to particular operations. For money and inventory there is no automatic merge: either you closed the minority side to writes up front, or a human process reconciles against the transaction ledger afterwards.
تلهٔ رایجThe usual trap
انتخاب اکثریت بهعنوان برنده، ولی اجازه دادن به اقلیت برای نوشتن «فقط این دفعه». کاربر پشت صفحهٔ خطا نمیماند و همه خوشحالاند، تا روزی که شبکه وصل میشود و دو نسخه از یک رکورد داری که هیچکدام نمیداند کدام درست است. اگر اقلیت نوشت، باید یک قانون حل تعارض قطعی داشته باشی، نه یک امید.Picking the majority as the winner while letting the minority write “just this once”. Nobody stares at an error page and everyone is happy, until the link returns and you hold two versions of one record with no way to tell which is right. If the minority writes, you need a deterministic conflict rule, not a hope.
ساعت گوشیات از کجا میداند ساعت چند است؟How does your phone know what time it is?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
در ایستگاه راهآهن، ساعت بزرگ روی سکو را نگاه میکنی و ساعت مچیات را کوک میکنی. اما تا برگردی سر جایت چند دقیقه گذشته، پس ساعت تو به همان اندازه عقب است. هر چه دورتر باشی، خطا بیشتر. NTP هم همین کار را میکند، فقط در چند هزارم ثانیه و با حساب کردن همان رفتوبرگشت.At the railway station you read the big platform clock and set your watch by it. But it takes you a few minutes to walk back to your seat, so your watch is that far behind. The further away you are, the bigger the error. NTP does the same thing, in milliseconds, by accounting for the trip there and back.
تشبیه و تصویر ذهنیThe analogy
جوابی که از ساعت مرجع میگیری، تا لحظهای که به تو میرسد کهنه شده است. برای همین نصف زمان رفتوبرگشت را به آن اضافه میکنی؛ ولی این فقط یک تخمین است و خطایش به همان رفتوبرگشت گره خورده. تازه، خود ساعت مرجع هم ممکن است اشتباه باشد، پس عاقلانه است چند ساعت را بپرسی و آن یکی را که با بقیه نمیخواند کنار بگذاری. و وقتی اختلاف کوچک است، ساعت را یکجا عوض نمیکنند؛ آرام کمی تندترش میکنند تا وقت حرکت نپرد.The answer you get from a reference clock is already stale by the time it reaches you. So you add half the round trip — but that is only an estimate, and its error is tied to that same round trip. The reference itself may be wrong, too, so the sane move is to ask several clocks and discard the one that disagrees with the rest. And when the difference is small, the clock is not jumped; it is nudged slightly faster until it catches up.
NTP با یک رفتوبرگشت اختلاف ساعت را تخمین میزند؛ خطا هم به همان رفتوبرگشت گره خورده است.NTP estimates the offset from one round trip, and its error is tied to that same round trip.
و بعدش، فنیAnd now, technically
کلاینت زمان t1 را میفرستد، سرور t2 و t3 را برمیگرداند و کلاینت t4 را ثبت میکند؛ اختلاف تقریباً ((t2−t1)+(t3−t4)) تقسیم بر دو است و تأخیر مسیر (t4−t1) منهای (t3−t2). دقت این تخمین به عدم تقارن مسیر گره خورده است، نه به پروتکل: اگر رفت و برگشت زمان یکسانی نبرند، خطا همانجا مینشیند. کلاینتها معمولاً چند سرور را میپرسند و با رأیگیری، سرورهایی را که با بقیه نمیخوانند کنار میگذارند؛ فاصله از ساعت مرجع را با stratum میسنجند. تنظیم دو حالت دارد: اگر اختلاف کوچک باشد ساعت را آرام کمی تندتر یا کندتر میکنند (slew) تا زمان نپرد، و اگر بزرگ باشد یکجا میپرند (step) — و همان پرش میتواند هر چیزی را که به ترتیب زمانی تکیه کرده بود بشکند. NTP بهصورت پیشفرض احراز هویت ندارد و میتواند هدف جعل باشد؛ نسخهٔ امنترش NTS است.The client sends t1, the server returns t2 and t3, and the client records t4; the offset is roughly ((t2−t1)+(t3−t4)) divided by two, and the path delay is (t4−t1) minus (t3−t2). The accuracy of that estimate is tied to path asymmetry, not to the protocol: if the two directions take different times, the error lands right there. Clients usually ask several servers and vote out the ones that disagree with the rest; distance from the reference is expressed as a stratum. Correction has two modes: for a small difference the clock is nudged faster or slower (slewing) so time does not jump, and for a large one it is stepped — and that step can break anything that relied on time being ordered. NTP is unauthenticated by default and can be spoofed; NTS is the hardened variant.
≈ 1–10 msدقت معمول روی اینترنت عمومیtypical accuracy across the public internet
< 1 msدر شبکهٔ محلی با سرور نزدیکon a LAN with a nearby server
RTT ÷ 2کف خطای ممکن؛ از رفتوبرگشت پایینتر نمیآیدthe error floor set by the round trip
تلهٔ رایجThe usual trap
فرض کردن اینکه NTP «ساعتِ درست» میدهد. NTP یک تخمین با خطای نامعلوم میدهد: مسیر نامتقارن، سرور اشتباه، یا یک step به عقب کافی است تا هر چیزی که به ساعت دیواری تکیه کرده بود بشکند — از مهلتها تا «آخرین نوشتن برنده است». برای فاصلهٔ زمانی از ساعت یکنوا استفاده کن و NTP را فقط برای برچسب زمان بخواه.Assuming NTP hands you the correct time. It hands you an estimate with an unstated error: an asymmetric path, a wrong server, or one backward step is enough to break everything that leaned on the wall clock — from timeouts to last-write-wins. Measure durations with a monotonic clock and ask NTP only for timestamps.
نیمی از سیستم مرده. کدام نیمه؟Half the system is down. Which half?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
در خانه، فیوز یک اتاق میپرد و بقیهٔ خانه روشن میماند. یخچال آشپزخانه کار میکند، ولی فریزرِ انبار خاموش است و تو تا صبح نمیفهمی. هیچ صدایی نمیآید و هیچ چیزی کاملاً خراب نیست؛ خرابی جزئی همین است: بخشی از سیستم مرده، بقیه سرحال، و تو از بیرون فقط یک نمای نصفه میبینی.At home one room's fuse blows and the rest of the house stays lit. The kitchen fridge hums along, the chest freezer in the storeroom is off, and you will not notice until morning. Nothing makes a sound and nothing is completely broken; partial failure is that: part of the system is dead, the rest is fine, and from outside you only get half a view.
تشبیه و تصویر ذهنیThe analogy
یک کامپیوتر تنها معمولاً یا کار میکند یا نیست: برنامه کرش میکند، صفحه آبی میشود، همهچیز میایستد. در چند ماشین اینطور نیست. یک گره ممکن است کند باشد، یکی دیسکش در حال مرگ باشد، یکی فقط مسیر شبکهایاش قطع شده باشد، و هیچکدام از اینها از دید بقیه «خرابی کامل» نباشد. بدتر، تو معمولاً نمیفهمی کدام اتفاق افتاده: درخواستی که فرستادی ممکن است انجام شده باشد و جوابش گم شده باشد، یا انجام نشده باشد و تو منتظر بمانی. از دید تو این دو حالت دقیقاً یکی است، و همین ابهام است که کار را سخت میکند.A single computer usually either works or does not: the program crashes, the screen goes blue, everything stops. With several machines that is not so. One node may be slow, another may have a dying disk, another may have lost only its network route, and none of these is a total failure from where the others stand. Worse, you usually cannot tell which happened: the request you sent may have completed with its reply lost, or may never have run while you wait. From your seat those two states are identical, and that ambiguity is what makes the work hard.
یک ماشین یا کار میکند یا نه؛ چند ماشین همزمان هم کار میکنند و هم نمیکنند، و نتیجه نامعلوم است.One machine works or it does not; several machines work and fail at once, and the outcome is unknown.
و بعدش، فنیAnd now, technically
در یک ماشین، خرابی معمولاً کلی و قابلتشخیص است: فرآیند میمیرد، هسته panic میکند، و کد یا اجرا میشود یا نه. در یک سیستم توزیعشده، خرابی جزئی است و ماهیتاً نامعین: بعضی اجزا از کار میافتند و بقیه ادامه میدهند، و از دید هر گره فقط یک نمای محلی وجود دارد. پیامی که فرستادی ممکن است گم شود، دیر برسد، یا برسد و جوابش گم شود؛ در حالت سوم کار انجام شده ولی تو نمیدانی. هیچ ساعت مشترک و هیچ حافظهٔ مشترکی نیست که از آن حقیقت را بپرسی، پس «خرابی» و «کندی» و «قطعی موقت» از هم قابل تفکیک نیستند. به همین دلیل طراحی برای سیستم توزیعشده یعنی طراحی برای ابهام، نه فقط برای خطا: تایماوت، retry، idempotency، و بازبینی وضعیت از یک منبع دیگر. هر جا کدی نوشتهای که فرض میکند خطا یعنی «کار انجام نشد»، یک باگ پنهان گذاشتهای.On one machine a failure is usually total and detectable: the process dies, the kernel panics, and code either runs or does not. In a distributed system the failure is partial and inherently nondeterministic: some components fail while the rest continue, and each node holds only a local view. A message you sent may be lost, arrive late, or arrive with its reply lost; in the third case the work happened and you cannot know. There is no shared clock and no shared memory to consult for the truth, so “failed”, “slow” and “briefly cut off” cannot be separated. Designing for a distributed system therefore means designing for ambiguity, not merely for errors: timeouts, retries, idempotency, and checking state from another source. Anywhere you wrote code that assumes an error means “nothing happened”, you planted a bug.
1 viewهر گره فقط نمای خودش را دارد، نه کل سیستمeach node has only its own view, never the whole
∞تأخیر شبکه کران ندارد، پس کند و مرده یکی به نظر میرسندnetwork delay is unbounded, so slow and dead look alike
چرا از بیرون نمیشود فهمید چه شدWhy the outside cannot tell what happened
درخواستی میفرستی و بعد از پنج ثانیه جوابی نمیآید. چهار حالت ممکن است: درخواست هرگز نرسیده؛ رسیده و رد شده؛ رسیده و انجام شده ولی جواب گم شده؛ یا هنوز در صف است و بعداً انجام میشود. از دید تو هر چهار حالت یک چیز است: سکوت. لاگ سرور هم کمک قطعی نمیکند، چون ممکن است سرور مرده باشد یا لاگش هنوز نرسیده باشد. تنها راه این است که وضعیت را از یک منبع دیگر بخوانی و آن هم خودش میتواند همین ابهام را داشته باشد. برای همین «تایماوت» هیچوقت یک جواب منفی نیست.You send a request and five seconds later nothing has come back. Four states are possible: it never arrived; it arrived and was rejected; it arrived and ran but the reply was lost; or it is still queued and will run later. From where you sit all four are one thing: silence. The server log does not settle it either, because the server may be dead or its log may not have arrived. The only route is to read the state from another source, and that source can carry the same ambiguity. That is why a timeout is never a negative answer.
طراحی برای ابهامDesigning for ambiguity
اگر نمیتوانی بفهمی کار انجام شد یا نه، باید طراحی را طوری بچینی که جواب این سؤال کمتر مهم باشد. هر عملیات را idempotent کن تا فرستادن دوباره بیخطر باشد؛ هر درخواست را با شناسهٔ یکتا بفرست تا گیرنده بتواند تکراری را تشخیص دهد؛ و برای هر تصمیم مهم، یک منبع حقیقت داشته باش که بتوانی از آن بازخوانی کنی. در معماریهای رویدادمحور، همین ایده به «حداقل یک بار تحویل بهعلاوهٔ حذف تکراری» تبدیل میشود. هدف این نیست که ابهام را از بین ببری — نمیشود — بلکه این است که سیستم بتواند با آن زندگی کند.If you cannot learn whether the work happened, arrange the design so the question matters less. Make every operation idempotent so resending is safe; send a unique id with every request so the receiver can spot a duplicate; and for each important decision keep a source of truth you can re-read. In event-driven architectures this becomes “at-least-once delivery plus deduplication”. The goal is not to remove the ambiguity — that is impossible — but to let the system live with it.
تلهٔ رایجThe usual trap
فرض اینکه خطا یعنی «انجام نشد». کد تایماوت میگیرد، پیام خطا نشان میدهد، و در همان لحظه سرور داشته کار را تمام میکرده؛ کاربر دوباره میزند و کار دو بار انجام میشود. هر جایی که «موفق نشد» را با «اثر نگذاشت» یکی میگیری، همین باگ را ساختهای.Assuming an error means “it did not happen”. The code times out, shows a failure message, and at that exact moment the server was finishing the work; the user tries again and now it happens twice. Everywhere you equate “did not succeed” with “left no effect”, you have built this bug.
یک لحظه ایستاد. در آن لحظه دنیا عوض شد.It stopped for a moment. In that moment the world moved on.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
در صف نانوایی ایستادهای و یک لحظه غرق گوشی میشوی؛ سر بلند میکنی و میبینی صف جلو رفته و نوبت تو رد شده. تو از صف بیرون نرفته بودی و نمرده بودی؛ فقط برای چند ثانیه هیچ کاری نکردی و دنیا در همان ثانیهها جلو رفت. توقف فرآیند هم همین است: کد زنده است، ولی برای مدتی هیچ دستوری اجرا نمیکند.You are in the bakery queue and get absorbed in your phone for a moment; you look up to find the queue has moved and your turn is gone. You had not left the queue and you were not dead; for a few seconds you simply did nothing, and the world moved on in those seconds. A process pause is that: the code is alive, but for a while not one instruction runs.
تشبیه و تصویر ذهنیThe analogy
این موضوع در یک ماشین معمولی مهم نیست، ولی در سیستم توزیعشده کشنده است. وقتی تنها کد خودت را داری، توقف یعنی کندی؛ کسی جز تو منتظر نیست. ولی وقتی تو رهبری، چند صد کلاینت منتظر جواباند و قرار است هر ثانیه ضربان بفرستی، همان توقف کوچک یعنی ضربان نرسیدن، یعنی مشکوک شدن بقیه، یعنی رهبر جدید. وقتی برمیگردی، حافظهات همان چیزی است که قبل از توقف بود: خودت را رهبر میدانی، قفل را مال خودت میدانی، و هیچکس به تو نگفته که در این فاصله عوض شده. بدترین قسمت همین بیداری است، نه خود توقف.This hardly matters on one machine and is lethal in a distributed one. When you own the only code, a pause is just slowness; nobody but you is waiting. When you are the leader, hundreds of clients are waiting and you are supposed to send a heartbeat every second, so the same small pause means no heartbeat, which means the others grow suspicious, which means a new leader. When you return, your memory is exactly what it was before the pause: you believe you lead, you believe the lock is yours, and nobody has told you that the world changed. The waking is the dangerous part, not the pause.
کد اجرا میشود، متوقف میشود، دنیا در همان بازه جلو میرود و برگشت با حالت کهنه انجام میشود.Code runs, freezes, the world advances during the gap, and the return happens with stale state.
و بعدش، فنیAnd now, technically
یک نخ میتواند بین هر دو دستور از CPU کنار گذاشته شود، و فرآیند هیچ حسّی از مدت آن ندارد؛ برای او انگار زمان نگذشته. منابع این توقف زیادند: GC با توقف کل دنیا، که با heapهای بزرگ میتواند صدها میلیثانیه یا حتی چند ثانیه طول بکشد؛ مهاجرت زندهٔ ماشین مجازی؛ ازدحام CPU روی هاست مشترک؛ swap؛ سیگنال توقف؛ و خوابیدن لپتاپ. در این فاصله ضربانها نمیرسند، اجارهنامه منقضی میشود، و اتصالهای TCP ممکن است ریست شوند. خطر اصلی این است که فرآیند بعد از بیدار شدن از همان فرضهای قبلی ادامه میدهد: «من رهبرم»، «قفل مال من است»، «این رکورد عوض نشده». هیچکدام از اینها را نمیتواند با نگاه کردن به ساعت محلی تأیید کند، چون ساعت محلی هم میتواند بیدقت باشد و توقف را ندیده باشد. راه درست این است که هر پروتکلی را طوری بنویسی که توقف در هر نقطه ممکن باشد و ایمنی به آن وابسته نباشد: توکن حصار برای نوشتن، تمدید اجاره پیش از انقضا، و کنار کشیدن وقتی تمدید شکست خورد.A thread can be descheduled between any two instructions, and the process has no sense of how long it was away; for it, no time passed. The sources are many: stop-the-world garbage collection, which on large heaps can take hundreds of milliseconds or several seconds; live VM migration; CPU contention on a shared host; swapping; a stop signal; a laptop going to sleep. During the gap heartbeats do not arrive, leases expire, and TCP connections may be reset. The real danger is that the process resumes from its old assumptions: “I am the leader”, “the lock is mine”, “this record has not changed”. None of these can be confirmed by reading the local clock, which may itself be coarse and may not have noticed the pause. The right response is to write every protocol so that a pause anywhere is possible and safety does not depend on its absence: fencing tokens for writes, renewal well before expiry, and standing down when renewal fails.
∞طول توقف کران ندارد؛ هر لحظه ممکن استpause length is unbounded; it can happen at any point
≈ 100 msتوقف معمول GC با heap بزرگ؛ گاهی چند ثانیهa typical stop-the-world pause; sometimes seconds
0دستور اجراشده در طول توقف؛ حتی یکیinstructions executed during the pause: none
چرا سیستم در آن مدت خیلی جلو میرودHow far the system travels in that gap
با یک مثال عددی ببین. اجارهٔ رهبر ده ثانیه است و رهبر در ثانیهٔ دوم برای چهل ثانیه متوقف میشود. در ثانیهٔ دهم اجاره تمام میشود، در ثانیهٔ دوازدهم گرههای دیگر مشکوک میشوند، و در ثانیهٔ پانزدهم رهبر جدید انتخاب میشود و کار را شروع میکند. تا ثانیهٔ چهلودوم، رهبر جدید بیستوهفت ثانیه کار کرده است. رهبر قدیمی در همان ثانیه بیدار میشود و اولین کاری که میکند، نوشتن است. در تمام این مدت، از دید خودش هیچ زمانی نگذشته است.Watch it with numbers. The leader's lease is ten seconds and in its second second it freezes for forty. At second ten the lease expires, at second twelve the other nodes grow suspicious, and at second fifteen a new leader is elected and starts work. By second forty-two the new leader has worked for twenty-seven seconds. The old leader wakes at that same second and the first thing it does is write. For the whole of that time, from its own point of view, no time passed at all.
چه چیزی را باید فرض کردWhat you have to assume
فرض کن بین هر دو دستور ممکن است توقف بیاید، و پروتکل را بر همین بنا کن. معنایش این است: قفل را در طول کار بیکران نگه ندار؛ بخش بحرانی را کوتاه کن؛ پیش از انقضای اجاره تمدید کن؛ و اگر تمدید شکست خورد، همان لحظه دست از نوشتن بکش. اما چون خودِ «همان لحظه» ممکن است در توقف باشد، لایهٔ آخر همیشه باید در سمت منبع داده باشد: توکنی که با هر نوشتن چک شود. با این ترتیب، توقف طولانی فقط یک کندی است، نه یک فاجعه.Assume a pause may land between any two instructions, and build the protocol on that. It means: do not hold a lock across unbounded work; keep the critical section short; renew before the lease expires; and when renewal fails, stop writing at once. But because “at once” may itself be inside a pause, the last layer must always sit at the resource: a token checked on every write. With that in place a long pause is only slowness, not a disaster.
تلهٔ رایجThe usual trap
فرض اینکه «اگر کد من اجرا نشده، پس هیچ اتفاقی نیفتاده». رهبر برای چهل ثانیه در GC میماند، بقیه رهبر جدید انتخاب میکنند، و او بیدار میشود و اولین نوشتنش را با همان توکن قدیمی میفرستد. اگر منبع داده توکن را چک نکند، نوشتهٔ رهبر جدید بیصدا بازنویسی میشود و هیچ خطایی هم دیده نمیشود.Assuming that “my code did not run, so nothing happened”. The leader spends forty seconds in garbage collection, the others elect a new leader, and it wakes up and sends its first write with the old token. If the resource does not check the token, the new leader's write is silently overwritten and no error appears anywhere.
دوباره بزنی، یا رهایش کنی؟Press it again, or let it go?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
پیامت را فرستادی و کنارش یک دایرهٔ قرمز نشست: نرسید. یک دقیقه بعد همان متن را دوباره میفرستی و این بار میرود. کاری که تازه کردی retry است: همان درخواست، دوباره، تا جواب بیاید. تنها چیزی که هنوز نمیدانی این است که بار اول واقعاً نرسیده بود یا رسیده بود و فقط خبرِ رسیدنش برنگشت — و همین یک تردید، همان چیزی است که retry را از یک کار ساده به یک تصمیم تبدیل میکند.You send the message and a small red circle appears beside it: not delivered. A minute later you send the same text again, and this time it goes. That is a retry: the same request, again, until an answer comes. The one thing you still do not know is whether the first attempt truly failed, or arrived with only its receipt lost — and that single doubt is what turns a retry from a simple act into a decision.
تشبیه و تصویر ذهنیThe analogy
صبح سرد، ماشین قدیمی. سوییچ را میچرخی، یک سرفه میکند و روشن نمیشود؛ دوباره میچرخی و بار سوم بالاخره میگیرد. هر تلاش اما مجانی نیست: هر بار باتری خالیتر میشود، و اگر بیست بار پشت سر هم بزنی، دیگر حتی چراغ هم روشن نمیشود. تازه، اگر مسئله تمام شدن بنزین باشد، هزار بار هم که بزنی فایده ندارد. retry هم دقیقاً همین سه سؤال را از تو میپرسد: چند بار، با چه فاصلهای، و آیا تلاش دوباره چیزی را که یک بار انجام شده دوباره انجام میدهد یا نه.A cold morning and an old car. You turn the key; it coughs and will not start. You turn it again, and on the third try it catches. No attempt is free: every turn drains the battery a little more, and twenty tries in a row leave you with not even a dashboard light. And if the real problem is an empty tank, a thousand tries change nothing. A retry policy asks you exactly those three questions: how many, how far apart, and whether trying again re-does something that already happened.
retry یک حلقه است: خطا، انتظار، همان درخواست دوباره — و هر دور، سؤال تکرارِ بیخطر را بلندتر میکند.A retry is a loop: error, wait, same request again — and each turn makes the duplicate question louder.
و بعدش، فنیAnd now, technically
در سطح پروتکل، retry یعنی وقتی درخواست با خطا یا با تمام شدن مهلت برمیگردد، کلاینت همان درخواست را دوباره بفرستد. اما «خطا» یک چیز نیست: خطای گذرا مثل قطع اتصال، مهلت تمامشده یا پاسخ ۵۰۳ یعنی شاید تلاش بعدی جواب بدهد؛ خطای دائمی مثل ۴۰۰ یا خطای اعتبارسنجی یعنی هر تلاش دیگر فقط بار اضافه است. پس تصمیم اول، دستهبندی خطاهاست. تصمیم دوم سقف تلاش و فاصلهٔ بین تلاشها است، که معمولاً نمایی رشد میکند و با jitter تصادفی میشود تا کلاینتها با هم نکوبند. تصمیم سوم و مهمترین، تکرارپذیری است: retry فقط روی عملیاتی امن است که تکرارش اثر اضافه ندارد، یا روی عملیاتی که کلید idempotency دارد و سرور با آن تکرار را میشناسد. بدون این، retry تضمین «حداقل یک بار» را به «شاید چند بار» تبدیل میکند. یک قاعدهٔ عملیاتی هم هست: retry را در یک لایه بگذار. اگر کلاینت، دروازه و سرویس هر سه دو بار تلاش کنند، یک درخواست هشت درخواست میشود، و این دقیقاً وقتی میافتد که سیستم از قبل تحت فشار است.At the protocol level, a retry means that when a request comes back as an error or runs out of time, the client sends the same request again. But an error is not one thing. A transient error — a dropped connection, an expired deadline, a 503 — may well succeed on the next attempt; a permanent one, such as a 400 or a validation failure, only adds load each time. So the first decision is how to classify errors. The second is the attempt cap and the spacing between attempts, which usually grows exponentially and is randomized with jitter so that clients do not knock in unison. The third and most important is whether the operation is repeatable: retry is safe on operations whose repetition adds nothing, or on operations carrying an idempotency key that lets the server recognize a repeat. Without that, retry turns an at-least-once promise into maybe-several-times. There is an operational rule too: retry at one layer only. If the client, the gateway and the service each try twice, one request becomes eight, and it happens exactly when the system is already under strain.
3–5تعداد معمول تلاشها پیش از تسلیم شدنa typical attempt count before giving up
2³ = 8retry در سه لایه: یک درخواست، هشت درخواستretrying at three layers turns one request into eight
1 idempotency keyکلیدی که سرور با آن تکرار را میشناسدthe key the server uses to recognize a repeat
کدام خطا ارزش تلاش دوباره داردWhich errors deserve another try
نه هر خطایی گذراست و نه هر خطای گذرا ارزش تکرار دارد. قطع اتصال، مهلت تمامشده و پاسخ ۵۰۳ معمولاً گذرا هستند؛ ۴۰۰ و خطای اعتبارسنجی دائمیاند و تلاش دوباره فقط مصرف منابع است. اما حتی در خطای گذرا هم باید بپرسی آیا این درخواست را میتوان بیخطر تکرار کرد. بدتر از همه، retry روی خطاهایی است که خودشان نشانهٔ اضافهبارند: وقتی سرویس با ۵۰۳ میگوید «جا ندارم»، سه تلاش فوری یعنی سه برابر فشار روی سرویسی که از قبل نفس ندارد. برای همین retry باید بودجه داشته باشد، نه فقط سقف.Not every error is transient, and not every transient error deserves another try. A dropped connection, an expired deadline and a 503 usually are transient; a 400 or a validation failure is permanent, and retrying it only burns resources. Even on a transient error you still have to ask whether this request can be repeated safely. Worst of all is retrying errors that are themselves a signal of overload: when a service answers 503 to say it has no room, three immediate attempts mean three times the pressure on something already out of breath. That is why a retry needs a budget, not just a cap.
طوفان تلاش، و لحظهای که خودت خرابی را بزرگ میکنیRetry storms, and the moment you enlarge the outage
بدترین خرابیها با retry بزرگ میشوند. سرویس کند شده و مهلت کلاینتها تمام میشود؛ کلاینتها فوراً دوباره میفرستند، پس تعداد درخواستهایی که سیستم باید جواب بدهد چند برابر میشود، پس کندتر میشود، پس مهلت بیشتری تمام میشود. این حلقه اسم دارد: خرابی متاستاتیک. سه چیز آن را میشکند: فاصلهٔ نمایی با jitter تا تلاشها پخش شوند، بودجهای که وقتی سهم تلاشها از حدی گذشت retry را ببندد، و قطعکنندهٔ مدار که بعد از چند خطای پشت سر هم برای مدتی کلاً نپرسد. هیچکدام از اینها در کتابخانه پیشفرض نیست؛ باید خودت بگذاری.The worst outages grow through retries. A service gets slow and client deadlines expire; clients resend at once, so the number of requests the system must answer multiplies, so it gets slower, so more deadlines expire. The loop has a name: a metastable failure. Three things break it — exponential spacing with jitter so attempts spread out, a budget that stops retrying once attempts pass a share of traffic, and a circuit breaker that stops asking entirely for a while after repeated failures. None of these is the library default; you have to put them there.
تلهٔ رایجThe usual trap
تلاش دوباره روی کاری که تکرارپذیر نیست. پرداخت را میفرستی، مهلت تمام میشود، دوباره میفرستی — و اگر سرور کلید یکتا نداشته باشد، مشتری دو بار پول داده و تو هر دو را «موفق» ثبت کردهای. تلاش دوباره درمانِ خطاست؛ ولی اگر عمل تکرارپذیر نباشد، خودش خطاست.Retrying work that is not repeatable. You send a payment, the deadline expires, you send it again — and if the server holds no unique key, the customer has paid twice and you have recorded both as successful. A retry is the cure for an error; when the operation cannot be repeated safely, it is the error.
چه چیزی هرگز نباید اتفاق بیفتد؟What must never happen?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
کنار خط آهن ایستادهای و راهبند پایین است. قاعده ساده است: تا وقتی قطار میآید، راهبند پایین میماند. اگر یک روز راهبند بالا باشد و قطار بگذرد، لازم نیست سالها صبر کنی تا بفهمی چیزی خراب است؛ همان یک ثانیه کافی است. safety همین شکل است: یک «هرگز» که با یک لحظه نقض میشود.You are stopped at a level crossing and the barrier is down. The rule is simple: while a train is coming, the barrier stays down. If one day the barrier is up and a train passes, you do not need years to notice something is broken; that one second is enough. Safety has this shape: a never, broken by a single moment.
تشبیه و تصویر ذهنیThe analogy
از یک سیستم دو جور قول میتوانی بگیری. قول اول میگوید «هیچوقت دو نفر با یک شمارهٔ نوبت جواب نمیگیرند»؛ برای رد کردنش یک لحظه کافی است — دو نفر با شمارهٔ ۴۷، همان بعدازظهر. قول دوم میگوید «بالاخره نوبت همه میشود»؛ این را با هیچ لحظهای نمیتوانی رد کنی، چون شاید نوبت تو همین حالا در صف باشد و فقط گذشت زمان نشانش میدهد. اولی safety است و دومی liveness. اشتباه رایج این است که فکر کنی دومی ضعیفتر است؛ نه، جنس دیگری است: یکی دربارهٔ چیزهایی است که نباید رخ دهد، دیگری دربارهٔ چیزی که بالاخره باید رخ دهد.A system can make you two very different promises. The first says two people never get served under the same queue number; to disprove it you need one moment — two people holding number 47, that same afternoon. The second says everyone's turn comes eventually; no single moment can disprove it, because your turn may be next in line and only the passage of time can settle it. The first is safety, the second liveness. The common mistake is to think the second is the weaker promise. It is not weaker; it is a different kind: one is about what must never happen, the other about what must happen in the end.
ایمنی با یک لحظه نقض میشود؛ زندهمانی فقط با گذشت زمان.Safety breaks in one moment; liveness only breaks across time.
و بعدش، فنیAnd now, technically
در تعریف دقیق، safety یک خاصیت روی اجراهای یک سیستم است که میگوید در هیچ پیشوندِ متناهی از اجرا، «چیز بد» رخ نمیدهد. همین «پیشوند متناهی» کل ماجراست: اگر ایمنی بشکند، همیشه میتوانی انگشت بگذاری روی لحظهای که شکسته — دو رهبر همزمان، موجودی منفی، رکورد کامیتشدهای که بعد از بازیابی نیست. به همین دلیل ایمنی را میتوان با قید و تراکنش تضمین کرد: قید یکتا نمیگذارد دو رکورد با یک کلید بنشینند، تراکنش سریال نمیگذارد دو نوشتن همزمان همدیگر را نبینند، و اشتراک اکثریت در quorum نمیگذارد خواندن، نوشتنِ تأییدشدهای را ندیده بگیرد. اما یک تلهٔ تعریفی هم دارد: سیستمی که هیچوقت جواب نمیدهد، از نظر ایمنی بیعیب است. ایمنی بدون زندهمانی تعریفاً درست و عملاً بیفایده است، و برای همین هر طراحی باید هر دو را با هم بخواهد.Strictly, safety is a property of a system's executions saying that no finite prefix of any execution contains the bad thing. That phrase — finite prefix — is the whole story: when safety breaks, you can always point at the moment it broke. Two leaders at once, a negative balance, a committed record missing after recovery. That is why safety can be enforced with constraints and transactions: a unique constraint keeps two rows from sharing a key, a serializable transaction keeps concurrent writers from missing each other, and quorum overlap keeps a read from overlooking an acknowledged write. It also has a definitional trap: a system that never answers is perfectly safe. Safety without liveness is correct by definition and useless in practice, which is why every design has to want both.
W + R > Nشرط ایمنی خواندن و نوشتن روی چند نسخهthe safety condition for reads and writes across replicas
1 لحظهبرای نقض ایمنی، یک لحظه کافی استone moment is enough to violate safety
∞نقض liveness فقط در اجرای بینهایت دیده میشودa liveness violation only appears over an infinite run
ایمنی را همانجا بگذار که همهٔ نوشتنها از آن رد میشوندPut safety where every write has to pass
اگر قید را در کد برنامه چک کنی، بین خواندن و نوشتن یک پنجره هست و دو درخواست همزمان میتوانند هر دو از آن رد شوند. همین یک پنجره کافی است تا موجودی منفی شود یا یک نام کاربری دو بار ثبت شود، هر چند کدت «درست» به نظر برسد. جایی که همهٔ نوشتنها از آن رد میشوند — قید یکتای پایگاه داده، تراکنش سریال، قفل ردیف — تنها جایی است که این پنجره را میبندد، چون آنجا همهٔ نویسندهها پشت یک صف میایستند. هر چه چک را بالاتر ببری، راحتتر مینویسی و کمتر تضمین میگیری.If you check the rule in application code, there is a window between the read and the write, and two concurrent requests can both slip through it. That one window is enough to drive a balance negative or register a username twice, however correct the code looks. The place every write must pass — a unique constraint, a serializable transaction, a row lock — is the only place that closes the window, because there all writers queue up. The higher you move the check, the easier the code and the weaker the guarantee.
بهای ایمنی: وقتی سیستم جواب نمیدهد تا خطا نکندThe price: a system that refuses to answer rather than be wrong
تضمین ایمنی مجانی نیست. اگر برای نگه داشتن یک نابردار لازم است اکثریت را ببینی و شبکه دو نیم شده باشد، آن نیمهٔ اقلیت فقط یک راه دارد: ننوشتن. سیستم ایمن میماند و از دسترس خارج میشود، و این انتخاب درست است اگر نابردار پول باشد. اما همان انتخاب برای «تعداد لایک» غلط است؛ آنجا میتوانی موقتاً ایمنی را شل کنی و بعداً جمع کنی. کار مهندسی این است که برای هر نابردار جدا تصمیم بگیری، نه اینکه یک سیاست کلی برای همه چیز بگذاری.Safety is not free. If holding an invariant requires seeing a majority and the network has split in two, the minority side has exactly one option: refuse to write. The system stays safe and goes unavailable, and that is the right call when the invariant is money. The same call is wrong for a like counter, where you can relax safety for a while and reconcile later. The engineering work is deciding this per invariant, not setting one global policy for everything.
تلهٔ رایجThe usual trap
شاهدِ ندیدن را شاهد دانستن. ماهها کار میکند و هیچوقت دو رهبر ندیدهای، پس خیالت راحت است؛ ولی نقض ایمنی در یک درهمتنیدگی نادر زندگی میکند — دو نوشتن همزمان، یک partition، ساعتی که عقب میرود. ایمنی را با استدلال و با گذاشتن قید جایی که همهٔ نوشتنها از آن رد میشوند تأیید کن، نه با مدت آپتایم.Treating the absence of a witness as evidence. It has run for months and you have never seen two leaders, so you relax; but a safety violation lives in a rare interleaving — two concurrent writes, one partition, a clock that steps back. Verify safety by argument and by a constraint where every write must pass, not by uptime.
فرضت دربارهٔ خرابی چیست؟What do you assume can go wrong?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
به دوستت میگویی «ساعت ۸ آنجا هستم»، و این را بر اساس ترافیک همیشگی میگویی. اگر بزرگراه بسته شود و دیر برسی، دروغ نگفتهای؛ فرضت غلط بوده. مشکل اینجاست که آن فرض را بلند نگفتی، پس دوستت «۸» را یک قول بیقید شنیده. مدل سیستم همان فرضهای ناگفتهٔ کنار قرار است.You tell a friend you will be there at eight, and you say it based on the usual traffic. If the highway closes and you arrive late, you did not lie; your assumption was wrong. The trouble is that you never said the assumption out loud, so your friend heard an unconditional promise. A system model is the unspoken terms attached to the promise.
تشبیه و تصویر ذهنیThe analogy
دو نفر میتوانند یک قرار بگذارند ولی دو چیز مختلف قول بدهند، چون فرضهایشان فرق دارد. یکی فرض میکند مترو کار میکند و میگوید «۸ میرسم»؛ دیگری فرض میکند ممکن است هر چیزی پیش بیاید و میگوید «تا ۹ خودم را میرسانم». دومی کندتر به نظر میرسد ولی قولش محکمتر است، چون چیزی را که ممکن است بشکند در حساب آورده. در سیستم هم همین است: هر چه فرض قویتر باشد الگوریتم سادهتر و ارزانتر میشود، ولی همان فرض مرزِ قول توست.Two people can make the same appointment and promise two different things, because their assumptions differ. One assumes the metro runs and says he will be there at eight; the other assumes anything can happen and says he will be there by nine. The second looks slower, but his promise is sturdier, because he priced in what might break. Systems are the same: the stronger the assumption, the simpler and cheaper the algorithm — and that same assumption is the edge of your promise.
هر پله بالاتر فرض قویتر و الگوریتم ارزانتری میدهد؛ ولی واقعیت تقریباً همیشه پلهٔ وسط است.Each step up is a stronger assumption and a cheaper algorithm — and reality is almost always the middle step.
و بعدش، فنیAnd now, technically
مدل سیستم سه محور دارد. محور زمان: همگام یعنی تأخیر شبکه و انحراف ساعت کراندار است؛ ناهمگام یعنی هیچ کرانی نیست؛ نیمههمگام یعنی کرانها بالاخره برقرار میشوند ولی نمیدانی کِی. محور گره: خرابی توقف (گره میمیرد و برنمیگردد)، خرابی با بازیابی (ممکن است برگردد و حافظهٔ پایدارش را داشته باشد)، و بیزانسی (هر رفتاری ممکن است). محور شبکه: پیام میتواند گم شود، تکرار شود، جابهجا برسد، یا شبکه دو نیم شود. سیستمهای واقعی تقریباً همیشه نیمههمگاماند، با خرابی و بازیابی و شبکهٔ غیرقابلاعتماد؛ همین است که timeout را از اثبات به حدس تبدیل میکند. نتیجهٔ معروف این است که در مدل کاملاً ناهمگام، با حتی یک خرابی توقف، هیچ پروتکل قطعی نمیتواند هم ایمنی و هم زندهمانی را تضمین کند. پس مدل را انتخاب میکنی تا بدانی چه چیزی را میتوانی قول بدهی؛ الگوریتمی که برای مدل ناهمگام اثبات شده، حق ندارد با مهلت نتیجه بگیرد که گرهای مرده است.A system model has three axes. Time: synchronous means network delay and clock drift are bounded, asynchronous means they are not, and partially synchronous means the bounds hold eventually but you do not know when. Node failure: crash-stop, where a node dies and stays dead; crash-recovery, where it may come back with its stable storage intact; and Byzantine, where anything goes. Network: messages may be lost, duplicated, reordered, or the network may split in two. Real systems are nearly always partially synchronous with crash-recovery and an unreliable network, which is what turns a timeout from a proof into a guess. The famous consequence is that in a fully asynchronous model, with even one crash failure, no deterministic protocol can guarantee both safety and liveness. So you pick the model in order to know what you may promise; an algorithm proven for the asynchronous model has no right to conclude from a timeout that a node is dead.
تلهٔ رایجThe usual trap
قاطی کردن مدلها. کتابخانهای که برای خرابی توقف اثبات شده، در سیستمی استفاده میشود که در آن یک گره میتواند پیام کهنه بفرستد؛ یا کدی با مهلت، سکوت را «مرگ» میخواند، در حالی که فرضش شبکهٔ همگام است و شبکه همگام نیست. فرضی که در کد هست ولی در مستندات نیست، همان جایی است که شب حادثه گم میشود.Mixing models. A library proven for crash-stop faults gets used in a system where a node can send a stale message; or code reads silence as death, because its author assumed a synchronous network that is not there. An assumption that lives in the code but not in the docs is exactly what goes missing on the night of the incident.
تا کِی صبر کنی و بعد راه بیفتی؟How long do you wait before you walk away?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
ایستگاه اتوبوس، بیستوپنج دقیقه گذشته. یک تاکسی میآید و باید انتخاب کنی: باز بمانی یا سوار شوی. هیچ راهی نداری که بفهمی اتوبوس دو دقیقهٔ دیگر میرسد یا نیم ساعت دیگر. پس برای خودت یک مهلت میگذاری و وقتی تمام شد راه میافتی — و ممکن است اتوبوس دقیقاً پشت سرت برسد. آن تصمیمِ خودسرانه، همان timeout است.Twenty-five minutes at the bus stop. A taxi pulls up and you have to choose: keep waiting or get in. There is no way to know whether the bus is two minutes away or half an hour. So you set yourself a deadline, and when it runs out you walk — and the bus may arrive right behind you. That arbitrary decision is what a timeout is.
تشبیه و تصویر ذهنیThe analogy
آن مهلت هیچ چیزی دربارهٔ اتوبوس به تو نمیگوید؛ فقط دربارهٔ خودت است. ممکن است اتوبوس گیر ترافیک باشد، ممکن است راننده شیفتش را عوض کرده باشد — از بیرون هر دو یک شکلاند. مهلت را همیشه کوتاه یا بلند میکند یک چیز: هزینه. کوتاه بگذاری، کارهای کند اما سالم را رها میکنی و دوباره میفرستی، پس بار بیشتر میشود. بلند بگذاری، تا رسیدن جواب هیچ کاری نمیکنی و همه منتظر میمانند. نکتهٔ آخر این است که مهلت، کارِ آن طرف را متوقف نمیکند: تو از انتظار دست کشیدی، ولی اتوبوس هنوز در راه است.That deadline tells you nothing about the bus; it is only about you. The bus may be stuck in traffic or the driver may have gone off shift — from the outside they look identical. What makes a deadline short or long is always the same thing: cost. Set it short and you abandon slow but healthy work and send it again, which adds load. Set it long and you do nothing until the answer comes, while everyone waits. And the last point: a deadline does not stop the work on the other side. You stopped waiting; the bus is still on its way.
مهلت، تایمرِ کلاینت است: انتظار را قطع میکند، ولی کارِ سمت سرور را متوقف نمیکند.A timeout is the client's timer: it ends the wait, not the work on the other side.
و بعدش، فنیAnd now, technically
یک timeout تایمری است که کلاینت کنار درخواست میگذارد؛ وقتی تمام شد، کلاینت از انتظار دست میکشد، نه از کاری که آن طرف در جریان است. همین یک جمله دو پیامد دارد. اول، مهلت ابزار محافظت از منابع خودت است: هر درخواست در انتظار یک نخ یا یک اتصال از استخر میگیرد، و اگر مهلت نداشته باشی، یک وابستگی کند کمکم کل استخر را میبلعد و خرابی از یک سرویس به کل سیستم میرسد. دوم، مهلت هرگز نمیگوید «آن طرف شکست خورد»؛ فقط میگوید «جوابش تا این لحظه نرسید»، و این همان نتیجهٔ نامشخص است. در سیستم چندلایه، مهلت باید همراه درخواست سفر کند: هر پرش از بودجهٔ باقیمانده کم میکند، و اگر پرشی بودجه را از صفر شروع کند ممکن است کاری را آغاز کند که تا رسیدنش کلاینت رفته است. مهلت را از روی دمِ توزیع تأخیر انتخاب کن، نه از روی حس؛ و برای اندازهگیریاش از ساعت یکنوا استفاده کن، چون ساعت دیواری میتواند بپرد.A timeout is a timer the client attaches to a request. When it expires the client stops waiting, not the work on the other side. That one sentence has two consequences. First, a deadline protects your own resources: every in-flight request holds a thread or a pooled connection, and without a deadline one slow dependency quietly eats the whole pool and turns a single sick service into a sick system. Second, a deadline never says the other side failed; it says the answer had not arrived by then, which is exactly the unknown outcome. In a multi-hop system the deadline has to travel with the request: each hop spends what is left of the budget, and a hop that restarts the budget can begin work that is pointless before it finishes. Pick the number from the tail of the latency distribution rather than from a feeling, and measure it with a monotonic clock, because the wall clock can jump.
2–3× p99مهلت معقول نسبت به دمِ توزیع تأخیرa sane timeout relative to the latency tail
∞مهلت بینهایت یعنی یک وابستگی کند، سرویس را میخواباندno timeout means one slow dependency takes the service down
1 deadlineیک مهلت که همراه درخواست میرود، نه یکی در هر پرشone deadline travelling with the request, not one per hop
مهلت، استخر اتصال را نجات میدهدThe deadline is what saves the connection pool
تعداد درخواستهای در جریان برابر است با نرخ ورود ضربدر مدت انتظار. اگر نرخ ثابت بماند و مدت انتظار دو برابر شود، دو برابر نخ و اتصال لازم داری، و این چیزی است که در ساعت شلوغی کم میآوری. وقتی استخر پر شد، درخواستهای جدید در صف میمانند، مهلتشان بدون آنکه حتی فرستاده شوند تمام میشود، و کاربر خطایی میبیند که هیچ ربطی به سرویس اصلی ندارد. مهلت این زنجیره را از پایین میبندد، ولی فقط اگر واقعاً کوتاهتر از صبر کاربر باشد.The number of in-flight requests equals arrival rate times wait time. If the rate holds and the wait doubles, you need twice the threads and connections, which is exactly what you run out of at peak. Once the pool fills, new requests queue, their deadlines expire before they are even sent, and users see an error with no relation to the service that actually broke. A timeout cuts this chain at the bottom — but only if it is genuinely shorter than the user's patience.
مهلتِ تمامشده یعنی «نمیدانم»، نه «نشد»An expired deadline means “I don’t know”, not “it didn’t happen”
وقتی مهلت تمام میشود، درخواست ممکن است هرگز نرسیده باشد، ممکن است رسیده باشد و در صف مانده باشد، یا ممکن است کامل انجام شده باشد و فقط پاسخش گم شده باشد. بستن سوکت هیچکدام را عوض نمیکند؛ سرور خبر ندارد که تو رفتی. پس رفتار درست بعد از مهلت، retry کورکورانه نیست: یا باید عملیات تکرارپذیر باشد، یا باید با کلید idempotency برگردی، یا باید بپرسی «انجام شد؟» و آن پرسش هم خودش میتواند شکست بخورد.When a deadline expires, the request may never have arrived, may be sitting in a queue, or may have completed with only its reply lost. Closing the socket changes none of this; the server does not know you left. So the right move after a deadline is not a blind retry: either the operation must be repeatable, or you come back with an idempotency key, or you ask whether it happened — and that question can fail too.
تلهٔ رایجThe usual trap
بزرگ کردن مهلت تا خطاها کم شوند. هر ثانیهٔ اضافه یعنی نخ و اتصال بیشتری در انتظار میماند؛ چند دقیقه بعد استخر پر است، صفها بلندتر شدهاند و سرویس بهجای سریع شکست خوردن، آرام خفه میشود. عدد را از روی دمِ توزیع انتخاب کن، نه از روی ترس.Raising the timeout so the errors go away. Every extra second parks more threads and connections in the wait; minutes later the pool is full, the queues are longer, and the service suffocates slowly instead of failing fast. Choose the number from the tail of the distribution, not from fear.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
پشت خودپرداز ایستادهای و ۵۰۰ هزار تومان خواستهای. دستگاه مکث میکند، بعد میگوید «خطا در ارتباط» و کارت را پس میدهد؛ نه پولی، نه رسیدی. حالا سؤالت این نیست که چرا خطا داد. سؤالت این است که از حسابم کم شد یا نه — و در آن لحظه هیچکس جوابش را ندارد: نه تو، نه دستگاه، نه بانک. همین «نمیدانم» است که به آن نتیجهٔ نامشخص میگویند.You are at the cash machine asking for cash. It pauses, then says the connection failed and returns your card; no money, no receipt. Your question is not why it failed. Your question is whether your balance was touched — and right then nobody has the answer: not you, not the machine, not the bank. That I-don't-know is what we call an unknown outcome.
تشبیه و تصویر ذهنیThe analogy
مثل این است که پولی به یک پیک بدهی و او راه بیفتد. یک ساعت بعد نه پیکی هست و نه خبری. پول دادی، ولی نمیدانی به دست صاحبش رسیده یا در جیب پیک مانده. از این حالت سه راه بیرون میآید: یا گیرنده رسیدی بدهد که بعداً بتوانی ببینی، یا یک شمارهٔ پیگیری داشته باشی که هر دو طرف با آن چک کنند، یا کاری کنی که فرستادن دوبارهٔ همان پول، دو پول نشود. بدون یکی از این سه، هر تصمیمی که بگیری با شانس است.It is like handing cash to a courier and watching him leave. An hour later there is no courier and no news. You paid, but you cannot tell whether the money reached its owner or is still in his pocket. Three ways out exist: the receiver gives you a receipt you can check later, or there is a tracking number both sides can query, or you arrange things so that sending the same money again does not become two payments. Without one of those, every decision you make is a guess.
بین «انجام شد» و «نشد» حالت سومی هست: نمیدانم — و بستنش فقط با حافظهٔ گیرنده ممکن است.Between “it happened” and “it didn’t” there is a third state — and only the receiver’s memory can close it.
و بعدش، فنیAnd now, technically
در یک فراخوانی محلی، تابع یا برمیگردد یا خطا میدهد؛ حالت سومی وجود ندارد. در فراخوانی راه دور حالت سوم هست: جواب نرسید. این «نرسید» هزار دلیل میتواند داشته باشد — درخواست در راه گم شد، سرور کند بود، سرور کار را کرد و پاسخش گم شد، سرور وسط کار مرد — و از دید کلاینت همه یک شکلاند. نکتهٔ تلخ این است که این ابهام قابل حذف نیست: پاسخ هم خودش یک پیام است و میتواند گم شود، پس هیچ شمارش معکوسی نمیتواند بگوید کار انجام شد یا نه. تنها راه، بردن تصمیم به سمت گیرنده است: درخواست را با تضمین at-least-once بفرست، یک کلید idempotency همراه کن، و گیرنده کلید را زیر یک قید یکتا نگه دارد؛ اگر کلید تکراری رسید، همان جواب قبلی را برگردان. به این effectively-once میگویند و تنها چیزی است که واقعاً میتوانی داشته باشی. همین پنجره در two-phase commit هم هست: شرکتکنندهای که رأی داده و ارتباطش قطع شده، تا برنگشتن هماهنگکننده نمیداند و قفلها را نگه میدارد.In a local call a function either returns or throws; there is no third outcome. A remote call has one: no reply. That no-reply has a thousand causes — the request was lost, the server was slow, the server did the work and the reply was lost, the server died mid-way — and from the client they all look the same. The bitter part is that the ambiguity cannot be removed: the reply is itself a message and can be lost, so no amount of counting tells you whether the work happened. The only way out is to move the decision to the receiver: send with at-least-once delivery, carry an idempotency key, and have the receiver keep that key under a unique constraint, returning the previous answer when a duplicate arrives. That is what effectively-once means, and it is the most you can actually have. The same window exists in two-phase commit: a participant that has voted and then lost contact still does not know until the coordinator returns, and it holds its locks meanwhile.
3 حالتموفق، ناموفق، و نامشخص؛ سومی همان دردسر استsuccess, failure, unknown — the third is the trouble
≥ 1تضمین واقعی تحویل: حداقل یک بارthe real delivery guarantee: at least once
1 کلیدکلید idempotency که تکرار را در گیرنده لو میدهدthe idempotency key that exposes a repeat at the receiver
چرا این حالت را نمیتوانی حذف کنیWhy you cannot delete this state
وسوسه این است که بگویی «اگر جواب نیامد، پس انجام نشد». ولی این جمله یک فرض اضافه دارد: اینکه جواب هیچوقت گم نمیشود. در شبکهای که پیام میتواند گم شود، «درخواست گم شد» و «جواب گم شد» از دید کلاینت دقیقاً یک چیزند، و هیچ ترتیبی از بایتها بین این دو فرق نمیگذارد. پس ابهام را نمیشود از سمت فرستنده حل کرد؛ باید جایی حافظه بگذاری که اثر عمل را میداند. هر راهحلی که این حافظه را نداشته باشد، فقط احتمال تکرار را کم میکند، نه اینکه آن را حذف کند.The temptation is to say that if no answer came, nothing happened. That sentence carries an extra assumption: that replies are never lost. On a network where messages can be lost, a lost request and a lost reply are the same thing to the client, and no sequence of bytes distinguishes them. So the ambiguity cannot be resolved on the sender's side; you must put memory somewhere that knows the effect of the operation. Any answer without that memory only lowers the chance of a duplicate, it does not remove it.
چیزهایی که این ابهام را بدتر میکنندWhat makes the window worse
چند چیز این حالت را پهنتر میکنند. اول، failover وسط درخواست: گره میمیرد و نسخهٔ جدید نمیداند کار انجام شده یا نه. دوم، retry در چند لایه که همزمان چند نسخه از یک عمل تولید میکند. سوم، کارهای دستهای که نصفشان اعمال شده و بعد خطا دادهاند. چهارم، صفهای ناهمگام با تحویل دوباره. و پنجم، لایهٔ انسانی: مشتری میگوید «پرداخت کردم» و تو نه راهی داری که تأیید کنی، نه راهی که رد کنی — و اگر بگویی «نشد»، ممکن است پولش رفته باشد.Several things widen the window. First, a failover mid-request: the node dies and the new one has no idea whether the work landed. Second, retries at several layers producing several copies of one operation at once. Third, batch jobs where half the batch applied before the error. Fourth, asynchronous queues that redeliver. And fifth, the human layer: a customer says they paid and you can neither confirm nor deny it — and if you say it did not happen, their money may already be gone.
تلهٔ رایجThe usual trap
یکی گرفتن «خطا» با «انجام نشد». وقتی مهلت تمام میشود یا اتصال میپرد، خیلیها در بخش catch مینویسند «ناموفق» و بعد retry میکنند؛ در حالی که سرور ممکن است کار را کامل کرده باشد و فقط جوابش گم شده باشد. خطا فقط میگوید «نمیدانم»، و رفتار درست با «نمیدانم» با رفتار با «نه» یکی نیست.Treating an error as proof that nothing happened. When a deadline expires or a connection drops, many code paths write “failed” and retry — while the server may have completed the work and lost only the reply. An error says “I don’t know”, and the right response to “I don’t know” is not the response to “no”.
ساعت آشپزخانه درست است؟Is the kitchen clock right?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
بعد از قطعی برق، ساعتِ مایکروویو صفر شده و یک هفته کسی حواسش نبوده. حالا وقتی میگویی «ساعت ۹ رسیدم»، به ساعتی تکیه میکنی که ممکن است یک ساعت عقب باشد؛ ولی وقتی میگویی «بیست دقیقه بگذار بپزد»، از تایمر استفاده میکنی که کاری به ساعت واقعی ندارد. دو سؤال مختلف، دو ساعت مختلف: اولی ساعت دیواری است، دومی تایمر.After a power cut the microwave clock reset, and nobody noticed for a week. So when you say you arrived at nine, you are leaning on a clock that may be an hour behind; but when you say give it twenty minutes, you use the timer, which does not care what time it is. Two different questions, two different clocks: the first is the wall clock, the second is the timer.
تشبیه و تصویر ذهنیThe analogy
دو سؤال هست که شبیهاند و یکی نیستند: «چه ساعتی بود؟» و «چقدر طول کشید؟». سؤال اول به توافق آدمها نیاز دارد — همه باید یک ساعت را قبول داشته باشند — و جوابش همیشه با کمی خطاست. سؤال دوم هیچ کاری به توافق ندارد؛ فقط باید ساعتی داشته باشی که تنها به جلو برود. ساعت دیواری سؤال اول را جواب میدهد و سؤال دوم را خراب میکند، چون وسط اندازهگیری ممکن است کسی آن را درست کند: یک تنظیم کوچک به عقب و مدت پنج ثانیهای میشود چهار ثانیه، یا حتی منفی.There are two questions that look alike and are not: what time was it, and how long did it take. The first needs people to agree — everyone has to accept one clock — and the answer always carries some error. The second needs no agreement at all; it just needs a clock that only moves forward. The wall clock answers the first and ruins the second, because someone may correct it mid-measurement: one small backward adjustment and a five-second interval becomes four, or even negative.
و بعدش، فنیAnd now, technically
ساعت دیواری همان چیزی است که با نام CLOCK_REALTIME میشناسی: ثانیههای سپریشده از یک مبدأ، که با NTP تنظیم میشود و میتواند آرام جابهجا شود یا یکجا بپرد. برای برچسب زمان لازم است: لاگ، «این کد تا فردا معتبر است»، اعتبار گواهی، و منطق تقویم. اما سه کار را با آن نکن. اول، اندازهگیری مدت: چون ممکن است وسط کار به عقب بپرد و تأخیر منفی گزارش شود؛ برای این کار ساعت یکنوا هست. دوم، مهلت: همان مشکل، و برای همین تایمرها روی ساعت یکنوا بسته میشوند. سوم، ترتیب دادن رویدادهای چند ماشین: اختلاف ساعت دو ماشین کراندار نیست، پس مهر زمانی نمیتواند بگوید کدام نوشتن دیرتر بوده. اگر «آخرین نوشتن برنده است» را روی ساعت دیواری بگذاری، نوشتنِ واقعاً دیرتر میتواند مهر زمانی کهنهتری بگیرد و دور ریخته شود. قاعدهٔ ساده: با یکنوا اندازه بگیر، با دیواری برچسب بزن، و هیچوقت این دو را جابهجا نکن.The wall clock is what you know as CLOCK_REALTIME: seconds since an epoch, kept in line by NTP, able to drift gently or jump outright. It is the right tool for labels — log lines, a token valid until tomorrow, certificate validity, calendar logic. But never for three things. First, measuring durations: it may step backwards mid-measurement and report a negative latency, which is what the monotonic clock exists for. Second, deadlines: same problem, which is why timers are built on the monotonic clock. Third, ordering events across machines: the skew between two machines is unbounded, so a timestamp cannot tell you which write came later. Base last-write-wins on the wall clock and a genuinely later write can carry an older stamp and be thrown away. The rule is short: measure with monotonic, label with wall, and never swap them.
1970-01-01مبدأ؛ ثانیههای سپریشده از این لحظهthe epoch; seconds elapsed since this instant
1 sیک پرش یکثانیهای کافی است تا مدت اندازهگیریشده غلط شودa one-second step is enough to falsify a measured duration
23:59:60ثانیهٔ کبیسه؛ زمانی که در فهرست ساعت دیواری هستthe leap second, a time the wall clock really contains
تلهٔ رایجThe usual trap
اندازه گرفتن مدت با ساعت دیواری. کدی که ابتدا و انتهای یک کار را با همین ساعت میگیرد، روزی تأخیر منفی گزارش میکند، چون NTP در آن فاصله ساعت را چند صد میلیثانیه عقب کشیده. همین اشتباه در ترتیبدادن نوشتنها هم هست: با اختلاف ساعت دو ماشین، «آخرین نوشتن برنده است» نوشتنِ دیرتر را بهخاطر مهر زمانی کهنهترش دور میریزد.Measuring durations with the wall clock. Code that stamps the start and end of a job with it will one day report a negative latency, because NTP pulled the clock back a few hundred milliseconds in between. The same mistake lives in write ordering: with skew between two machines, last-write-wins discards the genuinely later write because its timestamp looks older.