۳۱ واژه از فصل ۸ — هر کدام با یک مثال روزمره، یک نمودار و تلهٔ رایج.31 terms from chapter 8, each with an everyday example, a diagram and the usual trap.
چهار حرف که همه تکرار میکنند؛ کدامش را واقعاً داری؟Four letters everyone repeats. Which one do you actually get?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
سفارش یک پیتزا را ثبت میکنی. پول از کارتت کم میشود، موجودی انبار کم میشود، سفارش هم ثبت میشود — و فردا صبح هر سه سر جایشان هستند. اگر یکی از این سه نصفه میماند، آن خرید اصلاً یک تراکنش نبوده. چهار حرف ACID دقیقاً همین چهار قول جدا را نام میبرد.You order a pizza. Money leaves your card, the stock count drops, the order row lands — and the next morning all three are still there. If any one of them had stopped halfway, that purchase was never a transaction. The four letters of ACID name exactly those four separate promises.
تشبیه و تصویر ذهنیThe analogy
پیتزافروشی را تصور کن که سفارش تلفنی میگیرد. (A) یعنی یا کل سفارش ثبت میشود یا هیچ؛ نصف سفارش و نصف پول نداریم. (C) یعنی جمع فاکتور با اقلام میخواند — قانون خودِ فروشگاه، نه قانون کائنات. (I) یعنی دو تلفن همزمان نمیتوانند آخرین پیتزا را به دو نفر بفروشند. (D) یعنی بعد از اینکه گفتی «ثبت شد»، اگر برق برود سفارش سر جایش است. حالا ببین چرا این چهار تا یک چیز نیستند: میشود A و D را کامل داشت ولی I نصفه باشد — سفارش اتمی و پایدار، ولی دو نفر آخرین پیتزا را گرفته باشند. چهار حرف، چهار قرارداد جدا؛ هر کدام را جدا باید بخری.Picture a pizzeria that takes orders by phone. (A) means the whole order is recorded or none of it: no half order, half payment. (C) means the bill adds up to the items — the shop's own rule, not a law of nature. (I) means two calls at once cannot both sell the last pizza. (D) means that once you are told “recorded”, a power cut does not erase it. Now see why these are not one thing: you can have A and D completely and still have half of I — an atomic, durable order that two people believe they own. Four letters, four separate contracts; buy each one on its own.
چهار قول مستقل روی هم؛ ضعف در یکی، سه حرف دیگر را باطل نمیکند.Four independent promises stacked; a weak letter does not cancel the other three.
و بعدش، فنیAnd now, technically
ACID چهار تضمین مستقل است، نه یک برچسب. A یعنی abortability: اگر وسط کار خطا بیاید، همهٔ نوشتنهای آن تراکنش برگردانده میشود و تراکنش یا کامل هست یا نیست؛ این هیچ ربطی به اتمیبودن در سطح thread ندارد. C یعنی invariantهای خود برنامه — foreign key، unique، check — و دیتابیس فقط چیزی را نگه میدارد که در schema اعلام کرده باشی؛ سازگاری replica یا snapshot سازگار جزو این حرف نیست. I یعنی جدا ماندن از تراکنشهای همزمان؛ تعریف کلاسیکش serializability است، ولی بیشتر موتورها پیشفرض read committed میدهند و ناهنجاریهایی مثل read skew و lost update را باز میگذارند. D یعنی بعد از commit داده از دست نمیرود: نوشتن WAL و fsync در یک node، و کپی روی چند node در حالت توزیعشده. این اسم در ۱۹۸۳ توسط Härder و Reuter ساخته شد تا واژهها را جفتوجور کند، ولی امروز هر فروشندهای آن را جور خودش تفسیر میکند. به همین دلیل پرسیدن «دیتابیس ACID است؟» تقریباً بیمعناست؛ سؤال درست این است که کدام سطح جداسازی روشن است و سیاست دوام چیست.ACID is four independent guarantees, not one badge. A means abortability: if something fails mid-way, every write of that transaction is undone, so it either happened completely or not at all — nothing to do with thread-level atomicity. C means your application's own invariants — foreign keys, unique, check — and the database only enforces what you declared in the schema; replica consistency and consistent snapshots are a different word entirely. I means not tripping over concurrent transactions; classically that is serializability, but most engines default to read committed and leave read skew and lost updates open. D means a committed write survives: WAL plus fsync on one node, copies on several nodes when spread out. The acronym was minted in 1983 by Härder and Reuter to pin the vocabulary down, and every vendor has been re-interpreting it ever since. So “is this database ACID?” is close to a meaningless question; the useful one is which isolation level is actually on and what the durability policy is.
4تضمین مستقل زیر یک برچسب؛ یکی جای دیگری را نمیگیردindependent promises under one label; none substitutes for another
1983سال ساخت اسم ACID توسط Härder و Reuterthe year Härder and Reuter coined ACID
read committedپیشفرض بیشتر موتورها، یعنی حرف I عملاً نصفه فروخته میشودthe usual default, which sells only half of the I
چرا I سختترین حرف استWhy I is the hard letter
سه حرف دیگر را میشود با یک مکانیزم ساده خرید: A با undo log، C با قید، D با fsync و یک نسخهٔ دوم. I اینطور نیست، چون همان ضمانت را میشود به چند شکل و با چند قیمت فروخت. read committed ارزان است و خواندن را قفل نمیکند، ولی تصمیمهای برنامه میتواند روی دادهٔ کهنه بیفتد. snapshot isolation خواندن را ثابت میکند، ولی write skew را باز میگذارد. serializable همه را میبندد و در عوض یا انتظار میخرد یا abort و retry. به همین دلیل پروندهٔ I در هر سیستم واقعی یک تصمیم مهندسی است، نه یک تیک در جدول قابلیتها.The other three letters can be bought with one simple mechanism each: A with an undo log, C with constraints, D with fsync and a second copy. I is different, because the same promise is sold in several shapes at several prices. Read committed is cheap and never blocks readers, but your application's decisions can land on stale data. Snapshot isolation makes reads stable and leaves write skew open. Serializable closes everything and pays either in waiting or in aborts and retries. That is why I is an engineering decision in every real system, not a tick in a feature table.
تلهٔ رایجThe usual trap
گمان کردن اینکه «دیتابیس ACID داریم» یعنی همهچیز حل است. در واقع C دست خودت است، I پیشفرض ضعیف است و D به تنظیم fsync و تعداد نسخهها وصل است. یک سیستم کاملاً ACID با read committed همچنان میتواند موجودی را دو بار خرج کند یا شمارنده را گم کند.Assuming “we run an ACID database” closes the subject. In fact C is yours to write, I is weak by default, and D depends on fsync settings and copy count. A perfectly ACID system on read committed can still spend the same balance twice or lose a counter increment.
وسط کار خطا آمد. نوشتههای قبلی چه میشوند؟An error lands mid-way. What happens to the writes already made?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
پول از کارتت کم میشود، بعد صفحهٔ خطا میآید و سفارش ثبت نمیشود. دو روز بعد پول برمیگردد. آن دو روز دقیقاً همان چیزی است که atomicity میخواهد حذف کند: حالتی که نصف کار انجام شده و نصفش نه، و تو هم نمیدانی کدام نصف.The money leaves your card, then an error page appears and the order is not placed. Two days later the money comes back. Those two days are exactly what atomicity exists to remove: a state where half the work happened and half did not, and you cannot tell which half.
تشبیه و تصویر ذهنیThe analogy
نسخهات را به داروخانه میدهی. داروساز یا همهٔ اقلام را میدهد و یکجا حساب میکند، یا هیچکدام؛ «دو قلم از سه قلم» تحویل نمیدهد. برای همین اول همه را از قفسه جمع میکند و بعد حساب میکند؛ اگر یکی نبود، بقیه را سر جایشان برمیگرداند. بهای این کار روشن است: وقتی یک قلم کم است، تلاشش هدر میرود و باید از نو بیایی. و برای اینکه بتواند برگرداند، باید بداند چه چیزی برداشته — یعنی دفتری از کارهای نیمهتمام. موتور دیتابیس هم همین دفتر را دارد و به آن undo log میگوید.You hand a prescription to the pharmacy. The pharmacist fills all of it and charges once, or hands it back; “two of three items” is not a thing she sells. So she gathers everything from the shelves first and rings it up after; if one item is missing, the rest go back. The price is obvious: a single missing item wastes the whole trip and you come back tomorrow. And to be able to put things back, she must know what she took — a note of unfinished work. The database engine keeps the same note and calls it an undo log.
تراکنش تا لحظهٔ کامیت میتواند کامل برگردد؛ بعد از خطا، هیچ نوشتهای نصفه نمیماند.Until commit the transaction can be undone whole; after an error no write is left half-done.
و بعدش، فنیAnd now, technically
اتمیبودن در ACID یعنی «قابل abort کردن»: اگر تراکنش پیش از نقطهٔ کامیت شکست بخورد، موتور همهٔ تغییرهای آن را برمیگرداند، انگار که هرگز شروع نشده بود. در یک موتور تکمحلی این کار با undo log انجام میشود: نسخهٔ قبلی هر ردیف نگه داشته میشود تا در صورت abort بازگردانده شود، و در بازیابی بعد از crash همان لاگ میگوید کدام تراکنشها کامیت نشدهاند. atomicity با isolation فرق دارد: اولی دربارهٔ «همه یا هیچ» بودن نتیجه است، دومی دربارهٔ این که تراکنشهای همزمان چه چیزی از هم میبینند. این تضمین در سطح یک شیء هم معنا دارد: نوشتن یک سند بیستکیلوبایتی در یک ذخیرهگاه key-value میتواند اتمی باشد، حتی اگر آن ذخیرهگاه تراکنش چندردیفی نداشته باشد. هزینهاش این است که abort کار انجامشده را دور میریزد، پس منطق باید قابل تکرار باشد و اثرهای بیرونی جدا مدیریت شوند. و در سیستم توزیعشده همین ضمانت رایگان نیست: همهٔ شرکتکنندهها باید یک تصمیم مشترک بگیرند، و آنجا اسمش commit اتمیک میشود.Atomicity in ACID means abortability: if the transaction fails before its commit point, the engine undoes every change as if it had never begun. On a single node that is an undo log — the previous value of each row is kept so an abort can put it back, and after a crash the same log says which transactions never committed. Atomicity is not isolation: the first is about the outcome being all-or-nothing, the second about what concurrent transactions see of each other. The same guarantee also exists at single-object level: writing one 20 KB document into a key-value store can be atomic even when that store has no multi-row transactions. The cost is that an abort throws away work already done, so the logic must be safe to run again and side effects need separate handling. And across machines the promise is no longer free: every participant has to agree on one decision, which is called atomic commit.
all | noneدو حالت مجاز؛ حالت سومی وجود نداردtwo allowed outcomes; there is no third
undo logدفتری که نسخهٔ قبلی را نگه میدارد تا برگشت ممکن شودthe note that keeps the old value so undo is possible
abort → retryمسیر عادی شکست؛ کارِ انجامشده دور ریخته و از نو اجرا میشودthe normal failure path; finished work is discarded and run again
اتمیبودن کجا از دست میرودWhere atomicity leaks away
در یک دیتابیس تکمحلی، atomicity تقریباً همیشه هست و به آن فکر نمیکنی. جایی از دست میرود که تراکنش از مرز دیتابیس بیرون بزند. ایمیل فرستادن، گذاشتن پیام در صف، صدا زدن درگاه پرداخت و نوشتن در کش، همه بیرون از تراکنشاند: ردیفها برمیگردند و آن اثرها برنمیگردند. راه استاندارد این است که اثر بیرونی را هم داخل همان تراکنش ثبت کنی و بعد از روی همان ثبت منتشر کنی — الگوی outbox — یا کار را idempotent کنی تا تکرارش ضرری نداشته باشد. توجه کن که سؤال اینجا «همه یا هیچ» بودن اثر است، نه این که دو سیستم همزمان چه میبینند.On a single node, atomicity is almost always there and you never think about it. It leaks when the transaction steps outside the database. Sending an email, dropping a message on a queue, calling a payment gateway, writing to a cache — all of those live outside the transaction, so the rows roll back and the effects do not. The standard answer is to record the outbound effect inside the same transaction and publish from that record afterwards — the outbox pattern — or make the work idempotent so repeating it is harmless. Note the question here is all-or-nothing effects, not what two systems see at the same time.
تلهٔ رایجThe usual trap
فرض کردن اینکه abort یعنی «هیچ اتفاقی نیفتاده». اگر تراکنش یک اثر بیرونی داشته باشد — ایمیلی رفته، پیامی به صف رسیده، درگاه بانکی مبلغی را بلوکه کرده — برگرداندن ردیفها آن اثر را برنمیگرداند. دیتابیس فقط چیزی را برمیگرداند که خودش نوشته.Assuming an abort means “nothing happened”. If the transaction had an outside effect — an email sent, a message queued, a payment authorised — rolling back rows does not roll that back. The database only undoes what it wrote itself.
از کدام لحظه به بعد، دیگر نمیشود برگشت؟From which instant is there no way back?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
پیامت را میفرستی. اول یک ساعت شنی کوچک کنارش میآید، بعد یک تیک، بعد دو تیک. تا وقتی ساعت شنی هست، میشود پیام را پس گرفت؛ بعد از دو تیک، رفته و دیگر مال تو نیست. کامیت هم همان لحظه است، فقط جای تیک یک رکورد روی دیسک نشسته است.You send a message. A little clock appears beside it, then one tick, then two. While the clock is there you can still take it back; after two ticks it is gone and no longer yours. Commit is that same instant — only instead of a tick, a record has settled on disk.
تشبیه و تصویر ذهنیThe analogy
قرارداد اجارهٔ خانه را امضا میکنی. تا قبل از امضا هر بندی قابل بحث است: قیمت، مدت، ودیعه. لحظهٔ امضا یک لحظهٔ احساسی نیست، یک کارِ مشخص است: هر دو طرف زیر یک کاغذ را امضا میکنند و یک نسخه در دفتر مشاور املاک میماند. از آن لحظه بندها اجرا میشوند؛ اگر فردا پشیمان شوی باید قرارداد جدید ببندی، نه اینکه بگویی «منصرف شدم». حالا نکتهٔ ظریف: اگر امضا کردی و کاغذ گم شد، تو نمیدانی امضا شده یا نه — و همین «نمیدانم» گرانترین حالت است، چون نه میشود عقب رفت و نه میشود جلو.You sign a flat rental contract. Before the signature every clause is open: price, term, deposit. The moment of signing is not a feeling, it is a specific act: both sides sign one sheet and a copy stays in the agent's file. From then on the clauses run; if you change your mind tomorrow you sign a new contract, you do not “take it back”. Now the subtle part: if you signed and the sheet got lost, you do not know whether it was signed at all — and that not-knowing is the most expensive state, because you can neither go back nor go forward.
قبل از کامیت همهچیز قابل برگشت است؛ بعد از آن باید اجرا شود، حتی اگر پاسخ گم شود.Before commit everything is reversible; after it the decision must be carried out, even if the reply is lost.
و بعدش، فنیAnd now, technically
کامیت لحظهای است که رکورد تصمیم تراکنش پایدار میشود — در بیشتر موتورها یعنی نوشتن رکورد commit در WAL و fsync کردنش. قبل از آن تراکنش میتواند abort شود و هیچ اثری نماند؛ بعد از آن موتور موظف است در بازیابی همان تصمیم را اجرا کند، حتی اگر صفحههای داده هنوز بهروز نشده باشند. برای همین میشود یک نوشتن را پیش از رسیدن به ساختار اصلی به کلاینت تأیید کرد: مرز دوام، لاگ است نه صفحهٔ داده. کامیت و دیدهشدن دو چیز جدا هستند: بعد از کامیت، خوانندههای بعدی تراکنش را میبینند، ولی تحت snapshot isolation یک خوانندهٔ قدیمی همچنان نسخهٔ قبل را میبیند. تأیید کردن کامیت به کلاینت هم کار جداگانهای است؛ اگر پاسخ در شبکه گم شود، کلاینت فکر میکند شکست خورده. و group commit نشان میدهد که چند کامیت میتوانند در یک fsync جمع شوند: دوام یکی است، ولی هزینهٔ دیسک بین چند تراکنش تقسیم میشود.Commit is the instant the transaction's decision record becomes durable — on most engines that means writing the commit record to the WAL and fsyncing it. Before that point the transaction can abort and leave nothing; after it the engine must carry the decision out during recovery even if the data pages have not caught up. That is why a write can be acknowledged before the main structure is touched: the durability boundary is the log, not the data page. Commit and visibility are separate things: after the commit later readers see the transaction, but under snapshot isolation an older reader still sees the previous version. Telling the client about the commit is separate again — if the reply is lost in the network, the client believes it failed. And group commit shows that several commits can share one fsync: durability stays, the disk bill is split.
1یک نقطهٔ بیبازگشت در هر تراکنش، نه دو تاone point of no return per transaction, not two
commit recordرکوردی در لاگ که تصمیم را پایدار میکندthe log record that makes the decision durable
group commitچند تراکنش در یک fsync؛ همان دوام، هزینهٔ کمترseveral transactions per fsync; same durability, less cost
پاسخ گمشده، سختترین حالتThe lost reply is the hard case
سختترین حالت کامیت حالتی است که نمیدانی کامیت شد یا نه. کلاینت درخواست را فرستاده، دیتابیس کامیت کرده، و پاسخ در شبکه گم شده. کلاینت timeout میگیرد و دوباره میفرستد؛ اگر کلید یکتا نداشته باشی، همان کار دو بار انجام میشود. راه درست این است که هر درخواست یک شناسهٔ یکتا داشته باشد و ثبت آن شناسه هم داخل همان تراکنش باشد تا تلاش دوم به قید یکتا بخورد و فقط تأیید کند. این همان الگویی است که در پردازش پیام به آن exactly-once میگویند و در عمل از at-least-once بهعلاوهٔ idempotency ساخته میشود. توجه کن که هیچکدام از اینها جایگزین کامیت پایدار نیست؛ فقط اثر تکرار را برمیدارند.The hard case is the commit you cannot confirm. The client sent the request, the database committed, and the reply died on the way. The client times out and sends again; without a unique key the same work happens twice. The right answer is a unique id per request, recorded inside the same transaction, so the second attempt hits the unique constraint and simply acknowledges. That is the pattern people call exactly-once for message processing, and in practice it is at-least-once plus idempotency. Note that none of it replaces a durable commit; it only removes the duplicate effect.
تلهٔ رایجThe usual trap
تأیید کردن به کاربر پیش از پایدار شدن نوشتن، یا برعکس، تکیه کردن به اینکه «اگر commit شد، جواب هم رسیده». اولی یعنی ممکن است چیزی را تأیید کنی که با یک قطعی برق نیست؛ دومی یعنی کاربری که پول داده و پاسخ نگرفته، دوباره سفارش میدهد.Acknowledging to the user before the write is durable, or the opposite: trusting that “if it committed, the reply arrived”. The first confirms something a power cut can erase; the second makes a customer who paid and got no answer order again.
فقط اگر همان باشد که دیدی، عوضش کن.Change it only if it is still exactly what you saw.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
دو نفر همزمان آخرین صندلی یک کلاس را انتخاب میکنند. دکمهٔ ثبت فقط وقتی کار میکند که وضعیت صندلی هنوز همان «آزاد» باشد که تو دیدی. اگر کسی زودتر گرفته باشد، دکمه خطا میدهد و از تو میخواهد صندلی دیگری انتخاب کنی. هیچ قفلی هم لازم نبود؛ خودِ شرط مسابقه را داوری کرد.Two people pick the last seat in a class at the same moment. The confirm button only works if the seat is still “free” exactly as you saw it. If someone got there first, the button errors and asks you to pick another seat. No lock was needed; the condition itself refereed the race.
تشبیه و تصویر ذهنیThe analogy
بهای این کار، تلاش دوباره است. تا وقتی کسی مزاحم نشده، یک رفتوبرگشت کافی است و هیچکس معطل نمیشود؛ ولی وقتی ده نفر همزمان روی همان مقدار فشار میآورند، نه نفر بازنده میشوند و باید از نو بخوانند. اگر حلقهٔ تلاش دوباره را بیحوصلگی بنویسی، بهجای قفل، یک طوفان درخواست میسازی. نکتهٔ دوم این است که شرط فقط یک لحظه را میبیند: اگر مقدار از ۱۰۰ به ۱۲۰ و بعد برگشته باشد به ۱۰۰، شرط تو قبول میشود، هرچند در این فاصله دو تغییر واقعی رخ داده است. به همین دلیل در سیستمهای جدی، کنار مقدار یک شمارندهٔ نسخه نگه میدارند که هرگز برنمیگردد.The price is retrying. While nobody interferes, one round trip is enough and nobody waits; but when ten people press on the same value at once, nine lose and have to read again. Impatient retry loops turn into a request storm instead of a lock. The second point is that the condition only sees an instant: if the value went from 100 to 120 and back to 100, your condition passes even though two real changes happened in between. That is why serious systems keep a version counter beside the value, one that never goes back.
و بعدش، فنیAnd now, technically
سادهترین شکلش یک بهروزرسانی شرطی است: مقداری را بنویس، فقط اگر مقدار فعلی همان مقدار مورد انتظار باشد. اگر شرط برقرار نباشد، هیچ ردیفی عوض نمیشود و تعداد ردیفهای تأثیرگرفته صفر برمیگردد؛ همین صفر سیگنال باخت در مسابقه است و برنامه باید از نو بخواند و دوباره تلاش کند. در سطح سختافزار همین ایده یک دستور اتمی است که مقایسه و جایگذاری را در یک گام انجام میدهد و پایهٔ ساختن قفلها و ساختارهای بدون قفل است. در دیتابیس، ستون نسخه همان نقش را بازی میکند: هر نوشتن نسخه را یکی بالا میبرد و شرط روی نسخه است، نه روی مقدار، تا مشکل برگشتنِ مقدار پیش نیاید. یک هشدار هم دارد: موفقیت در مقایسه و نوشتن فقط همان یک لحظه را امن میکند. اگر بعد از آن بخواهی روی یک منبع بیرونی — فایل، سرویس دیگر — بنویسی، ممکن است نسخهٔ تو تا آن لحظه کهنه شده باشد؛ آنجا به نشانگری نیاز داری که منبع بیرونی بتواند کهنه بودنش را تشخیص دهد.Its simplest form is a conditional update: write a value, but only if the current value equals the expected one. If the condition fails nothing changes and the affected-row count is zero; that zero is the signal that you lost the race and must read again and retry. At the hardware level the same idea is one atomic instruction that compares and stores in a single step, and it is the basis of locks and lock-free structures. In a database the version column plays that part: every write bumps the version and the condition is on the version, not on the value, so the value going back and forth cannot fool it. One warning: a successful compare-and-set only secures that instant. If you then write to an outside resource — a file, another service — your version may already be stale by the time you get there, and you need a marker the outside resource can use to reject the stale one.
0 rowsنشانهٔ باخت در مسابقه؛ باید از نو بخوانیthe sign you lost the race; read again
ABAمقدار رفته و برگشته؛ شرط قبول میشود روی تاریخی که عوض شدهthe value left and came back; the check passes on a changed history
تلهٔ رایجThe usual trap
گمان کردن اینکه این کار یک قفل است. مقایسه و جایگذاری یک لحظه را داوری میکند، نه یک بازه را. قفل توزیعشدهای که روی یک کلید با همین روش ساخته میشود، در برابر نگهدارندهٔ کند شکست میخورد: کسی که قفلش منقضی شده ولی هنوز مشغول نوشتن است. برای منابع بیرونی نشانگر ترتیب هم لازم است، وگرنه همان نوشتنِ کهنه دیرتر میرسد و برنده میشود.Treating it as a lock. Compare-and-set referees an instant, not an interval. A distributed lock built on one key this way dies in the hands of a slow holder: someone whose lease expired but who is still writing. Outside resources need a fencing token too, or the stale write arrives later and wins.
همه یک چیز را میخواهند، در همان یک لحظه.Everyone wants one thing, at the same moment.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
در خانهٔ دانشجویی یک حمام هست و چهار نفر. ساعت هفت صبح هر چهار نفر بیدار میشوند و همه یک چیز را در یک لحظه میخواهند. نفر چهارم بهجای ده دقیقه، چهل دقیقه منتظر میماند و کل خانه دیر میشود. کسی هم کار اشتباهی نکرده؛ فقط منبع یکی است.A student flat has one bathroom and four people. At seven in the morning all four get up and all four want the same thing at the same moment. The fourth waits forty minutes instead of ten and the whole flat is late. Nobody did anything wrong; there is simply one bathroom.
تشبیه و تصویر ذهنیThe analogy
چیزی که آزاردهنده است، خودِ انتظار نیست؛ شکلِ رشدش است. وقتی حمام نیمی از صبح مشغول است، صف کوتاه است و کسی شکایت نمیکند. وقتی نه دهم صبح مشغول است، صف بهطور انفجاری بلند میشود، چون هر کسی که دیر میرود نفر بعدی را هم عقب میاندازد. همین قاعده در سیستمها هم دیده میشود: تا حدود نصف ظرفیت، تأخیر تقریباً ثابت است؛ نزدیک ظرفیت، هر درصد اضافه، تأخیر را چند برابر میکند. و اگر یکی از ساکنین عادت کند یک ساعت زیر دوش بماند، بقیه بهجای اینکه کمی دیر شوند، عملاً هیچوقت به نوبت نمیرسند.What hurts is not the waiting itself but its shape. When the bathroom is half busy the queue is short and nobody complains. When it is nine-tenths busy the queue explodes, because everyone who runs late pushes the next person later. The same curve shows up in systems: up to about half capacity latency is roughly flat; near capacity every extra percent multiplies it. And if one resident takes an hour under the shower, the others do not arrive a little late — they effectively never get a turn.
و بعدش، فنیAnd now, technically
contention وقتی است که چند کار همزمان سر یک منبع مشترک صف میکشند: یک ردیف، یک صفحه، یک قفل، یک اتصال. دو چیز را با هم میآورد که همدیگر را بدتر میکنند: صف و کارِ هدررفته. در سطح قفل، توان عملیاتی سقف دارد و آن سقف یک تقسیم است: یک تقسیم بر مدت نگهداشتن قفل. اگر هر تراکنش یک ردیف را ده میلیثانیه قفل کند، بیشتر از حدود صد تراکنش در ثانیه از آن ردیف رد نمیشود — هر تعداد thread که اضافه کنی. بدتر از آن، اثر کاروان است: یک عملیات کند صف پشتش را میسازد و آن صف تأخیر بقیه را بالا میبرد؛ و اگر برنامه در برابر خطا دوباره تلاش کند، همان تلاش بار را بیشتر و صف را بلندتر میکند. راههای واقعی کم کردنش اینهاست: تراکنش کوتاهتر، پخش کردن نوشتن روی چند ردیف یا چند shard، دستهای کردن نوشتنها، جایگزین کردن قفل با صف، و عقب نشستنِ تصادفی بین تلاشها. اضافه کردن ماشین به سیستمی که گلوگاهش یک ردیف است هیچ کاری نمیکند.Contention is several jobs queueing for one shared resource: a row, a page, a lock, a connection. It brings two things that make each other worse — a queue and wasted work. At the lock level throughput has a ceiling, and the ceiling is a division: one over the lock hold time. If each transaction holds a row for ten milliseconds, that row will not carry much more than a hundred transactions a second, no matter how many threads you add. Worse is the convoy effect: one slow operation builds a queue behind it, the queue raises everyone's latency, and a retrying application feeds more load into the same queue. The real fixes are shorter transactions, spreading writes over several rows or shards, batching, replacing a lock with a queue, and randomised backoff between attempts. Adding machines to a system whose bottleneck is one row does nothing.
≈ 1/holdسقف توان یک ردیف: یک تقسیم بر مدت نگهداشتن قفلone row's throughput ceiling: one over the lock hold time
≈ 90%بهرهای که بعد از آن، زمان انتظار بهسرعت بالا میرودutilization past which waiting time climbs steeply
تلهٔ رایجThe usual trap
افزودن ظرفیت وقتی گلوگاه یک ردیف است. اگر همهٔ نوشتنها روی یک شمارنده یا یک رکورد پیکربندی جمع شوند، ده ماشین جدید فقط ده برابر بیشتر در صف میایستند. اول باید بفهمی کدام منبع مشترک است، بعد یا آن را بشکن، یا بارش را کم کن.Adding capacity when the bottleneck is one row. If every write lands on one counter or one configuration record, ten new machines simply queue ten times as much. Find the shared resource first, then either split it or take load off it.
هر دو منتظر دیگریاند و هیچکدام کوتاه نمیآید.Each waits for the other, and neither backs off.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
در راهروی باریک ساختمان به همسایهات میرسی. او هم همان لحظه میخواهد رد شود. هر دو یک قدم عقب میکشی و میگویی «بفرمایید»، او هم همین کار را میکند. حالا هر دو ایستادهاید و هیچکدام رد نمیشود. بدون یک قاعده — مثلاً «همیشه من اول» — این راهرو قفل میماند، و دو تراکنش هم دقیقاً همینطور بنبست میشوند.You meet your neighbour in the narrow hallway of your building. He wants to pass at the same moment. You step back and say “after you”, and he does exactly the same. Now you are both standing still and nobody gets through. Without a rule — “I always go first” — that hallway stays locked, and two transactions deadlock in exactly this shape.
تشبیه و تصویر ذهنیThe analogy
دو نفر میخواهند یک نامهٔ اداری را با هم امضا کنند. اولی خودکار را برداشته و منتظر کاغذ است؛ دومی کاغذ را برداشته و منتظر خودکار. هیچکدام حاضر نیست اول بگذارد زمین، چون میترسد نوبتش برود. سه راه خروج وجود دارد. یکی از سر لجبازی بگذرد و کارش را از دست بدهد (timeout و abort). یک ناظر یکی را از اتاق بیرون کند (تشخیص بنبست توسط دیتابیس). یا از اول قانون بگذاریم که همه اول خودکار را بردارند و بعد کاغذ (ترتیب ثابت قفل). هیچکدام مجانی نیست: راه اول کار انجامشده را دور میریزد، راه دوم به ناظر نیاز دارد، و راه سوم به این شرط بسته است که همه همان قانون را رعایت کنند.Two people have to sign one letter together. One has picked up the pen and waits for the paper; the other has the paper and waits for the pen. Neither will put theirs down first, because they fear losing their turn. There are three ways out. One of them gives up and loses the work already done (timeout and abort). A referee throws one of them out of the room (the database detecting the cycle). Or a rule up front: everyone takes the pen first, then the paper (a fixed lock order). None is free — the first throws away work, the second needs a referee, the third only works if everyone follows the same rule.
هر طرف یک قفل گرفته و قفل بعدی در دست دیگری است؛ چرخه باید شکسته شود.Each side holds one lock and needs the other's; the cycle has to be broken.
و بعدش، فنیAnd now, technically
بنبست وقتی میآید که دو تراکنش مجموعهای از قفلها را به ترتیبهای مختلف میگیرند و هر کدام منتظر قفلی است که در دست دیگری است. موتور معمولاً گراف انتظار میسازد: هر تراکنش یک گره و هر انتظار یک یال؛ اگر چرخهای پیدا شود، یکی از تراکنشها بهعنوان قربانی انتخاب و abort میشود تا بقیه راه بیفتند. بعضی موتورها مثل PostgreSQL تا یک مهلت کوتاه صبر میکنند و بعد گراف را چک میکنند، چون ساختن گراف هم هزینه دارد. بنبست نتیجهٔ قفل گرفتن است، نه نتیجهٔ ضعیف بودن سطح جداسازی: هر جا SELECT ... FOR UPDATE یا قفل صریح داشته باشی ممکن است. درمان اصلی ترتیب ثابت قفلهاست: اگر همهٔ تراکنشها منابع را با یک ترتیب بگیرند، چرخه ممکن نیست. درمان دوم کوتاه کردن تراکنش و کم کردن تعداد قفل است، چون هر قفل اضافه یک یال اضافه در گراف است. و در سیستم توزیعشده، بنبست میتواند بین دو سیستم رخ دهد که هیچکدام گراف کل را نمیبیند.A deadlock happens when two transactions take locks in different orders and each waits for a lock the other holds. The engine usually builds a wait-for graph: one node per transaction, one edge per wait; when it finds a cycle it picks a victim and aborts it so the rest can move. Some engines, PostgreSQL among them, wait a short interval before checking, because building the graph costs something too. Deadlocks come from locking, not from a weak isolation level: anywhere you use SELECT ... FOR UPDATE or an explicit lock, they are possible. The main cure is a fixed lock order — if every transaction acquires resources in the same order, no cycle can form. The second cure is shorter transactions with fewer locks, because every extra lock is one more edge. Across machines, a deadlock can span two systems that each see only half the graph.
2حداقل دو طرف لازم است تا چرخه بسته شودat least two parties are needed to close a cycle
≈ 1 sمهلت معمول موتور پیش از چک کردن گراف انتظارthe usual pause before the engine checks the wait-for graph
1 victimدر هر چرخه یک تراکنش قربانی میشود و باید از نو اجرا شودone transaction per cycle is aborted and has to be retried
چرا در سیستم توزیعشده سختتر استWhy it gets worse across machines
در یک دیتابیس، گراف انتظار کل سیستم را میبیند و چرخه را پیدا میکند. در سیستم توزیعشده اینطور نیست: تراکنش A روی یک node منتظر قفلی است که در دست تراکنش B روی node دیگر است، و B هم منتظر چیزی است که A گرفته؛ هیچکدام تصویر کامل را ندارد. پروتکل 2PC این را بهتر نمیکند، چون در فاز اول فقط رأی جمع میکند و بعد منتظر میماند. اگر یکی از شرکتکنندهها در حالت in-doubt بماند، قفلهایش هم میمانند و بقیه پشت سرش صف میکشند. به همین دلیل آنجا پیشگیری از تشخیص مهمتر است: ترتیب ثابت، تراکنش کوتاه، و تا جای ممکن نگه داشتن مرز تراکنش داخل یک node.Inside one database the wait-for graph sees the whole system and finds the cycle. Across machines it does not: transaction A on one node waits for a lock held by B on another, and B waits for something A holds; neither side has the full picture. Two-phase commit does not help, because phase one only collects votes and then waits. If a participant is left in doubt, its locks stay, and everything else queues behind it. That is why prevention beats detection out there: a fixed order, short transactions, and keeping the transaction boundary inside one node wherever you can.
تلهٔ رایجThe usual trap
کشتن تراکنش بهعنوان «راهحل» و دوباره اجرا نکردنش. abort شدن یعنی کار آن تراکنش رفت؛ اگر کلاینت آن را دوباره نفرستد، سفارش گم میشود. بدتر، retry بدون تأخیر با احتمال زیاد دوباره به همان بنبست میخورد.Treating the abort as the fix and never re-running the transaction. An abort means that work is gone; if the client does not send it again, the order disappears. Worse, an immediate retry is very likely to hit the same deadlock.
عددی خواندی که هنوز تصویب نشده بود.You read a number that was never approved.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
همکارت دارد عدد بودجه را در فایل مشترک عوض میکند. هنوز ذخیره نکرده؛ تو عدد جدید را روی صفحه میبینی، باور میکنی و به مدیر میگویی. بعد همکارت پشیمان میشود و عدد را برمیگرداند. مدیر حالا با عددی حرف میزند که هیچوقت وجود نداشت.A colleague is changing the budget figure in a shared file. Nothing is saved yet; you see the new number, believe it, and quote it to your manager. Then your colleague changes their mind and puts the old number back. Your manager is now quoting a figure that never existed.
تشبیه و تصویر ذهنیThe analogy
تختهٔ وایتبرد اتاق جلسه را تصور کن. یکی با ماژیک دارد عددی را مینویسد و هنوز پاک نکرده که تو میخوانی و تصمیم میگیری. دو راه برای اینکه این اتفاق نیفتد وجود دارد. یا نویسنده تا تمام نشده تخته را بپوشاند و خواننده صبر کند (قفل خواندن)، یا تخته دو لایه داشته باشد: لایهٔ قدیمی که همه میبینند و لایهٔ جدید که تا تأیید نشده فقط برای نویسنده است. راه اول ساده است و خوانندهها را معطل میکند؛ راه دوم خواننده را آزاد میگذارد و به جایش حافظه و پاکسازی میخواهد. بیشتر موتورهای امروزی راه دوم را انتخاب کردهاند.Picture the whiteboard in the meeting room. Someone is writing a number with a marker and has not wiped anything yet, and you read it and decide on it. There are two ways to stop that. Either the writer covers the board until done and readers wait (a read lock), or the board has two layers: the old one everybody sees and the new one only the writer sees until it is approved. The first is simple and makes readers wait; the second leaves readers free and costs memory and cleanup. Most engines today pick the second.
خواننده مقدار commitنشده را میگیرد و نویسنده آن را پس میگیرد؛ عددی که هرگز وجود نداشت.The reader takes an uncommitted value, the writer takes it back; a number that never was.
و بعدش، فنیAnd now, technically
خواندن کثیف یعنی تراکنشی مقدار نوشتهشدهٔ تراکنش دیگری را پیش از کامیت بخواند. خطرش این است که آن نوشتن ممکن است abort شود — نقض قید، خطای برنامه، یا مرگ کلاینت — و خواننده با عددی تصمیم گرفته که هرگز وجود نداشته. این ناهنجاری فقط در read uncommitted مجاز است؛ read committed و بالاتر جلویش را میگیرند، و در عمل بعضی موتورها read uncommitted را به read committed نگاشت میکنند، یعنی روی کاغذ داری و در عمل نداری. جلوی خواندن کثیف را با دو مکانیزم میگیرند: قفل خواندن روی ردیف، که نویسندهٔ کند خوانندهها را معطل میکند، یا MVCC که نسخهٔ کامیتشدهٔ قبلی را به خواننده میدهد و نسخهٔ در جریان را پنهان میکند. خواندن کثیف با نوشتن کثیف فرق دارد: نوشتن کثیف یعنی بازنویسی نوشتهٔ کامیتنشدهٔ دیگری، و تقریباً همهٔ موتورها با قفل ردیف جلویش را میگیرند؛ به همین دلیل در ماتریس ناهنجاریها ستونی برایش نمانده. پیامد عملی خواندن کثیف دو چیز است: عددی که در گزارش میماند و بعداً ردی از خودش پیدا نمیکنی، و تصمیمی که روی دادهای گرفته شده که هرگز کامیت نشد.A dirty read is one transaction reading another's uncommitted value. The danger is that the write may abort — a constraint, an application error, a dead client — and the reader has decided on a number that never existed. Only read uncommitted allows it; read committed and above prevent it, and some engines map read uncommitted onto read committed, so it exists on paper and not in practice. Two mechanisms prevent it: a read lock on the row, which lets a slow writer stall the readers, or MVCC, which hands the reader the last committed version and hides the in-flight one. Dirty reads are not dirty writes: a dirty write is overwriting someone else's uncommitted write, and nearly every engine stops that with a row lock, which is why it has no column in the anomaly table. The practical damage is two things: a number left in a report whose origin you cannot trace, and a decision taken on data that never committed.
RUتنها سطحی که خواندن کثیف را مجاز میداندthe only level that permits dirty reads
RC+read committed و بالاتر جلویش را میگیرندread committed and above prevent it
0تعداد فایدهای که یک عدد ناپدیدشده برای گزارش بعدی داردthe value of a vanished number to next week's report
چرا در ماتریس، ستون نداردWhy it has no column in the table
ماتریس ناهنجاریها چهار ستون اصلی دارد و نوشتن کثیف یکی از آنها نیست، چون تقریباً همهٔ موتورها حتی در read committed هم جلویش را میگیرند: برای بازنویسی یک ردیف باید قفل انحصاریاش را بگیری و آن قفل تا پایان تراکنش آزاد نمیشود. این تقارن مهم است: خواندن کثیف را میشود با یک قفل یا یک نسخه بست، ولی نوشتن کثیف تقریباً همیشه با قفل بسته میشود. اگر روزی به موتوری برخوردی که نوشتن کثیف را مجاز میداند، بدان که با چیزی عجیب طرفی، چون برگرداندن دو نوشتن روی هم تقریباً غیرممکن است.The anomaly table has four main columns and dirty writes is not one of them, because nearly every engine stops it even at read committed: overwriting a row requires its exclusive lock, and that lock is held to the end of the transaction. The symmetry matters: a dirty read can be stopped by a lock or by a version, while a dirty write is stopped by a lock almost without exception. If you ever meet an engine that permits dirty writes, you are dealing with something strange, because untangling two writes on top of each other is close to impossible.
تلهٔ رایجThe usual trap
گمان کردن اینکه «read uncommitted فقط تندتر است». این سطح مجموعهٔ ناهنجاریهای مجاز را باز میکند و یکی از آنها نوشتهای است که ممکن است هرگز وجود نداشته باشد. اگر سطح را برای سرعت پایین میآوری، بدان دقیقاً چه چیزی میخری.Assuming read uncommitted is merely faster. It widens the set of allowed anomalies, and one of them is a write that may never exist. If you drop the level for speed, know exactly what you bought.
نوشتن روی چیزی که هنوز تصمیمش گرفته نشده.Writing over something whose fate is not decided yet.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
دفتر مهمانی روی میز است. مادرت اسم خاله را با مداد مینویسد و میگوید «شاید». تو پاکش میکنی و جایش اسم عمو را مینویسی. مادرت پشیمان میشود و میخواهد نوشتهٔ خودش را پاک کند؛ حالا اسم عمو هم رفته. آن یک پاککردن، دو تصمیم را با هم برد.The guest list is on the table. Your mother writes your aunt's name in pencil and says “maybe”. You rub it out and write your uncle's name instead. Then your mother changes her mind and wants her entry gone — and now your uncle's name is gone too. One eraser took two decisions with it.
تشبیه و تصویر ذهنیThe analogy
تفاوتش با خواندن کثیف همینجاست: آنجا مشکل این بود که چیزی را باور کردی که بعداً پس گرفته شد. اینجا مشکل بدتر است — کسی روی کارِ ناتمام دیگری نوشته، و حالا بازگشت به عقب معنا ندارد. اگر مادرت بخواهد نوشتهٔ خودش را پس بگیرد، باید بداند قبل از او چه بود؛ ولی آن «قبل» دیگر وجود ندارد، چون تو رویش نوشتهای. دقیقاً به همین دلیل، دیتابیسها اجازه نمیدهند دو تراکنش همزمان یک ردیف را بنویسند: قفل انحصاری ردیف تا پایان تراکنش نگه داشته میشود، نه فقط تا پایان دستور، تا نوشتنِ دوم صبر کند و مقدار قبلی جایی برای برگشتن داشته باشد.This is where it differs from a dirty read: there the problem was believing something that was later taken back. Here it is worse — someone wrote over unfinished work, and undoing no longer means anything. If your mother wants her entry removed, she needs to know what was there before her, and that “before” no longer exists because you wrote over it. That is exactly why databases refuse to let two transactions write one row at the same time: the exclusive row lock stays held right through to commit, not merely to the end of the statement, so the second write waits and the previous value has somewhere to go back to.
و بعدش، فنیAnd now, technically
نوشتن کثیف یعنی تراکنش دوم مقداری را بنویسد که تراکنش اول نوشته و هنوز کامیت نکرده است. خطرش این است که اگر اولی abort شود، برای برگرداندن باید مقدار قبلی را بازگرداند — ولی آن مقدار قبلی دیگر نیست، چون دومی رویش نوشته. نتیجه، ردیفی است که هیچ تاریخ درستی ندارد: نه مقدار قبل، نه مقدار اولی، نه مقدار دومی. تقریباً همهٔ دیتابیسهای رابطهای این را با یک قفل انحصاری روی ردیف میبندند که تا کامیت یا abort نگه داشته میشود؛ به همین دلیل نوشتن کثیف در عمل کمتر از خواندن کثیف دیده میشود. جاهایی که دیده میشود، سیستمهایی هستند که قفل نوشتن ندارند و «آخرین نوشتن برنده» را پیشفرض گذاشتهاند: آنجا دو نوشتن همزمان میتوانند روی هم بیفتند و بعد از یک خرابی، بازیابی نمیتواند بگوید کدام نوشتن مال چه کسی بود. اگر خودت لایهٔ کش یا همگامسازی مینویسی، همین سؤال را بپرس: اگر تراکنش عقب کشید، مقدار قبلی کجاست؟A dirty write is a second transaction writing a value the first transaction wrote and has not committed. The danger is that if the first one aborts, it must restore the previous value — and that previous value is gone, because the second write covered it. What is left is a row with no true history: not the old value, not the first writer's, not the second writer's. Almost every relational database closes this with an exclusive row lock held to commit or abort, which is why a dirty write is rarer in practice than a dirty read. Where it does appear is in systems with no write lock and a last-write-wins default: two writes can land on each other, and after a crash recovery cannot say whose write was whose. If you are writing a cache layer or a synchronisation path yourself, ask exactly that question: if the transaction backs out, where is the previous value?
1 lockقفل انحصاری ردیف تا پایان تراکنشan exclusive row lock that lives until commit
تلهٔ رایجThe usual trap
ساختن مسیر بهروزرسانی بدون قفل و بدون نسخه، به این امید که «آخرین نوشتن برنده» کافی است. در عمل، یک abort یا یک خرابی وسط کار ردیفی میسازد که هیچکس نمیتواند بگوید از کجا آمده، و اشتباه فقط ماهها بعد در یک گزارش دیده میشود.Building an update path with no lock and no version, hoping last-write-wins is enough. In practice an abort or a crash mid-way leaves a row nobody can explain, and the mistake only surfaces months later in a report.
دو سیستم، یک تصمیم؛ اگر یکی بمیرد چه؟Two systems, one decision. What if one of them dies?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
از فروشگاهی خرید میکنی که جنسش از دو انبار میآید. پول از کارتت کم میشود، انبار اول جنس را برمیدارد، انبار دوم میگوید موجود نیست. سه روز بعد پول برمیگردد و سفارش لغو میشود. آن سه روز، فاصلهٔ بین «پول رفت» و «سفارشی نیست» است — دقیقاً چیزی که یک تراکنش توزیعشده نمیگذارد اتفاق بیفتد.You order from a shop whose goods ship from two warehouses. The money leaves your card, the first warehouse picks the item, the second says it is out of stock. Three days later the money returns and the order is cancelled. Those three days are the gap between “paid” and “no order” — exactly what a distributed transaction exists to prevent.
تشبیه و تصویر ذهنیThe analogy
دو نفر در دو اتاق جدا باید با هم یک تصمیم بگیرند: یکی مسئول پذیرش و یکی مسئول انبار. قاعده این است که یا هر دو «بله» بگویند یا هیچکدام؛ «بلهٔ نصفه» فاجعه است. برای همین اول از هر دو میپرسند «میتوانی؟» هر کس بله گفت دیگر حق ندارد بعداً نظر عوض کند و باید منابعش را نگه دارد. وقتی همه بله گفتند، یک نفر تصمیم نهایی را در دفتر خودش مینویسد و بعد به همه اعلام میکند. اگر همان لحظه بمیرد، بقیه در حالت «نمیدانم» میمانند و منابع را نگه میدارند تا تصمیم برسد — نه اینکه حدس بزنند.Two people in separate rooms have to reach one decision together: one runs admissions, one runs the warehouse. The rule is that both say yes or nobody does; a half-yes is a disaster. So first both are asked “can you?”. Whoever said yes has given up the right to change their mind and must hold their resources. When everyone has said yes, one person writes the final decision in their own book and only then announces it. If that person dies at that moment, the rest stay in “I don't know” and keep their resources until the decision arrives — they do not guess.
اول پرسش و قول، بعد نوشتن تصمیم در لاگ، بعد اعلام؛ تصمیم پیش از اعلام پایدار میشود.Ask and promise first, then log the decision, then announce; the decision is durable before anyone hears it.
و بعدش، فنیAnd now, technically
تراکنش توزیعشده مسئلهٔ commit اتمیک را حل میکند: چند شرکتکننده باید به یک تصمیم مشترک برسند، همه کامیت یا همه abort. پروتکل استاندارد two-phase commit است. در فاز اول هماهنگکننده از هر شرکتکننده prepare میپرسد؛ شرکتکننده اگر بله بگوید تغییرها را پایدار کرده و حق abort را از خودش گرفته است. در فاز دوم هماهنگکننده تصمیم را اول در لاگ خودش پایدار میکند — این نقطهٔ کامیت است — و بعد اعلام میکند. اگر هماهنگکننده بعد از رأیها بمیرد، شرکتکنندهها در حالت in-doubt میمانند و قفلهایشان را نگه میدارند؛ همین است که 2PC را blocking میگویند. پیادهسازی ناهمگن این کار XA است و شکننده: هماهنگکننده معمولاً یک کتابخانه در دل خود برنامه است، پس crash برنامه یعنی رفتن هماهنگکننده و لاگش. در مقابل، تراکنش توزیعشدهٔ داخلی که همهٔ شرکتها یک نرمافزار دارند، هماهنگکننده را با consensus تکثیر میکند و در عمل خیلی بهتر کار میکند. و همیشه یک راه ارزانتر هم هست: مرز تراکنش را جوری بکش که همهچیز در یک پایگاه بیفتد.A distributed transaction solves atomic commitment: several participants must reach one shared decision, all commit or all abort. The standard protocol is two-phase commit. In phase one the coordinator asks each participant to prepare; a participant that says yes has made its changes durable and given up its right to abort. In phase two the coordinator first makes the decision durable in its own log — that is the commit point — and then announces it. If it dies after the votes, participants stay in doubt and keep their locks, which is why 2PC is called blocking. The heterogeneous version of this is XA, and it is fragile: the coordinator is usually a library inside your application process, so an application crash takes the coordinator and its log with it. By contrast, an internal distributed transaction, where every participant runs the same software, replicates the coordinator through consensus and works far better in practice. And there is always a cheaper route: redraw the transaction boundary so everything fits in one database.
2 فازprepare و commit؛ بین این دو، شرکتکننده حق برگشت نداردprepare and commit; between them the participant cannot back out
1 لاگتصمیم اول در لاگ هماهنگکننده پایدار میشود، بعد اعلامthe decision lands in the coordinator's log first, then travels
≥ 2شرکتکننده؛ با یکی، تراکنش توزیعشده معنا نداردparticipants; with one there is nothing distributed
چرا in-doubt گران استWhy in-doubt is expensive
شرکتکنندهای که بله گفته، منابعش را قفل کرده و منتظر تصمیم است. اگر هماهنگکننده غیب شود، این قفل تا رسیدن تصمیم معتبر باقی میماند، چون حدس زدن یعنی شکستن atomicity: اگر یکی خودسرانه abort کند و بقیه کامیت کرده باشند، سیستم دو نیمهٔ ناسازگار میشود. به همین دلیل هیچ timeout ای در این حالت جواب درست نمیدهد و تنها راه، برگشتن هماهنگکننده و خواندن لاگش است. اگر لاگ هم از بین رفته باشد، کار به تصمیم انسانی میرسد؛ و همین است که بعضی پیادهسازیها یک راه فرار به نام heuristic decision گذاشتهاند که در واقع اعتراف به شکستن ضمانت است.A participant that voted yes has locked its resources and is waiting for the decision. If the coordinator disappears, those locks stay until the decision arrives, because guessing would break atomicity: one side aborting while the others committed leaves two inconsistent halves. That is why no timeout gives a correct answer here; the only way out is the coordinator coming back and reading its log. If the log is gone too, the case escalates to a human — which is why some implementations offer an escape hatch called a heuristic decision, which is really an admission that the guarantee is broken.
راههای دور زدنRoutes around it
دو راه عملی وجود دارد که هر دو از خود پروتکل ارزانترند. اول، کوچک کردن مسئله: اگر رزرو و دفتر موجودی در یک پایگاه باشند، دیگر commit اتمیک توزیعشده لازم نیست و همهچیز یک تراکنش محلی است. دوم، idempotency: برای خیلی از کارها — پردازش پیام، ثبت سفارش، ارسال ایمیل — فقط لازم است تکرار اثر نگذارد، و این با یک شناسهٔ یکتا و یک قید یکتا ساخته میشود، نه با 2PC. توجه کن که این دو راه مسئله را حل نمیکنند، عوضش میکنند؛ گاهی همین کافی است و گاهی نه.There are two practical routes and both are cheaper than the protocol. First, shrink the problem: if the reservation and the stock ledger live in one database, no distributed atomic commit is needed and everything is a local transaction. Second, idempotency: for a lot of work — processing a message, placing an order, sending mail — all you need is that a repeat leaves no second effect, and that comes from a unique id plus a unique constraint, not from 2PC. Neither route solves the problem; they trade it for a different one, which is sometimes exactly the right trade.
تلهٔ رایجThe usual trap
استفاده از 2PC برای هر چیزی که به نظر چند مرحلهای میآید. 2PC قفل نگه میدارد و اگر هماهنگکننده بترکد، آن قفلها تا آمدن تصمیم میمانند؛ یک تراکنش کند میتواند بخشی از سیستم را بخواباند. قبل از آن بپرس آیا میشود مرز را دوباره کشید یا کار را idempotent کرد.Reaching for 2PC for anything that looks multi-step. 2PC holds locks, and if the coordinator breaks those locks stay until a decision arrives; one slow transaction can put a whole section of the system to sleep. Before that, ask whether the boundary can be redrawn or the work made idempotent.
هیچ دو نوبتی نباید روی هم بیفتند.No two bookings may overlap.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
در ساختمان یک ماشین لباسشویی مشترک است و یک تقویم روی در یخچال. اگر دو نفر برای ساعت پنج تا هفت نوبت بزنند، تقویم باید خودش یکی را رد کند؛ وگرنه سر ساعت پنج دو سبد لباس جلوی یک ماشین است.The building has one shared washing machine and a calendar on the fridge door. If two people book five to seven, the calendar has to reject one of them by itself; otherwise at five o'clock there are two baskets in front of one machine.
تشبیه و تصویر ذهنیThe analogy
چیزی که اینجا ممنوع است، «برابر بودن» نیست؛ «همپوشانی» است. دو نوبت میتوانند شروعشان فرق کند، طولشان فرق کند، و باز هم با هم بیفتند. پس قاعده نمیتواند مثل یکتایی روی یک ستون باشد؛ باید بگوید «هیچ دو ردیفی نباشند که هم اتاقشان یکی باشد و هم بازهٔ زمانیشان همپوشانی داشته باشد». و بدتر: باید ردیفی را هم بگیرد که هنوز نوشته نشده — همان لحظهای که دو نفر همزمان تقویم را نگاه میکنند و هر دو ساعت پنج را خالی میبینند. به همین دلیل این قاعده را نمیشود در کد برنامه چک کرد؛ باید جایی باشد که خودِ درج را میبیند.What is forbidden here is not equality but overlap. Two bookings can start at different times, run for different lengths, and still collide. So the rule cannot be a unique value in one column; it has to say no two rows share the same room and overlapping time. Worse, it has to catch a row that does not exist yet — the moment two people look at the calendar and both see five o'clock free. That is why this rule cannot live in application code; it has to sit where the insert itself happens.
و بعدش، فنیAnd now, technically
در پستگرس این کار با یک قید طرد انجام میشود: یک ایندکس GiST که روی ترکیب چند ستون ساخته میشود و برای هر ستون یک عملگر مقایسه تعریف میکند. برای رزرو اتاق، ترکیب «اتاق برابر است» و «بازه همپوشانی دارد» نوشته میشود؛ بعد از آن، هر درج یا بهروزرسانی که با ردیف موجودی همپوشانی داشته باشد رد میشود، و این بررسی داخل همان تراکنش انجام میشود، پس دو درج همزمان هم نمیتوانند از آن رد شوند. دو نکتهٔ عملی: برای ستونهای معمولی مثل شناسهٔ اتاق به افزونهٔ btree_gist نیاز داری تا عملگر تساوی داخل ایندکس GiST کار کند؛ و بازهها باید با دقت تعریف شوند، چون [) و [] مرزها را متفاوت میگیرند و نوبتی که ساعت هفت تمام میشود نباید با نوبتی که هفت شروع میشود تعارض پیدا کند. چیزی که این قید نمیتواند بگیرد، قیدهای جمعی است: «مجموع صندلیهای رزروشده از ظرفیت بیشتر نشود» یک قانون روی چند ردیف است، نه یک جفت، و برای آن باید ردیف نگهبان را قفل کنی.In PostgreSQL this is an exclusion constraint: a GiST index built over a combination of columns, with a comparison operator declared for each one. For room booking you write room equals and period overlaps. After that, any insert or update that overlaps an existing row is rejected, and the check runs inside the same transaction, so two simultaneous inserts cannot both slip through. Two practical notes. For ordinary columns such as a room id you need the btree_gist extension so equality works inside a GiST index. And the ranges must be defined with care, because [) and [] treat the boundary differently and a booking that ends at seven must not conflict with one that starts at seven. What the constraint cannot enforce is an aggregate rule: the total number of seats booked must not exceed capacity is a rule over several rows, not a pair, and there you have to lock a guard row instead.
&&عملگر همپوشانی در قید؛ نه تساوی سادهthe overlap operator in the rule, not plain equality
GiSTنوع ایندکسی که این قید رویش ساخته میشودthe index type the rule is built on
تلهٔ رایجThe usual trap
فکر کردن اینکه یک قید طرد جای همهٔ قیدهای «نباید همزمان» را میگیرد. برای قیدهای جمعی — ظرفیت، سقف بودجه، تعداد مجاز — کار نمیکند، چون قانون روی مجموعهای از ردیفهاست نه یک جفت؛ آنجا باید با قفل ردیف نگهبان یا با سطح serializable حل کنی. اشتباه دوم، تعریف بازه با مرز اشتباه است: با [] دو نوبت پشت سر هم هم تعارض پیدا میکنند و کاربران پشت سر هم خطا میگیرند.Believing one exclusion constraint covers every “not at the same time” rule. It cannot express aggregates — capacity, a budget ceiling, a maximum count — because those are rules over a set of rows, not over a pair; there you need a guard row or a serializable level. The second mistake is getting the range boundary wrong: with [] two back-to-back bookings conflict and users get errors for no reason.
همه بله گفتند؛ تصمیم گم شد.Everyone said yes; the decision went missing.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
چهار نفر برای سفر پول کنار گذاشتهاند و همه پیام دادهاند «موافقم». حالا هماهنگکننده میگوید پیام «خریدم» را فرستاده و سه نفر میگویند هیچ پیامی نرسیده. پول کنار گذاشته شده، بلیتها معلوم نیست، و هیچکس هم دروغ نمیگوید. همه گیر کردهاند تا معلوم شود آن پیام واقعاً رفته یا نه.Four people have set money aside for a trip and everyone has replied “I am in”. Now the organiser says the “booked” message went out, and the other three say nothing ever arrived. The money is set aside, the tickets are unknown, and nobody is lying. Everyone is stuck until it is clear whether that message ever left.
تشبیه و تصویر ذهنیThe analogy
چرا کسی خودش تصمیم نمیگیرد؟ چون قرار بود یا همه با هم بروند یا هیچکس. اگر یکی از چهار نفر بگوید «من که پولم را کنار گذاشتم، خودم بلیت میخرم»، آن یکی سفر میکند و بقیه با پول کنارگذاشته و بلیت نخریده میمانند — یعنی همان چیزی که کل قرار برای جلوگیری از آن بود. پس هیچکس تصمیم نمیگیرد و همه منتظر میمانند. این انتظار عجیب نیست؛ تنها رفتار درست است. تنها چیزی که میتواند وضع را باز کند، رسیدنِ همان یک پیام یا دیدنِ دفترِ اوست.Why does nobody decide alone? Because the deal was all of us or none of us. If one of the four says “my money is set aside, I'll just buy my own ticket”, that one travels and the others are left with money set aside and no ticket — exactly what the whole arrangement existed to prevent. So nobody decides and everyone waits. The waiting is not strange; it is the only correct behaviour. The only thing that can unblock it is that one person coming back with the notebook.
و بعدش، فنیAnd now, technically
وقتی شرکتکنندهای رأی بله میدهد، وارد وضعیت آماده میشود: کار را انجام داده، تغییرها را روی دیسک نوشته، ولی کامیت نکرده و قفلهایش را نگه داشته است. از آن لحظه، تصمیمش دست خودش نیست؛ فقط هماهنگکننده میتواند بگوید کامیت یا لغو. اگر هماهنگکننده قبل از رساندن تصمیم بیفتد یا شبکه قطع شود، شرکتکننده در ابهام میماند: نه میتواند کامیت کند، چون شاید تصمیم لغو بوده، و نه میتواند لغو کند، چون شاید بقیه کامیت کرده باشند. این حالت تا وقتی هماهنگکننده برنگردد و لاگش را نخواند ادامه دارد. ابزارهای مدیریتی معمولاً اجازه میدهند یک تراکنش در ابهام را دستی کامیت یا لغو کنی؛ ولی این یک حدس است، نه یک تصمیم، و اگر اشتباه باشد نصف شرکتکنندهها یک طرف رفتهاند و نصف دیگر طرف دیگر. تنها راههای واقعی، کوتاه نگه داشتن این پنجره است: لاگ هماهنگکنندهٔ پایدار و تکثیرشده، شرکتکنندههای تکراریپذیر، و هشدار روی تعداد تراکنشهای در ابهام — نه یک تایماوت که در سکوت حدس بزند.When a participant votes yes it enters the prepared state: the work is done, the changes are written to disk, but nothing is committed and its locks are still held. From that moment the decision is not its own; only the coordinator may say commit or abort. If the coordinator dies before delivering the decision, or the network partitions, the participant is in doubt: it cannot commit because the decision may have been abort, and it cannot abort because the others may have committed. That state lasts until the coordinator comes back and reads its log. Administrative tools usually let you force a commit or a rollback; that is a guess, not a decision, and a wrong guess leaves half the participants on one side and half on the other. The only real answers shorten the window: a durable, replicated coordinator log, idempotent participants, and an alert on the number of in-doubt transactions — not a timeout that guesses quietly.
1 decisionبرای کل تراکنش یک تصمیم هست، و همان گم شدهthe whole transaction has one decision, and that is the one lost
0 guessحدس زدن تصمیم، اتمیک بودن را میشکندguessing the decision breaks atomicity
تلهٔ رایجThe usual trap
باز کردن گره با تایماوت. تراکنش در ابهام با انتظار بدتر نمیشود، ولی با حدس بدتر میشود: یک لغوِ خودکار روی نیمی از شرکتکنندهها اتمیک بودن را میشکند و کسی هم تا روزها نمیفهمد. تایماوت را برای هشدار بگذار، نه برای تصمیم.Unblocking it with a timeout. An in-doubt transaction does not get worse by waiting, but it does get worse by guessing: an automatic rollback on half the participants breaks atomicity and nobody notices for days. Keep the timeout for alerting, not for deciding.
چقدر اجازه میدهی این دو تراکنش همدیگر را ببینند؟How much do you let these two transactions see of each other?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
با همسرت یک لیست خرید مشترک دارید. هر دو همزمان بازش میکنید: تو «نان» را اضافه میکنی، او «شیر» را. اگر برنامه هر تغییر را روی نسخهٔ خودش بنویسد، آخرش یکی از دو قلم گم میشود؛ اگر نوبتی و با دیدن تغییر دیگری بنویسد، هر دو میمانند. سطح ایزولاسیون همان تصمیم است که دو نفر چقدر همدیگر را ببینند.You and your partner share a shopping list. You both open it at the same time: you add bread, they add milk. If the app writes each change onto its own copy, one of the two items disappears; if it takes turns and shows the other's change, both stay. The isolation level is that decision about how much the two of you see of each other.
تشبیه و تصویر ذهنیThe analogy
چهار جور میشود دو نفر را در یک آشپزخانه کنار هم گذاشت. اول، هیچ قاعدهای نیست و هر کس هر چه میخواهد بردارد: سریعترین و پرحادثهترین. دوم، کسی غذایی را که دارد میپزد به دیگری نشان نمیدهد، ولی قابلمهٔ آماده را هر کسی میتواند بردارد. سوم، هر کس از اول آشپزخانه را همانطور که بوده عکس میگیرد و تا آخر کار فقط عکس خودش را میبیند. چهارم، یک نفر در آشپزخانه و بقیه در صف. هیچکدام «بهترین» نیست؛ هر کدام یک نوع حادثه را میبندد و یک هزینه را باز میکند.There are four ways to put two people in one kitchen. First, no rules at all and everyone grabs what they want: fastest and most accident-prone. Second, nobody shows the dish they are still cooking, but any finished pot can be taken. Third, each person photographs the kitchen at the start and works only from that photo until the end. Fourth, one person in the kitchen and everyone else in a queue. None is “best”; each closes one kind of accident and opens one kind of cost.
از بالا به پایین: هر پله که پایین میآیی یک تضمین را میفروشی و یک ناهنجاری را میخری.Top to bottom: every step down sells one guarantee and buys one anomaly.
و بعدش، فنیAnd now, technically
چهار سطح استاندارد داری و هر کدام مجموعهٔ مشخصی از ناهنجاریها را مجاز میداند. read uncommitted حتی خواندن کثیف را اجازه میدهد و ارزانترین است. read committed فقط دادهٔ کامیتشده را نشان میدهد، پس خواندن کثیف و نوشتن کثیف را میبندد، ولی دو خواندن پشت سر هم در یک تراکنش میتوانند جوابهای مختلف بدهند — این همان read skew است. snapshot isolation به هر تراکنش یک عکس ثابت از لحظهٔ شروعش میدهد و read skew و شبح را در کوئریهای فقطخواندنی میبندد؛ lost update در آن «بستگی دارد»، چون بعضی پیادهسازیها مثل PostgreSQL تشخیصش میدهند و بعضی مثل MySQL/InnoDB نه، و write skew باز میماند. serializable همهٔ ستونها را میبندد و هزینهاش انتظار یا abort و retry است. یک هشدار عملی: اسم سطحها در موتورها یک معنا ندارد — repeatable read در PostgreSQL یعنی snapshot isolation، در Db2 یعنی serializability، و Oracle اسم serializable را روی چیزی میگذارد که عملاً snapshot isolation است. پس قرارداد همان محصول و همان نسخه را بخوان، نه اسم را.There are four standard levels and each allows a specific set of anomalies. Read uncommitted even permits dirty reads and is the cheapest. Read committed shows committed data only, so it stops dirty reads and dirty writes, but two reads in a row inside one transaction can disagree — that is read skew. Snapshot isolation gives each transaction a fixed picture from its start and closes read skew and phantoms for read-only queries; lost updates in it “depend”, because some implementations such as PostgreSQL detect them and others such as MySQL/InnoDB do not, and write skew stays open. Serializable closes every column and pays in waiting or in aborts and retries. One practical warning: the names do not mean the same thing everywhere — repeatable read in PostgreSQL is snapshot isolation, in Db2 it is serializability, and Oracle puts the name serializable on what is effectively snapshot isolation. Read the contract of that product and version, not the label.
4سطح استاندارد، از read uncommitted تا serializablestandard levels, from read uncommitted up to serializable
?ستون lost update در snapshot isolation: بستگی به پیادهسازی داردlost updates under snapshot isolation: it depends on the implementation
0ناهنجاری باقیمانده در serializableanomalies left open at serializable
اسمها دروغ میگویندThe names lie
استاندارد SQL در سالی نوشته شد که snapshot isolation هنوز وجود نداشت، پس repeatable read را مبهم گذاشت و بعدها هر موتور آن را جور خودش پر کرد. نتیجه این است که یک اسم میتواند در چهار محصول چهار معنا داشته باشد و تیمها با همان اسم تصمیم میگیرند. راهحل عملی این است که هیچوقت از اسم سطح به رفتار نرس: به جای «ما repeatable read هستیم»، بگو «ما snapshot isolation هستیم و write skew در ما ممکن است». این جمله در جلسهٔ طراحی بیشتر از هر جدولی جلوی خرابی را میگیرد، چون دقیقاً میگوید چه چیزی هنوز باز است.The SQL standard was written in a year when snapshot isolation did not exist yet, so it left repeatable read vague and every engine later filled the gap its own way. The result is one name meaning four things in four products, while teams decide by the name. The practical fix is never to travel from the label to the behaviour: instead of “we run repeatable read”, say “we run snapshot isolation, and write skew is possible here”. That sentence prevents more damage in a design meeting than any table, because it says exactly what is still open.
کدام سطح چه چیزی را میبنددWhat each level closes
read committedread committed
snapshot isolationsnapshot isolation
serializableserializable
خواندن کثیفdirty read
جلوگیریprevented
جلوگیریprevented
جلوگیریprevented
read skewread skew
ممکن استpossible
جلوگیریprevented
جلوگیریprevented
بهروزرسانی گمشدهlost update
ممکن استpossible
بستگی داردdepends
جلوگیریprevented
write skewwrite skew
ممکن استpossible
ممکن استpossible
جلوگیریprevented
شبح (phantom)phantom read
ممکن استpossible
جلوگیریprevented
جلوگیریprevented
تلهٔ رایجThe usual trap
انتخاب سطح از روی عادت یا از روی پیشفرض موتور. پیشفرض MySQL/InnoDB یعنی repeatable read است و همین میتواند روی جدول خالی قفل گپ بگیرد و دو تراکنش را به بنبست ببرد، در حالی که read committed همان کار را بدون آن قفل انجام میدهد. سطح را از روی نیاز انتخاب کن، نه از روی تجربهٔ پروژهٔ قبلی.Picking the level out of habit or from the engine's default. The MySQL/InnoDB default is repeatable read, and it can take a gap lock on an empty table and deadlock two transactions, while read committed does the same job without that lock. Choose the level from the requirement, not from your last project.
دو نفر یک عدد را از ۱۰ به ۱۱ بردند؛ نتیجه ۱۱ شد.Two people raised a number from 10 to 11. It ended at 11.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
تو و همسرت هر دو با یک کارت هدیه خرید میکنید. هر دو موجودی صد هزار تومان را میبینید. هر دو شصت هزار تومان خرج میکنید و هر دو خرید موفق میشود. موجودی آخر چهل هزار تومان است، نه منفی بیست — یعنی یکی از دو خرید اصلاً از حساب کم نشده.You and your partner both shop with the same gift card. You both see a balance of 100,000. You both spend 60,000 and both purchases succeed. The balance ends at 40,000, not minus 20 — one of the two purchases never came off the card.
تشبیه و تصویر ذهنیThe analogy
دفتر موجودی یک مغازه را تصور کن. دو فروشنده همزمان یک جنس میفروشند. هر دو عدد ۱۰ را در دفتر میبینند، هر دو در ذهنشان ۹ حساب میکنند و هر دو ۹ را مینویسند. دو جنس از قفسه رفته، ولی دفتر میگوید ۹. سه راهحل وجود دارد: به جای نوشتن عدد نهایی خودِ تغییر را بنویس («یکی کم شد»)، دفتر را قفل کن تا یکی صبر کند، یا شمارهٔ صفحه را چک کن و اگر عوض شده از نو بخوان. هر سه یک چیز میخواهند: تصمیم روی عددی گرفته نشود که تا لحظهٔ نوشتن کهنه شده.Picture a shop's stock ledger. Two assistants sell one item at the same time. Both read 10 in the book, both work out 9 in their heads, both write 9. Two items left the shelf but the book says 9. There are three answers: write the change instead of the total (“one less”), lock the book so one of them waits, or check the page number and re-read if it moved. All three want the same thing: no decision made on a number that went stale before the ink dried.
هر دو از یک مقدار قدیمی شروع میکنند؛ هر دو مقدار محاسبهشده را مینویسند و یکی میبازد.Both start from the same old value, both write a computed one, and one of them loses.
و بعدش، فنیAnd now, technically
بهروزرسانی گمشده وقتی رخ میدهد که دو تراکنش یک مقدار را بخوانند، هر کدام در برنامه تغییرش دهند و مقدار محاسبهشده را بنویسند. نکتهٔ ظریف این است که این وضعیت نوشتن کثیف نیست: نوشتن دوم بعد از کامیت اولی میرسد، پس قفلی سر راهش نیست و موتور اصلاً تعارضی نمیبیند. در read committed این الگو کاملاً مجاز است. چهار درمان دارد و هر کدام قیمتی دارد: عملیات اتمی در خود پایگاه مثل SET n = n - 1 که چرخهٔ خواندن را حذف میکند و بهترین گزینه است اگر منطق در همان یک دستور جا شود؛ قفل صریح با SELECT ... FOR UPDATE که وقتی منطق برنامه باید وسط کار اجرا شود لازم است و بنبست میسازد؛ مقایسه و نوشتن با ستون نسخه که برای سیستمهای بدون تراکنش کار میکند و در تعارض باید retry شود؛ و تشخیص خودکار توسط موتور، که در بعضی پیادهسازیهای snapshot isolation هست و در بعضی نیست. در دیتابیسهای چندلیدر، last-write-wins پیشفرض عملاً یک ماشین تولید بهروزرسانی گمشده است.A lost update happens when two transactions read one value, each changes it in application code, and each writes the computed result. The subtle part is that this is not a dirty write: the second write arrives after the first commit, so no lock stands in its way and the engine sees no conflict at all. Under read committed the pattern is fully allowed. There are four remedies, each with a price: a built-in atomic operation such as SET n = n - 1, which removes the read from the cycle and is the best seat when the logic fits one statement; an explicit lock with SELECT ... FOR UPDATE, needed when application logic must think mid-flight, and which brings deadlocks; a compare-and-write using a version column, which suits stores without transactions and must retry on conflict; and automatic detection by the engine, present in some snapshot-isolation implementations and absent in others. In multi-leader databases, the default last-write-wins is effectively a lost-update machine.
10 → 11دو افزایش، یک نتیجه؛ یکی از دو تغییر گم شدtwo increments, one result; one of the two changes vanished
LWWپیشفرض چندلیدر که بهروزرسانی گمشده میسازدthe multi-leader default that manufactures lost updates
چرا تشخیص خودکار همهجا نیستWhy auto-detection is not everywhere
برای تشخیص بهروزرسانی گمشده، موتور باید بداند تراکنشی که نوشته را مینویسد، قبلاً همان ردیف را خوانده است یا نه. این یعنی نگه داشتن تاریخچهٔ خواندنها و مقایسهاش در لحظهٔ نوشتن — دقیقاً همان دفترداریای که SSI انجام میدهد. بعضی موتورها این کار را در سطح snapshot isolation انجام میدهند و بازنده را abort میکنند، ولی این رفتار بخشی از قرارداد آن سطح نیست، یک اضافهٔ پیادهسازی است. به همین دلیل نمیشود روی «اگر ما snapshot isolation داریم پس امن است» حساب کرد؛ باید در مستندات همان نسخه بگردی و ببینی آیا از serialization failure خطا میدهد یا نه.To detect a lost update the engine must know whether the transaction doing the write had read that row earlier, which means keeping a history of reads and checking it at write time — the same bookkeeping SSI does. Some engines do it at snapshot isolation and abort the loser, but that behaviour is not part of what the level promises; it is an implementation extra. So “we run snapshot isolation, therefore we are safe” does not follow. Go and read that version's documentation to see whether it raises a serialization failure at all.
تلهٔ رایجThe usual trap
گمان کردن اینکه چون نوشتن در سطح ردیف اتمی است، خواندن-تغییر-نوشتن هم اتمی است. موتور فقط میداند مقدار را مینویسی؛ نمیداند آن مقدار از خواندن کهنه آمده. ORM هم این دام را پنهان میکند: ذخیرهٔ یک رکورد خواندهشده، بیسروصدا همان چرخه را مینویسد.Assuming that because a row write is atomic, read-modify-write is atomic too. The engine only knows you wrote a value; it does not know the value came from a stale read. An ORM hides the trap: saving a record you read earlier quietly performs that same cycle.
وقتی چیزی برای قفلکردن نیست، چه میکنی؟What do you lock when there is nothing there to lock?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
ماشین خانه یک دانه است و روی در یخچال یک تقویم هفتگی آویزان است؛ برای پنجشنبه شش تا هفت، فقط نمینویسی «من میخواهم» — با ماژیک روی همان خانهٔ ساعت خط میکشی. از آن لحظه هر کسی تقویم را نگاه کند میبیند آن خانه گرفته شده، حتی اگر ماشین همانجا پارک باشد. وقتی چیزی برای گرفتن وجود ندارد، اول خودت چیزی میسازی که بشود گرفت.There is one family car, and a weekly calendar hangs on the fridge door: for Thursday, six to seven, you do not just write “I want it” — you draw a line with a marker through that hour's box. From that moment anyone who looks at the calendar sees the box is taken, even if the car is parked outside. When there is nothing to grab, you first make something that can be grabbed.
تشبیه و تصویر ذهنیThe analogy
همین تقویم را جدی بگیر. برای اینکه خط ماژیک معنا داشته باشد، باید خانهها از پیش روی کاغذ باشند: یک خانه برای هر ساعت. اگر خانهها یکساعته باشند، کسی که نیم ساعت ماشین میخواهد باید دو خانه را بگیرد، و کسی که فقط به همان نیم ساعت دوم کار دارد بیخود بیرون میماند. اگر نیمساعته باشند، کاغذ شلوغ میشود و هر رزرو چند خط میخورد. بدتر از همه، اگر قانون عوض شود و بگوییم «ماشین باید تا هفت در پارکینگ باشد»، دیگر هیچ خانهای این شرط را نشان نمیدهد و باید کل تقویم را از نو طراحی کنی. اینجا بهای کار معلوم میشود: کار برای تو راحت شد، ولی شکلِ دادهات را از روی نیازِ قفل چیدی.Take that calendar seriously. For a marker line to mean anything, the boxes have to exist on paper beforehand: one box per hour. Hourly boxes force someone who needs half an hour to take two, and anyone who only needs the second half waits outside for no reason. Half-hour boxes make the page crowded, and every booking crosses several of them. Worse, if the rule changes — say the car must be back in the garage by seven — no box can express that, and the whole calendar has to be redesigned. That is the price: your job got easier, but you bent your data into the shape the lock needs.
دو راه برای گرفتنِ چیزی که هنوز نیست: پایگاه داده بازه را قفل میکند، یا تو خودت ردیف میسازی.Two ways to catch what does not exist yet: the database locks the range, or you build the row yourself.
و بعدش، فنیAnd now, technically
مسئله از phantom میآید. تراکنشی میخواند «هیچ رزروی برای این بازه نیست» و میخواهد بنویسد؛ ولی ردیفی وجود ندارد که قفل شود، و دو تراکنش همزمان هر دو مینویسند. مادیسازی تعارض این شکاف را با ساختن ردیف پر میکند: جدولی از خانههای زمان میسازی، مثلاً یک ردیف برای هر اتاق و هر ربع ساعت، و تراکنش بهجای «چک کردن»، در خانههای موردنیاز درج میکند. حالا تعارض روی یک کلید یکتا یا یک قفل معمولی ردیف اتفاق میافتد و موتور میتواند بگیردش. هزینهاش سه چیز است: باید خانهها را از قبل بسازی و نگه داری، دانهبندی خانهها تعارض الکی میسازد، و هر تغییری در شرط جستوجو یعنی تغییر شکل جدول. به همین دلیل کتاب آن را راهحل آخر میداند: مکانیزم همزمانی از پایگاه داده بیرون میزند و به مدل دادهات نشت میکند.The problem starts with a phantom. A transaction reads “there is no booking for this range” and wants to insert, but there is no row to lock, so two transactions can both pass the check and both write. Materializing conflicts fills the gap by creating the rows: you keep a table of time slots, say one row per room per quarter hour, and the transaction inserts into the slots it needs instead of only checking. The conflict now happens on a unique key or an ordinary row lock, which the engine already knows how to enforce. The bill has three lines: the slots must be built and maintained in advance, their granularity creates false conflicts, and any change to the search condition means reshaping the table. That is why the book calls it a last resort: the concurrency mechanism leaks out of the database and into your data model.
1 row / slotبرای هر خانه یک ردیف واقعی که میشود قفلش کردone real row per slot, so each slot can be locked
0 predicateهیچ گزارهای قفل نمیشود؛ خودت ردیف ساختهایno predicate is locked; you built the rows instead
unique keyبدون قید یکتا، ردیفها فقط تزئیناندwithout a unique constraint the rows are decoration
تلهٔ رایجThe usual trap
جدول خانهها را میسازی و پر میکنی، ولی روی (اتاق، زمان) کلید یکتا نمیگذاری. آنوقت جدول فقط یک تقویم تزئینی است: دو تراکنش همزمان دو ردیف جدا درج میکنند، هر دو موفق میشوند، و اتاق دو بار رزرو شده. مادیسازی بدون یک قید واقعی که درج دوم را رد کند، هیچچیز را قفل نمیکند.You build and fill the slot table but forget the unique key on (room, time). Now the table is a decorative calendar: two transactions insert two separate rows, both succeed, and the room is booked twice. Materialization without a real constraint that rejects the second insert locks nothing at all.
خواندن، کسی را قفل نمیکند. نوشتن هم.A read blocks nobody. A write blocks nobody either.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
صفحهٔ ویکی تیمت را باز کردهای و مشغول خواندنی. همکارت همان لحظه یک پاراگراف را عوض میکند و ذخیره میکند. صفحه زیر دستت عوض نمیشود، خطایی هم نمیگیری؛ تو همان نسخهٔ قبلی را میخوانی و او هم معطل تو نمیشود. آخر کار دو نسخه از یک صفحه وجود داشته و هیچکس منتظر دیگری نمانده.You have your team's wiki page open and you are reading. A colleague edits a paragraph and saves at that very moment. The page under your eyes does not change and you get no error; you read the earlier version and they are not held up by you. At the end there were two versions of one page and nobody waited for anybody.
تشبیه و تصویر ذهنیThe analogy
کتابخانهٔ محله را تصور کن که از یک کتاب چند نسخه دارد. هر کسی میآید، نسخهای را میگیرد که همان لحظه روی قفسه است. کسی که دیرتر میآید نسخهٔ تازهتر را میگیرد؛ کسی که هنوز نسخهٔ قبلی دستش است همان را میخواند و کسی مجبورش نمیکند عوض کند. نتیجه این است که هیچ خوانندهای کتابدار را معطل نمیکند و هیچ کتابداری کتاب را از دست خواننده نمیکشد. بهایش این است که قفسه پر میشود از نسخههای قدیمی و باید هر چند وقت یک بار کسی بیاید و نسخههایی را که دیگر کسی نگه نداشته جمع کند.Picture a neighbourhood library that keeps several copies of one book. Every visitor takes the copy that is on the shelf at that moment. Someone arriving later gets the newer copy; someone still holding the older one keeps reading it and nobody forces a swap. So no reader holds up the librarian and no librarian snatches a book out of a reader's hands. The price is a shelf filling up with old copies, and someone has to come round now and then and clear the ones nobody is holding any more.
نوشتن روی نسخهٔ قبلی نمینویسد؛ نسخهٔ تازه میسازد و خوانندهٔ قدیمی نسخهٔ خودش را میبیند.A write does not overwrite the old version; it creates a new one, and an old reader keeps their own.
و بعدش، فنیAnd now, technically
در MVCC نوشتن هیچوقت نسخهٔ موجود را بازنویسی نمیکند؛ یک نسخهٔ تازه اضافه میشود و نسخهٔ قبلی با یک مهر حذف باقی میماند تا خوانندههای قدیمی بتوانند ببینندش. هر تراکنش در لحظهٔ شروع مجموعهٔ تراکنشهای در جریان را ثبت میکند و بعد با قواعد visibility تصمیم میگیرد کدام نسخه را ببیند: نوشتن تراکنشهایی که در آن لحظه در جریان بودند نادیده گرفته میشود، حتی اگر بعداً کامیت کنند؛ نوشتن تراکنشهایی که بعد از ما شروع شدهاند هم نادیده گرفته میشود؛ و نوشتن تراکنشهای abortشده همیشه پنهان است. نتیجهٔ این قاعده ساده است: خوانندهها نویسندهها را قفل نمیکنند و نویسندهها خوانندهها را. این پایهٔ snapshot isolation است، ولی خودش serializability نیست — دو تراکنش میتوانند روی نسخههای جدا تصمیم بگیرند و write skew بسازند. هزینهاش هم واقعی است: هر ردیف چند نسخه دارد، ایندکسها به نسخهها اشاره میکنند و باید بهروز بمانند، و یک snapshot طولانی پاکسازی را عقب میاندازد و انبار نسخهها را باد میکند.Under MVCC a write never overwrites the existing version; a new version is added and the old one stays behind with a deletion stamp so older readers can still see it. Each transaction records the set of in-flight transactions at its start and then uses visibility rules to decide which version it sees: writes from transactions that were in flight then are ignored even if they commit later, writes from transactions that started after us are ignored too, and writes from aborted transactions are always hidden. The consequence is simple: readers never block writers and writers never block readers. That is the base of snapshot isolation, but it is not serializability by itself — two transactions can decide on separate versions and build write skew. The cost is real: several versions per row, indexes that point at versions and must be maintained, and a long snapshot that holds cleanup back and bloats the store.
0 lockخواندن و نوشتن همدیگر را قفل نمیکنندreads and writes never lock each other
N نسخههر ردیف میتواند چند نسخه داشته باشد، نه یکیa row can carry several versions, not one
oldest snapshotسقف پاکسازی؛ هرچه قدیمیتر، نسخههای بیشتری زنده میمانندthe cleanup ceiling; the older it is, the more versions stay alive
چرا نسخهها باید جمع شوندWhy versions have to be collected
هر نسخهای که نگه داشته میشود، فضای دیسک و کار ایندکس دارد. موتور نمیتواند نسخهای را پاک کند که هنوز ممکن است کسی ببیندش، و سقف این کار قدیمیترین snapshot زندهٔ سیستم است. یعنی یک تراکنش گزارشگیری که کسی فراموش کرده ببندد، کل تاریخچه را زنده نگه میدارد و دیسک باد میکند؛ همین اتفاق در جدولهای بزرگ به یک حادثهٔ عملیاتی واقعی تبدیل میشود. دو کار کمک میکند: کوتاه نگه داشتن تراکنشهای تعاملی، و مانیتور کردن قدیمیترین snapshot بهعنوان یک متریک — نه بهعنوان چیزی که فقط وقت خطا نگاهش میکنی.Every version kept costs disk and index work. The engine cannot drop a version somebody might still see, and the ceiling on that is the oldest live snapshot in the system. So one reporting transaction that nobody closed keeps the whole history alive and the disk grows; on a large table that becomes a real operational incident. Two things help: keep interactive transactions short, and watch the oldest snapshot as a live metric — not as something you look at only after the alert.
تلهٔ رایجThe usual trap
باز گذاشتن یک snapshot طولانی — گزارش تحلیلی، یا تراکنش تعاملیای که کسی فراموش کرده ببندد — و بعد تعجب از باد کردن دیسک. پاکسازی نسخههای مرده نمیتواند از قدیمیترین snapshot رد شود؛ یک تراکنش جامانده کل تاریخچه را زنده نگه میدارد.Leaving one snapshot open — an analytical report, or an interactive transaction somebody forgot to close — and then wondering why the disk grows. Cleanup cannot pass the oldest snapshot, so one forgotten transaction keeps the whole history alive.
چرا از اول قفل بزنیم اگر کسی مزاحم نشود؟Why lock up front when nobody gets in your way?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
در اپ سینما صندلی F12 را انتخاب میکنی، بعد چند دقیقه مشغول انتخاب تنقلات میشوی و آخرش پرداخت میکنی. اپ صندلی را در آن چند دقیقه نگه نداشته؛ فقط لحظهٔ پرداخت نگاه میکند و میگوید «همین الان فروش رفت»، و تو باید از اول انتخاب کنی. این همان معاملهٔ خوشبینانه است: آزادی در طول راه، بررسی در لحظهٔ آخر.In a cinema app you pick seat F12, spend a few minutes choosing snacks, then pay. The app never held the seat for you; it only looks at payment time, says “that seat just sold”, and you start over. That is the optimistic bargain: freedom along the way, one check at the end.
تشبیه و تصویر ذهنیThe analogy
همین صندلی را با یک سالن پر مقایسه کن. اگر فقط تو و دو نفر دیگر دنبال F12 باشید، هیچکس معطل نمیشود، هیچ قفلی نگه داشته نمیشود، و بیشتر وقتها همهچیز در یک رفتوبرگشت حل میشود. حالا تصور کن اکران اول است و هزار نفر همزمان روی ده صندلی باقیمانده میپرند: همه نقشه را میخوانند، همه تنقلات را انتخاب میکنند، و همه به جز ده نفر آخرش برمیگردند. کارِ هدررفته اینجا از یک صف منظم هم بیشتر است. برای همین سیستمهای خوشبینانه روی دادهٔ داغ بدتر از بدبینانه عمل میکنند: قفل چیزی را نگه نمیدارد، ولی همه یک بار کل مسیر را میروند و آخرش میبازند.Compare that seat with a packed hall. If only you and two others want F12, nobody waits, no lock is held, and most of the time it resolves in one round trip. Now picture opening night: a thousand people jump on the ten remaining seats at once. All of them read the map, all pick snacks, and all but ten walk away at the end. The wasted work is now worse than an orderly queue. That is why optimistic systems do badly on hot data: no lock is held, but everyone runs the whole path once and loses at the finish line.
هر دو تراکنش را امن میکنند، ولی یکی اول میبندد و دیگری آخر میسنجد.Both keep transactions safe, but one blocks at the start and the other checks at the end.
و بعدش، فنیAnd now, technically
کنترل همزمانی خوشبینانه سه گام دارد: خواندن، اعتبارسنجی، نوشتن. در گام خواندن، تراکنش هر چیزی را که میخواند و نسخهاش را نگه میدارد. در گام اعتبارسنجی، پیش از کامیت بررسی میکند که هیچکدام از آن چیزها از زمان خواندن عوض نشده باشد. اگر عوض شده باشد، تراکنش abort میشود و کل کار از نو انجام میشود. مزیتش این است که تا لحظهٔ آخر قفلی نگه داشته نمیشود، پس خواندنهای طولانی و تراکنشهای فقط-خواندنی هیچکس را بلاک نمیکنند. هزینهاش دو چیز است: کارِ هدررفته در هر abort، و رقابت شدید روی ردیفهای داغ که همه را با هم شکست میدهد. پیادهسازی سادهاش یک ستون version و یک UPDATE شرطی است؛ پیادهسازی جدیاش، مثل SSI، همین ایده را روی گراف سریالسازی دنبال میکند.Optimistic concurrency control has three phases: read, validate, write. In the read phase the transaction records everything it reads and the version of each item. In the validate phase, before commit, it checks that none of those items changed since it read them. If one did, the transaction aborts and the whole thing is redone. The advantage is that no lock is held until the very end, so long reads and read-only transactions block nobody. The price has two parts: wasted work on every abort, and heavy contention on hot rows, where everyone fails together. The simple implementation is a version column and a conditional UPDATE; the serious one, such as SSI, runs the same idea over a serialization graph.
0 locksتا لحظهٔ تأیید هیچ قفلی نگه داشته نمیشودno lock is held until commit time
1 versionیک ستون نسخه، سادهترین پیادهسازیone version column is the simplest form
تلهٔ رایجThe usual trap
فقط همان ردیفی را اعتبارسنجی میکنی که میخواهی بنویسی، نه چیزهایی را که خواندهای. مثلاً جمع موجودی دو حساب را میخوانی و بعد فقط حساب خودت را با نسخه چک میکنی؛ آنوقت هر دو تراکنش سبز میشوند و شرط «جمع ثابت بماند» میشکند. اعتبارسنجی باید روی کل مجموعهٔ خواندهشده باشد، وگرنه فقط یک قفل تزئینی ساختهای.You validate only the row you are about to write, not the rows you read. You read two balances to check a total, then confirm only your own account's version, so both transactions pass and the invariant breaks. Validation has to cover the whole read set, or it is a decorative lock.
جستوجو خالی بود؛ حالا یک ردیف تازه هست.Your search came back empty. Now there is a row that fits.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
میخواهی برای ساعت پنج نوبت دکتر بگیری. سیستم میگوید آن ساعت خالی است. همان لحظه کس دیگری هم همان ساعت را میبیند و نوبت میگیرد. تو هم نوبت میگیری. حالا دو نفر برای یک ساعت نوبت دارند و هیچکدام دروغ نگفته.You want the five o'clock appointment. The system says that slot is free. At that same moment somebody else sees the same free slot and books it. You book it too. Now two people hold one slot, and neither of them lied.
تشبیه و تصویر ذهنیThe analogy
دفتر نوبت آرایشگاه را تصور کن. صفحهٔ ساعت پنج خالی است. دو مشتری همزمان نگاه میکنند، هر دو «خالی» میبینند، هر دو اسمشان را مینویسند. هیچکس ردیفی را بازنویسی نکرده؛ مشکل این است که «نبودن» را نمیشود قفل کرد. سه راه هست: کل صفحهٔ آن روز را قفل کنی تا یکی اول بنویسد، یا از اول برای هر ربع ساعت یک خط چاپ کنی تا هر کس خط خودش را قفل کند، یا کل دفتر را نوبتی پر کنی. راه دوم ارزانتر است و به همین دلیل بیش از همه استفاده میشود.Picture the appointment book at a barber shop. The five o'clock line is blank. Two customers look at the same time, both see blank, both write their name. Nobody overwrote a row; the problem is that you cannot lock an absence. There are three ways: lock the whole page for that day so one person writes first, print a line for every quarter hour in advance so each person locks their own line, or fill the book one customer at a time. The second is the cheapest, which is why it is used most.
چک کردن نبودن، تصمیم گرفتن، و بعد درج ردیفی که همان شرط را میشکند.Check for absence, decide, then a row arrives that breaks the very condition you checked.
و بعدش، فنیAnd now, technically
شبح وقتی ظاهر میشود که یک تراکنش با شرطی جستوجو کند، تراکنش دیگری ردیفی درج کند که در آن شرط جا میگیرد، و تصمیم اولی بر اساس «نبودن» گرفته شده باشد. این همان الگوی سهمرحلهای است: چک کن، تصمیم بگیر، بنویس. نکتهٔ عملی این است که SELECT ... FOR UPDATE روی نتیجهٔ خالی چیزی برای قفل کردن ندارد — هوا را که نمیشود قفل کرد. درمان کلاسیک predicate lock است: قفل روی همهٔ ردیفهایی که با شرط جور میشوند، حتی آنهایی که هنوز وجود ندارند؛ دقیق است ولی جور کردن این قفلها گران است. تقریب عملیاش index-range locking است: قفل روی بازهای از ایندکس که شرط را پوشش میدهد؛ گاهی بیشتر از لازم قفل میکند، ولی هزینهاش خیلی کمتر است. اگر ایندکسی برای شرط نباشد، موتور به قفل کل جدول برمیگردد. snapshot isolation شبح را در کوئریهای فقطخواندنی میبندد، ولی در الگوی نوشتن نه؛ آنجا فقط serializability یا قفل بازهای کار میکند. آخرین راه هم materializing conflicts است: ساختن جدولی که فقط برای قفل کردن وجود دارد — کثیف، ولی جواب میدهد.A phantom appears when one transaction searches with a condition, another inserts a row that fits that condition, and the first one's decision was based on absence. That is the three-beat pattern: check, decide, write. The practical catch is that SELECT ... FOR UPDATE on an empty result has nothing to lock — you cannot lock thin air. The classic cure is a predicate lock: a lock on everything that matches the condition, including rows that do not exist yet. It is exact, and matching those locks is expensive. The practical approximation is index-range locking: a lock on the slice of an index that covers the condition, which sometimes over-locks but costs far less. With no suitable index the engine falls back to a whole-table lock. Snapshot isolation closes phantoms for read-only queries but not for the writing pattern; there only serializability or a range lock helps. The last resort is materializing conflicts: a table that exists purely to be locked — ugly, but it works.
0 rowsقفل روی نتیجهٔ خالی چیزی برای گرفتن نداردa lock on an empty result has nothing to hold
index rangeتقریب عملی predicate lock؛ کمی بیشتر قفل، خیلی کمتر هزینهthe practical stand-in for a predicate lock: more locking, far less cost
SSIشبح را بدون قفل، با ردیابی خواندنها میبنددcloses phantoms without locks, by tracking what was read
چرا شبح با write skew یکی استWhy the phantom is write skew
در مثال دو پزشک، ردیفی که عوض میشود همان ردیفی است که جستوجو برگردانده، پس یک قفل ردیف میتواند بگیردش. در مثال نوبت دکتر، جستوجو دنبال نبودن ردیف است و نوشتن ردیف تازهای میسازد که شرط را میشکند؛ آنجا چیزی برای قفل کردن نیست. همین تفاوت است که شبح را از باقی ناهنجاریها جدا میکند: مسئله در ردیف نیست، در بازهای است که هنوز خالی است. برای همین هم درمانش از جنس بازه است، نه از جنس ردیف — predicate lock، index-range lock، یا ردیابی خواندنها در SSI.In the two-doctors example the row being changed is the row the search returned, so a row lock can grab it. In the appointment example the search looks for absence and the write creates the row that breaks the condition; there is nothing to lock. That difference is what separates a phantom from the rest: the problem is not a row, it is a range that is still empty. Which is why the cure is range-shaped too — a predicate lock, an index-range lock, or read tracking under SSI.
تلهٔ رایجThe usual trap
تکیه کردن به SELECT ... FOR UPDATE برای چک کردن «چیزی نیست». وقتی نتیجه خالی است هیچ ردیفی قفل نمیشود و دو تراکنش همزمان هر دو میتوانند درج کنند. اگر شرطت وجود یا نبودن است، قفل باید روی چیزی باشد که همیشه هست — ردیف اتاق، یا بازهای از ایندکس.Relying on SELECT ... FOR UPDATE to check that “nothing is there”. An empty result locks no rows, so two transactions can both insert. If your condition is about presence, the lock has to sit on something that always exists — the room row, or a slice of an index.
قفل روی «هر چیزی که این شرط را دارد» یعنی چه؟What does it mean to lock “everything that matches this condition”?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
روز اسبابکشی، سه جای خالی جلوی ساختمان را با مخروط میبندی، نه فقط جایی که خودت پارک میکنی. تا عصر اگر ماشین تازهای هم بیاید، مخروطها همانجا هستند و راننده میفهمد این محدوده گرفته شده. قفل روی «این سه جای خالی» است، نه روی ماشینهایی که الان ایستادهاند؛ به همین دلیل هر چیزی که بعداً هم بیاید داخل محدوده میافتد.On moving day you cone off three empty spots in front of the building, not just the one your truck will use. If another car arrives before evening, the cones are still there and the driver knows the stretch is taken. The lock sits on “these three empty spots”, not on the cars parked now — which is why anything that arrives later still falls inside it.
تشبیه و تصویر ذهنیThe analogy
این کار دو طرف دارد. اگر فقط جای دقیق ماشین حمل را خالی کنی و بقیه را آزاد بگذاری، تا ظهر یک ماشین تازه آنجا میایستد و کارت لنگ میماند؛ این همان چیزی است که یک نگاه ساده نمیتواند جلویش را بگیرد. اگر هم کل خیابان را ببندی، همسایهای که اصلاً به آن سه جا کار ندارد هم گیر میافتد و شکایت میکند. کار درست این است که محدوده را دقیقاً بهاندازهٔ همان سه جا بگیری: چیزی که الان هست، و چیزی که تا پایان کار ممکن است آنجا پیدا شود. تازه هر بار که یک جای جدید به آن محدوده اضافه شود، مخروط باید از قبل آنجا باشد؛ وگرنه همان شکاف اول برمیگردد.That move cuts both ways. Cone off only the exact spot the truck needs and leave the rest free, and by noon a new car is parked there and the job stalls; that is exactly what a plain read cannot prevent. Cone off the whole street and a neighbour who never wanted those three spots is blocked too, and complains. The right answer is a range cut to the size of the job: what exists now, plus whatever may appear there before you finish. And every time the range gains a new spot, the cone has to be there in advance, or the original gap comes back.
مخروطها کل محدوده را میگیرند، از جمله جاهای خالی؛ ردیف پایین دستنخورده میماند.The cones take the whole stretch, empty spots included; the row below stays free.
و بعدش، فنیAnd now, technically
قفل گزارهای همهٔ ردیفهایی را میگیرد که با یک شرط جور درمیآیند، از جمله ردیفهایی که هنوز درج نشدهاند؛ به همین دلیل جلوی phantom را میگیرد. اگر تراکنشی میخواند «هیچ رزروی برای این اتاق و این بازه نیست»، قفل روی خودِ همان شرط مینشیند، نه روی ردیفهای موجود. اجرای دقیق این کار گران است، چون موتور باید هر پرسوجوی تازه را با هزاران شرط مقایسه کند. برای همین اکثر پایگاهها بهجای آن قفل بازهٔ ایندکس میگذارند: بازهای از ایندکس را که پیمایش شده قفل میکنند و درج هر کلیدی داخل آن بازه منتظر میماند. نتیجه دو چیز است: اگر ایندکس مناسبی نداشته باشی، موتور مجبور است کل جدول را قفل کند و نوشتنهای بیربط هم صف میکشند؛ و اگر بازه را درشت بگیری، رقابت الکی میسازی.A predicate lock covers every row that satisfies a condition, including rows not inserted yet, which is exactly why it stops phantoms. When a transaction reads “no booking for this room and this range”, the lock sits on the condition itself rather than on the rows that exist. Doing this literally is expensive, because the engine would have to compare every new query against thousands of predicates. So most databases use index-range locks instead: they lock the range of the index that was scanned, and any insert with a key inside that range waits. Two consequences follow. Without a suitable index the engine may have to lock the whole table, so unrelated writes queue up; and with a range wider than the predicate, you create conflicts nobody needed.
1 rangeیک بازه از ایندکس، شامل شکاف بین کلیدهاone index range, gaps between keys included
full tableبدون ایندکس مناسب، قفل به کل جدول میرسدwith no usable index the lock falls back to the table
تا کامیتتا پایان تراکنش نگه داشته میشودkept until the transaction ends
تلهٔ رایجThe usual trap
شرط را عوض میکنی، ایندکس را عوض نمیکنی. پرسوجو حالا روی دو ستون است و فقط یکیشان ایندکس دارد؛ موتور همان بازهٔ یک ایندکس را قفل میکند و ردیفی که از راه دیگر درج شود، از شکاف رد میشود. قفل گزارهای فقط تا جایی معتبر است که پیمایش واقعی موتور با شرط ذهنی تو یکی باشد.You change the condition but not the index. The query now filters on two columns and only one is indexed, so the engine locks the range for that index, and a row inserted through the other path slips through the gap. A predicate lock is only as good as the scan the engine actually performed.
دو بار نگاه میکنی، دو عدد میبینی؛ کدام درست است؟You look twice and see two numbers. Which one is right?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
موجودی کیف پولت را در اپ میبینی: چهارصد هزار. میروی چای بریزی و برمیگردی؛ همان صفحه حالا سیصد و پنجاه نشان میدهد، چون بین دو نگاه کرایهٔ تاکسی تسویه شده. اپ هر بار فقط چیزهای قطعیشده را نشان میدهد و همین میشود خواندنِ کامیتشده: هیچ دروغی، ولی هیچ قولی هم برای جور درآمدن دو نگاه.You check your wallet balance: 400,000. You go make tea and come back; the same screen now says 350,000, because the taxi fare settled between the two looks. The app only ever shows what is final, and that is read committed: no lies, but no promise that two looks will agree.
تشبیه و تصویر ذهنیThe analogy
این را با تابلوی اعلانات ساختمان بسنج. هر بار که نگاه میکنی، تابلو دقیق است: ساعت قطع آب، جلسهٔ ساختمان، نوبت نظافت. ولی کسی که دو بار در روز نگاه میکند ممکن است دو فهرست متفاوت ببیند و هر دو هم درست باشند؛ فقط با هم جور درنمیآیند. مسئله این نیست که تابلو دروغ میگوید، این است که هیچکس قول نداده بین دو نگاه چیزی عوض نشود. اگر فقط یک اطلاعیه میخواهی، همین کافی و ارزان است. اگر میخواهی کل ساختمان را یکجا بسنجی، باید یا تابلو را قفل کنی یا از همهاش عکس بگیری.Compare it with the notice board in your building. Each time you look, the board is accurate: water cut-off, the residents' meeting, cleaning duty. But someone who looks twice a day may see two different lists, both correct, that do not fit together. The problem is not that the board lies; it is that nobody promised it would hold still between two glances. If you only need one notice, that is enough, and cheap. If you need to reason about the whole building at once, you must either lock the board or photograph all of it.
نوشتن تا لحظهٔ کامیت برای بقیه نامرئی است؛ بعد از آن، هر خواندن تازه آن را میبیند.A write stays invisible until commit; after that, every fresh read sees it.
و بعدش، فنیAnd now, technically
خواندن کامیتشده دو قول میدهد و نه بیشتر: هیچ نوشتن ناتمامی دیده نمیشود (dirty read ممنوع)، و هیچ نوشتن ناتمامی روی نوشتن دیگر نمینشیند (dirty write ممنوع). برای این کار موتور نوشتنها را با قفل ردیف تا لحظهٔ کامیت نگه میدارد و خواندنها را از نسخههای کامیتشده میخواند. ولی هر دستور یک عکس تازه میگیرد: دو SELECT پشت سر هم میتوانند دو نسخهٔ مختلف ببینند، و اگر همان ردیف را دوباره بخوانی، ممکن است مقدار تازهای بگیری. همین است که read skew و non-repeatable read از آن بیرون میآید. ارزان است، چون هیچ عکس طولانی نگه نمیدارد و پاکسازی نسخههای قدیمی را معطل نمیکند؛ و پیشفرض خیلی از پایگاههاست، از پستگرس و اوراکل تا اسکیوال سرور.Read committed makes exactly two promises. No uncommitted write is ever visible (no dirty reads), and no uncommitted write ever lands on top of another (no dirty writes). The engine does this by holding row locks on writes until commit and by serving reads from committed versions. But every statement takes a fresh snapshot: two SELECTs in a row can see two different versions, and re-reading a single row can show a new value. That is where read skew and the non-repeatable read come from. It is cheap, because no long-lived snapshot is held and cleanup of old versions is not delayed, which is why it is the default in many databases, from PostgreSQL and Oracle to SQL Server.
1 / statementهر دستور یک عکس تازه میگیردeach statement takes a fresh snapshot
0 dirty readsنوشتهٔ ناتمام هرگز دیده نمیشودan uncommitted write is never visible
2 readsدو خواندن، دو نسخه؛ جمعشان ممکن است دروغ باشدtwo reads, two versions; together they may lie
تلهٔ رایجThe usual trap
دو کار را در دو دستور انجام میدهی و خیال میکنی یک تراکنش است. موجودی را میخوانی، بعد برداشت را مینویسی؛ بین این دو، یک برداشت دیگر کامیت میشود و حساب منفی میشود. خواندن کامیتشده تضمینی بین دستورها نمیدهد؛ برای آن باید قفل بگیری یا سطح بالاتر بروی.You do two steps in two statements and assume they act as one transaction. You read the balance, then write the withdrawal; in between, another withdrawal commits and the account goes negative. Read committed gives you no guarantee across statements; that needs a lock or a stronger level.
دو چیز را در دو لحظه میخوانی؛ جمعشان دروغ میشود.You read two things at two moments; their sum is a lie.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
در خانه دو قلک داریم: یکی برای اجاره، یکی برای خواربار. اول قلک اجاره را میشماری: یک میلیون؛ همان موقع همسرت دویست هزار از آن برمیدارد و میگذارد در قلک خواربار، و بعد قلک خواربار را چهارصد هزار میشماری. جمع این دو عدد دویست هزار بیشتر از چیزی است که واقعاً دارید، و هیچکس دزدی نکرده — این همان کجی خواندن است: هر عدد درست، جمع بیمعنا.You keep two jars at home: one for the rent, one for groceries. You count the rent jar: one million. Right then your partner takes two hundred thousand out of it and puts it in the grocery jar, and you then count the grocery jar at four hundred thousand. The two numbers add up to two hundred thousand more than you actually have, and nobody stole anything — that is read skew: every number right, the sum meaningless.
تشبیه و تصویر ذهنیThe analogy
همین اتفاق در انبار مغازه هم میافتد. دو نفر یکی قفسهٔ کنسرو را میشمارد و دیگری قفسهٔ نوشیدنی را، و همزمان یک کارگر بین دو قفسه جابهجایی میکند. هر شمارش درست است، ولی جمعشان موجودیای را نشان میدهد که در هیچ لحظهای وجود نداشته. سه راه داری: همهٔ جابهجاییها را متوقف کنی (گران و کند)، از هر دو قفسه یکجا عکس بگیری (یک لحظهٔ مشترک)، یا دوباره بشماری و اختلاف را جدا کنی (کارِ دوباره). انتخاب بین این سه، همان انتخاب بین قفل، اسنپشات و تلاش دوباره است.The same thing happens in a shop's stockroom. Two people count the canned-goods shelf and the drinks shelf while a worker moves boxes between them. Each count is correct, but together they describe a stock level that never existed at any instant. You have three options: stop all movement (slow and expensive), photograph both shelves at once (one shared moment), or count again and reconcile the difference (redo the work). Choosing among those three is choosing among locking, a snapshot, and a retry.
بین دو خواندن یک انتقال کامیت میشود؛ هر خواندن درست، جمعشان در هیچ لحظهای نبوده.A transfer commits between two reads: each read is right, their sum existed at no single moment.
و بعدش، فنیAnd now, technically
کجی خواندن یعنی یک تراکنش در دو لحظهٔ مختلف به داده نگاه کند و نتیجهاش با هیچ لحظهای از پایگاه داده جور درنیاید. مثال کلاسیکش انتقال پول است: تراکنش اول موجودی حساب A را میخواند، بعد تراکنش دوم صد واحد از A به B میبرد و کامیت میشود، بعد تراکنش اول موجودی B را میخواند. هر خواندن درست است، ولی جمع دو حساب یا صد واحد کم دارد یا صد واحد اضافه. این نقض خواندن کامیتشده نیست؛ آن سطح اصلاً قولی بین دستورها نمیدهد. خطرش جایی جدی میشود که یک گزارش یا یک پشتیبانگیری چندمرحلهای بسازی: نسخهٔ بازیابیشده میتواند حالتی را نشان دهد که هرگز وجود نداشته، مثلاً ایندکسی که به ردیف حذفشده اشاره میکند. راهحل، اسنپشات است: همهٔ خواندنها از یک نسخهٔ ثابت، که هزینهاش نگه داشتن نسخههای قدیمی است.Read skew means a transaction looks at the data at two different moments and its result matches no single moment of the database. The classic case is a transfer: the first transaction reads account A's balance, a second transaction moves a hundred units from A to B and commits, and the first transaction then reads B. Every read is correct, yet the two balances are a hundred short or a hundred over. This is not a violation of read committed; that level makes no promise across statements. It turns serious when you build a multi-step report or backup: the restored copy can show a state that never existed, such as an index entry pointing at a deleted row. The fix is a snapshot: all reads from one fixed version, paid for by keeping old versions around.
۲ خواندنهر کدام در یک لحظه؛ جمعشان در هیچ لحظهایeach at one instant; the sum at no instant
1 snapshotهمهٔ خواندنها از یک نسخهٔ ثابتevery read from one fixed version
تلهٔ رایجThe usual trap
میگویی «این فقط مشکل گزارش است» و بعد پشتیبانگیری آنلاین میگیری. فایل بازیابیشده سالم به نظر میرسد، ولی یک تراکنش نصفه در آن مانده: پول از یکی کم شده و به دیگری نرسیده. کجی خواندن در گزارش آزاردهنده است، در پشتیبان فاجعه.You call it a reporting problem and then take an online backup. The restored file looks healthy but carries half a transaction: money left one account and never reached the other. Read skew is annoying in a report and a disaster in a backup.
یک بار خواندی؛ دفعهٔ بعد هم همان را میخوانی؟You read it once. Will you read the same thing again?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
در رستوران منو را که باز میکنی، قیمتها روی کاغذ چاپ شدهاند. تا آخر شام هر بار نگاه کنی همان قیمتها هستند، حتی اگر آشپزخانه وسط شام برای میز بعدی منوی تازه با قیمتهای جدید چاپ کند. سفارش و صورتحسابت با همان یک نسخهای که دستت بود جور درمیآید، و اسم این قول خواندن تکرارپذیر است.In a restaurant the menu you open has its prices printed on paper. Until the end of your meal, every look shows the same prices, even if the kitchen prints a fresh menu with new prices for the next table. Your order and your bill match the one copy that was in your hands, and that promise is repeatable read.
تشبیه و تصویر ذهنیThe analogy
این قول دقیقاً بهاندازهٔ همان شام است. فردا شب که برگردی، منوی تازه دستت میآید و قیمتهای تازه؛ هیچکس قول نداده قیمتها برای همیشه ثابت بمانند. هر میزی هم میتواند نسخهٔ چاپ خودش را داشته باشد: دو نفر با دو منوی متفاوت، هر دو درست، و هر دو میتوانند غذا سفارش دهند. هزینهاش این است که آشپزخانه باید نسخههای قدیمی را نگه دارد و تا وقتی تو سر میز نشستهای دورشان نریزد؛ هر چه دیرتر برخیزی، کاغذ بیشتری روی هم تلنبار میشود. و این قول جلوی نوشتن دیگران را نمیگیرد؛ فقط آن را از چشم تو پنهان میکند.That promise lasts exactly as long as the meal. Come back tomorrow and you get a fresh menu and fresh prices; nobody promised they would hold forever. Each table can also have its own printing: two people with two different menus, both correct, and both can order. The cost is that the kitchen must keep the old printouts and cannot throw them away while you are still at the table; the longer you sit, the more paper piles up. And the promise does not stop other people from writing; it only hides their writing from your eyes.
یکی برای هر دستور عکس میگیرد، دیگری یک عکس برای کل تراکنش.One takes a snapshot per statement, the other one snapshot for the whole transaction.
و بعدش، فنیAnd now, technically
در خواندن تکرارپذیر، هر خواندن داخل تراکنش از یک نسخهٔ ثابت میآید؛ اگر یک ردیف را دو بار بخوانی، همان مقدار را میبینی. پیادهسازی رایجش همان اسنپشات است: موتور در شروع تراکنش یک نسخهٔ سازگار برمیدارد و تا آخر همان را به خواندنها میدهد، بدون اینکه نوشتنها را بلاک کند. نتیجه این است که خواندنهای غیرتکرارپذیر و کجی خواندن از بین میروند و در بیشتر پیادهسازیها phantom هم دیده نمیشود. ولی سریالپذیر نیست: دو تراکنش میتوانند از یک عکس شروع کنند، هر کدام جای دیگری بنویسند و هر دو کامیت شوند. اسمش هم بین پایگاههای داده جابهجا شده؛ چیزی که اوراکل serializable مینامد، پستگرس و مایاسکیوال repeatable read مینامند.Under repeatable read every read inside the transaction comes from one fixed version: read a row twice and you get the same value. The common implementation is a snapshot — the engine takes a consistent version at transaction start and serves all reads from it without blocking writers. Non-repeatable reads and read skew disappear, and in most implementations so do phantoms. It is not serializable, though: two transactions can start from the same snapshot, each write where the other is not looking, and both commit. The name also moves between vendors; what Oracle calls serializable, PostgreSQL and MySQL call repeatable read.
1 snapshotبرای کل تراکنش، نه برای هر دستورfor the whole transaction, not per statement
0 re-readsهیچ خواندن دومی مقدار تازه نمیبیندno second read sees a new value
≠ serializableکجنویسی هنوز ممکن استwrite skew is still possible
تلهٔ رایجThe usual trap
خیال میکنی خواندن تکرارپذیر یعنی سریالپذیر. دو تراکنش از یک عکس شروع میکنند، هر کدام چیزی میخواند و جای دیگری مینویسد، و هر دو سبز میشوند؛ شرطی که هر دو خواندهاند شکسته است. برای این حالت باید یا قفل بگیری یا سریالپذیر واقعی بگذاری.You assume repeatable read means serializable. Two transactions start from the same snapshot, each reads one thing and writes another, and both commit; the condition both of them read is now broken. That case needs a lock or true serializability.
نتیجه باید با یک اجرای نوبتی جور باشد.The result has to match some one-at-a-time order.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
دو نفر با یک حساب مشترک، همزمان از دو دستگاه خودپرداز برداشت میکنند. مهم نیست کی اول دکمه را زد؛ چیزی که باید درست باشد این است که حساب آخر با یکی از دو حالت جور باشد: اول او برداشت کرد بعد تو، یا اول تو بعد او. اگر موجودی با هیچکدام جور نباشد، یک نفر پول را از هوا برداشته.Two people draw from a shared account at two cash machines at the same moment. Who pressed first does not matter; what must hold is that the final balance matches one of two stories: they withdrew and then you, or you and then they. If the balance matches neither, somebody took money out of thin air.
تشبیه و تصویر ذهنیThe analogy
دو کارمند یک دفتر کل مشترک دارند. اگر هر دو همزمان سند بزنند، ترتیب واقعی حرکت قلمها نامعلوم است. سریالپذیری نمیگوید کدام اول بوده؛ میگوید آخر ماه دفتر کل باید همان چیزی باشد که اگر سندها یکییکی زده شده بودند، بود. این تفاوت مهم است: ترتیب واقعی لازم نیست معلوم شود، فقط نتیجه باید با یک ترتیب ممکن جور باشد. بهایش هم روشن است: برای رسیدن به این تضمین یا باید قفل بگذاری و انتظار بخری، یا اجازه بدهی همه جلو بروند و در لحظهٔ تأیید یکی را برگردانی.Two clerks share one ledger. If both post entries at once, the real order of the pen strokes is unknown. Serializability does not say which came first; it says that at month end the ledger must read as if the entries had been posted one at a time. That distinction matters: the real order need not be known, only matched by some possible one. The price is clear too — to get this guarantee you either take locks and buy waiting, or let everyone run and turn one of them back at commit time.
دو راه به یک تضمین: یکی از اول قفل میکند، یکی در پایان حسابرسی میکند. راه سوم، اجرای سریالی است.Two roads to one guarantee: one locks up front, one audits at the end. Serial execution is the third.
و بعدش، فنیAnd now, technically
سریالپذیری یعنی مجموعهٔ تراکنشهای موفق معادل یک اجرای سریال آنهاست، گرچه در واقع همزمان اجرا شدهاند. این تضمین دربارهٔ ترتیب منطقی است، نه ترتیب ساعت. سه راه پیادهسازی دارد. اول، اجرای سریالی: کلاً همزمانی را حذف میکنی و تراکنشها یکییکی روی یک thread اجرا میشوند؛ سادهترین و گرانترین، چون سقف یک هسته داری و یک تراکنش کند همه را میبندد. دوم، two-phase locking: خوانندهها قفل مشترک و نویسندهها قفل انحصاری میگیرند و تا آخر کار نگه میدارند؛ تعارض از اول بسته میشود، به قیمت انتظار و بنبست. سوم، SSI: روی snapshot isolation مینشیند، خواندنها و نوشتنها را ردیابی میکند و در لحظهٔ کامیت میبیند آیا نتیجه با هیچ ترتیب سریالی جور است یا نه؛ اگر نه، یکی را abort میکند. در هر سه حالت، برنامه باید خطای serialization failure را بشناسد و کل تراکنش را دوباره اجرا کند — این بخشی از قرارداد است، نه استثنا.Serializability means the set of successful transactions is equivalent to running them serially, even though they actually overlapped. The guarantee is about logical order, not clock order. There are three implementations. First, serial execution: delete concurrency and run transactions one at a time on a single thread — simplest and most expensive, because you live under a one-core ceiling and one slow transaction stalls the parade. Second, two-phase locking: readers take shared locks and writers take exclusive ones and hold them to the end; conflicts are closed up front, at the price of waiting and deadlocks. Third, SSI: it rides on snapshot isolation, tracks reads and writes, and at commit decides whether the outcome matches any serial order; if not, one transaction is aborted. In all three, the application has to recognise a serialization failure and run the whole transaction again — that is part of the contract, not an exception.
3راه پیادهسازی: اجرای سریال، 2PL، SSIimplementations: serial execution, 2PL, SSI
1 threadاجرای سریالی: تنها راهی که هیچ تعارضی برای تشخیص نداردserial execution: the one road with no conflict to detect
retryدر هر سه راه، برنامه باید بتواند کل تراکنش را از نو اجرا کندon every road the application must be able to run it all again
چرا همه روی آن نیستندWhy not everyone runs it
سریالپذیری در دههٔ هفتاد راهحل تمیز مسئله بود و بعد سی سال کمتر جایی دیده شد، چون هزینهاش در بار واقعی سنگین است: 2PL انتظار و بنبست میسازد، اجرای سریالی سقف یک هسته دارد، و SSI زیر رقابت زیاد مرتب abort میکند. برای همین اکثر سیستمها سطح ضعیفتر میفروشند و مسئولیت را به برنامه میدهند. اما دو چیز در سالهای اخیر عوض شد: رم ارزان شد و کل دادهٔ داغ در حافظه جا شد، و SSI نشان داد که میشود تضمین کامل را با اضافهبهای کم روی snapshot isolation فروخت. نتیجه این است که امروز انتخاب واقعی است، نه اجبار.Serializability was the clean answer in the 1970s and then spent thirty years in the shade, because its cost under real load is heavy: 2PL brings waiting and deadlocks, serial execution has a one-core ceiling, and SSI aborts a lot under contention. So most systems sold a weaker level and handed the responsibility to the application. Two things changed: RAM got cheap enough to hold the hot data, and SSI showed the full guarantee could be sold for a small surcharge on snapshot isolation. The result is that today it is a real choice, not a sentence.
تلهٔ رایجThe usual trap
فکر کردن اینکه «سطح serializable را روشن کردیم، پس کدمان درست است». سریالپذیری فقط نتیجه را معادل یک ترتیب میکند؛ اگر برنامه خودش بین خواندن و نوشتن منطق غلطی داشته باشد، همان منطق غلط سریالی هم اجرا میشود. تازه، بدون retry روی خطای serialization failure، تراکنشهای بازنده بیصدا شکست میخورند.Thinking that turning on serializable means the code is now correct. Serializability only makes the outcome equivalent to some order; if the application's logic between read and write is wrong, that wrong logic runs serially too. And without a retry on serialization failure, the losing transactions fail silently.
چهار کارگر، ده کار؛ چرا یکی منتظر میماند؟Four workers, ten jobs. Why is one of them waiting?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
چهار نفر پای یک فهرست کارهای خانه ایستادهاند: ظرفها، لباسها، خرید، قبضها. هر کس اولین کاری را برمیدارد که دست کسی نیست؛ کاری که رویش دست کس دیگری است را رد میکند و میرود سراغ بعدی. هیچ کاری دو بار انجام نمیشود، هیچکس هم نمیایستد تا نفر قبلی فارغ شود. قفل ردیف هم همان «دست کس دیگری است»، و SKIP LOCKED همان «رد کن و برو بعدی».Four people stand at a list of housework: dishes, laundry, shopping, bills. Each one takes the first chore nobody is already holding, steps over the ones in someone else's hands and moves to the next. No chore gets done twice, and nobody stands still waiting for the person ahead. A row lock is that “in someone else's hands”, and SKIP LOCKED is “step over it, take the next one”.
تشبیه و تصویر ذهنیThe analogy
سوپرمارکتی را تصور کن که یک صندوق دارد و صف طولانی. حالا صندوق دوم را باز میکنند: کسی که در صف ایستاده میتواند مستقیم برود سر صندوق دوم، به شرطی که آن صندوق آزاد باشد. این همان کاری است که SKIP LOCKED میکند: بهجای انتظار، سراغ کار بعدی میرود. ولی بهایش را هم ببین. ترتیب از بین میرود؛ ممکن است کارهای آخر زودتر تمام شوند و کارهای اول بمانند. اگر همهٔ صندوقها مشغول باشند، SKIP LOCKED به تو هیچ نمیدهد و باید چند لحظه بعد دوباره سر بزنی. و اگر کارها به هم وابسته باشند — مثلاً قبضی که باید قبل از قبض دیگر پرداخت شود — رد کردن، ترتیب را میشکند و کار بعدی بیفایده میشود.Picture a supermarket with one till and a long queue. Now they open a second till: whoever is in the queue can walk straight to it, provided that till is free. That is what SKIP LOCKED does — instead of waiting, it goes to the next piece of work. But look at the bill. Order is gone: the last jobs may finish first and the first ones may sit there. If every till is busy, SKIP LOCKED gives you nothing and you have to look again in a moment. And if the work is ordered — a bill that must be paid before another one — skipping breaks the order and makes the next job pointless.
کارگر ردیفهای قفلشده را رد میکند و سراغ ردیف آزاد بعدی میرود؛ هیچکس منتظر نمیماند.The worker steps over locked rows and takes the next free one, so nobody waits.
و بعدش، فنیAnd now, technically
در سطح SQL، این کار با یک خواندنِ قفلگیرنده انجام میشود که موتور آن را «برای بهروزرسانی» میگیرد، با یک تفاوت کلیدی: اگر ردیفی از قبل قفل باشد، بهجای ایستادن در صف، آن ردیف را رد میکند و سراغ ردیف بعدی میرود. نتیجه این است که هر کارگر مجموعهٔ جدا و ناهمپوشانی از کارها را میگیرد، بیآنکه هیچ کارگری منتظر دیگری بماند. این الگو برای صف کار، جدول outbox و ارسال ایمیل یا پیامک تقریباً استاندارد شده است: کارگر تراکنش را باز میکند، یک ردیف برمیدارد، کار را انجام میدهد و کامیت میکند. سه هزینه دارد که کمتر گفته میشود. اول، ترتیب از بین میرود؛ ردیفها به ترتیبی که آزادند برداشته میشوند، نه به ترتیبی که رسیدهاند. دوم، اگر تراکنش کارگر طولانی باشد — مثلاً وسط کار یک درخواست شبکه بزند — آن ردیف برای همه قفل میماند و بقیه مجبورند ردش کنند. سوم، وقتی همهٔ ردیفها قفلاند، نتیجه خالی است، نه انتظار؛ پس کارگر باید عقب بنشیند و دوباره سر بزند، و اگر این حلقه را درست ننویسی، بهجای کار، CPU میسوزانی.At the SQL level this is a locking read — the engine takes it “for update” — with one decisive difference: if a row is already locked, the statement steps over it instead of queueing and takes the next one. Each worker therefore gets its own disjoint set of jobs and no worker waits for another. The pattern has become standard for job queues, outbox tables and email or SMS fan-out: open a transaction, take one row, do the work, commit. Three costs are less often mentioned. First, ordering disappears; rows are taken in the order they happen to be free, not the order they arrived. Second, if the worker's transaction is long — say it makes a network call in the middle — that row stays locked for everyone else, who can only skip it. Third, when every row is locked the result is empty rather than blocking, so the worker has to back off and poll, and a careless loop burns CPU instead of doing work.
1 rowهر کارگر در هر لحظه یک ردیف را قفل میکندeach worker holds exactly one row at a time
0 waitردیف قفلشده، کارگر را بلوکه نمیکند؛ ردش میکندa locked row never blocks the worker; it is stepped over
9.5+نسخهای از PostgreSQL که این گزینه را آوردthe PostgreSQL release that introduced this option
صف بکش، یا صف را رد کن، یا جدول را ترک کنQueue, skip the queue, or leave the table
سه الگو برای برداشتن کار از یک جدول وجود دارد و انتخاب بینشان تعیینکننده است. اگر فقط ردیف را برای بهروزرسانی بگیری، کارگرها پشت سر هم صف میکشند: درست، ولی کند. اگر SKIP LOCKED بگذاری، هر کارگر مستقل جلو میرود: سریع، ولی بدون ترتیب. و اگر صف واقعاً بزرگی داری — میلیونها پیام در دقیقه، چند مصرفکنندهٔ مستقل، نیاز به بازپخش — جدول دیتابیس جای درستی نیست و باید سراغ یک لاگ توزیعشده بروی. بیشتر تیمها اولی را انتخاب میکنند چون ساده است، بعد دومی، و فقط وقتی به سومی میرسند که همان جدول به گلوگاه تبدیل شده باشد.There are three ways to take work out of a table and the choice matters. Take the row for update and workers queue politely behind each other: correct, but slow. Add SKIP LOCKED and each worker moves independently: fast, but unordered. And if the queue is genuinely large — millions of messages a minute, several independent consumers, replay requirements — a database table is the wrong home and you want a distributed log. Most teams pick the first because it is simple, then the second, and only reach the third once that table has become the bottleneck.
کجا بیصدا خراب میشودWhere it breaks quietly
دو جا. اول در گزارشها: هر ردیفی که همان لحظه در دست کارگر دیگری است از نتیجهٔ خواندن غایب میشود، پس شمارشی که میگیری کمتر از واقعیت است و هیچ خطایی هم نمیگیری. SKIP LOCKED برای برداشتن کار است، نه برای شمردن. دوم در کارهای وابسته: اگر کارها باید به ترتیب انجام شوند — مثلاً تسویه بعد از تأیید — رد کردن ردیف اول و برداشتن ردیف سوم، ترتیب را میشکند. برای کارهای وابسته، از یک کارگر با ترتیب مشخص یا از یک صف واقعی استفاده کن. و اگر تعداد کارگرها را زیاد کنی ولی یک تراکنش طولانی همهٔ ردیفها را قفل کرده باشد، کارگر بیشتر هیچ چیزی را سریعتر نمیکند؛ فقط حلقهٔ poll شلوغتر میشود.Two places. First, reports: every row currently in another worker's hands is missing from your result, so the count you get is lower than reality and nothing reports an error. SKIP LOCKED is for taking work, not for counting. Second, dependent work: if jobs must run in order — settle after approve — skipping the first row and taking the third breaks the order. For dependent work use a single ordered worker or a real queue. And if you add workers while one long transaction holds every row, more workers make nothing faster; they only make the polling loop busier.
تلهٔ رایجThe usual trap
استفاده از SKIP LOCKED برای خواندنِ گزارشی: ردیفهایی که همان لحظه در دست کارگر دیگریاند از نتیجه غایب میشوند و عددت بیصدا کم میشود، بدون هیچ خطایی. و اگر تراکنش کارگر را در طول یک درخواست HTTP باز نگه داری، همهٔ ردیفها قفل میمانند، صف خالی به نظر میرسد و کارگرها بهجای کار، در حلقهٔ poll میچرخند.Using SKIP LOCKED as a reporting read: rows that another worker is holding right now simply vanish from your result and your count comes out low, with no error at all. And if you keep the worker's transaction open across an HTTP call, every row stays locked, the queue looks empty, and the workers spin in a polling loop instead of doing work.
میخواهی بدانی در یک لحظه، همه چیز چه شکلی بود.You want to know what everything looked like at one instant.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
قبل از اینکه حسابدار دفتر را باز کند و شروع به خطزدن کند، از صفحه عکس میگیری. ده دقیقه بعد دفتر پر از تغییر است، ولی عکس همان وضع اول را نشان میدهد. عکس دروغ نمیگوید؛ فقط دربارهٔ لحظهٔ خودش حرف میزند.Before the accountant opens the ledger and starts crossing things out, you photograph the page. Ten minutes later the book is full of changes, but the photo still shows the original state. The photo is not lying; it only speaks about its own moment.
تشبیه و تصویر ذهنیThe analogy
عکس یادگاری خانوادگی را تصور کن. همه برای یک لحظه میایستند و عکس گرفته میشود؛ بعد از آن هر کسی موهایش را درست میکند، ولی عکس عوض نمیشود. حالا برعکسش را ببین: اگر عکاس بدون اینکه بگوید «ایست»، از جمعیتِ در حال حرکت عکس بگیرد، نتیجه یک تصویر تار است که نه وضع قبل است و نه وضع بعد. بیشتر خرابیهای پشتیبانگیری از همین جنس است: کپیای که در میانهٔ تغییر گرفته شده و هیچ لحظهای را نشان نمیدهد.Think of a family photograph. Everyone holds still for a moment and the picture is taken; afterwards people fix their hair, but the picture does not change. Now the reverse: if the photographer shoots a moving crowd without saying “hold still”, the result is a blur that is neither the state before nor the state after. Most backup failures are exactly this: a copy taken mid-change that represents no instant at all.
عکس در یک لحظه گرفته میشود و بعد از آن ثابت میماند، هرچند وضعیت عوض شود.A snapshot is taken at one instant and stays fixed while the state moves on.
و بعدش، فنیAnd now, technically
عکس لحظهای یعنی یک «برش» سازگار از وضعیت در یک نقطهٔ منطقی از زمان: هیچ تراکنشی نصفه در آن دیده نمیشود، و تغییرهایی که بعد از آن نقطه کامیت شدهاند در آن نیستند. سه کاربرد اصلی دارد. اول، در جداسازی: در snapshot isolation هر تراکنش از یک عکس میخواند و نسخههای قدیمی نگه داشته میشوند تا آن عکس معتبر بماند؛ بهایش فضای اضافه و نیاز به پاکسازی نسخههای مرده است. دوم، در پشتیبانگیری: یک پشتیبان معتبر باید از یک نقطهٔ سازگار گرفته شود، یا با کمک لاگ تراکنشی به آن نقطه برسد. سوم، در پردازش جریانی: چکپوینت، عکسی از وضعیت عملگرها بههمراه آفست ورودی است، تا بعد از خرابی بتوان از همان نقطه ادامه داد.A snapshot is a consistent cut of the state at one logical point in time: no transaction is caught half-applied, and changes committed after that point are absent. It has three main uses. In isolation: under snapshot isolation each transaction reads from a snapshot, and old versions are retained so that snapshot stays valid — paid for in space and in cleaning up dead versions. In backup: a valid backup must be taken at a consistent point, or brought to one using the transaction log. In stream processing: a checkpoint is a snapshot of operator state together with the input offset, so work can resume from there after a failure.
1 instantعکس، وضعیت یک لحظهٔ منطقی است، نه یک بازهa snapshot is one logical instant, not a range
2 partsچکپوینت جریانی: وضعیت عملگرها + آفست ورودیa stream checkpoint: operator state plus input offset
چرا کپیکردن فایل، پشتیبان نیستWhy copying files is not a backup
اگر فایلهای یک پایگاه دادهٔ در حال کار را کپی کنی، ممکن است بعضی صفحهها مربوط به بعد از یک تراکنش باشند و بعضی قبل از آن. نتیجه یک تصویر ناسازگار است که هیچ لحظهای را نشان نمیدهد — نه وضع قبل، نه وضع بعد. به همین دلیل ابزارهای پشتیبانگیری یا از قابلیت عکس خودِ موتور استفاده میکنند، یا فایلها را همراه با لاگ تراکنشی کپی میکنند و هنگام بازیابی لاگ را تا نقطهٔ سازگار بازپخش میکنند. تفاوت این دو، همان تفاوت «عکس با اجازهٔ ایست» و «عکس از جمعیت در حال حرکت» است.If you copy the files of a running database, some pages may belong after a transaction and some before it. The result is an inconsistent image that represents no instant — neither before nor after. That is why backup tools either use the engine's own snapshot facility, or copy the files along with the transaction log and replay the log to a consistent point during restore. The difference is the one between photographing a crowd that was asked to stand still and one that was not.
تلهٔ رایجThe usual trap
فکر کردن اینکه «عکس گرفتن» خودش رایگان است. نگهداشتن یک عکس معتبر یعنی نگهداشتن نسخههای قدیمی؛ اگر نسخهها را زودتر پاک کنی، عکسی که باز است ناگهان بیاعتبار میشود و تراکنشهای درازمدت خراب میشوند. مرزِ پاکسازی باید همیشه عقبتر از قدیمیترین عکسِ در حال استفاده باشد.Assuming taking a snapshot is free. Keeping a snapshot valid means keeping old versions; clean them up too early and an open snapshot suddenly becomes invalid, breaking long-running transactions. The cleanup horizon must always stay behind the oldest snapshot still in use.
وسط خواندن، یکی داده را عوض میکند. تو کدام را میبینی؟Someone changes the data mid-read. Which version do you see?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
صورتحساب ماهانهات را باز میکنی: جمع تراکنشها با ماندهٔ آخر جور است. اگر همان وسط، خرید تازهای ثبت شود و برنامه جمع را دوباره حساب کند، عددها با هم نمیخوانند و تو فکر میکنی بانک اشتباه کرده. برنامهٔ درست، همان عکسی را نشانت میدهد که لحظهٔ باز کردن صفحه گرفته، تا آخر. snapshot isolation دقیقاً همان قرار است: کل تراکنش از یک عکس ثابت میخواند.You open your monthly statement and the sum of the transactions matches the closing balance. If a new purchase were inserted halfway through and the app recomputed the total, the numbers would stop matching and you would decide the bank had made an error. A correct app shows you the picture taken when you opened the page, all the way to the end. Snapshot isolation is that promise: the whole transaction reads one frozen photograph.
تشبیه و تصویر ذهنیThe analogy
از تختهٔ سفید کلاس عکس میگیری و بعد سر جا مینشینی و از روی عکس جزوه مینویسی. هر چه سؤال هم که بپرسی، جواب از همان عکس میآید؛ پس جزوهات با خودش نمیجنگد. این خوبیِ بزرگ عکس است: یک دنیای ثابت برای یک کار. ولی دو نفر میتوانند از دو عکس مختلف همان تخته استفاده کنند — یکی عکس ساعت نه، یکی عکس ساعت ده — و هر دو بر اساس چیزی تصمیم بگیرند که دیگر نیست. عکس فقط خواندن را ثابت میکند؛ اگر دو نفر بخواهند همان جای تخته را عوض کنند، عکس هیچ کمکی نمیکند و جای دیگری باید جلویشان را بگیرد.You photograph the classroom whiteboard, then sit down and write your notes from the photo. Whatever you ask, the answer comes from that photo, so your notes never contradict themselves. That is the great virtue of a photo: one fixed world for one job. But two people can work from two different photos of the same board — one from nine o'clock, one from ten — and each decide on something that is already gone. A photo only fixes what you read; if two people want to change the same corner of the board, the photo is no help and something else has to stop them.
تراکنش همهٔ خواندنهایش را از یک عکس میگیرد؛ فقط نوشتنها با هم برخورد میکنند.Every read in the transaction comes from one snapshot; only writes collide.
و بعدش، فنیAnd now, technically
در snapshot isolation، تراکنش در لحظهٔ شروع یا در اولین خواندنش یک عکس از پایگاه داده میگیرد و تا آخر از همان عکس میخواند: هر ردیفی که بعد از آن لحظه نوشته شده باشد، با هر کامیتی، دیده نمیشود. پیادهسازی عملی این عکس با MVCC است: نسخهها نگه داشته میشوند و قاعدهٔ دیدپذیری تصمیم میگیرد کدام نسخه مال این تراکنش است. سه چیز از این قرار میآید. اول، خواندنها هیچ نوشتنی را قفل نمیکنند و برعکس، پس خواندن و نوشتن روی یک ردیف همزمان جلو میرود. دوم، خواندن تکراری همان جواب را میدهد، حتی اگر بین دو خواندن کسی ردیف را عوض کرده باشد. سوم، نوشتنها هنوز باید همدیگر را ببینند: اگر دو تراکنش یک ردیف را عوض کنند، یکی در لحظهٔ کامیت رد میشود. همینجا مرز معلوم میشود — این سطح از خواندنهای ناسازگار جلوگیری میکند، ولی serializable نیست، چون دو تراکنش میتوانند روی دو ردیف جدا تصمیم بگیرند و هر دو کامیت شوند.Under snapshot isolation a transaction takes a snapshot of the database at its start or at its first read, and reads from that snapshot to the end: any row written after that instant is invisible no matter who committed it. In practice the snapshot is MVCC — versions are kept and a visibility rule decides which version belongs to this transaction. Three things follow. Reads never lock writes or the reverse, so a read and a write on the same row proceed together. A repeated read returns the same answer even though someone changed the row between the two reads. And writes still have to meet each other: if two transactions change the same row, one is rejected at commit. That is where the boundary shows — the level stops inconsistent reads but is not serializable, because two transactions can decide on two separate rows and both commit.
per transactionیک عکس برای کل تراکنش، نه برای هر دستورone snapshot for the whole transaction, not per statement
write skewآسیبی که این سطح باز میگذاردthe anomaly this level leaves open
MVCCزیربنایی که عکس را ارزان میکندthe machinery that makes the picture cheap
برای گزارش عالی است، برای تصمیم خطرناکPerfect for reports, dangerous for decisions
جایی که این سطح میدرخشد، خواندنهای طولانی است. گزارشی که ده دقیقه طول میکشد و هزار جدول را میخواند، با snapshot isolation عددهایی میبیند که با هم جورند؛ با read committed هر دستورش یک عکس تازه میگیرد و گزارش میتواند نیمی از دنیای قبل و نیمی از دنیای بعد را نشان دهد. اما همان ویژگی در تراکنشهای کوتاهِ نوشتنی خطر میشود: تراکنش موجودی را میخواند، تصمیم میگیرد و مینویسد، در حالی که بین خواندن و نوشتن دنیا عوض شده است. عکس برای این ساخته نشده که تصمیمت را درست کند؛ فقط ساخته شده که خواندنت ثابت بماند.The level shines on long reads. A report that takes ten minutes and touches a thousand tables sees numbers that agree with each other; under read committed every statement takes a fresh snapshot and the report can show half the old world next to half the new one. The same property turns dangerous in short writing transactions: the transaction reads a balance, decides, and writes, while the world changed between the read and the write. The snapshot was never built to make your decision correct; it was built to keep your reading still.
دو نفر، دو عکسTwo people, two photos
رایجترین اشتباه این است که فکر کنی عکس جلوی همه چیز را میگیرد. دو تراکنش میتوانند دو عکس مختلف داشته باشند، هر کدام بر اساس عکس خودش تصمیم بگیرد، و هر دو روی ردیفهای جدا بنویسند؛ موتور هیچ تعارضی نمیبیند، چون نوشتنها به هم نمیخورند. این همان جایی است که write skew بیرون میآید. بعضی موتورها یک لایهٔ اضافه دارند و بهروزرسانی گمشده را در همان ردیف تشخیص میدهند و تراکنش بازنده را برمیگردانند، ولی این رفتار استاندارد نیست و بین پیادهسازیها فرق میکند. اگر جوابت به قیدی روی مجموعهای از ردیفها وابسته است، بدان که عکس تنهایی کافی نیست.The usual mistake is believing the snapshot covers everything. Two transactions can hold two different photos, each decide from its own, and both write to separate rows; the engine sees no conflict because the writes never meet. That is exactly where write skew comes out. Some engines add a layer that detects a lost update on the same row and rolls the loser back, but that is not standard behaviour and differs between implementations. If your answer depends on a rule over a set of rows, the snapshot alone is not enough.
تلهٔ رایجThe usual trap
تنظیم سطح ایزولاسیون روی یک اتصال و بعد برگشتن آن اتصال به pool. اتصال بعدی دوباره روی پیشفرض read committed است، هر دستورش یک عکس تازه میگیرد و گزارش تو بیسروصدا تکهتکه از دو دنیا میآید. سطح را در خودِ تراکنش تنظیم کن، نه یک بار برای همیشه روی اتصال.Setting the isolation level on a connection and then handing that connection back to the pool. The next user gets read committed again, every statement takes a fresh snapshot, and your report quietly becomes a patchwork of two worlds. Set the level inside the transaction, not once and forever on the connection.
بگذار همه رد شوند، آخر کار یکی را برگردان.Let everyone run, then turn one of them back at the gate.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
دو نفر همزمان یک کد تخفیف یکبارمصرف را وارد میکنند. هر دو صفحه را میبینند که «استفادهنشده» است، هر دو خرید را ادامه میدهند و هر دو تا لحظهٔ آخر فکر میکنند برندهاند. هیچکدام پشت در نمانده و سیستم کند نشده؛ فقط در لحظهٔ ثبت، یکی ثبت میشود و آن یکی پیام میگیرد «کد استفاده شد، دوباره تلاش کن». SSI همان نگهبان لحظهٔ آخر است: بدون قفل، بدون انتظار، با یک بازنده که باید از نو شروع کند.Two people type the same single-use discount code at the same moment. Both see the page say “unused”, both continue to checkout, and both believe they are winning right up to the last second. Nobody was made to wait and nothing got slow; at the moment of record only one goes through and the other is told the code is used and to try again. SSI is that guard at the last second: no locks, no waiting, one loser who has to start over.
تشبیه و تصویر ذهنیThe analogy
این روش شبیه امتحانی است که اول همه پاسخ میدهند و بعد برگهها با هم مقایسه میشوند. هیچکس وسط امتحان معطل نمیشود و کلاس سریع جلو میرود. ولی ممکن است برگهات درست بوده باشد و باز هم رد شوی، چون کس دیگری هم روی همان صندلی نشسته است. یعنی هزینهاش «کارِ دوباره» است، نه «انتظار». برای کاری که بیشترش خواندن است و بهندرت دو نفر سر یک چیز به هم میخورند، این معامله عالی است. برای کاری که همهٔ نوشتنها به یک ردیف میرسند — شمارندهٔ بازدید، موجودی یک کالای پرطرفدار — مدام برگردانده میشوی و همان انتظارِ حذفشده، در قالب تلاش دوباره برمیگردد.The method is like an exam where everyone answers first and the papers are compared afterwards. Nobody waits in the middle and the class moves fast. But your paper may have been right and still be sent back, because someone else sat on the same chair. The cost is rework, not waiting. For a workload that is mostly reads and where two people rarely collide on one thing, that is a great trade. For a workload where every write lands on one row — a view counter, the stock of a popular item — you get sent back constantly and the waiting you removed returns as retries.
هر دو از یک عکس میخوانند؛ SSI یک لایهٔ ردیابی اضافه میکند که در کامیت تصمیم میگیرد.Both read from one snapshot; SSI adds a tracking layer that decides at commit time.
و بعدش، فنیAnd now, technically
SSI روی همان snapshot isolation مینشیند و یک کار اضافه میکند: ردیابی اینکه هر تراکنش چه چیزی خوانده و چه چیزی نوشته، تا در لحظهٔ کامیت بشود گفت این نتیجه با یک اجرای نوبتی جور درمیآید یا نه. این ردیابی روی خواندنها انجام میشود — دقیقاً همان جایی که قفلگذاری دومرحلهای قفل میگذاشت — ولی بهجای قفل، یک نشانهٔ سبک ثبت میشود که کدام بازه یا کدام ردیف خوانده شده. موتور دنبال یک الگوی خاص میگردد: تراکنشی که چیزی خوانده که تراکنش دیگری نوشته، و آن دیگری هم چیزی خوانده که سومی نوشته. وقتی چنین زنجیرهای پیدا شود، یکی از تراکنشها در کامیت abort میشود و برنامه باید کل آن را از نو اجرا کند. دو نکتهٔ مهم: اول، چون هیچ قفلی روی خواندن نیست، خوانندهها هیچوقت منتظر نویسنده نمیمانند و بنبستِ ناشی از خواندن نداریم. دوم، این روش محافظهکار است؛ ممکن است تراکنشی را هم که در واقع بیخطر بود برگرداند، چون فقط میتواند اثبات کند یک ترتیب نوبتی وجود دارد یا نه، نه اینکه حتماً تعارض رخ داده است. پس abortهای الکی بخشی از هزینه است و برنامه باید تلاش دوباره را جدی بگیرد.SSI sits on top of snapshot isolation and adds one job: tracking what each transaction read and wrote, so that at commit time the engine can say whether the outcome matches any serial order. The tracking is on reads — exactly where two-phase locking would have put locks — but instead of a lock it records a lightweight marker that a range or a row was read. The engine looks for a particular shape: a transaction that read something another transaction wrote, where that other one also read something a third wrote. When such a chain appears, one of the transactions aborts at commit and the application must run it again. Two things matter. Because no lock is taken on reads, readers never wait for writers and read-induced deadlocks disappear. And the method is conservative: it may reject a transaction that was in fact harmless, because it can only prove whether a serial order exists, not that a conflict happened. Spurious aborts are part of the bill, and retries are part of the contract.
9.1+نسخهای از PostgreSQL که SSI را آوردthe PostgreSQL release that brought SSI
2 rwزنجیرهٔ دو وابستگی خواندن-نوشتن که جرقه را میزندa chain of two read-write dependencies is the trigger
retryبازنده باید همان تراکنش را از ابتدا تکرار کندthe loser must run the whole transaction again
نگهبان چه چیزی را میبیندWhat the guard actually watches
نکتهٔ ظریفی که خیلیها را گیج میکند این است که SSI دنبال تعارض نوشتن-نوشتن نمیگردد. آن تعارض را خود snapshot isolation با قاعدهٔ «اولین کامیتکننده برنده است» میبندد. کاری که SSI اضافه میکند این است که ببیند کسی چیزی را خوانده که بعداً عوض شده و آن تغییر هنوز در عکس او نیست. به همین دلیل میتواند چیزی را بگیرد که قفل ردیف هرگز نمیبیند: دو تراکنش که روی ردیفهای جدا نوشتهاند ولی تصمیمشان به هم وابسته بود. و چون ردیابی روی بازههای ایندکس هم انجام میشود، شبح هم بدون قفل بسته میشود.The subtle part that confuses people is that SSI is not hunting write-write conflicts. Snapshot isolation already closes those with first-committer-wins. What SSI adds is noticing that someone read something which was later changed and that change is not in their snapshot. That is why it catches what a row lock never could: two transactions that wrote separate rows while their decisions depended on each other. And because tracking also covers index ranges, phantoms close without locks.
هزینهاش کجاستWhere the bill lands
SSI مجانی نیست. حالت ردیابی حافظه میخورد و با طول عمر تراکنشها رشد میکند؛ یک تراکنش طولانی که هزاران ردیف را خوانده، هزاران نشانه را زنده نگه میدارد. روی بار خواندنی سنگین با تعارض کم، این معامله بهشدت بهصرفه است، چون هیچکس منتظر هیچکس نمیماند. روی باری که همهٔ نوشتنها به یک ردیف میرسند، برعکس: مرتب برگردانده میشوی و همان کاری که با قفل صف میکشید، حالا در قالب حلقهٔ تلاش دوباره انجام میشود، با هزینهٔ بیشتر و تأخیر بدتر. انتخاب درست به نسبت خواندن به نوشتن و به داغی ردیفها بستگی دارد، نه به اینکه کدام روش مدرنتر است.SSI is not free. The tracking state costs memory and grows with transaction lifetime; a long transaction that read thousands of rows keeps thousands of markers alive. On a read-heavy load with little contention the trade is excellent, because nobody waits for anybody. On a load where every write hits one row it is the opposite: you are rejected over and over and the queueing you removed returns as a retry loop, with more overhead and worse latency. The right choice depends on the read-to-write ratio and how hot the rows are, not on which method sounds newer.
تلهٔ رایجThe usual trap
روشن کردن SSI بدون نوشتن حلقهٔ تلاش دوباره. تراکنشی که با خطای سریالسازی برمیگردد، در خیلی از کدها به کاربر «خطای داخلی» نشان میدهد یا بیصدا هیچ کاری نمیکند؛ در واقع سیستم درست کار کرده و فقط از تو میخواهد دوباره تلاش کنی. اگر تلاش دوباره ننویسی، SSI برای کاربرِ تو معادل خرابی است.Turning SSI on without writing the retry loop. A transaction that comes back with a serialization failure shows the user an internal error in a lot of codebases, or silently does nothing; the system actually did its job and is asking you to try again. Without a retry, SSI looks exactly like an outage to your user.
چرا هر بار دستور پخت را از اول تلفنی میگویی؟Why dictate the whole recipe over the phone every single time?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
دستور کوکوی مادربزرگ روی یک کارت داخل کشوی آشپزخانه است. هر وقت میخواهی کوکو بپزی، لازم نیست زنگ بزنی و ده مرحله را تلفنی بشنوی؛ فقط میگویی «کارت کوکو» و از روی همان کارت جلو میروی. کارت همیشه کنار مواد است و کسی هم که تازه به آشپزخانه آمده میتواند پیدایش کند — رویهٔ ذخیرهشده هم دستوری است که کنار خودِ داده مینشیند، نه آنطرف خط.Your grandmother's kuku recipe is on a card in the kitchen drawer. Whenever you want to make it, you do not phone for ten steps; you say “the kuku card” and work from it. The card lives next to the ingredients, and someone new to that kitchen can find it too — a stored procedure is a recipe that sits beside the data instead of across the phone line.
تشبیه و تصویر ذهنیThe analogy
حالا فکر کن همان دستور را تلفنی بگویی. هر مرحله یک تماس است، و بین دو تماس ممکن است کسی مواد را جابهجا کند یا کسی دیگر همان غذا را شروع کند. کارت این فاصلهها را حذف میکند: یک بار میگویی «کارت»، و ده مرحله پشت سر هم و بدون وقفه انجام میشود. ولی کارت به آشپزخانهٔ مادربزرگ وابسته است: خطش، واحدهایش، ترتیب کارهایش. بردنش به آشپزخانهٔ دوستت یعنی از نو نوشتن، و اگر مادربزرگ وسط کار یک خط را عوض کند، دفعهٔ بعد غذای همهٔ خانه فرق میکند، بیآنکه کسی تستش کرده باشد.Now imagine dictating the same recipe. Every step is a call, and between two calls someone can move the ingredients or start cooking the same dish. The card removes those gaps: you say “the card” once, and ten steps run back to back with nothing in between. But the card belongs to your grandmother's kitchen: her units, her stove, her order of operations. Taking it to a friend's kitchen means rewriting it, and if she changes one line mid-week, everyone's dinner changes the next time, with nobody having tested it.
یک فراخوانی بهجای چند رفتوبرگشت: همهٔ گامها آنطرف خط، داخل یک تراکنش.One call instead of many round trips: every step on the far side, inside one transaction.
و بعدش، فنیAnd now, technically
رویهٔ ذخیرهشده کدی است که داخل سرور پایگاه داده ذخیره و با یک فراخوانی اجرا میشود؛ اپلیکیشن بهجای فرستادن ده دستور، یک CALL میفرستد و بقیه آنطرف انجام میشود. سود اصلی دو چیز است: رفتوبرگشت شبکه کم میشود، و تراکنش کوتاهتر میماند، چون قفلها کمتر نگه داشته میشوند. میشود اجرای رویه را جدا از دسترسی به جدولها داد، پس سطح دسترسی هم تنگتر میشود. هزینهاش این است که زبانش مخصوص هر موتور است (PL/pgSQL، T-SQL، PL/SQL)، ابزار تست و دیباگش ضعیف است، و تغییرش با مهاجرت schema قاطی میشود. در تراکنشهای توزیعشده هم همین کوتاهشدن پنجرهٔ قفل است که آن را توصیه میکند.A stored procedure is code stored inside the database server and run by a call: instead of sending ten statements, the application sends one CALL and the rest happens on the far side. The benefit is twofold: fewer network round trips, and a shorter transaction, because locks are held for less time. You can also grant execution without granting table access, which tightens permissions. The cost is that its language belongs to one engine (PL/pgSQL, T-SQL, PL/SQL), its testing and debugging tools are weak, and changing it gets tangled with schema migrations. In distributed transactions it is that same shortening of the lock window that makes it worth the trouble.
1 callاپ یک بار صدا میزند، نه ده بارthe app calls once, not ten times
N → 1N رفتوبرگشت شبکه به یک فراخوانیN network round trips become one call
1 languageزبان مخصوص هر موتور؛ قابل انتقال نیستeach engine's own language; not portable
تلهٔ رایجThe usual trap
داخل رویه خطا را میگیری و ادامه میدهی؛ تراکنش کامیت میشود و اپ فکر میکند همهچیز درست بوده. یک EXCEPTION WHEN OTHERS THEN NULL کافی است تا نیمهٔ کار بیصدا ذخیره شود و هفتهها بعد، موقع حسابرسی، بفهمی ترازها نمیخوانند. مرز تراکنش را از دست اپلیکیشن گرفتی و به کدی دادی که هیچکس تستش نکرده.You catch the error inside the procedure and carry on; the transaction commits and the app believes all went well. One EXCEPTION WHEN OTHERS THEN NULL is enough to save half the work silently, and weeks later, during an audit, the books do not balance. You took the transaction boundary away from the application and gave it to code nobody tested.
همه بله گفتند؛ بعد هماهنگکننده غیبش میزند.Everyone said yes. Then the coordinator vanishes.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
با سه دوستت میخواهید بلیت قطار بگیرید و بلیتها فقط با هم معنا دارند. اول به هر سه پیام میدهی: «پول داری؟ موافقی؟» هر سه میگویند بله و پول را کنار میگذارند. تازه بعد از آن، تو میخری و به همه میگویی «خریدم». اگر یکی همان اول میگفت نه، هیچکس پول نمیگذاشت. آن پرسیدنِ اول، prepare است؛ آن گفتنِ دوم، commit؛ و هر سه تا شنیدن تصمیم دوم دست نگه داشتهاند.You and three friends want train tickets, and the tickets only make sense together. First you message all three: do you have the money, are you in? All three say yes and set the money aside. Only then do you buy and tell everyone it is bought. If one had said no at the start, nobody would have set anything aside. That first asking is prepare; that second telling is commit; and all three held back until they heard the decision.
تشبیه و تصویر ذهنیThe analogy
چرا اول نمیپرسی و همانجا نمیخری؟ چون اگر یکی بگوید نه، پول بقیه رفته و بلیت نصفه مانده. پس اول همه چیز را آماده میکنی و پول را کنار میگذاری — ولی هنوز چیزی نخریدهای — و تنها وقتی همه آمادهاند، یک تصمیم واحد میگیری. بهایش انتظار است: تا وقتی یکی جواب نداده، همه معطلاند. بدتر، اگر همان لحظه که همه «بله» گفتهاند گوشیِ تو خاموش شود، هیچکس نمیداند بلیت خریده شده یا نه. پول کنار گذاشته شده، صندلیها رزرو نشده، و هیچکدام از سه نفر اجازه ندارد خودش تصمیم بگیرد.Why not just buy as you go? Because if one person says no, everyone else's money has moved and the trip is half-bought. So you get everything ready and set the money aside — without buying anything yet — and only when all three are ready do you take one decision. The bill is waiting: until the last person answers, everyone is stuck. Worse, if your phone dies at the very moment all three have said yes, nobody knows whether the tickets were bought. The money is set aside, the seats are not booked, and none of the three is allowed to decide for themselves.
دو دور: دور اول همه رأی میدهند و آماده میشوند، دور دوم با یک تصمیم همه کامیت میکنند.Two rounds: everyone votes and prepares first, then one decision commits everyone.
و بعدش، فنیAnd now, technically
در دور اول، هماهنگکننده به همهٔ شرکتکنندهها میگوید «آماده شو». هر شرکتکننده کار را تا آخر انجام میدهد، همه چیز را مینویسد ولی کامیت نمیکند؛ رأی خودش را در لاگ خودش پایدار میکند و میگوید بله یا نه. اگر یک نفر نه بگوید یا جواب ندهد، هماهنگکننده به همه «لغو» میفرستد و کار تمام است. اگر همه بله گفتند، هماهنگکننده وارد دور دوم میشود: تصمیم را پیش از هر پیامی در لاگ خودش مینشاند بعد به همه خبر میدهد «کامیت کن». شرکتکنندهها اعمال میکنند و تأیید میفرستند. آن نوشتنِ تصمیم در لاگ هماهنگکننده، مهمترین خط کل پروتکل است؛ بدون آن، اگر هماهنگکننده بعد از فرستادن چند کامیت خراب شود، هیچکس نمیداند تصمیم چه بود. هزینهاش سه چیز است: دو رفتوبرگشت کامل، یک fsync اضافه روی هر شرکتکننده، و قفلهایی که از لحظهٔ رأی تا لحظهٔ کامیت بسته میمانند.In round one the coordinator tells every participant to prepare. Each participant does its work, writes everything, but does not commit; it persists its vote in its own log and answers yes or no. If anyone says no or fails to answer, the coordinator sends abort to all and the matter is closed. If everyone says yes, the coordinator enters round two: it persists the decision in its own log first, then tells everyone to commit. Participants apply and acknowledge. That write of the decision into the coordinator's log is the single most important line in the protocol; without it, a coordinator that crashes after sending a few commits leaves nobody able to say what the decision was. The bill is three things: two full round trips, an extra fsync on every participant, and locks that stay held from the vote to the commit.
2 roundsیک دور رأی، یک دور تصمیمone round to vote, one to decide
2 fsyncروی هر شرکتکننده: یکی برای رأی، یکی برای کامیتper participant: one for the vote, one for the commit
N locksقفل هر شرکتکننده از رأی تا کامیت بسته میماندeach participant holds its locks from vote to commit
چرا تصمیم باید قبل از گفتن نوشته شودWhy the decision must be written before it is spoken
یک وسوسهٔ همیشگی این است که هماهنگکننده اول به همه بگوید «کامیت» و بعد خودش تصمیم را جایی ثبت کند. این کار کل پروتکل را میشکند. فرض کن پیام به دو نفر از چهار نفر رسیده و بعد هماهنگکننده میافتد. حالا کسی که تصمیم را نمیداند نمیتواند بگوید بقیه چه کار کنند؛ بعضی کامیت کردهاند، بعضی نه، و هیچ راهی برای برگرداندن آن دو نفر نیست. به همین دلیل ترتیب اهمیت دارد: تصمیم اول پایدار میشود، بعد پخش میشود. گران است، ولی تنها چیزی است که در برابر خرابیِ وسط کار دوام میآورد.There is a standing temptation to have the coordinator tell everyone to commit and only afterwards write the decision down. That breaks the whole protocol. Suppose the message reached two of four participants and then the coordinator died. Whoever does not know the decision cannot tell the others what to do; some have committed, some have not, and those two cannot be brought back. Order matters for that reason: the decision becomes durable first, and is then broadcast. It is expensive, and it is the only thing that survives a crash in the middle.
کجا لازم است و کجا نهWhen you need it, and when you do not
دو مرحلهای برای وقتی است که نوشتن باید در چند سیستمِ جدا اتمیک باشد: دو دیتابیس مختلف، یک دیتابیس و یک صف پیام، یا یک سرویس پرداخت بیرونی. اگر همه چیز داخل یک دیتابیس است، تراکنش خودش همین کار را میکند و این پروتکل فقط هزینهٔ اضافه است. در معماریهای امروز، خیلی از تیمها بهجای آن سراغ الگوهای سبکتر میروند: outbox، مصرفکنندهٔ تکراریپذیر، و سازگاری نهایی. اینها ارزانترند، ولی تضمینشان ضعیفتر است و باید در طراحی جبران شوند. پروتکل را وقتی انتخاب کن که اتمیک بودن واقعاً لازم است و میتوانی هزینهٔ انتظار و ابهام را بپذیری.Two-phase commit is for writes that must be atomic across separate systems: two different databases, a database and a message queue, or an external payment service. If everything lives in one database, the transaction already does this and the protocol is pure overhead. Many teams now reach for lighter patterns instead — outbox, idempotent consumers, eventual consistency. They are cheaper, but their guarantee is weaker and the design has to make up for it. Choose the protocol when atomicity is genuinely required and you can afford the waiting and the ambiguity.
کامیت دومرحلهای در برابر قفلگذاری دومرحلهایTwo-phase commit versus two-phase locking
2PC2PC
2PL2PL
هدفGoal
اتمی بودن بین چند سیستمatomicity across systems
سریالپذیری داخل یک موتورserializability inside one engine
دو مرحله چیستThe two phases
رأی دادن، بعد تصمیمvote, then decide
قفل گرفتن، بعد آزاد کردنacquire, then release
هزینهCost
انتظار + تراکنش در ابهامwaiting plus in-doubt transactions
انتظار + بنبستwaiting plus deadlocks
کجاWhere
چند دیتابیس یا سرویسseveral databases or services
یک موتور، چند تراکنشone engine, several transactions
تلهٔ رایجThe usual trap
تصور اینکه 2PC همان 2PL است، چون اسمشان شبیه هم است. یکی پروتکلِ تصمیمِ مشترک بین چند سیستم است، دیگری روش قفلگذاری داخل یک موتور. بدتر: کسی 2PC را روشن میکند به امید سریالپذیری، و بعد میبیند تراکنشها کند شدهاند و همچنان ناسازگاری دارد.Assuming 2PC is 2PL because the names look alike. One is a shared decision protocol across systems; the other is a locking scheme inside one engine. Worse, someone enables 2PC hoping for serializability, then finds the transactions slow and the anomalies still there.
تا همه را نگیری، هیچکدام را پس نمیدهی.You may not let go of one until you hold them all.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
برای پروژهٔ درس، پنج کتاب مرجع از کتابخانه لازم داری. کتابخانه یک قاعده دارد: تا وقتی هر پنج کتاب را برداشتهای، حق نداری هیچکدام را برگردانی. پس اول همه را جمع میکنی، بعد شروع میکنی به خواندن، و تازه آخر کار یکییکی برمیگردانی. این قاعده جلوی این را میگیرد که دو گروه نصفِ کتابها را بگیرند و هیچکدام نتوانند کار کنند. بهایش هم روشن است: تا وقتی تو مشغول جمعکردنی، بقیه پشت در میمانند.For a course project you need five reference books from the library. The library has one rule: until you have taken all five, you may not put any of them back. So you gather them all first, then start reading, and only at the end do you return them one by one. The rule stops two groups from each holding half the books and neither being able to work. The price is plain: while you are gathering, everyone else waits at the door.
تشبیه و تصویر ذهنیThe analogy
در همین کتابخانه، خواندن و بردن دو چیز متفاوتاند. چند نفر میتوانند همزمان یک کتاب را روی میز بخوانند، ولی اگر کسی بخواهد کتاب را با خودش ببرد، هیچ خوانندهای نباید سر میز باشد. این تفاوت بین «فقط خواندن» و «عوض کردن» است. حالا بدترین حالت را ببین: دو گروه، هر کدام یک کتاب از کتابهای گروه دیگر را در دست دارند و هر دو کتاب بعدی را میخواهند. هیچکدام پس نمیدهد، چون قاعده گفته تا همه را نگرفتهای پس نده. هر دو میمانند و کتابخانه باید یکی را بیرون کند تا جلو برود.In that same library, reading and borrowing are different things. Several people can read one book at the table at once, but if someone wants to take it away, no reader may be sitting there. That is the line between “only reading” and “changing”. Now look at the worst case: two groups each hold one of the books the other needs and both want the next one. Neither lets go, because the rule says you may not release until you hold them all. Both sit there, and the library has to throw one of them out to move on.
دو فاز: تا لحظهای که آخرین قفل گرفته میشود هیچ قفلی آزاد نمیشود؛ بعد یکییکی آزاد میشوند.Two phases: nothing is released until the last lock is taken, then locks come off one by one.
و بعدش، فنیAnd now, technically
در این روش هر تراکنش دو مرحله دارد. در مرحلهٔ رشد فقط قفل میگیرد: قفل مشترک برای خواندن، قفل انحصاری برای نوشتن. در مرحلهٔ کاهش فقط قفل آزاد میکند و حق گرفتن قفل تازه ندارد. نقطهٔ گذار، لحظهای است که آخرین قفل را گرفتهای؛ از آن به بعد فقط میتوانی کم کنی. همین قاعدهٔ ساده تضمین سریالپذیری را میدهد: هر دو تراکنش متعارض حتماً روی یک داده به هم میرسند و چون هیچکدام تا پایان کار قفلش را پس نمیدهد، یکی مجبور است صبر کند؛ پس یک ترتیب واقعی بینشان شکل میگیرد. نسخهٔ سختگیرانهاش قفل انحصاری را تا کامیت نگه میدارد تا اگر تراکنشی abort شد، کسی مجبور به abort آبشاری نشود. مشکل اصلی بنبست است: تراکنش الف قفل ردیف یک را دارد و ردیف دو را میخواهد، تراکنش ب برعکس. هر دو منتظرند و هیچکدام جلو نمیرود؛ موتور باید یکی را قربانی کند. برای شبح هم قفل ردیف کافی نیست: باید بازهای از ایندکس یا یک شرط را قفل کرد، وگرنه ردیف تازهای که هنوز نیست از قفل رد میشود.Every transaction has two phases. In the growing phase it only acquires locks — shared for reading, exclusive for writing. In the shrinking phase it only releases them and may not take new ones. The turning point is the instant the last lock is acquired; after that you can only take away. This simple rule is what gives serializability: two conflicting transactions must meet on some piece of data, and since neither releases until it is done, one of them has to wait, so a real order forms between them. The strict variant holds exclusive locks until commit, so an abort does not force anyone else to abort. The main problem is deadlock: transaction A holds row one and wants row two while B does the reverse, so both wait and the engine must kill one. And row locks alone do not close phantoms — you have to lock an index range or a predicate, or a row that does not exist yet slips past the lock.
2 phasesرشد و کاهش؛ مرز، آخرین قفل گرفتهشده استgrowing then shrinking; the boundary is the last lock taken
strict 2PLنگه داشتن قفل تا کامیت، تا abort آبشاری رخ ندهدhold locks to commit so aborts do not cascade
deadlockهزینهٔ ساختاری این روش، نه یک باگa structural cost of the design, not a bug
چرا این قاعده سریالپذیری میدهدWhy the rule delivers serializability
استدلالش کوتاه است. دو تراکنش را بردار که به یک داده دست میزنند و یکی از آنها مینویسد. چون هیچکدام قفلش را قبل از گرفتن آخرین قفل پس نمیدهد، دیر یا زود یکی از دو حالت رخ میدهد: یا یکی قفل دیگری را میگیرد و صبر میکند تا طرف کارش تمام شود، یا هر دو در همان لحظه به هم میرسند و بنبست میشود که آن هم یعنی تعارض دیده شده است. در هیچ حالتی نمیشود هر دو همزمان بنویسند و هر دو موفق شوند. همین «دیده شدنِ» اجباری، ترتیب را میسازد.The argument is short. Take two transactions that touch the same data and one of which writes. Because neither releases before taking its last lock, one of two things must happen: either one takes the other's lock and waits for it to finish, or they meet at the same instant and deadlock, which also means the conflict was seen. In no case can both write at once and both succeed. That forced visibility is what builds the order.
قیمت: انتظار، بنبست، و صفThe bill: waiting, deadlocks, and queues
هزینهٔ این روش سه چیز است و هر سه در تولید دیده میشوند. اول، خواننده و نویسنده یکدیگر را معطل میکنند؛ روی یک صفحهٔ داغ، صف قفل به جایی میرسد که کل توان سیستم صرف مدیریت نوبت میشود. دوم، بنبست؛ موتور باید گراف انتظار را نگه دارد، دور را پیدا کند و یک تراکنش را بکشد، و برنامه باید بتواند آن خطا را دوباره اجرا کند. سوم، قفلهای طولانی؛ هر چه تراکنش بیشتر طول بکشد، قفلها بیشتر زنده میمانند و صف بلندتر میشود. به همین دلیل این روش در بارهای پرتعارض کند است و SSI برای همان بارها جذاب شده: هر دو سریالپذیری میدهند، ولی یکی با انتظار و دیگری با abort.The cost has three parts and all three show up in production. Readers and writers hold each other up; on a hot page the lock queue grows until the system spends its whole capacity managing turns. Deadlocks appear, so the engine has to keep a wait-for graph, find the cycle and kill a transaction, and the application has to be able to run that one again. And locks live long: the longer a transaction runs, the longer they stay and the longer the queue. That is why the method is slow under heavy contention and why SSI looks attractive for the same loads — both give serializability, one through waiting and the other through aborts.
تلهٔ رایجThe usual trap
گرفتن قفلها به ترتیبهای مختلف در کدهای مختلف. اگر یک مسیر ابتدا ردیف حساب و بعد ردیف سفارش را قفل کند و مسیر دیگر برعکس، دیر یا زود دو تراکنش روی هم قفل میشوند و موتور یکی را میکشد. قاعدهٔ ساده — همهجا یک ترتیب ثابت برای گرفتن قفل — بیشتر بنبستهای تولیدی را حذف میکند، بدون آنکه چیزی از تضمین کم شود.Taking locks in different orders on different code paths. If one path locks the account row and then the order row while another does the reverse, two transactions will eventually lock each other out and the engine will kill one. One simple rule — the same acquisition order everywhere — removes most production deadlocks without weakening the guarantee.
هر دو تصمیم درست گرفتند؛ نتیجه غلط شد.Both decisions were right. The outcome was wrong.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
تو و همخانهات هر دو ساعت هفت میزنید بیرون. هر کدام با خودش میگوید «او نان میخرد»، پس هیچکدام نمیخرد. نه تو تنبلی کردی نه او: هر دو یک چیز را دیده بودید — نان هست — و هر کدام بر اساس همان، بار را از دوش خودش برداشت. شب سفره خالی است و هیچکس هم خطا نگرفته. write skew همین است: دو نوشتن روی دو چیز جدا، هر دو مجاز، ولی با هم قید را میشکنند.You and your flatmate both leave at seven. Each of you thinks “the other one will buy bread”, so neither does. Neither of you was lazy: you both saw the same thing — there is bread — and each of you took the load off the other's shoulders on that basis. At night the table is empty and nobody got an error. That is write skew: two writes on two different things, both allowed, and together they break the rule.
تشبیه و تصویر ذهنیThe analogy
چرا این حالت با بقیهٔ مشکلها فرق دارد؟ چون هیچ نوشتنی روی نوشتن دیگر نیفتاده. اگر هر دو نفر همان یک ردیف را عوض میکردند، دیتابیس تعارض را میدید و یکی را برمیگرداند؛ ولی اینجا دو ردیف جدا عوض شدهاند، پس هیچ قفلی برخورد نکرده است. قید هم جای دیگری زندگی میکند: «باید حداقل یک نفر در خانه بماند»، «باید حداقل یک پزشک کشیک باشد». قید در ردیفها نیست، در جمعِ ردیفهاست، و دقیقاً همین جمع است که هیچکس قفلش نکرده. برای همین هم فقط چیزی که خواندنها را جدی میگیرد میتواند بگیردش.Why is this different from the other anomalies? Because no write landed on another write. If both people had changed the same row, the database would have seen the conflict and rolled one back; here two separate rows changed, so no lock ever met anything. And the rule lives somewhere else: at least one person stays home, at least one doctor stays on call. The rule is not in the rows, it is over the rows, and that is precisely the thing nobody locked. So only machinery that takes reads seriously can catch it.
هیچ نوشتنی روی نوشتن دیگر نیفتاد؛ ولی تصمیم هر دو به هم وابسته بود و قید شکست.No write landed on another write, yet each decision depended on the other and the rule broke.
و بعدش، فنیAnd now, technically
به این حالت وابستگی خواندن-نوشتن میگویند: تراکنش الف چیزی را میخواند که تراکنش ب مینویسد، و تراکنش ب چیزی را میخواند که الف مینویسد، در حالی که هیچکدام ردیف دیگری را دست نمیزند. موتوری که فقط نوشتنها را با هم مقایسه میکند هیچ تعارضی نمیبیند؛ قفل ردیف هم چیزی برای گرفتن ندارد، چون دو ردیف جدا هستند. قیدی که شکسته میشود هم جای عجیبی زندگی میکند: در هیچ ردیفی نیست، در جمع ردیفهاست — «حداقل یک پزشک کشیک»، «حداقل یک نفر خانه میماند»، «مجموع برداشتها از موجودی بیشتر نشود». به همین دلیل هیچ کلید خارجی و هیچ ایندکس یکتایی هم نمیگیردش. سه راه برای بستنش هست. اول، برو روی سطح serializable تا موتور خودش با SSI یا 2PL بگیرد. دوم، تعارض را بساز: یک ردیف نگهبان را که هر دو تراکنش باید بهروزش کنند قفل کن، تا تعارض پنهان به یک تعارض واقعی نوشتن-نوشتن تبدیل شود. سوم، منطق را در یک دستور اتمی جمع کن که اصلاً فضای تصمیمگیری همزمان نداشته باشد. چک کردن در کد برنامه، بیرون از تراکنش، هیچکدام از اینها نیست و فقط مشکل را به یک بازهٔ زمانی بزرگتر منتقل میکند.This is a read-write dependency: transaction A reads something B writes and B reads something A writes, while neither touches the other's row. An engine that compares only writes sees no conflict, and a row lock has nothing to hold, because the rows are different. The rule being broken lives somewhere odd too: it is not inside any row, it is over the rows — at least one doctor on call, at least one person at home, total withdrawals never above the balance. No foreign key and no unique index will catch it. There are three ways to close it. Go to serializable and let the engine catch it with SSI or 2PL. Manufacture the conflict: pick a guard row both transactions must update and lock it, turning a hidden conflict into a real write-write one. Or collapse the logic into one atomic statement that leaves no room for a concurrent decision at all. Checking in application code outside the transaction is none of these and only moves the problem to a wider window.
2 rowsدو ردیف جدا نوشته میشود، پس تعارضی دیده نمیشودtwo separate rows are written, so no conflict shows up
1 ruleقید روی مجموع ردیفهاست، نه درون هیچ ردیفیthe rule lives over the set of rows, not inside one
SIجلویش را نمیگیرد، چون نوشتنها به هم نمیخورندit does not stop this, because the writes never meet
چرا دیتابیس نمیبیندشWhy the database cannot see it
تصور کن موتور فقط این را میداند که کدام ردیفها قفل شدهاند و کدام مقدارها عوض شدهاند. در اینجا دو ردیف مختلف عوض شده، پس فهرست تغییرات هیچ اشتراکی ندارد. حتی اگر سطح ایزولاسیون اسنپشات روشن باشد، هر تراکنش عکس خودش را دارد و کاملاً مجاز است که بگوید «آن یکی روی کشیک است، پس من میروم». هیچ قاعدهای در آن عکس نقض نشده است. چیزی که نقض شده، قراری است که بیرون از دیتابیس در ذهن برنامهنویس بوده و هیچجا نوشته نشده — و دیتابیس نمیتواند قاعدهای را که نمیداند اجرا کند.Picture an engine that knows only which rows are locked and which values changed. Here two different rows changed, so the change lists share nothing. Even with a snapshot level on, each transaction has its own photo and is perfectly entitled to conclude that the other one is on call, so I can go. No rule inside that photo is violated. What is violated is an agreement that lived in the programmer's head, outside the database, and was never written down — and a database cannot enforce a rule it does not know.
قید را به دیتابیس بدهHand the rule to the database
راهحلهای خوب یک ویژگی مشترک دارند: قید را از ذهن برنامهنویس بیرون میآورند و جایی میگذارند که موتور مجبور باشد نگاهش کند. یا سطح ایزولاسیون را بالا میبری تا خواندنها هم تحت نظر بیایند، یا خودت تعارض میسازی: در مثال کشیک، هر دو تراکنش همان ردیفِ «وضعیت کشیک» را با یک قفل بهروز میکنند، پس یکی صبر میکند. در مثال بودجه، یک ردیف نگهبان برای کل بودجه میگذاری. در قیدهای بازهای، قید طرد همین نقش را بازی میکند. هیچکدام جادو نیستند؛ همه یک کار میکنند: تعارضی که نامرئی بود را مرئی میکنند.The good fixes share one property: they take the rule out of the programmer's head and put it somewhere the engine is forced to look. Either you raise the isolation level so reads are watched too, or you manufacture the conflict: in the on-call example, both transactions update the same on-call row under a lock, so one waits. In the budget example you keep one guard row for the whole budget. For range rules, an exclusion constraint plays exactly this part. None of it is magic; all of it makes an invisible conflict visible.
تلهٔ رایجThe usual trap
جمع کردن قید در کد برنامه: دو کوئری، یک شرط در زبان برنامه، بعد دو نوشتن. این الگو همیشه درست به نظر میرسد چون تست تکنفرهاش پاس میشود، و همیشه در تولید میشکند چون فاصلهٔ بین خواندن و نوشتن بهاندازهٔ کافی بزرگ است. قید را جایی بگذار که همان لحظهٔ نوشتن چک شود.Enforcing the rule in application code: two queries, an if statement, then two writes. It always looks correct because the single-user test passes, and it always breaks in production because the gap between the read and the write is wide enough to matter. Put the rule where it is checked at the instant of the write.
دو سیستم جدا، یک تصمیم؛ چه کسی هماهنگ میکند؟Two separate systems, one decision. Who coordinates it?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
ماشینت را که میفروشی، برای تعویض پلاک هم باید بیمهنامهٔ جدید صادر شود، هم سند در دفترخانه ثبت شود؛ دو سازمان جدا که هر کدام کاغذ و مهر خودش را دارد. یک فرم استاندارد هست که هر دو قبول دارند و یک نفر آن را بینشان میبرد، و تا هر دو مهر نخورده هیچکدام قطعی نیست. اگر آن نفر بین دو مهر گم شود، هر دو طرف مهر زدهاند و هیچکس نمیداند معامله تمام شده یا نه؛ XA همان فرم استاندارد است.When you sell your car, transferring the registration needs a new insurance policy and a stamped deed at the notary: two separate offices, each with its own paper and seal. There is a standard form both accept, and one person walks it between them; until both seals are on it, neither side is final. If that person disappears between the two seals, both offices have stamped and nobody knows whether the deal is done — XA is that standard form.
تشبیه و تصویر ذهنیThe analogy
فرم استاندارد یک شرط دارد: هر سازمان قبول میکند که مهرش را بزند و بعد منتظر بماند، بیآنکه خودش تصمیم نهایی را بگیرد. تا وقتی فرم دست آن نفر است، بیمه صادر شده ولی فعال نیست و ثبت سند بلوکه است؛ یعنی هر دو طرف منابعشان را نگه داشتهاند. اگر آن نفر برنگردد، هیچکدام نمیتوانند جلو بروند یا عقب؛ فقط میتوانند منتظر بمانند و هر از گاهی بپرسند «فرم رسید؟». این انتظار تا وقتی طول میکشد که یک آدم پیدا شود و از روی دفترِ آن نفر تصمیم بگیرد.The standard form comes with one condition: each office agrees to stamp and then wait, without deciding the outcome on its own. While the walker holds the form, the insurance is issued but not active and the deed is blocked; both sides are holding their resources. If the walker never returns, neither office can move forward or back; they can only wait and occasionally ask “has the form arrived?”. That wait lasts until a person finds the walker's ledger and decides for them.
prepare، بعد مکث؛ اگر هماهنگکننده نماند، منابع در ابهام میمانند و قفلها باز نمیشوند.Prepare, then pause: if the coordinator is gone, resources stay in doubt and the locks do not open.
و بعدش، فنیAnd now, technically
XA یک استاندارد (X/Open XA) برای رابط بین یک هماهنگکننده و چند resource manager است: پایگاهها و صفها با چند فراخوانی مثل xa_start، xa_prepare، xa_commit و xa_recover همان پروتکل کامیت دومرحلهای را اجرا میکنند. هماهنگکننده اول از همه از هر منبع میپرسد که آماده است یا نه؛ هر منبع تصمیمش را روی دیسک مینویسد و قفلهایش را نگه میدارد؛ اگر همه بله بگویند، دستور commit میرود و اگر یکی نه بگوید، همه rollback میشوند. اگر هماهنگکننده وسط کار از کار بیفتد، منابعی که prepare کردهاند در حالت in-doubt میمانند: تصمیم را میدانند ولی حکم نهایی را ندارند، و تا وقتی xa_recover جواب ندهد قفلها آزاد نمیشوند. XA پروتکل اجماع نیست؛ هماهنگکننده تکنسخه است و اگر لاگش از دست برود، بعضی منابع ممکن است heuristic تصمیم بگیرند و از بقیه جدا بیفتند.XA is a standard (X/Open XA) for the interface between one coordinator and several resource managers: databases and queues expose calls such as xa_start, xa_prepare, xa_commit and xa_recover and thereby run two-phase commit. The coordinator first asks everyone “ready?”; each resource writes the decision to disk and keeps its locks; if all say yes, commit goes out, and if one says no, everyone rolls back. If the coordinator dies mid-flight, resources that prepared sit in doubt: they know their own vote but not the outcome, and their locks are not released until xa_recover answers. XA is not a consensus protocol; the coordinator is a single copy, and if its log is lost some resources may take a heuristic decision and drift away from the rest.
2 phasesprepare برای همه، بعد commit برای همهprepare for all, then commit for all
1 coordinatorتکنسخه؛ نقطهٔ شکست واحدa single copy, a single point of failure
in-doubtحالتی که تصمیم معلوم نیست و قفل آزاد نمیشودthe outcome is unknown and the locks stay
تلهٔ رایجThe usual trap
تراکنشی که prepare شده و هیچوقت جواب commit نگرفته را رها میکنی. آن تراکنش قفل ردیفهایش را نگه داشته و هیچ تایماوتی هم آزادش نمیکند؛ چند ساعت بعد نوشتن روی همان ردیفها میایستد و اپ به نظر قفل میکند، در حالی که هیچ درخواستی در جریان نیست. in-doubt را باید پایش کنی و دستی حلش کنی.You leave a prepared transaction that never received its commit answer. It keeps holding its row locks, and no timeout releases it; hours later writes to those rows stall and the app looks frozen while nothing is in flight. In-doubt transactions have to be watched and settled by hand.