۲۴ واژه از فصل ۱۲ — هر کدام با یک مثال روزمره، یک نمودار و تلهٔ رایج.24 terms from chapter 12, each with an everyday example, a diagram and the usual trap.
«پیام را گرفتم» را قبل از کار میگویی یا بعدش؟Do you say “got it” before the work, or after?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
پیک بسته را میدهد و یک برگه برای امضا جلو میگیرد. اگر پیش از گرفتن بسته امضا کنی، بعداً معلوم نیست بسته دستت رسیده یا نه. اگر بعد از گرفتن امضا کنی و پیک برگه را گم کند، ممکن است همان بسته را فردا دوباره بیاورد. تأیید دریافت (ack) همان امضاست، و اینکه کِی امضا میکنی تعیین میکند بسته گم شود یا دوبار بیاید.The courier hands you a parcel and holds out a slip for your signature. Sign before you take the box and nobody can later prove it ever arrived. Sign after you take it, and if the courier loses the slip the same parcel may come back tomorrow. An ack is that signature, and when you sign decides whether the parcel goes missing or arrives twice.
تشبیه و تصویر ذهنیThe analogy
در دفتر پست محله، هر نامهدار یک ستون دارد. وقتی نامهای را تحویل میگیری، مسئول دفتر یک تیک در ستون میزند و نامه از فهرست «در راه» بیرون میآید. این تیک کار را ساده میکند: لازم نیست دفتر بداند تو با نامه چه کردی، فقط بداند که دیگر گردن او نیست. ولی همین سادگی جای خطا دارد. اگر مسئول دفتر تیک را همان لحظه بزند که نامه از دستش میگیری و تو در راه خانه گمش کنی، دفتر خیالش راحت است و نامه هیچوقت برنمیگردد. اگر تیک را آخر روز و یکجا بزند، تا آن موقع نامههای تحویلشده هنوز «در راه» حساب میشوند و ممکن است دوباره فرستاده شوند. پس تأیید هم راه نجات است و هم نقطهٔ ریسک: هرچه دیرتر بزنی احتمال تکرار بیشتر، هرچه زودتر بزنی احتمال گم شدن.In the neighbourhood post office every carrier has a column. When a letter is handed over, the clerk ticks that column and the letter leaves the “in transit” list. The tick simplifies everything: the office does not need to know what you did with the letter, only that it is no longer their problem. That simplicity is exactly where the risk lives. Tick the moment the letter leaves the clerk's hand and it can vanish on your way home; the office is satisfied and the letter never comes back. Tick once at the end of the day and every delivered letter still counts as in transit until then, so some get sent out again. Confirmation is both the rescue and the exposure: later means more repeats, earlier means more losses.
تأیید بعد از پردازش پیام را از گردن کارگزار برمیدارد؛ تأیید قبل از پردازش، همان پیام را بیصدا قربانی میکند.An ack after the work clears the message from the broker; an ack before the work is how a message dies quietly.
و بعدش، فنیAnd now, technically
ack یک پیام کنترلی است که مصرفکننده به کارگزار میفرستد تا بگوید مسئولیت این پیام را پذیرفته است؛ کارگزار بعد از آن میتواند پیام را دور بریزد یا نشانگر آن مصرفکننده را جلو ببرد. همه چیز به ترتیب بستگی دارد. اگر ack پیش از پردازش برود، یک خرابی بین ack و پایان کار پیام را برای همیشه میبرد؛ این همان at-most-once است. اگر ack بعد از پردازش برود، خرابی در فاصلهٔ بین انجام کار و رسیدن ack باعث redelivery میشود و کار دو بار انجام میشود؛ این at-least-once است. نکتهٔ ظریف این است که ack هیچ تضمینی دربارهٔ اثر پایینستری نمیدهد: کارگزار فقط میداند مصرفکننده ادعا کرده است، نه اینکه نوشتن در مقصد کامیت شده باشد. خیلی از سیستمها ack را دستهای یا تجمعی میفرستند و میگویند «تا آفست N»؛ در واقع ack همان کامیت آفست است با دانهٔ ریزتر. در کارگزارهای صفی، پیام تأییدنشده بعد از یک مهلت دوباره تحویل داده میشود و اگر چند بار تلاش بینتیجه بماند به صف نامهٔ مرده میرود.An ack is a control message a consumer sends to the broker to say it has taken responsibility for a message; the broker may then drop it, or move that consumer's cursor past it. Order is everything. Send the ack before the work and a crash between them loses the message for good, which is at-most-once. Send it after the work and a crash in between triggers a redelivery and the work runs twice, which is at-least-once. The subtle part is that an ack promises nothing about the downstream effect: the broker only knows the consumer claimed it, not that the write to the sink committed. Many systems ack in batches or cumulatively (“up to offset N”), which in a log broker is the same act as committing an offset at a finer grain. In queue-style brokers an unacked message reappears after a visibility timeout and, after repeated failures, is parked in a dead-letter queue.
۱ پیامهر ack به یک پیام یا یک آفست اشاره میکندone ack covers one message or one offset
۰ بارسقف تحویل وقتی ack پیش از پردازش میرود و خرابی رخ میدهدwhat survives a crash when the ack went out before the work
۲ بارسقف تکرار وقتی ack بعد از پردازش گم میشودthe duplicate you get when the ack is lost after the work
ack، کامیت و تراکنش سه چیزندack, commit and transaction are three things
خیلیها این سه را یکی میگیرند و بعد در تولید گیر میکنند. ack دربارهٔ یک پیام است: «این یکی را دیدم». کامیت آفست دربارهٔ یک موقعیت است: «تا اینجا را انجام دادم». تراکنش دربارهٔ یک واحد کار است: «یا همهٔ این تغییرها، یا هیچکدام». تفاوت وقتی روشن میشود که خرابی رخ دهد. با ack بهتنهایی، بعد از بازیابی نمیدانی از کدام نقطه شروع کنی، چون کارگزار فقط میداند چند پیام تأیید شده، نه اینکه آفست تا کجا ذخیره شده. با کامیت آفست بهتنهایی هم اگر کامیت پیش از نوشتن خروجی انجام شود همان گمشدن رخ میدهد، فقط با دانهای درشتتر. تنها وقتی هر سه به هم میچسبند به اثر یکبار میرسی: آفست و خروجی، در یک تراکنش.People collapse these three and then get caught in production. An ack is about one message: “I saw this one.” An offset commit is about a position: “I finished everything up to here.” A transaction is about a unit of work: “all of these changes, or none.” The difference shows up when something crashes. With acks alone you cannot tell where to resume, because the broker only counts confirmed messages and does not know which offset was stored. With an offset commit alone, committing before the write lands gives you exactly the same loss at a coarser grain. Only when all three line up do you get a single effect: the offset and the output saved inside one transaction.
ack وقتی گم میشودWhen the ack itself is lost
ack یک پیام شبکه است و مثل هر پیام شبکهای میتواند گم شود. مصرفکننده کار را تمام کرده، ack را فرستاده، و بسته در راه برگشت افتاده است. کارگزار چه میداند؟ فقط میداند که ack نرسیده، پس بعد از یک مهلت پیام را دوباره تحویل میدهد؛ شاید به همان مصرفکننده، شاید بعد از ریبالانس به یکی دیگر. از دید سیستم، کار دوبار انجام شده و هیچکس خطایی نکرده. به همین دلیل هر سیستم at-least-once باید جایی برای تکرار آماده باشد: یا نوشتن در مقصد idempotent باشد، یا کلید یکتا داشته باشد، یا هر دو. ack گمشده همچنین میتواند ترتیب را به هم بزند، چون نسخهٔ دوم ممکن است بعد از پیامهای بعدی برسد.An ack is a network message and can be lost like any other. The consumer finished the work, sent the ack, and the packet died on the way back. What does the broker know? Only that no ack arrived, so after a timeout it delivers the message again — perhaps to the same consumer, perhaps to another after a rebalance. From the system's point of view the work happened twice and nobody made a mistake. That is why every at-least-once design needs somewhere for the duplicate to land safely: an idempotent write at the sink, a unique key, or both. A lost ack can also scramble ordering, because the second copy may arrive after later messages.
تلهٔ رایجThe usual trap
تأیید کردن بهمحض خواندن پیام از سوکت، چون «رسید» را با «انجام شد» یکی گرفتهای. همان یک خط، at-least-once را بیصدا به at-most-once تبدیل میکند و تا وقتی یک مشتری شکایت نکند کسی نمیفهمد. ack فقط ادعای مصرفکننده است، نه سند انجام کار.Acking the moment the message leaves the socket, because you have confused “received” with “done”. That one line silently turns at-least-once into at-most-once, and nobody notices until a customer complains. An ack is a consumer's claim, not a receipt for the work.
«دادهٔ قدیمی را هم میخواهم.» از کجا شروع میکنی؟“I want the old data too.” Where do you start?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
دفترچهٔ تلفن تازهای خریدهای و میخواهی همهٔ شمارههای دفترچهٔ قبلی را هم در آن داشته باشی. یک هفته طول میکشد، ولی بعد از آن دو دفترچه یکی میشوند و از آن به بعد فقط شمارههای تازه را اضافه میکنی. پر کردن عقبمانده (backfill) همین است: گذشته را با همان قاعدهای که آینده را مینویسی، یکجا پر میکنی.You bought a new address book and want every number from the old one in it. It takes a week, but afterwards the two books match and you only add new numbers. Backfilling is that week: you fill the past using the same rule you will use for the future.
تشبیه و تصویر ذهنیThe analogy
چیزی که این کار را ممکن میکند این است که دفترچهٔ قدیمی را دور نریخته بودی. اگر پارسال همه را پاک کرده بودی، هیچ راهی نبود؛ پس backfill همیشه یک بدهی به نگهداشت داده دارد. کار دو فاز دارد: اول عقبمانده را پر میکنی، بعد باید به جریان زنده برسی، و همین لحظهٔ رسیدن سختترین بخش است. اگر موقع پر کردن، شمارههای تازه هم میآیند، باید همزمان آنها را هم بنویسی، وگرنه دقیقاً در نقطهای که به «حالا» رسیدی یک شکاف میماند. برای همین معمولاً از قدیم به جدید میروی و تا وقتی به دمِ جریان نرسیدهای، نوشتن زنده را هم موازی نگه میداری.What makes the week possible is that you kept the old book. Had you thrown it away last year there would be no path at all, which is why backfill is always a debt owed to data retention. The work has two phases: fill the backlog, then reach the live stream, and that arrival is the hard part. New numbers keep coming while you copy, so you have to write them too, or a gap opens exactly where you caught up to “now”. So you go oldest first and keep the live write path running until you touch the tail.
پر کردن عقبمانده گذشته را با کد تازه میسازد؛ سختترین لحظه جایی است که به دمِ جریان زنده میرسی.A backfill rebuilds the past with new code; the hard moment is the one where you touch the live tail.
و بعدش، فنیAnd now, technically
backfill یعنی یک خط لولهٔ تازه، یا نسخهٔ تازهای از یک خط لوله، را روی دادهای که قبلاً ذخیره شده اجرا کنی تا خروجی جدید از گذشته پر شود. سه چیز لازم است: منبع باید نگهداشت داشته باشد تا تاریخ هنوز موجود باشد، اجرا باید قابل تقسیم باشد تا بتوانی آن را روی چند ماشین و در چند بازهٔ زمانی ببری، و مقصد باید نوشتن دوباره را تحمل کند. رایجترین شکلش این است که یک نمای مشتقشدهٔ جدید بسازی، مثلاً یک ایندکس جستوجوی تازه یا یک جدول تجمیعی، و بعد ترافیک را از نمای قدیم به جدید ببری. خطر اصلی، شکاف بین پایان backfill و شروع جریان زنده است؛ اگر این دو بازه دقیقاً به هم نچسبند، رکوردهایی که در آن فاصله رسیدهاند در هیچکدام نیستند. راهحل استاندارد اجرای همزمان است: هم تاریخ را میخوانی، هم جریان زنده را، و مقصد را با کلید یکتا مینویسی تا نوشتن دوباره بیضرر باشد. backfill سنگین روی منبع هم فشار میآورد، پس معمولاً با نرخ محدود و در ساعت خلوت اجرا میشود.A backfill runs a new pipeline, or a new version of one, over data that was already stored so the new output covers the past. It needs three things: the source must still have the history, the run must be divisible so you can spread it across machines and time ranges, and the sink must tolerate being written twice. The usual shape is to build a new derived view — a fresh search index, a new aggregate table — and then move traffic from the old view to the new one. The main hazard is the seam between the end of the backfill and the start of live traffic; if the two ranges do not meet exactly, records that arrived in the gap exist in neither. The standard answer is to run them together: read history and live traffic at once, and write to the sink under a unique key so a second write is harmless. A heavy backfill also leans on the source, so it is normally rate-limited and run off-peak.
۱ بارهر رکورد تاریخی معمولاً یک بار در backfill خوانده میشودeach historical record is normally read once
۲ منبعتاریخ و جریان زنده، تا لحظهٔ رسیدن به دمhistory and live traffic, until the tail is reached
۱ شکافاگر دم به هم نچسبد، رکوردهای آن فاصله گم میشوندif the seam misses, the records in that gap are lost
چرا backfill گران استWhy a backfill costs so much
backfill همهٔ تاریخ را میخواند، پس زمانش با حجم تاریخ رشد میکند، نه با حجم ترافیک امروز. همان دیسک و همان شبکهای را مصرف میکند که سرویس زنده روی آن است، و همان مقصدی را مینویسد که کاربران منتظرش هستند. اگر نرخش را محدود نکنی، سرویس زنده کند میشود و کاربر فکر میکند سیستم خراب است. اگر محدود کنی، چند روز طول میکشد و در این چند روز باید هم تاریخ را ببری و هم زنده را نگه داری. هزینهٔ سوم پنهان است: خروجی backfill خودش داده تولید میکند و آن داده هم باید ذخیره و ایندکس شود، پس فشار روی مقصد دو برابر میشود.A backfill reads all of history, so its duration grows with the size of that history, not with today's traffic. It eats the same disk and network the live service is using, and it writes to the same sink users are waiting on. Without a rate limit the live service slows down and users conclude the system is broken. With a rate limit the job takes days, and during those days you must carry both history and live traffic. The third cost is hidden: the backfill's own output is data too, which must be stored and indexed, doubling the pressure on the sink.
شکافِ دم، جایی که همه اشتباه میکنندThe tail seam, where everyone slips
تصور کن backfill ساعت ده شب تمام میشود و جریان زنده از ساعت ده شب شروع میکند. رکوردی که ساعت ۹:۵۹:۵۹ رسیده و در خروجی backfill نیامده، در هیچکدام نیست. این شکاف همیشه کوچک است و همیشه پیدا نمیشود، تا روزی که گزارشی با گزارش قبلی نمیخواند. راه درست این است که backfill را تا یک نقطهٔ مشخص در لاگ ببری، آفست آن نقطه را ذخیره کنی، و جریان زنده را دقیقاً از همان آفست شروع کنی؛ یعنی مرز را از لاگ بگیری، نه از ساعت. حتی با این کار هم چند دقیقه همپوشانی بهتر از چند ثانیه شکاف است، چون نوشتن دوباره را میشود تحمل کرد و رکورد گمشده را نه.Imagine the backfill ends at ten in the evening and live processing starts at ten. A record that arrived at 9:59:59 and is not in the backfill output is in neither. The gap is always small and always invisible, until a report disagrees with the previous one. The right move is to take the backfill up to a known point in the log, store that offset, and start live processing from exactly that offset — take the boundary from the log, not from the clock. Even then, a few minutes of overlap beats a few seconds of gap, because a duplicate write can be absorbed and a missing record cannot.
تلهٔ رایجThe usual trap
فکر کردن اینکه backfill یک کار آفلاین است. backfill همان منبع و همان مقصد تولید را میزند؛ اگر نرخش را محدود نکنی، اولین قربانی خودِ سرویس زنده است، و اگر بعد از تمام شدنش مرز را با ساعت بگذاری، شکافی میسازی که ماهها بعد در یک گزارش پیدا میشود.Treating a backfill as offline work. It hits the same source and the same production sink; without a rate limit the first casualty is the live service, and if you cut the boundary by wall clock instead of by offset you build a gap that surfaces months later in a report.
مصرفکننده عقب افتاده؛ چه کسی باید صبر کند؟The consumer is behind. Who waits?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
از کتری برای مهمانها چای میریزی. لیوان کوچک است؛ اگر شیر را باز بگذاری، چای سرریز میشود. بهجای اینکه زیر لیوان یک سطل بگذاری، شیر را کم میکنی تا به سرعت لیوان برسی. فشار برگشتی (backpressure) همین است: سرعت تولید را با ظرفیت مصرف هماهنگ میکنی، نه اینکه ظرف را بینهایت بزرگ کنی.You are pouring tea for guests from a kettle. The cup is small; leave the spout wide open and tea goes everywhere. Instead of putting a bucket under the cup, you ease the spout back until it matches the cup. Backpressure is that: you match the rate of production to the capacity of consumption instead of making the vessel infinitely large.
تشبیه و تصویر ذهنیThe analogy
میشد زیر لیوان یک سطل گذاشت؛ مدتی جواب میدهد، ولی فقط زمان خرابی را عقب میاندازد، چون سطل هم پر میشود و آنوقت همه چیز یکجا سرریز میکند. راه دوم این است که چای را روی زمین بریزی، یعنی پیام را دور بیندازی؛ سریع است و داده از دست میرود. راه سوم همان کم کردن شیر است: تولیدکننده با سرعت مصرفکننده جلو میرود. بهایش این است که فشار به عقب برمیگردد. کتری دست کسی میماند، صف پشت درِ فروشگاه تشکیل میشود، و اگر تولیدکننده مشترک باشد همهٔ مصرفکنندههایش کند میشوند. به همین دلیل انتخاب واقعی بین «حافظهٔ بیشتر»، «داده از دست بده» و «کار را کند کن» است و هیچکدام مجانی نیست.You could put a bucket under the cup: it works for a while and only postpones the failure, because the bucket fills too and then everything spills at once. You could pour the tea on the floor instead, which is dropping messages: fast, and the data is gone. Or you can ease the spout, so the producer moves at the consumer's pace. The price is that the pressure travels backwards: the kettle stays in someone's hand, a queue forms outside the shop, and if many consumers share that producer they all slow down together. The real choice is therefore between more memory, lost data and slower work, and none of the three is free.
تا وقتی تولید از مصرف جلوتر است، صف هر ثانیه رشد میکند؛ فشار برگشتی همان ترمزی است که این اختلاف را صفر میکند.While the producer outruns the consumer the queue grows every second; backpressure is the brake that closes the gap.
و بعدش، فنیAnd now, technically
backpressure یعنی مکانیزمی که وقتی مصرفکننده از ظرفیت عقب افتاد، سرعت تولید را کم کند. سه راه شناختهشده وجود دارد. اول، بافر محدود: وقتی صف پر شد، نوشتن تولیدکننده بلاک میشود؛ سادهترین و رایجترین شکل که فشار را دقیقاً به همان نقطهٔ تولید برمیگرداند. دوم، نرخ محدود در مبدأ: تولیدکننده از قبل سهمیه دارد و بیشتر از آن نمیفرستد. سوم، نمونهبرداری یا دور ریختن: وقتی بار از حد گذشت، بخشی از داده قربانی میشود تا بقیه سرویس بماند؛ این انتخاب درستی است برای متریک و لاگ، و انتخاب غلطی برای پرداخت. نکتهٔ مهم این است که هیچ بافری مشکل را حل نمیکند، فقط جابهجا میکند: اگر متوسط تولید از متوسط مصرف بیشتر باشد، هر بافری دیر یا زود پر میشود. فشار برگشتی در زنجیره هم منتقل میشود و اگر در هیچ حلقهای مهار نشود، به کندی کل مسیر و بعد به timeout و تلاش دوباره تبدیل میشود که خودش بار را بیشتر میکند.Backpressure is any mechanism that slows production when a consumer falls behind capacity. There are three familiar forms. Bounded buffering: once the queue is full, the producer's write blocks, which is the simplest and most common shape and pushes the pressure back to the exact point of production. Rate limiting at the source: the producer has a quota and does not exceed it. Sampling or shedding: when load passes a threshold, some data is sacrificed so the rest of the service survives, which is right for metrics and logs and wrong for payments. The important point is that no buffer solves the problem, it only moves it: if average production exceeds average consumption, every buffer fills eventually. In a chain the pressure travels, and if no link absorbs it the whole path slows, then times out, then retries, which adds more load.
λ > μتا وقتی نرخ ورود از نرخ سرویس بیشتر است، صف بیپایان رشد میکندwith arrival rate above service rate the queue grows without bound
۱ بافربافر زمان میخرد، اختلاف نرخ را حل نمیکندa buffer buys time, not throughput
p99اولین جایی که فشار را میبینی دم تأخیر است، نه میانگینthe pressure shows up in the tail, not the average
بافر، انتخاب نیستA buffer is not a choice
صف بزرگترین دروغ سیستمهای توزیعشده است، چون مدتها همه چیز مرتب به نظر میرسد. تولیدکننده سریع جواب میگیرد، مصرفکننده با سرعت خودش کار میکند، و تنها چیزی که عوض میشود یک عدد در داشبورد است. بعد آن عدد از یک حدی میگذرد و ناگهان تأخیر از چند میلیثانیه به چند دقیقه میرود، چون هر پیام باید منتظر همهٔ پیامهای قبلی بماند. بدتر، مصرف حافظه بالا میرود و اگر صف روی دیسک بیفتد، همان مکانیزمی که قرار بود نجات بدهد خودش گلوگاه میشود. صف فقط وقتی ابزار درستی است که بدانی اختلاف نرخ موقتی است و میتوانی بگویی تا کِی.A queue is the biggest lie in distributed systems, because for a long while everything looks fine. The producer gets fast acknowledgements, the consumer works at its own pace, and only a number on a dashboard changes. Then the number crosses a threshold and latency jumps from milliseconds to minutes, because every message now waits behind all the earlier ones. Memory climbs too, and if the queue spills to disk the very mechanism meant to save you becomes the bottleneck. A queue is the right tool only when you know the rate mismatch is temporary and can say for how long.
کجا دور بریزیم، کجا کند شویمWhere to shed, where to slow down
پاسخ درست به نوع داده بستگی دارد، نه به سلیقه. برای متریک، لاگ و رخداد تحلیلی، دور ریختن با احتمال کم کاملاً قابل قبول است؛ کسی دنبال لاگ شمارهٔ ۹۹۹۹ نمیرود. برای پرداخت، سفارش و هر چیزی که کاربر منتظرش است، دور ریختن یعنی خرابی؛ آنجا باید کند شوی و صریح بگویی. الگوی عملی این است که بهجای بلاک کردن بینهایت، یک پاسخ «الان نه» برگردانی و به فراخواننده بگویی چهوقت دوباره تلاش کند. این کار صف را کوتاه نگه میدارد، فشار را به لایهٔ بالاتر میبرد که میداند چه کند، و از timeoutهای تصادفی جلوگیری میکند.The right answer depends on the data, not on taste. For metrics, logs and analytics events, dropping a small fraction is entirely acceptable; nobody goes looking for log line 9,999. For payments, orders and anything a user is waiting on, dropping is a failure, so there you slow down and say so plainly. The practical pattern is to answer “not now” instead of blocking forever, and tell the caller when to try again. That keeps the queue short, lifts the pressure to a layer that knows what to do with it, and avoids random timeouts.
تلهٔ رایجThe usual trap
گذاشتن یک صف بیحد بهعنوان راهحل. صف جهش را قورت میدهد، تأخیر بیسقف بالا میرود، فراخوانندهها timeout میخورند و دوباره میفرستند، و بار دو برابر میشود؛ بعد سیستم با یک صف پر میمیرد، نه با یک خطای روشن که بشود فهمید.Adding an unbounded queue and calling it fixed. The queue swallows the spike, latency rises without limit, callers time out and retry, and the load doubles; the system then dies holding a full queue instead of returning an error you could read.
خرابی وسط جریان؛ از کجا ادامه میدهی؟A crash mid-stream. Where do you resume?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
کتاب قطور ۸۰۰ صفحهای میخوانی و هر شب یک نشانک میگذاری. اگر کتاب از دستت بیفتد و صفحه گم شود، از نشانک ادامه میدهی، نه از صفحهٔ اول. نقطهٔ بازرسی (checkpoint) همان نشانک است، با یک تفاوت: نشانک فقط جای تو را میگوید، ولی checkpoint هم جا را میگوید و هم چیزهایی را که تا آنجا یاد گرفتهای.You are reading an 800-page book and put a bookmark in every night. If the book falls and you lose the page, you resume from the bookmark, not from page one. A checkpoint is that bookmark with one difference: a bookmark only says where you are, while a checkpoint says where you are and everything you have learned so far.
تشبیه و تصویر ذهنیThe analogy
نشانک فقط شمارهٔ صفحه را نگه میدارد. اگر وسط خواندن، خلاصهٔ فصلها را در دفترچهای نوشته باشی، نشانک آن دفترچه را برنمیگرداند. برای همین یک checkpoint واقعی دو چیز را با هم نگه میدارد: جای کتاب و دفترچهٔ یادداشت. حالا فرض کن خرابی رخ دهد و مجبور شوی از نشانک قبلی شروع کنی؛ هر چه بین دو نشانک خواندهای دوباره خوانده میشود. اگر آن بخش را با مداد در حاشیه علامت زده باشی، علامتها دو بار زده میشوند. checkpoint از این تکرار جلوگیری نمیکند؛ فقط میگوید از کجا شروع کنی. هرچه بیشتر نشانک بگذاری، تکرار کمتر و نوشتنِ نشانک بیشتر.A bookmark holds a page number and nothing else. If you also kept chapter notes in a notebook, the bookmark will not bring the notebook back, so a real checkpoint stores both: the place and the notes. Now suppose a crash sends you back to the previous bookmark. Everything you read since it gets read again, and if you underlined that stretch in pencil the marks get made twice. The checkpoint does not prevent that repetition; it only tells you where to start. More bookmarks mean less repetition and more writing.
checkpoint وضعیت و آفست را با هم نگه میدارد؛ بازگشت از آخرین نقطه یعنی هر چه بعدش بود دوباره اجرا میشود.A checkpoint stores state and offset together; resuming from the last one replays everything that came after it.
و بعدش، فنیAnd now, technically
checkpoint یک عکس فوری سازگار از وضعیت پردازنده و موقعیت ورودی است، نه بیشتر. «سازگار» مهمترین کلمه است: آفست ذخیرهشده باید دقیقاً به همان لحظهای اشاره کند که وضعیت در آن ذخیره شده، وگرنه بعد از بازیابی، وضعیت و ورودی با هم نمیخوانند. سیستمهای جدی این کار را با سد انجام میدهند: یک نشانه در جریان تزریق میشود، هر اپراتور تا رسیدنش کارهای قبلی را تمام میکند، وضعیتش را مینویسد و نشانه را رد میکند. بعد از خرابی، وضعیت از checkpoint بازسازی میشود، ورودی به آفست ذخیرهشده برمیگردد و هر چه بعد از آن نقطه رسیده بود دوباره اجرا میشود. اینجا دقیقاً جایی است که باید مرز را ببینی: checkpoint تضمینی دربارهٔ خروجی نمیدهد. اگر بین دو checkpoint یک ایمیل فرستاده باشی، بازگشت آن ایمیل را پس نمیگیرد. دورهٔ checkpoint یک معامله است: کوتاهتر یعنی تکرار کمتر و نوشتن بیشتر؛ بلندتر یعنی نوشتن کمتر و بازپخش بیشتر. برای وضعیتهای چند گیگابایتی، checkpoint کامل گران میشود و سیستمها به checkpoint افزایشی میروند.A checkpoint is a consistent snapshot of processor state plus input position, and nothing more. “Consistent” is the load-bearing word: the stored offset must point at exactly the moment the state was captured, or state and input will disagree after recovery. Serious systems do this with a barrier injected into the stream: each operator finishes the work before the marker, writes its state, and passes the marker on. After a crash the state is restored, the input seeks back to the stored offset, and everything past that point is replayed. That is exactly where you must see the boundary: a checkpoint promises nothing about output. If you sent an email between two checkpoints, a restore cannot take it back. Checkpoint interval is a trade: shorter means less replay and more writing, longer means less writing and more replay. With state measured in gigabytes, a full checkpoint gets expensive and systems move to incremental ones.
۲ چیزوضعیت اپراتورها و آفست ورودی، هر دو با همoperator state and input offset, captured together
≥ ۱ بازپخشهر چیزی بعد از آخرین checkpoint دوباره اجرا میشودeverything after the last checkpoint runs again
GBsاندازهٔ وضعیتی که checkpoint کامل را گران میکندstate size is what makes a full checkpoint expensive
checkpoint چه چیزی نیستWhat a checkpoint is not
این کلمه در دیتابیس معنای دیگری هم دارد: نوشتن صفحههای کثیف روی دیسک و کوتاه کردن WAL. آن یکی دربارهٔ لاگ است، این یکی دربارهٔ وضعیت اپراتور. در جریان، checkpoint اینها نیست: کامیت خروجی نیست، پس خروجی ممکن است بعد از بازیابی تکرار شود؛ پشتیبان ورودی نیست، چون ورودی همانجاست و از آفست خوانده میشود؛ و تراکنش سراسری نیست، چون سیستم بیرونی که در آن نوشتی از checkpoint خبر ندارد. اگر کسی میگوید «checkpoint داریم پس دقیقاً یک بار هستیم»، دارد سه چیز را با هم قاطی میکند. اثر یکبار وقتی به دست میآید که خودِ خروجی و آفست در یک تراکنش کامیت شوند، نه وقتی وضعیت داخلی ذخیره شود.The word also means something else in databases: writing dirty pages to disk and truncating the WAL. That one is about a log, this one about operator state. In streaming, a checkpoint is not an output commit, so output may be repeated after a restore; it is not a backup of the input, because the input is still there and is read from an offset; and it is not a global transaction, because the external system you wrote to knows nothing about it. Anyone who says “we have checkpoints so we are exactly-once” is merging three things into one. A single effect requires the output and the offset to commit inside one transaction, not the internal state to be saved.
دورهٔ checkpoint، یک معاملهٔ واقعیThe interval is a real trade
دوره را کوتاه کن و هر خرابی کوچک است، چون بازپخش کم است؛ ولی هر بار باید وضعیت را بنویسی، و اگر وضعیت بزرگ باشد همان نوشتن، تأخیر پردازش را بالا میبرد. بلندش کن و نوشتن کم میشود، ولی هر خرابی یعنی دقیقهها بازپخش و دوباره خواندن ورودی، دقیقاً وقتی که سیستم زیر فشار است. سه راه برای کم کردن هزینه وجود دارد: افزایشی نوشتن، یعنی فقط تغییرات از checkpoint قبلی؛ جدا کردن نوشتن از مسیر پردازش تا مکث ندهد؛ و نگه داشتن وضعیت کوچک با طراحی، مثلاً با پنجرههای محدود بهجای تجمیع ابدی. اندازهٔ وضعیت همان چیزی است که این تصمیم را تعیین میکند، نه تعداد پیام در ثانیه.Shorten the interval and every crash is cheap because replay is small, but you write state constantly, and with large state that writing raises processing latency. Lengthen it and you write less, but a crash now means minutes of replay and re-reading input, precisely when the system is under stress. Three ways to reduce the cost: incremental checkpoints that store only the delta, decoupling the write from the processing path so it does not pause, and keeping state small by design, for example bounded windows instead of eternal aggregation. State size, not messages per second, is what decides this.
checkpoint در برابر ackCheckpoint versus ack
checkpointCheckpoint
ackAck
دامنهScope
کل وضعیت پردازندهall operator state
یک پیامone message
چه چیزی را ثابت میکندWhat it proves
نقطهٔ شروع بعدیwhere to start next
ادعای مصرفکنندهthe consumer's claim
بعد از خرابیAfter a crash
بازگشت به نقطهٔ ذخیرهشدهreturn to the stored point
تحویل دوباره تا وقتی تأیید نشودredelivery until it is confirmed
تلهٔ رایجThe usual trap
یکی گرفتن checkpoint با exactly-once. با checkpoint، بعد از خرابی دقیقاً به همان جایی برمیگردی که بودی و کارِ بعد از آن نقطه دوباره انجام میشود؛ اگر اثر جانبی بیرونی داشته باشی، همان اثر دو بار میشود و checkpoint هیچ کاری نمیکند.Reading a checkpoint as exactly-once. A checkpoint returns you to where you were and the work after that point runs again; any external side effect in that window happens a second time, and the checkpoint does nothing about it.
چند اتفاق کوچک، یک معنی بزرگ.Small events, one big meaning.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
از صدای خانه میفهمی مهمان رسیده: اول زنگ، بعد صدای در، بعد خنده. هیچکدام از این سه صدا بهتنهایی معنی «مهمان» نمیدهد؛ ترتیبشان میدهد. CEP همین کار را با رویدادهای یک سیستم میکند.You can hear that guests have arrived: first the bell, then the door, then laughter. None of those three sounds means “guests” on its own; their order does. CEP does that with the events of a system.
تشبیه و تصویر ذهنیThe analogy
سرایدار ساختمان یک دفترچه دارد و چند قاعده. اگر زنگ خورد و تا یک دقیقه کسی در را باز نکرد، زنگ بزند بالا. اگر سه بار پشت سر هم زنگ خورد، یعنی بچهها بازی میکنند و کاری نکند. اگر زنگ خورد و بعد صدای دو نفر آمد، یعنی مهمان با میزبان آمده. سه چیز در این قاعدهها کار را سخت میکند: ترتیب مهم است، فاصلهٔ زمانی مهم است، و «چیزی که نیامد» هم بخشی از الگوست. دفترچه هم باید نصفهکارهها را نگه دارد: تا معلوم نشود زنگ دوم میآید یا نه، نمیشود قاعده را بست.The building's caretaker keeps a notebook and a few rules. If the bell rings and nobody opens within a minute, call upstairs. If the bell rings three times in a row, it is children playing and he should leave it. If the bell rings and then two voices come, a guest has arrived with the host. Three things make these rules hard: order matters, timing matters, and “what did not happen” is part of the pattern too. The notebook also has to keep the half-finished cases: until it is clear whether a second ring comes, the rule cannot be closed.
و بعدش، فنیAnd now, technically
CEP یعنی الگوهایی روی جریان اجرا شوند، نه اینکه رویدادها را ذخیره کنی و بعد پرسوجو بزنی؛ کوئری ثابت میماند و داده از میانش میگذرد. الگو معمولاً با ترتیب، تکرار، بازهٔ زمانی و نفی نوشته میشود: «ورود، بعد دو خرید ناموفق، بعد خروج، همه در ده دقیقه» یا «درخواست، و تا سی ثانیه پاسخی نیامد». پیادهسازیاش برای هر الگو و هر کلید یک ماشین حالت است که مطابقتهای نیمهکاره را نگه میدارد؛ «نفی» با یک تایمر پیاده میشود و نتیجه، لحظهٔ کامل شدن الگوست، نه پایان یک بازهٔ ثابت. دو جا میشکند. اول، حالت: هر مطابقت نیمهکاره حافظه میگیرد، و الگویی که هیچوقت کامل نمیشود باید عمر داشته باشد، وگرنه بیصدا رشد میکند. دوم، معنی زمان: اگر تایمر نفی روی ساعت دیوار باشد، رویدادی که کمی دیر برسد الگو را باطلشده میبیند، در حالی که در واقع به موقع اتفاق افتاده بود.CEP runs patterns over the stream instead of storing events and querying them later; the query stays put and the data flows through it. A pattern is usually written with sequence, repetition, a time bound and negation: “login, then two failed purchases, then logout, all within ten minutes”, or “request, and no reply for thirty seconds”. The implementation is a state machine per pattern per key, holding partial matches; negation becomes a timer, and the output is the moment a pattern completes, not the end of a fixed interval. Two things break. First, state: every partial match takes memory, and a pattern that never completes needs an expiry or it grows silently. Second, the meaning of time: if the negation timer runs on the wall clock, a slightly late event finds the pattern already cancelled, even though it happened on time.
1 state / keyهر الگوی نیمهکاره برای هر کلید یک وضعیت نگه میدارد تا کامل شودeach unfinished pattern keeps one state per key until it completes
absence«نیامدن» هم بخشی از الگوست و فقط با تایمر معنی پیدا میکند“did not happen” is part of the pattern and needs a timer
تلهٔ رایجThe usual trap
فکر کردن اینکه CEP همان چند if روی جریان است. if ساده حالت ندارد، پس نمیتواند بگوید «دو خرید ناموفق پشت سر هم»؛ بهمحض اینکه حالت اضافه شد، باید عمر و حجمش را هم مدیریت کنی. و اگر تایمرها را روی ساعت دیوار بگذاری، همان ifها با تأخیر شبکه دروغ میگویند.Treating CEP as a handful of if-statements over the stream. A plain if has no state, so it cannot say “two failed purchases in a row”; the moment you add state you also owe it an expiry and a size bound. And if those timers run on the wall clock, network delay makes the same if-statements lie.
داده را بیآنکه خط لوله بنویسی جابهجا کن.Move the data without writing the plumbing.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
دوشاخهٔ شارژر تو به پریز هتل نمیخورد؛ یک تیکه پلاستیک کوچک بینشان میگذاری و شارژ میشود. مبدل خودش برق نمیسازد و باتری را پر نمیکند، فقط دو شکل ناسازگار را جفت میکند. connector هم همان تیکه است، برای داده.Your charger plug does not fit the hotel socket, so you put a small piece of plastic in between and it charges. The adapter makes no electricity and does not fill the battery; it only mates two shapes that did not fit. A connector is that piece, for data.
تشبیه و تصویر ذهنیThe analogy
همهٔ مبدلها یک کار نمیکنند. بعضی فقط شکل دوشاخه را عوض میکنند و اگر ولتاژ شهر را تبدیل نکنند، دستگاه را میسوزانند؛ بعضی ترانس هم دارند و ولتاژ را پایین میآورند. با داده هم همین است: مبدلی که فقط بایت را جابهجا میکند و نمیداند اسکیما چیست، حذفها را چطور منتقل کند، و از کجا ادامه بدهد، لوله است ولی دروغ میگوید. مبدل درست، آن چیزی را که در دو طرف به هم نمیخورد واقعاً ترجمه میکند: نوع داده، نام فیلد، و ترتیب. و بدترین حالت این است که مبدل یادش نرود کجا مانده؛ وگرنه هر ریاستارت یعنی از اول، یا یعنی نصفه.Adapters are not all the same. Some only change the shape of the prongs, and if they do not deal with voltage they cook the device; others carry a transformer and step the voltage down. Data is the same: a connector that moves bytes but does not know the schema, how to carry deletes, or where it stopped is a pipe that lies. A good one really translates what does not match on the two sides: data types, field names, order. And the worst case is a connector that forgets its position, because then every restart means starting over, or starting halfway.
و بعدش، فنیAnd now, technically
connector یک پلاگین است که یک سیستم بیرونی را به یک جریان وصل میکند، و دو نقش دارد: منبع که از سیستم میخواند و رویداد تولید میکند، و مقصد که از جریان میخواند و در سیستم مینویسد. بیشتر پیادهسازیها آن را بهعنوان کارگر داخل خود کارگزار اجرا میکنند، با یک محل ذخیرهٔ آفست، تقسیم کار به تسکهای موازی و قاعدهٔ بازتوزیع. سختی کار جایی است که connector باید معنی داده را حمل کند، نه فقط بایت را: کلید و ترتیب، حذفها (که در لاگ به شکل tombstone میآیند)، تغییر اسکیما، و پیش از همه «از کجا ادامه بدهم». منبعی که بهجای آفست لاگ از ستون زمان استفاده میکند، ردیفهایی را که با تراکنشهای همپوشان نوشته شدهاند جا میگذارد؛ مقصدی که نوشتنش idempotent نیست، بعد از هر ریاستارت تکرار میکند. پس connector فقط شکل را جفت میکند؛ تضمینهایی مثل «دقیقاً یک بار» از سیستمهای دو طرف میآید، نه از خودش.A connector is a plugin that attaches an outside system to a stream, in one of two roles: a source that reads the system and produces events, or a sink that reads the stream and writes the system. Most implementations run it as a worker inside the broker itself, with offset storage, parallelism split into tasks, and a rebalance rule. The hard part is that a connector must carry the meaning of the data, not just its bytes: key and ordering, deletes (which arrive in the log as tombstones), schema change, and above all where it stopped. A source that polls a timestamp column instead of a log offset misses rows written by overlapping transactions; a sink whose writes are not idempotent duplicates after every restart. So a connector only mates the shapes; guarantees like exactly-once come from the systems on either side, not from the plugin.
source / sinkدو نقش: خواندن از سیستم به جریان، و نوشتن از جریان به سیستمtwo roles: system to stream, and stream to system
offsetجای توقف را باید جایی ذخیره کند، وگرنه هر ریاستارت از اول یا از نصفه استit must store its position, or every restart starts over or halfway
تلهٔ رایجThe usual trap
اعتماد به connector بهعنوان لولهٔ جادویی که «دقیقاً یک بار» را تضمین میکند. منبعی که بر اساس ستون زمان میخواند، ردیفهای تراکنشهای طولانی را از دست میدهد و هیچ خطایی هم نمیدهد؛ مقصدی که upsert نمیکند، بعد از هر بازتوزیع تکرار میسازد. تضمین را باید در دو طرف ببینی، نه در تنظیمات connector.Trusting a connector as a magic pipe that guarantees exactly-once. A source reading by timestamp silently loses rows from long transactions; a sink that does not upsert duplicates after every rebalance. The guarantee lives on the two endpoints, not in the connector's config.
بعد از خرابی، از کدام پیام شروع کنم؟After a crash, which message do I start from?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
شب برای بچه کتاب میخوانی و لای صفحه را تا میزنی. فردا از همان صفحه شروع میکنی، نه از اول کتاب. اگر لای صفحه را یک صفحه دیرتر بزنی، یک صفحه دوباره خوانده میشود؛ اگر زودتر بزنی، یک صفحه از دست میرود. آفست همان لای صفحه است، فقط یک عدد است نه یک تیکه کاغذ.You read your child a book at night and fold the corner of the page. Tomorrow you start from there, not from page one. Fold one page too far and a page gets read twice; fold one page early and a page is lost. An offset is that folded corner, only it is a number instead of paper.
تشبیه و تصویر ذهنیThe analogy
دو نکته در همین لای صفحه پنهان است. اول، نشانه را کی میگذاری: اگر بعد از خواندن بگذاری و وسط شب کتاب از دستت بیفتد، فردا یک صفحه دوباره خوانده میشود؛ اگر قبل از خواندن بگذاری و خوابت ببرد، آن صفحه هیچوقت خوانده نمیشود. دوم، نشانه کجاست: اگر خودِ کتاب مال کتابخانه باشد و چند نفر همزمان بخوانند، هر کس باید نشانهٔ خودش را داشته باشد، وگرنه یکی نشانهٔ دیگری را جابهجا میکند. و اگر کسی که کتاب را میخواند عوض شود، نفر بعد از همان نشانهٔ آخر شروع میکند، حتی اگر آن نشانه کمی کهنه باشد.Two things hide in that folded corner. First, when you set it: mark after reading and a book that slips away mid-night re-reads a page tomorrow; mark before reading and a doze loses that page forever. Second, where it lives: if the book belongs to a library and several people read it at once, each needs their own marker, or one person moves another's. And when the person reading is swapped out, the next one starts from the last marker, even if that marker is a little stale.
و بعدش، فنیAnd now, technically
در کارگزار لاگی، هر بخش از جریان یک دنبالهٔ صعودی از آفست دارد و مصرفکننده شمارهای را نگه میدارد که میگوید تا کجا خوانده است. خودِ آفست فقط یک عدد است؛ چیزی که اهمیت دارد این است که کِی و کجا آن را ثبت میکنی. اگر بعد از پردازش ثبت کنی، خرابی بین این دو یعنی چند پیام دوباره خوانده میشوند (حداقل یک بار)؛ اگر قبل از پردازش ثبت کنی، خرابی یعنی چند پیام از دست میروند (حداکثر یک بار). ثبت معمولاً در یک موضوع داخلی کارگزار یا یک فروشگاه بیرونی انجام میشود، و در بازتوزیع، بخشها بین اعضای گروه جابهجا میشوند و هر عضو از آفست ثبتشدهٔ خودش ادامه میدهد. یک ابهام کلاسیک هم هست: بعضی سیستمها آفستِ آخرین رکورد خواندهشده را ذخیره میکنند و بعضی آفستِ رکورد بعدی؛ همین یک عدد اختلاف، منبع همیشگی پرش یا تکرار یک پیام است. و آفست زمان نیست: برای «از ساعت ده صبح» باید نمایهای از زمان به آفست داشته باشی.In a log-based broker each partition has a rising sequence of offsets, and a consumer keeps a number that says how far it has read. The offset itself is just a number; what matters is when and where you record it. Commit after processing and a crash between the two re-reads some messages (at least once); commit before processing and a crash loses some (at most once). The commit usually lands in an internal broker topic or an external store, and on a rebalance partitions move between group members, each resuming from its own committed offset. There is one classic ambiguity: some systems store the offset of the last record read, others the offset of the next one; that single digit of difference is a steady source of a skipped or duplicated message. And an offset is not a time: to “start at ten in the morning” you need an index from timestamp to offset.
1 per partitionهر بخش آفست خودش را دارد؛ آفستِ سراسری وجود نداردevery partition has its own; there is no global offset
commit → ackترتیب ثبت آفست و اثر جانبی، تعیین میکند تکرار بگیری یا گمشدنthe order of commit and side effect decides duplication or loss
تلهٔ رایجThe usual trap
ثبت خودکار آفست با فاصلهٔ زمانی، در حالی که پردازش هر دسته بیشتر از آن فاصله طول میکشد. یک بازتوزیع وسط کار، بخش را به مصرفکنندهٔ دیگری میدهد که از آفست ثبتشده شروع میکند، و نیمی از دستهٔ قبلی دوباره پردازش میشود؛ اگر اثر جانبی idempotent نباشد، همان مشتری دو بار ایمیل میگیرد.Auto-committing the offset on a timer while processing a batch takes longer than that timer. A rebalance mid-batch hands the partition to another consumer that resumes from the committed offset, so half the previous batch is processed again — and if the side effect is not idempotent, the same customer gets the email twice.
پیامی که همیشه میشکند، کجا باید برود؟Where does a message that always breaks belong?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
در دفتر ساختمان کشوی کوچکی هست پر از کلیدهایی که به هیچ واحدی نمیخورند. سرایدار کلیدها را دور نمیریزد، ولی هر کلید را هم توی قفل بقیه نمیچپاند. آن کشو همان صف نامههای مرده است: نه دور میریزد و نه جلوی کار را میگیرد.In the building's office there is a small drawer full of keys that fit no flat. The caretaker does not throw them away, and he does not jam them into other people's locks either. That drawer is a dead-letter queue: it neither discards nor blocks the work.
تشبیه و تصویر ذهنیThe analogy
کشو فقط وقتی به درد میخورد که سه کار را بکند. اول، باید برچسب داشته باشد: کدام در، چه روزی، با چه خطایی؛ کلید بیبرچسب بعد از یک ماه یک تیکه فلز است. دوم، کسی باید سراغش برود؛ کشویی که هیچوقت باز نمیشود بدتر از سطل زباله است، چون تو فکر میکنی مشکل حل شده. سوم، باید راه برگشت داشته باشد: وقتی قفل عوض شد، کلیدها باید بتوانند برگردند سر در خودشان، وگرنه فقط جابهجا شدهاند. و یک چیز دیگر: اگر کلید یک در را کنار بگذاری، کلیدهای بعدی همان در هم باید کنار بروند، وگرنه ترتیب باز کردن درها به هم میریزد.The drawer is only useful if it does three things. First, it labels: which door, which day, which fault; an unlabelled key is a piece of metal after a month. Second, someone has to look: a drawer nobody opens is worse than a bin, because you believe the problem is handled. Third, there must be a way back: when the lock is fixed the keys must return to their own doors, or they have only been moved around. One more thing: if you set aside the key for one door, you must set aside that door's later keys too, or the order of opening breaks.
و بعدش، فنیAnd now, technically
صف نامههای مرده یک موضوع جدا (یا یک صف جدا) است که پیام بعد از چند تلاش ناموفق، یا بهمحض یک خطای غیرقابلتکرار، به آن منتقل میشود تا مصرفکننده از پیام سمی رد شود و بقیهٔ صف پیش برود. پیام باید سرصفحههایی با خودش ببرد: نوع خطا، تعداد تلاش، موضوع و بخش و آفست اصلی، و زمان. سه جزئیات بیشتر کار را تعیین میکند. اگر خطا در خودِ deserialize باشد، پیام را نمیشود خواند، پس باید بایت خام را نگه داشت. اگر ترتیب برای یک کلید مهم است، کنار گذاشتن یک پیام یعنی پیامهای بعدی همان کلید هم باید کنار بروند. و خودِ صف هم نگهداشت و پایش میخواهد: عمق و سن پیامها باید هشدار بدهد، وگرنه DLQ تبدیل میشود به قبرستانی که کسی نمیداند چه در آن است. بازگرداندن پیام هم باید از همان مسیر عادی بگذرد و idempotent باشد.A dead-letter queue is a separate topic, or queue, that a message moves to after a few failed attempts or immediately on a non-retryable error, so the consumer gets past a poison message and the rest of the queue moves. The message must carry headers: error class, attempt count, original topic, partition and offset, and time. Three details decide whether this works. If the failure is deserialization itself the message cannot be read, so keep the raw bytes. If ordering matters for a key, skipping one message means the key's later messages must be parked too. And the queue needs retention and monitoring: depth and age should alert, or the dead-letter queue becomes a graveyard nobody can account for. Replaying must go through the normal path and be idempotent.
after N triesپس از چند تلاش ناموفق، یا بهمحض یک خطای غیرقابلتکرارafter a few failed attempts, or on a non-retryable error
topic · offsetپیام باید جای اصلیاش را همراه داشته باشد، وگرنه برگرداندنش ممکن نیستthe message must carry its origin, or it cannot be replayed
تلهٔ رایجThe usual trap
ساختن DLQ بدون هشدار و بدون ابزار بازگرداندن. صف پر میشود، هیچکس نگاه نمیکند، و بعد از شش ماه کسی نمیداند چه چیزی آنجاست؛ عملاً داده را دور ریختهای، ولی با این تفاوت که حالا فکر میکنی نگهش داشتهای. همینطور کنار گذاشتن یک پیام بدون کنار گذاشتن پیامهای بعدی همان کلید، ترتیب را بیصدا میشکند.Standing up a dead-letter queue with no alert and no replay tool. It fills, nobody looks, and six months later nobody knows what is in it; you have effectively dropped the data, except now you believe you kept it. Likewise, parking one message without parking that key's later messages silently breaks ordering.
همان پیام دو بار رسید؛ کدام یکی حساب است؟The same message arrived twice. Which one counts?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
دو پیامک یادآوری قبض برایت میآید و تو هر دو را جدی میگیری و دو بار پرداخت میکنی. ماه بعد یاد میگیری پیامکها را با شمارهٔ قبض مقایسه کنی و اگر دیدی همان است، دومی را نادیده بگیری. حذف تکراری (dedup) همین است: فهرستی از چیزهایی که قبلاً دیدهای، تا کار را دو بار انجام ندهی.Two payment reminders arrive and you take both seriously and pay twice. Next month you learn to compare the bill number and ignore the second reminder when it matches. Deduplication is that: a list of what you have already seen, so the work does not happen twice.
تشبیه و تصویر ذهنیThe analogy
این کار یک دفترچهٔ کوچک میخواهد: هر بار قبضی را پرداخت کردی، شمارهاش را هم بنویسی. حالا دو هزینه داری. اول، دفترچه باید نگه داشته شود؛ تا ابد که نمیشود، پس عملاً فقط قبضهای یک ماه اخیر را چک میکنی و یادآوری که دیر برسد از صافی رد میشود. دوم، باید بدانی «همان» یعنی چه. اگر کلید را شمارهٔ قبض بگیری، دو پرداخت واقعی برای یک قبض را هم یکی میبینی و اشتباهی دومی را دور میریزی. اگر کلید را تاریخ و مبلغ بگیری، دو قبض متفاوت با مبلغ برابر را قاطی میکنی. پس dedup بدون کلید درست فقط یک اشتباه مرتب است. و اگر دفترچه را در خرابی از دست بدهی، همه چیز از نو تکراری میشود، درست وقتی که بیشترین فشار روی سیستم است.The habit needs a small notebook: every bill you paid, write its number down. That brings two costs. The notebook has to be kept, and not forever, so in practice you only check the last month and a reminder that arrives late slips through. And you have to decide what “same” means: key on the bill number and you will merge two genuine payments for one bill and throw the second away; key on date and amount and you will confuse two different bills of equal value. Dedup with the wrong key is just tidier confusion. Lose the notebook in a crash and everything becomes a duplicate again, exactly when the system is under the most pressure.
حذف تکراری با یک دفترچهٔ شناسه کار میکند؛ نوشتن idempotent اصلاً دفترچه نمیخواهد، چون تکرارش بیاثر است.Dedup works from a ledger of ids; an idempotent write needs no ledger because its repeat is harmless.
و بعدش، فنیAnd now, technically
dedup یعنی پیش از اعمال یک پیام بررسی کنی که قبلاً دیده شده یا نه و اگر شده، رد کنی. این کار دقیقاً به همان اندازه که ساده به نظر میرسد هزینه دارد. اول به یک کلید یکتا نیاز داری که پایدار بماند: شناسهٔ تولیدکننده بههمراه شمارهٔ ترتیب، یا یک UUID که تولیدکننده در پیام میگذارد. دوم به یک انبار وضعیت نیاز داری که کلیدهای دیدهشده را نگه دارد؛ این انبار با زمان رشد میکند و باید پنجره و TTL داشته باشد، وگرنه خودش به یک مسئلهٔ حافظه تبدیل میشود. سوم، ترتیب مهم است: بررسی و نوشتن باید اتمیک باشند، وگرنه دو نسخه که همزمان میرسند هر دو از صافی رد میشوند؛ اینجاست که dedup به یک عملیات idempotent یا به یک تراکنش روی مقصد تکیه میکند. اگر وضعیت dedup بین مصرفکنندهها مشترک نباشد و پیام بعد از ریبالانس به مصرفکنندهٔ دیگری برسد، آن مصرفکننده کلید را ندیده است و نسخهٔ دوم را دوباره اعمال میکند. برای همین در عمل dedup معمولاً روی خود مقصد انجام میشود، جایی که کلید یکتا و تراکنش هر دو در دسترساند.Deduplication checks whether a message has been seen before applying it, and drops it if so. That is exactly as expensive as it sounds. First you need a stable unique key: a producer id plus a sequence number, or a UUID the producer stamps into the message. Second you need a store of seen keys; it grows with time and needs a window and a TTL, or it becomes its own memory problem. Third, ordering matters: the check and the write must be atomic, or two copies arriving at once both pass the filter, which is where dedup leans on either an idempotent operation or a transaction at the sink. If the dedup state is not shared between consumers and a message lands on a different consumer after a rebalance, that consumer has never seen the key and applies the second copy anyway. Which is why in practice dedup usually lives at the sink, where the unique key and the transaction are both available.
۱ کلیدبدون کلید پایدار، dedup فقط حدس استwithout a stable key, dedup is guesswork
TTLشناسهها تا ابد نگه داشته نمیشوند؛ همین مهلت مرز استids cannot be kept forever; the TTL is the boundary
۲×بدون dedup، خرابی و تلاش دوباره اثر را دو برابر میکندwithout dedup a retry after failure doubles the effect
dedup و idempotency یکی نیستندDedup is not idempotency
این دو اغلب بهجای هم به کار میروند و این تفاوتشان را پنهان میکند. عملیات idempotent عملیاتی است که اجرای دوبارهاش همان نتیجه را میدهد، بدون اینکه چیزی به خاطر بسپارد: نوشتن مقدار ۵ در یک خانه، یا «این ردیف را با این مقدار بگذار» بهجای «یکی به آن اضافه کن». dedup چیزی به خاطر میسپارد: فهرست شناسهها. اگر عملیاتت بهطور طبیعی idempotent است، به هیچ فهرستی نیاز نداری و مسئله تمام است. اگر نیست — مثل افزایش موجودی یا فرستادن ایمیل — باید یا آن را idempotent کنی یا فهرست نگه داری. dedup راه عمومیتر است و idempotency راه ارزانتر، و انتخاب بینشان به این بستگی دارد که بتوانی عملیات را بازنویسی کنی یا نه.The two get used interchangeably, which hides the difference. An idempotent operation gives the same result when repeated, without remembering anything: writing the value 5 into a cell, or “set this row to this value” instead of “add one to it”. Deduplication remembers: it keeps a list of ids. If your operation is naturally idempotent you need no list and the problem is over. If it is not — incrementing a balance, sending an email — you either make it idempotent or keep a list. Dedup is the general answer and idempotency the cheap one, and which you pick depends on whether you can rewrite the operation at all.
کلید بد، بدتر از نداشتن کلیدA bad key is worse than no key
بدترین حالت dedup این نیست که تکراری از صافی رد شود؛ این است که یک رکورد واقعی را اشتباهاً تکراری بشماری و دور بریزی. آن خطا سکوت میکند: هیچ خطایی ثبت نمیشود و فقط یک سفارش یا یک پرداخت نیست که هیچوقت پیدا نمیشود. کلید باید از منبع بیاید، نه از حدس سیستم پاییندستی. کلیدی مثل «شناسهٔ کاربر + مبلغ + روز» برای گزارشهای روزانه خوب است و برای پرداخت فاجعه، چون دو پرداخت واقعی با مبلغ برابر در یک روز را یکی میبیند. قاعدهٔ ساده این است: اگر تولیدکننده نمیتواند یک شناسهٔ یکتا بدهد، هیچ لایهٔ پاییندستی نمیتواند یکی از خودش دربیاورد و مطمئن باشد.The worst dedup failure is not a duplicate slipping through; it is a real record being declared a duplicate and dropped. That error is silent: nothing is logged, and one order or one payment simply never exists. The key must come from the source, not from a guess made downstream. A key like user id plus amount plus day is fine for a daily report and a disaster for payments, because it merges two genuine payments of equal value on the same day. The simple rule: if the producer cannot supply a unique id, no downstream layer can invent one and be sure.
تلهٔ رایجThe usual trap
نگه داشتن مجموعهٔ شناسهها در حافظهٔ خودِ مصرفکننده. با یک ریاستارت یا یک ریبالانس، مجموعه خالی میشود و همه چیز دوباره تکراری میشود؛ یا بدتر، دو مصرفکننده هر کدام نصف مجموعه را دارند و هیچکدام تکراریهای طرف دیگر را نمیبیند.Keeping the set of seen ids in the consumer's own memory. A restart or a rebalance empties it and everything is a duplicate again; worse, two consumers each hold half the set and neither sees the other's repeats.
این دستور از رئیس قبلی است یا جدید؟Is this order from the old boss or the new one?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
دکتر دارویات را عوض میکند و نسخهٔ تازه مینویسد. داروخانه نسخهٔ قبلی را قبول نمیکند، هرچند کاغذش سالم و خوانا است. روی نسخه شماره و تاریخی هست که میگوید کدام تازهتر است، و نسخهٔ تازه نسخهٔ قبلی را باطل میکند. epoch همان شماره است: نه برای اینکه بفهمی کدام کاغذ قشنگتر است، برای اینکه بفهمی کدام معتبر است.Your doctor changes your medicine and writes a fresh prescription. The pharmacy will not take the previous one, even though that sheet is intact and legible. A number and a date on the sheet say which is newer, and the new prescription voids the old. An epoch is that number: not to tell which paper is prettier, but which one still counts.
تشبیه و تصویر ذهنیThe analogy
مشکل وقتی شروع میشود که کاغذهای قدیمی در کشو بمانند. نسخهٔ قبلی هنوز مهر دارد، هنوز خواناست، و اگر داروخانه فقط به محتوایش نگاه کند، دو داروی متناقض دارد. راهحل این نیست که کاغذهای قدیمی را جمع کنی، چون همیشه یکی جا میماند؛ راهحل این است که هر کاغذ یک شماره داشته باشد و داروخانه همیشه بالاترین شمارهای را که دیده به یاد بیاورد و پایینتر را رد کند. همین یک قاعدهٔ کوچک، کل مشکل را حل میکند، به شرطی که شمارهها از یک جا صادر شوند و هیچوقت پایین نیایند.The trouble starts when old sheets stay in the drawer. The previous prescription still has a stamp, still reads perfectly, and a pharmacy that only looks at the content now has two contradictory medicines. The answer is not to collect every old sheet, because one always escapes; the answer is to number each sheet and have the pharmacy remember the highest number it has seen and reject anything lower. That one small rule solves the whole thing, provided the numbers are issued from one place and never go down.
و بعدش، فنیAnd now, technically
epoch یک شمارندهٔ یکنواست که با هر تغییرِ رهبری، پیکربندی یا راهاندازی بالا میرود: term در Raft، epoch در ZooKeeper، epoch number در HDFS، leader epoch در کارگزار لاگی. هر درخواست یا پیام آن را همراه میبرد، و گیرنده بالاترین عددی را که دیده نگه میدارد و هر چیز پایینتر را رد میکند. همین است که رهبرِ قدیمیِ بیدارشده نمیتواند نوشتنِ کهنهاش را روی وضعیت تازه بنشاند. epoch مکمل lease است، نه جای آن: lease میگوید «تا ده ثانیه مالک هستی» و epoch میگوید «و هنوز جدیدترین مالکی»؛ بدون شماره، رهبری که فقط کند بوده و فکر میکند هنوز مالک است، میتواند دیر برسد و خرابی بنویسد. در ذخیرهسازی، epoch همچنین برای تشخیص واگرایی لاگ به کار میرود. شرط اصلی این است که شماره پایدار بماند و از یک منبع بیاید، و منابع مقصد واقعاً آن را بررسی کنند.An epoch is a monotonic counter bumped on every leadership change, reconfiguration or restart: the term in Raft, the epoch in ZooKeeper, the epoch number in HDFS, the leader epoch in a log-based broker. Every request or message carries it, and the receiver keeps the highest number it has seen and rejects anything lower. That is what stops a woken old leader from writing its stale view over the new one. An epoch complements a lease rather than replacing it: the lease says “you own this for ten seconds”, the epoch says “and you are still the newest owner”; without the number, a leader that was merely slow and still believes it owns the resource can arrive late and write damage. In storage systems the epoch also detects divergent logs. The conditions are that the number stays durable, comes from one place, and that resources actually check it.
monotonicفقط بالا میرود؛ اگر جایی ریست شود، دیگر نگهبان نیستonly goes up; a reset anywhere makes it useless
termدر Raft همین مفهوم با نام term شناخته میشودin Raft the same idea is called a term
تلهٔ رایجThe usual trap
ساختن epoch از چیزی که میتواند عقب برگردد: هش پیکربندی، نام میزبان، یا شمارندهای که با هر ریاستارت از صفر شروع میشود. همچنین کلاینتی که شماره را کش میکند و بعد از تغییر رهبری همان عدد قدیمی را میفرستد، دقیقاً همان دستور کهنهای است که میخواستی جلویش را بگیری.Deriving the epoch from something that can go backwards: a config hash, a hostname, or a counter that restarts from zero. And a client that caches the number and keeps sending the old one after a failover is exactly the stale order you meant to block.
کوچکترین چیزی که واقعاً اتفاق افتاد.The smallest thing that actually happened.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
از گیت مترو رد میشوی و دستگاه یک مهر روی کارت میزند: ایستگاه، ساعت، مبلغ. آن مهر بعداً عوض نمیشود؛ اگر اشتباهی رخ داده باشد، مهر بعدی میآید و تصحیحش میکند. رویداد هم همان مهر است: یک تیکهٔ کوچک و ثابت که میگوید چه شد، نه اینکه الان چه خبر است.You pass through the metro gate and the machine stamps your card: station, time, fare. That stamp is not edited later; if something was wrong, the next stamp arrives and corrects it. An event is that stamp: a small fixed piece that says what happened, not what the state is now.
تشبیه و تصویر ذهنیThe analogy
صورتحساب بانکیات فهرستی از همین مهرهاست، و موجودیات چیزی است که از رویشان حساب میشود. هیچوقت سطر دیروز را پاک نمیکنند تا موجودی را درست کنند؛ یک سطر تازه اضافه میکنند. همین تفاوت، دو خاصیت میدهد. اول، فهرست را میشود از چند زاویه خواند: یکی مجموع ماه را میخواهد، یکی فقط خریدهای نان را. دوم، اگر حساب اشتباه شد، میشود از روی همان فهرست دوباره حساب کرد. بهایش این است که فهرست مدام بلندتر میشود و «اشتباه» را نمیشود پاک کرد، فقط میشود جبرانش کرد.Your bank statement is a list of those stamps, and your balance is something computed from them. Nobody deletes yesterday's line to fix the balance; they add a new line. That difference buys two properties. First, one list can be read from several angles: one person wants the month's total, another only the bread. Second, if the sum went wrong, it can be recomputed from the list. The price is that the list keeps growing, and a mistake cannot be erased, only compensated.
و بعدش، فنیAnd now, technically
از نظر فنی، یک رویداد یک شیء کوچک و تغییرناپذیر است با زمان، نوع، کلید و بدنه که میگوید در یک لحظه چه اتفاقی افتاد؛ معمولاً یک اعلان است، نه یک درخواست، و انتظار جواب ندارد. رویداد را با command و با state اشتباه نگیر: command میگوید «این کار را بکن» و میتواند رد شود، رویداد میگوید «این اتفاق افتاد» و دیگر قابل رد نیست. رویداد یک بار تولید میشود، در یک لاگ مینشیند و هر تعداد مصرفکننده میتوانند بخوانندش، و همین است که منبع حقیقت در event sourcing میشود. سه محدودیت را باید بپذیری: ترتیب فقط داخل هر بخش معنی دارد، تصحیح با رویداد تازه میآید نه با ویرایش قدیمی، و اسکیما عوض میشود، پس مصرفکنندهای که فقط نسخهٔ امروز را میفهمد، فردا دادهٔ دیروز را نمیتواند بخواند.Technically an event is a small immutable object with a time, a type, a key and a body saying what happened at one moment; usually it is a notification rather than a request, and it expects no reply. Do not confuse it with a command or with state: a command says “do this” and can be rejected, an event says “this happened” and cannot. An event is produced once, lands in a log, and any number of consumers can read it, which is what makes it the source of truth in event sourcing. Three limits come with it: ordering only means anything inside a partition, a correction arrives as a new event rather than an edit, and the schema changes, so a consumer that only understands today's version cannot read yesterday's data tomorrow.
1 write, N readsیک رویداد یک بار نوشته میشود و هر تعداد مصرفکننده میخوانندشwritten once, read by any number of consumers
per partitionترتیب رویدادها فقط داخل یک بخش تضمین میشودevent order is guaranteed inside one partition only
تلهٔ رایجThe usual trap
گذاشتن کل وضعیت رکورد در بدنهٔ رویداد. آنوقت مصرفکننده نمیفهمد چه چیزی عوض شده، و رویداد به یک عکس فوری از وضعیت تبدیل میشود که حجمش با هر تغییر بالا میرود و ترتیبش هم بیمعنی میشود. رویداد باید بگوید چه اتفاقی افتاد، نه اینکه بعد از آن همهچیز چه شکلی بود.Putting the whole record state in the event body. Consumers can no longer tell what changed, and the event becomes a state snapshot whose size grows with every edit and whose ordering means nothing. An event should say what happened, not what everything looked like afterwards.
رویدادی که دیر رسید، مال کدام دقیقه است؟A late event. Which minute does it belong to?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
پیامی که دیشب فرستاده شده را صبح باز میکنی. اگر بخواهی بدانی «دیشب چه اتفاقی افتاد»، تاریخ فرستادن مهم است، نه لحظهای که تو خواندی. زمان رویداد (event time) همان تاریخ فرستادن است؛ لحظهای که تو خواندی زمان پردازش است، و این دو تقریباً هیچوقت یکی نیستند.You open in the morning a message that was sent last night. If you want to know what happened last night, the send time matters, not the moment you read it. Event time is the send time; the moment you read it is processing time, and the two are almost never the same.
تشبیه و تصویر ذهنیThe analogy
حالا فرض کن میخواهی گزارش «هر روز چند نامه رسید» را بنویسی. اگر تاریخ رسیدن به دستت را مبنا بگیری، نامهای که سه روز در مرکز توزیع مانده در روز اشتباه شمرده میشود و گزارش دیروزت فردا عوض میشود. اگر مهر پست را مبنا بگیری، هر نامه سر جای درستش مینشیند، ولی باید صبر کنی تا نامههای دیررس برسند. چقدر صبر؟ تا ابد که نمیشود، چون گزارش هیچوقت بسته نمیشود. پس یک مهلت میگذاری، مثلاً تا پایان روز بعد، و بعد از آن پنجره را میبندی و میپذیری که نامهٔ خیلی دیررس بیرون بماند یا جداگانه اصلاح شود. همین معامله، هستهٔ همهٔ سیستمهای جریانی است که با زمان رویداد کار میکنند.Now suppose you want a report of how many letters arrived each day. Use the day they reached you and a letter stuck three days in a sorting office lands in the wrong day, so yesterday's figures change tomorrow. Use the postmark and every letter falls in the right place, but now you must wait for the stragglers. Wait how long? Not forever, or the report never closes. So you set a deadline, say until the end of the next day, then close the window and accept that the very late letter stays out or gets corrected separately. That trade is the heart of every streaming system built on event time.
هر رویداد دو زمان دارد: وقتی رخ داد و وقتی پردازش شد؛ پنجره باید با اولی بسته شود، نه با دومی.Every event has two times: when it happened and when it was read; a window must close on the first, not the second.
و بعدش، فنیAnd now, technically
زمان رویداد همان لحظهای است که اتفاق در منبع رخ داده و معمولاً بهصورت یک فیلد در خود رکورد میآید. زمان پردازش لحظهای است که پردازنده رکورد را میبیند، و اختلاف این دو همان تأخیر مسیر است که با بار و شبکه بالا و پایین میرود. پنجرهبندی بر اساس زمان رویداد نتیجهٔ درست میدهد، ولی یک مشکل تازه میسازد: پردازنده نمیداند چه زمانی همهٔ رویدادهای یک بازه رسیدهاند. جواب، واترمارک است؛ نشانهای که میگوید «بهاحتمال زیاد دیگر رویدادی قبل از این لحظه نمیآید». واترمارک یک قطعیت نیست، یک تصمیم است: اگر محتاط باشی پنجره دیر بسته میشود و تأخیر خروجی بالا میرود؛ اگر عجول باشی رویدادهای دیررس میافتند و باید پنجره را دوباره باز کنی و نتیجهٔ قبلی را اصلاح کنی. رویدادهای دیررس همیشه وجود دارند، چون زمان رویداد از دستگاهی میآید که ساعتش با بقیه یکی نیست. به همین دلیل جریانهایی که زمان رویداد را جدی میگیرند هر دو مسیر را دارند: نتیجهٔ اولیه با واترمارک، و اصلاح بعدی برای دیررسها.Event time is the moment the thing happened at the source, usually carried as a field inside the record. Processing time is the moment the processor sees the record, and the difference between them is the travel delay, which rises and falls with load and network. Windowing on event time gives the right answer and creates a new problem: the processor cannot know when it has received every event for a period. The answer is a watermark, a marker meaning “nothing earlier than this is likely to arrive now”. A watermark is not a fact but a decision: be cautious and windows close late, raising output latency; be eager and late events fall outside, forcing you to reopen the window and correct the earlier result. Late events always exist, because event time comes from devices whose clocks disagree. So streams that take event time seriously carry both paths: an initial result at the watermark, and a correction for the stragglers.
۲ ساعتزمان رویداد و زمان پردازش، هیچوقت دقیقاً یکی نیستندevent time and processing time never quite agree
lagاختلاف این دو، همان تأخیر صف استthe gap between them is the queueing delay
T + Δپنجره تا این مهلت باز میماند، بعد بسته میشودthe window stays open until this deadline, then closes
چرا زمان پردازش فریب میدهدWhy processing time lies
گزارشی که با زمان پردازش ساخته شود، در لحظه درست به نظر میرسد و بعد بیسروصدا غلط میشود. دیروز یک جهش ترافیک داشتی و پردازنده عقب افتاد؛ حالا رویدادهای ساعت نه شب در بازهٔ ده شب شمرده شدهاند. بدتر، همان گزارش را اگر فردا دوباره اجرا کنی، عدد دیگری میدهد، چون تأخیر این بار متفاوت است. یعنی نتیجه به این بستگی دارد که سیستم آن روز چقدر شلوغ بوده، نه به این که چه اتفاقی افتاده. برای داشبورد عملیاتی این قابل قبول است، چون سؤال «الان چه خبر» است. برای صورتحساب، تجمیع روزانه و هر عددی که کسی بر اساسش تصمیم میگیرد، قابل قبول نیست.A report built on processing time looks right when it is produced and then quietly goes wrong. You had a traffic spike yesterday and the processor fell behind, so events from nine in the evening are counted in the ten o'clock bucket. Worse, re-run the same report tomorrow and you get a different number, because the delay is different this time. The result depends on how busy the system was that day, not on what happened. For an operations dashboard that is acceptable, because the question is “what is happening now”. For billing, daily aggregation and any number someone makes a decision on, it is not.
واترمارک، یک تصمیم نه یک اندازهگیریA watermark is a decision, not a measurement
هیچ راهی نیست که بدانی همهٔ رویدادهای یک بازه رسیدهاند؛ فقط میتوانی احتمال بدهی. واترمارک همان احتمال را به یک قاعده تبدیل میکند: «فرض کن بعد از این نقطه رویدادی با زمان قدیمیتر نمیآید». این قاعده یک هزینهٔ مستقیم دارد. اگر سخاوتمندانه باشد، هر پنجره مدتها باز میماند و خروجی دیر میرسد؛ اگر تنگ باشد، دیررسها بیرون میافتند. راهحل عملی، دو مرحلهای است: اول یک نتیجهٔ سریع منتشر کن، بعد پنجرهٔ مجاز را باز نگه دار و اگر رویدادی در آن بازه رسید، نتیجه را اصلاح کن. این کار یعنی خروجی جریانی دیگر فقط «افزودن» نیست؛ میتواند بهروزرسانی هم باشد، و مقصد باید بتواند همان کلید را دوباره بنویسد.There is no way to know that every event for a period has arrived; you can only estimate. A watermark turns the estimate into a rule: “assume nothing older than this point is still coming.” That rule has a direct cost. Be generous and every window stays open for ages, so output arrives late; be tight and the stragglers fall outside. The practical answer is two-stage: publish a fast result first, then keep an allowed-lateness window open and correct the result if a late event shows up inside it. That means streaming output is no longer append-only; it can be an update, and the sink must be able to write the same key again.
زمان رویداد در برابر زمان پردازشEvent time versus processing time
زمان رویدادEvent time
زمان پردازشProcessing time
چه چیزی را میگویدWhat it says
کِی اتفاق افتادwhen it happened
کِی به دست ما رسیدwhen we got to it
اجرای دوبارهRe-run
همان نتیجه، اگر ورودی یکی باشدsame result for the same input
نتیجه عوض میشودthe result changes
مناسبِGood for
صورتحساب و گزارش روزانهbilling and daily reports
داشبورد و هشدارdashboards and alerts
تلهٔ رایجThe usual trap
انتخاب زمان پردازش چون سادهتر است، بدون گفتنش به کسی. گزارش سالبهسال میچرخد، هیچکس نمیتواند عدد دیروز را بازتولید کند، و وقتی کسی میپرسد چرا دو اجرا دو جواب داده، جوابی نداری جز «آن روز شلوغ بود».Choosing processing time because it is easier, and not telling anyone. The report drifts, nobody can reproduce yesterday's number, and when someone asks why two runs disagreed the only answer is “it was a busy day”.
«دقیقاً یک بار» یعنی پیام یک بار میرسد؟Does exactly-once mean the message arrives once?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
کارت میکشی، دستگاه پیام میدهد، و تو نمیدانی تراکنش انجام شده یا نه، پس دوباره میزنی. اگر بانک هوشمند باشد، بار دوم همان تراکنش اول را برمیگرداند و پول دو بار کم نمیشود. دقیقاً یک بار (exactly-once) همین است: نه اینکه دستگاه یک بار پیام بدهد، بلکه نتیجه همان یک بار باشد.You tap your card, the terminal says something, and you cannot tell whether the payment went through, so you tap again. A sensible bank returns the first transaction instead of charging you twice. Exactly-once is that: not that the terminal speaks once, but that the result is one charge.
تشبیه و تصویر ذهنیThe analogy
سه راه پیش روی توست. اول، یک بار بزنی و بروی؛ اگر پیام نیامد، تراکنش گم شده است. دوم، آنقدر بزنی تا رسید بگیری؛ تراکنش گم نمیشود، ولی ممکن است دو بار انجام شود. سوم، صندوق خودش بداند که این درخواست را دیده و بار دوم را بیاثر کند. راه سوم همان چیزی است که همه میخواهند و هیچکدامش جادو نیست: صندوق باید یک حافظه داشته باشد و آن حافظه باید همراه با خود تراکنش ذخیره شود، وگرنه یک خرابی بین این دو دوباره همه چیز را به هم میریزد. پس exactly-once یک ویژگی تحویل نیست؛ یک ویژگی اثر است که از دل at-least-once و یک حافظهٔ مشترک بیرون میآید.Three options are open to you. Tap once and walk away: if the message never arrived, the payment is lost. Tap until a receipt prints: nothing is lost, but you may pay twice. Or the till itself knows it has seen this request and makes the second tap harmless. The third is what everyone wants and none of it is magic: the till needs a memory, and that memory must be saved together with the payment, or a crash between the two undoes everything. Exactly-once is not a delivery property; it is a property of the effect, built out of at-least-once plus shared state.
دقیقاً یک بار روی همان at-least-once ساخته میشود، بهعلاوهٔ حافظهای که نسخهٔ دوم را بیاثر میکند.Exactly-once is built on at-least-once, plus the state that makes the second copy harmless.
و بعدش، فنیAnd now, technically
در جریانها، «دقیقاً یک بار» بهمعنای یک بار تحویل فیزیکی نیست؛ هیچ کارگزاری نمیتواند آن را تضمین کند، چون خرابی میتواند بعد از تحویل و قبل از ack رخ دهد. معنای دقیقتر این است که اثر مشاهدهشدنی برابر با یک بار پردازش باشد، و کتاب اسمش را effectively-once میگذارد. سه راه برای رسیدن به آن وجود دارد. اول، نوشتن idempotent در مقصد: پیام کلید یکتا دارد و نوشتن دوباره همان ردیف را مینویسد، نه ردیف دوم. دوم، تراکنش اتمیک: بهروزرسانی وضعیت، نوشتن خروجی و ثبت آفست هر سه در یک تراکنش کامیت میشوند، پس بعد از بازیابی هیچکدام نصفه نمیماند. سوم، dedup در مقصد با کلید پایدار. هر سه یک چیز مشترک دارند: جایی باید حافظه داشته باشی که تکرار را تشخیص دهد. اگر اثر جانبی بیرون از این حافظه باشد — ایمیل، پیامک، فراخوانی درگاه پرداخت — هیچکدام از این راهها آن را برنمیگرداند و تکرار واقعاً تکرار میشود.In streaming, exactly-once does not mean one physical delivery; no broker can promise that, because a crash can land between delivery and ack. It means the observable effect equals processing once, which the book calls effectively-once. Three routes get you there. Idempotent writes at the sink: the message carries a unique key and a second write rewrites the same row instead of adding one. An atomic transaction: state update, output write and offset commit all commit together, so a restore never leaves half of it behind. Deduplication at the sink with a stable key. All three share one thing: somewhere a memory must exist that can recognise a repeat. If the side effect lives outside that memory — an email, an SMS, a call to a payment gateway — none of the routes can take it back, and the duplicate is a real duplicate.
۳ راهنوشتن idempotent، تراکنش اتمیک، یا dedup در مقصدidempotent writes, atomic commit, or dedup at the sink
≥ ۱ بارتحویل فیزیکی در عمل همیشه at-least-once استphysical delivery is at-least-once in practice
۰ تضمینبرای اثر جانبی بیرون از تراکنشfor a side effect outside the transaction
چرا یک بار تحویل ممکن نیستWhy one delivery is impossible
مصرفکننده پیام را میگیرد، کار را انجام میدهد و ack میفرستد. اگر خرابی بین انجام کار و رسیدن ack رخ دهد، کارگزار دو گزینه دارد و هر دو بد است: فرض کند کار انجام شده و پیام را تحویل ندهد، که در آن صورت اگر کار انجام نشده بود داده گم شده است؛ یا فرض کند انجام نشده و دوباره تحویل دهد، که در آن صورت کار دو بار انجام میشود. هیچ اطلاعاتی در سیستم نیست که این ابهام را حل کند، چون ack میتواند در شبکه گم شود. پس تحویل، همیشه یا at-most-once است یا at-least-once؛ «دقیقاً یک بار» فقط در سطح اثر معنا دارد، جایی که یک حافظهٔ مشترک میتواند بگوید این یکی قبلاً اعمال شده.A consumer receives a message, does the work and sends an ack. If a crash lands between the work and the ack, the broker has two options and both are bad: assume it finished and drop it, which loses data if it had not; or assume it did not and deliver again, which runs the work twice. Nothing in the system can resolve that ambiguity, because the ack can be lost on the network. Delivery is therefore always either at-most-once or at-least-once; exactly-once only makes sense at the level of the effect, where shared state can say this one has already been applied.
قیمت و مرزهایشThe price and its edges
هیچکدام از این سه راه مجانی نیست. تراکنش اتمیک یعنی هماهنگی بین پردازنده و مقصد، و هماهنگی یعنی تأخیر و یک نقطهٔ شکست تازه؛ همچنین مقصد باید چنین تراکنشی را بپذیرد، که خیلی از مقصدها نمیپذیرند. نوشتن idempotent یعنی باید بتوانی کلید را در مقصد یکتا کنی، و این همیشه ممکن نیست. dedup یعنی حافظهای که با زمان رشد میکند و باید دورهای پاک شود. اگر مقصدت یک API بیرونی است که هیچکدام را نمیپذیرد، انتخاب واقعی بین at-least-once با احتمال تکرار و at-most-once با احتمال گمشدن است، و باید بفهمی کدام برای این کار خاص ارزانتر تمام میشود.None of the three routes is free. An atomic transaction means coordination between processor and sink, and coordination means latency and a new failure point; the sink must also accept such a transaction, which many will not. Idempotent writes mean you can enforce a unique key at the sink, which is not always possible. Dedup means a store that grows with time and must be expired. If your sink is an external API that accepts none of this, the real choice is at-least-once with possible duplicates against at-most-once with possible loss, and you have to work out which is cheaper for this particular job.
سه ضمانت تحویلThe three delivery guarantees
at-most-onceAt-most-once
at-least-onceAt-least-once
exactly-onceExactly-once
خرابی چه میکندWhat a crash does
پیام را میبردtakes the message
پیام را تکرار میکندrepeats the message
اثر را تکرار نمیکندdoes not repeat the effect
کِی ack میرودWhen the ack goes
قبل از پردازشbefore the work
بعد از پردازشafter the work
همراه با کامیتwith the commit
مناسبِGood for
متریک و لاگmetrics and logs
کارهای معمولmost ordinary work
پرداخت و موجودیpayments and balances
تلهٔ رایجThe usual trap
باورداشتن به «exactly-once» که کارگزار تبلیغ میکند، بدون نگاه کردن به جایی که خروجی مینشیند. ضمانت در مرز پردازنده تمام میشود؛ اگر مقصد یک فراخوانی HTTP ساده باشد، تکرار همانجا از پنجره بیرون میافتد و کسی هم خبردار نمیشود.Believing the exactly-once label on the broker without checking where the output lands. The guarantee stops at the processor's boundary; if the sink is a plain HTTP call, the duplicate escapes right there and nobody is told.
پیام را که خواندی، از لاگ پاک میشود؟Does reading a message delete it?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
در گروه خانوادگی، پیامها پاک نمیشوند. عمهات صبح همه را میخواند، تو شب میخوانی، و هیچکدام پیام دیگری را از بین نمیبرید. هر کس از هر جا که رسید ادامه میدهد. کارگزار لاگمحور (log-based broker) همین است: پیامها میمانند و هر مصرفکننده نشان خودش را دارد.In the family group chat the messages do not disappear. Your aunt reads them in the morning, you read them at night, and neither of you removes anything for the other. Everyone resumes from wherever they got to. A log-based broker is that: messages stay, and each reader carries its own marker.
تشبیه و تصویر ذهنیThe analogy
این با یک صف معمولی فرق دارد. در صف، هر پیام به یکی از کارگرها داده میشود و با تأیید او دور ریخته میشود؛ اگر کارگر دوم نداند، پیام برایش وجود ندارد. در لاگ، پیام یک بار نوشته میشود و هر تعداد مصرفکننده میتواند از هر جایش بخواند. سودش سه چیز است: میتوانی عقب برگردی و دوباره بخوانی، یک جریان میتواند به چند سیستم غذا بدهد، و کندی یک مصرفکننده بقیه را متوقف نمیکند. بهایش این است که دیسک پر میشود و باید تصمیم بگیری چقدر نگه داری؛ و اینکه ترتیب فقط داخل یک پارتیشن معنا دارد، نه روی کل لاگ.This differs from an ordinary queue. In a queue each message goes to one worker and is thrown away when that worker confirms it; a second worker never sees it. In a log the message is written once and any number of readers can read from any position. That buys three things: you can go back and read again, one stream can feed several systems, and one slow reader does not hold up the others. It costs disk, so you must decide how much to keep, and it means ordering holds inside a partition, not across the whole log.
پیامها در لاگ میمانند و با خواندن پاک نمیشوند؛ هر مصرفکننده فقط نشان خودش را جلو میبرد.Messages stay in the log and are not deleted by reading; each consumer only moves its own marker.
و بعدش، فنیAnd now, technically
در کارگزار لاگمحور، هر پارتیشن یک فایل فقط-افزودنی است و هر پیام یک آفست یکنوا میگیرد. تولیدکننده در انتها میافزاید؛ مصرفکننده از هر موقعیتی میخواند و آفست خودش را نگه میدارد. این طراحی چند پیامد مستقیم دارد. اول، خواندن پیام را حذف نمیکند؛ پاکشدن بر اساس نگهداشت زمانی یا حجمی اتفاق میافتد. دوم، چند مصرفکنندهٔ مستقل میتوانند یک جریان را بخوانند بدون اینکه روی هم اثر بگذارند؛ این همان چیزی است که fan-out را ارزان میکند. سوم، بازپخش ممکن میشود و همین پایهٔ backfill و بازیابی است. چهارم، ترتیب فقط در محدودهٔ یک پارتیشن تضمین میشود، پس انتخاب کلید پارتیشن تعیین میکند کدام رویدادها به هم وفادار بمانند. هزینهها هم روشن است: نوشتن ترتیبی است و سریع، ولی دیسک مصرف میشود و تعداد پارتیشنها محدودیت عملی دارد. این مدل، «صف با حذف پس از تأیید» را کنار میگذارد و «دفتر کل با نشانِ هر خواننده» را جایش میگذارد.In a log-based broker each partition is an append-only file and every message takes a monotonic offset. Producers append at the tail; consumers read from any position and keep their own offset. That has direct consequences. Reading does not remove anything; deletion happens by time or size retention. Several independent consumers can read one stream without affecting each other, which is what makes fan-out cheap. Replay becomes possible, and replay is the foundation of backfill and recovery. And ordering is guaranteed only within a partition, so the partition key decides which events stay faithful to each other. The costs are equally clear: appends are sequential and fast, but disk is consumed and partition count has a practical ceiling. The model replaces “queue that deletes on ack” with “ledger with a marker per reader”.
۱ آفستهر مصرفکننده یک موقعیت در هر پارتیشن داردone position per consumer per partition
O(1)افزودن در انتها هزینهٔ ثابت داردappending at the tail is constant cost
TTLپاکشدن با زمان یا حجم، نه با تأییدmessages expire by time or size, not on ack
صف در برابر لاگQueue versus log
تفاوت اصلی در این است که چه کسی مسئولیت را نگه میدارد. در صف، کارگزار حالت هر پیام را دنبال میکند: تحویل شده، تأیید شده، چند بار تلاش شده. در لاگ، کارگزار فقط فایل را نگه میدارد و همهٔ حالت در سمت مصرفکننده است. این جابهجایی سه پیامد دارد. تحویل دوباره در صف یک تصمیم کارگزار است و در لاگ یک تصمیم مصرفکننده، پس در لاگ میتوانی آگاهانه به عقب برگردی. fan-out در صف یعنی کارگزار باید برای هر مصرفکننده کپی و حالت جدا نگه دارد، و در لاگ یعنی هر کس آفست خودش را میخواند. و ترتیب در صف شکننده است چون تحویل دوباره آن را به هم میزند، در حالی که در لاگ آفست یکنواست و ترتیب پارتیشن ثابت میماند.The real difference is who holds responsibility. In a queue the broker tracks per-message state: delivered, acknowledged, how many attempts. In a log the broker only keeps the file, and all the state lives on the consumer side. That shift has three consequences. Redelivery is the broker's decision in a queue and the consumer's in a log, so in a log you can deliberately rewind. Fan-out in a queue means the broker duplicates the message and its state for each consumer; in a log it means each reader uses its own offset. And ordering in a queue is fragile because redelivery scrambles it, whereas in a log the offset is monotonic and partition order is fixed.
چیزی که لاگ نمیگویدWhat the log does not promise
نگهداشت، یک مرز است و مرز همیشه دیر یا زود کسی را میگیرد. مصرفکنندهای که بیشتر از مدت نگهداشت خوابیده باشد، وقتی بیدار میشود آفستش دیگر وجود ندارد؛ آنوقت یک سیاست انتخاب میکنی و هر دو گزینه بد است: از قدیمیترین موجود شروع کن، که یعنی سیل دادهٔ قدیمی، یا از انتها شروع کن، که یعنی بیصدا یک روز داده را رد کردهای. لاگ همچنین ack و nack ندارد، پس پیام سمی که همیشه خطا میدهد تا ابد دوباره خوانده میشود و کل پارتیشن را بلاک میکند. برای همین در عمل، کنار لاگ به یک مسیر کنارگذاری، شمارش تلاش و هشدار روی آفست عقبمانده نیاز داری.Retention is a boundary, and a boundary eventually catches someone. A consumer asleep for longer than the retention window wakes to an offset that no longer exists, and both reset policies are bad: start from the earliest and you get a flood of old data, start from the latest and you have silently skipped a day. A log also has no ack or nack, so a poison message that always fails is re-read forever and blocks its partition. In practice that means you need a side path for bad messages, attempt counts, and an alert on consumer lag.
تلهٔ رایجThe usual trap
فرض کردن اینکه لاگ همه چیز را برای همیشه نگه میدارد. مصرفکنندهای که بیشتر از مدت نگهداشت خوابیده، بعد از بیدار شدن یا خطا میدهد یا روی سیاست «از انتها» بیصدا یک روز داده را رد میکند و هیچکس نمیفهمد.Assuming the log keeps everything forever. A consumer that slept past the retention window either errors out or silently skips a day under a “latest” reset policy, and nobody finds out.
پیام را تحویل بده و برو؛ بقیهاش با کیست؟Hand over the message and leave; whose problem is it now?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
همسایهات نیست و بستهاش رسیده. بسته را به نگهبانی ساختمان میدهی و میروی. نگهبانی یک قفسه دارد و همسایه شب که آمد برش میدارد. نه لازم بود صبر کنی، نه لازم بود بدانی کِی میآید؛ بسته از دست تو خارج شد و جایی منتظر ماند. کارگزار پیام همان نگهبانی است، برای پیامهای سیستم.Your neighbour is out and a parcel arrives. You hand it to the building's guard and leave. The guard has a shelf and the neighbour picks it up at night. You did not have to wait, and you did not have to know when they would be in; the parcel left your hands and waited somewhere. A message broker is that guard, for a system's messages.
تشبیه و تصویر ذهنیThe analogy
نگهبانی دو جور میتواند قفسهاش را بچیند. یا یک قفسهٔ مشترک است و هر بسته فقط یک بار برداشته میشود: هر کس اول بیاید صاحبش است و بسته از قفسه بیرون میرود. یا برای هر واحد یک حفرهٔ جدا دارد و اعلانها را برای همه میگذارد: هر واحد هر وقت خواست میآید و برمیدارد، حتی هفتهٔ بعد. اولی سبک است ولی حافظه ندارد؛ دومی حافظه دارد ولی قفسه پر میشود و باید تصمیم بگیری بستههای قدیمی کِی دور ریخته شوند. در هر دو حالت یک سؤال دیگر هم هست: نگهبان وقتی بسته را میگیرد رسید میدهد یا نه؟ اگر رسید بدهد و بعد قفسه آتش بگیرد، تو فکر میکنی تحویل شده.The guard can arrange the shelf in two ways. Either it is one shared shelf and every parcel is taken exactly once: whoever comes first owns it and it leaves the shelf. Or every flat has its own pigeonhole and notices are left for all of them: each flat collects whenever it likes, even next week. The first is light but has no memory; the second remembers but fills up, and you must decide when old parcels are thrown out. Either way there is one more question: does the guard give you a receipt? If he does and the shelf then burns, you believe it was delivered.
و بعدش، فنیAnd now, technically
کارگزار پیام تولید داده را از مصرفش جدا میکند: تولیدکننده مینویسد و میرود، مصرفکننده هر وقت آماده شد میخواند. برای اینکه این جدایی ارزش داشته باشد، سه چیز باید روشن باشد. دوام: پیام پیش از تأیید روی چند نسخه نوشته شود، وگرنه یک ریاستارت آن را میبرد. ترتیب: تضمین معمولاً فقط داخل هر بخش است، نه در کل جریان. و معنی تأیید: چه کسی، کِی، و به چه شرطی پیام را «انجامشده» اعلام میکند. دو خانوادهٔ اصلی هست. کارگزار صفی هر پیام را به یک مصرفکننده میدهد و بعد از تأیید پاکش میکند، پس چند مصرفکننده بار را تقسیم میکنند ولی کسی نمیتواند عقب برگردد. کارگزار لاگی پیامها را در یک لاگ فقط-افزودنی نگه میدارد و هر گروه مصرفکننده آفست خودش را دارد، پس چند گروه مستقل میخوانند و میتوانند بازپخش کنند، به بهای نگهداشت بیشتر. تأیید پیش از تکثیر، صف بیانتها، و مصرفکنندهای که پیام را میگیرد و تأیید نمیکند، سه شکست کلاسیک همین طرحاند.A broker separates producing data from consuming it: the producer writes and leaves, the consumer reads when it is ready. For that separation to be worth anything, three things must be clear. Durability: the message is on several replicas before the ack, or a restart takes it. Order: the guarantee is usually per partition, not across the stream. And the meaning of the ack: who declares a message done, when, and on what condition. There are two main families. A queue broker hands each message to one consumer and deletes it on ack, so consumers share the load but nobody can go back. A log broker keeps messages in an append-only log and gives each consumer group its own offset, so groups read independently and can replay, at the cost of retention. Acking before replication, an unbounded queue, and a consumer that takes a message and never acks are the three classic failures of the design.
at-least-onceپیشفرض بیشتر کارگزارها: حداقل یک بار، پس تکرار ممکن استthe default of most brokers: at least once, so duplicates are possible
1 order / keyترتیب فقط داخل یک بخش معنا دارد، نه در کل جریانordering holds inside a partition, not across the stream
تلهٔ رایجThe usual trap
تأیید کردن پیام بهمحض دریافت، پیش از پردازش. تا وقتی همهچیز خوب است تفاوتی نمیبینی؛ روزی که مصرفکننده وسط کار بیفتد، پیامهایی که «تحویل شده» بودند هیچجا نیستند. همانقدر هم رایج است که کسی انتظار ترتیب سراسری داشته باشد و بعد از اضافه کردن مصرفکنندهٔ دوم، ببیند ترتیب شکسته است.Acking on receipt, before processing. Nothing looks different while things are healthy; the day the consumer dies mid-work, the messages that were “delivered” are nowhere. Just as common is expecting global ordering, then adding a second consumer and finding the order broken.
یکییکی، یا هر ثانیه یک دستهٔ کوچک؟One at a time, or a small batch every second?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
ظرفها را یکییکی نمیشویی؛ هر شب همهٔ ظرفهای روز را یکجا میشویی. اگر مهمان سرزده بیاید و بخواهی همان لحظه ظرف تمیز داشته باشی، این روش به تو نمیخورد. ریزدسته (micro-batch) همین است: کمی صبر میکنی، همه را یکجا و ارزانتر انجام میدهی، و بابتش تأخیر میدهی.You do not wash dishes one at a time; you do the whole day's pile in one go each evening. If a guest arrives unannounced and you need a clean plate right now, the method fails you. A micro-batch is that: you wait a little, do everything together and more cheaply, and pay for it in delay.
تشبیه و تصویر ذهنیThe analogy
اندازهٔ دسته یک درجه است، نه یک انتخاب دوگانه. دستهٔ کوچک یعنی سربار کارهای مقدماتی برای هر دسته، ولی تأخیر کم. دستهٔ بزرگ یعنی سربار کم و بازدهی بالا، ولی رویدادها مدتی در صف میمانند. یک ثانیه یک انتخاب رایج است، چون هم تأخیر را قابل قبول نگه میدارد و هم سربار را کم میکند. ریزدسته یک سود پنهان هم دارد: هر دسته میتواند بهصورت یک واحد کامیت شود، پس اگر دستهای شکست بخورد هیچکدام از اعضایش نصفه اعمال نشده است و همین کار exactly-once را سادهتر میکند. بهایش این است که تأخیر یک دسته، دستهٔ بعدی را عقب میاندازد؛ اگر یک دسته دو برابر طول بکشد، صف به همان اندازه رشد میکند.Batch size is a dial, not a binary choice. A small batch pays setup cost on every batch but keeps latency low. A large batch pays little overhead and moves a lot of work, but events sit in the queue meanwhile. One second is a common setting because it keeps latency tolerable and overhead small. Micro-batching has a hidden benefit too: each batch can commit as one unit, so a failed batch leaves none of its members half applied, which makes exactly-once easier. The cost is that a slow batch delays the next one; if a batch takes twice as long, the queue grows by exactly that much.
ریزدسته چند رویداد را در یک بازهٔ کوتاه جمع میکند و یکجا اجرا میکند: تأخیر بیشتر، سربار کمتر، کامیت اتمیک.A micro-batch gathers a short window of events and runs them together: more latency, less overhead, an atomic commit.
و بعدش، فنیAnd now, technically
ریزدسته یعنی موتور جریان، ورودی را در بازههای کوتاه و معمولاً زمانی جمع کند و هر بازه را بهصورت یک کار کوچک دستهای اجرا کند. این کار دو چیز را به هم میچسباند: مدل برنامهنویسی دستهای که ساده و کارآمد است، و تأخیر چند ثانیهای که هنوز «جریانی» به حساب میآید. مزیت اصلی بازدهی است: سربار راهاندازی، ترتیبدهی و کامیت بین هزار رویداد تقسیم میشود، نه اینکه برای هر رویداد پرداخت شود. مزیت دوم، سادگی تحمل خرابی است: هر دسته یک واحد اتمیک است، پس کامیت خروجی و ثبت آفست میتوانند با هم انجام شوند و بازیابی، دستههای کامیتشده را دوباره اجرا نمیکند. هزینهاش تأخیر است: کمترین تأخیر ممکن به اندازهٔ فاصلهٔ دستههاست، و اگر یک دسته کند شود، دستههای بعدی در صف میمانند و تأخیر جمع میشود. به همین دلیل اندازهٔ دسته یک تنظیم واقعی است، نه عددی که یک بار انتخاب شود. ریزدسته برای تحلیل و تجمیع عالی است؛ برای واکنش لحظهای — مثلاً تشخیص تقلب روی یک تراکنش — معمولاً به پردازش رویدادبهرویداد یا دستههای خیلی کوچک نیاز داری.Micro-batching means the stream engine gathers input over short, usually time-based, intervals and executes each interval as a small batch job. It glues two things together: the simple, efficient batch programming model, and a delay of a few seconds that still counts as streaming. The main gain is throughput, because setup, ordering and commit costs are shared across a thousand events instead of paid per event. The second gain is simpler fault tolerance: a batch is an atomic unit, so output commit and offset commit can happen together and recovery does not re-run committed batches. The cost is latency: the floor is the batch interval, and a slow batch leaves the next ones waiting so delays accumulate. Batch size is therefore a real tuning decision, not a constant. Micro-batching is excellent for analytics and aggregation; for reaction within a single transaction — fraud checks, for instance — you usually need per-event processing or very small batches.
۱ ثانیهفاصلهٔ رایج دستهها؛ کمترین تأخیر ممکن هم همین استa common interval, and also the latency floor
all or noneهر دسته یک واحد اتمیک استeach batch is one atomic unit
۲×دستهای که دو برابر طول بکشد، صف را به همان اندازه عقب میاندازدa batch that takes twice as long pushes the queue by that much
چرا ریزدسته دقیقاً یک بار را سادهتر میکندWhy batching makes exactly-once cheaper
در پردازش رویدادبهرویداد، هر رویداد یک واحد کار است و برای هر کدام باید آفست را جایی ذخیره کنی؛ اگر بین نوشتن خروجی و ذخیرهٔ آفست خرابی رخ دهد، همان ابهام همیشگی برمیگردد. در ریزدسته، کل دسته یک واحد است: آفست پایانی دسته همراه با خروجی دسته در یک تراکنش کامیت میشود. بعد از خرابی، دستههای کامیتشده دوباره اجرا نمیشوند و دستهٔ نیمهکاره از اول تکرار میشود، که بیضرر است چون کامیت نشده بود. این سود فقط تا مرز مقصد دوام دارد: اگر داخل دسته به یک API بیرونی بنویسی، آن نوشتن با کامیت دسته یکی نیست و همانجا از ضمانت بیرون میافتد.In per-event processing each event is its own unit of work and you must store an offset for each one; a crash between the output write and the offset write brings back the usual ambiguity. In a micro-batch the whole batch is the unit: the batch's final offset commits in the same transaction as the batch's output. After a crash, committed batches do not run again and the half-done batch repeats from the start, which is harmless because it never committed. That benefit survives only up to the sink: if you call an external API inside the batch, that write is not part of the batch commit and the guarantee stops there.
هزینه در تأخیر و در صفThe cost shows up as latency and as a queue
کمترین تأخیر ممکن، فاصلهٔ دستههاست، و این عدد ثابت نیست: یک دسته که کند اجرا شود، شروع دستهٔ بعدی را عقب میاندازد و تأخیر جمع میشود. اگر ورودی برای مدتی از ظرفیت بیشتر باشد، صف بین دستهها رشد میکند و همان چیزی رخ میدهد که در هر سیستم صفی رخ میدهد: تأخیر از ثانیه به دقیقه میرود و بعد به هشدار timeout تبدیل میشود. برخی موتورها با اجرای همپوشان دستهها این را کم میکنند، که تأخیر را پایین میآورد ولی ترتیب و اتمیک بودن را پیچیده میکند. انتخاب ساده و صادقانه این است: اندازهٔ دسته را با تأخیر قابل قبول تنظیم کن و روی عقبماندگی صف هشدار بگذار، نه روی میانگین تأخیر.The latency floor is the batch interval, and it is not constant: a slow batch delays the start of the next and delays accumulate. If input exceeds capacity for a while, the queue between batches grows and you get what every queueing system gives you: latency moves from seconds to minutes and then turns into timeout errors. Some engines reduce this by overlapping batches, which lowers latency but complicates ordering and atomicity. The honest simple choice is to set batch size from acceptable latency and alert on queue backlog rather than on average latency.
تلهٔ رایجThe usual trap
فکر کردن اینکه چون دسته اتمیک است پس دقیقاً یک بار داریم. اتمیک بودن فقط داخل موتور است؛ اگر وسط دسته به یک سرویس بیرونی بنویسی و دسته شکست بخورد، بخشی از آن نوشته شده و تکرار دسته همان بخش را دوباره مینویسد.Concluding that an atomic batch means exactly-once. Atomicity lives inside the engine only; if you write to an external service mid-batch and the batch fails, part of it landed and replaying the batch writes that part again.
مصرفکننده از کدام رکورد ادامه میدهد؟Where does the consumer pick up again?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
سریالی را از قسمت ۱۲ رها کردهای. هفتهٔ بعد از قسمت ۱۳ شروع میکنی؛ لازم نیست از اول ببینی و لازم نیست حدس بزنی. عددی که یادت مانده همان جایی است که بودی. آفست (offset) همین شماره است، با یک تفاوت: آن را خودت نگه نمیداری، سیستم ثبتش میکند.You stopped a series at episode twelve. Next week you start at thirteen; you neither rewatch from the beginning nor guess. The number you remember is where you were. An offset is that episode number, except the system records it for you.
تشبیه و تصویر ذهنیThe analogy
حالا تصور کن چند نفر همان سریال را میبینند. هر کس شمارهٔ خودش را دارد؛ یکی قسمت ۳ است، یکی قسمت ۹. یک عدد مشترک کافی نیست، وگرنه همه باید با هم جلو بروند. نکتهٔ دوم اینکه شماره فقط یک موقعیت است، نه شناسهٔ قسمت: اگر قسمتهای قدیمی از فهرست حذف شوند، شمارهها عوض نمیشوند، ولی چیزی که به آن اشاره میکنند ممکن است دیگر موجود نباشد. و نکتهٔ سوم همان جایی است که همه اشتباه میکنند: کِی شماره را یادداشت میکنی؟ اگر پیش از دیدن بنویسی و وسطش برق برود، آن قسمت را از دست دادهای. اگر بعد از دیدن بنویسی و برق برود، دوباره میبینی. آفست هم همین معامله را دارد، فقط اسمش کامیت است.Now suppose several people watch the same series. Each has their own number: one is on episode three, another on episode nine. A shared number would not do, or everyone would have to move together. Second, the number is a position, not an identity: delete old episodes and the numbers do not shift, but what they point to may no longer exist. Third is where everyone slips: when do you write the number down? Write it before watching and a power cut loses the episode. Write it after watching and a power cut replays it. An offset makes the same bargain, and its name for it is commit.
آفست ۴۳ کامیت نشده بود، پس بعد از خرابی از همانجا ادامه میدهی و کارِ نیمهتمام تکرار میشود.Offset 43 was never committed, so the restart resumes there and the unfinished work runs again.
و بعدش، فنیAnd now, technically
آفست شمارهٔ ترتیبی هر رکورد در یک پارتیشن است، یکنوا رشد میکند و فقط در محدودهٔ همان پارتیشن معنا دارد. آنچه واقعاً اهمیت دارد آفست کامیتشده است: آخرین موقعیتی که مصرفکننده اعلام کرده تا آنجا را انجام داده است. نسبت زمان کامیت با زمان انجام کار، همان چیزی است که ضمانت را تعیین میکند. اگر آفست را پیش از پردازش کامیت کنی و بعد خرابی رخ دهد، آن رکورد دیگر خوانده نمیشود؛ at-most-once. اگر بعد از پردازش کامیت کنی، خرابی بین این دو باعث میشود رکورد دوباره خوانده شود؛ at-least-once. به همین دلیل کامیت آفست در بسیاری از سیستمها بخشی از همان تراکنش خروجی است، وگرنه یک خرابی کوچک بین نوشتن و کامیت یک نسخهٔ تکراری جا میگذارد. آفست را با شناسهٔ پیام اشتباه نگیر: شناسه در خود پیام است و ثابت میماند، آفست جای پیام در لاگ است و با پارتیشن و نگهداشت جابهجا میشود. ریبالانس آفست را از بین نمیبرد، ولی ممکن است باعث شود مصرفکنندهٔ دیگری از آفست کامیتشده ادامه دهد و بخشی از کار تکرار شود.An offset is the sequence number of a record inside a partition; it grows monotonically and means something only within that partition. What actually matters is the committed offset: the last position the consumer has declared finished. The relationship between commit time and work time is what sets the guarantee. Commit before processing and a crash makes the record unreadable forever, which is at-most-once. Commit after processing and a crash in between re-reads it, which is at-least-once. That is why the offset commit is often part of the output transaction; otherwise a small crash between the write and the commit leaves a duplicate behind. Do not confuse an offset with a message id: the id lives in the message and never changes, while the offset is the message's place in the log and moves with partitions and retention. A rebalance does not destroy the offset, but it may hand the partition to another consumer that resumes from the committed position and repeats part of the work.
۱ پارتیشنآفست فقط در محدودهٔ یک پارتیشن معنا داردan offset means something only inside one partition
+۱هر رکورد یک آفست جلو میرود، بدون پرشeach record advances the offset by exactly one
commitآفستِ ذخیرهشده، نقطهٔ شروع اجرای بعدی استthe stored offset is where the next run starts
آفست در برابر شناسهٔ پیامOffset versus message id
این دو شبیهاند و کارشان یکی نیست. آفست یک موقعیت است: «چندمین رکورد این پارتیشن». شناسه یک هویت است: «همین رکورد، هر جا که بنشیند». تفاوت وقتی مهم میشود که داده جابهجا شود. با نگهداشت و حذف بخشهای قدیمی، آفستهای باقیمانده ثابت میمانند ولی ممکن است دیگر رکوردی در آن موقعیت نباشد؛ با بازپخش یا نوشتن دوباره در یک موضوع دیگر، همان رکورد آفست کاملاً تازهای میگیرد. برای dedup و برای «آیا این را قبلاً دیدم» باید از شناسه استفاده کنی، نه از آفست. برای «از کجا ادامه بدهم» باید از آفست استفاده کنی، نه از شناسه. قاطی کردن این دو، یک باگ کلاسیک است که فقط بعد از یک بازپخش ظاهر میشود.They look alike and do different jobs. An offset is a position: “the nth record of this partition.” An id is an identity: “this record, wherever it lands.” The difference matters when data moves. Retention deletes old segments and the remaining offsets stay put, but there may no longer be a record at one of them; a replay or a copy into another topic gives the same record a completely fresh offset. Use the id for dedup and for “have I seen this”, and the offset for “where do I resume”. Mixing them is a classic bug that only appears after a replay.
چه کسی آفست را نگه میداردWho stores the offset
در بعضی سیستمها کارگزار آفست هر مصرفکننده را نگه میدارد و در بعضی، خودِ مصرفکننده آن را در یک جای بیرونی ذخیره میکند. حالت دوم انعطاف بیشتری میدهد، چون میتوانی آفست را همراه با خروجی در یک تراکنش بنویسی، و همین چیزی است که اثر یکبار را ممکن میکند. اما یک هزینه دارد: حالا خودت مسئول آن هستی و اگر ذخیرهگاه آفست از دست برود، از صفر یا از انتها شروع میکنی. حالت اول سادهتر است و برای خیلی از کارها کافی، ولی کامیت خودکار دورهای، همان تلهٔ همیشگی است: آفست جلو میرود حتی وقتی کار تمام نشده.Some systems keep every consumer's offset on the broker; others let the consumer store it externally. The second is more flexible, because you can write the offset inside the same transaction as the output, which is exactly what a single effect needs. It costs you responsibility: if the offset store is lost you start from zero or from the end. The first is simpler and enough for many jobs, but periodic auto-commit is the usual trap: the offset advances even when the work has not finished.
تلهٔ رایجThe usual trap
کامیت خودکار هر چند ثانیه. آفست بعد از هر خواندن جلو میرود، نه بعد از هر انجام؛ یک خرابی کوتاه، یک دستهٔ در جریان را بیصدا میبلعد و چون هیچ خطایی ثبت نشده، ماهها بعد فقط یک عدد کم در گزارش میبینی.Auto-committing every few seconds. The offset advances after each read, not after each completion; a short crash silently swallows the batch in flight, and because nothing errored you only notice months later as a number that is slightly low.
جواب نیامد؛ کار انجام شده یا نه؟No reply came. Was the work done or not?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
بچه را برای نان میفرستی. نیم ساعت میگذرد، نه نانی هست نه خبری. برادر کوچک را هم میفرستی. حالا ممکن است دو نان داشته باشی، چون بچهٔ اول توی صف بود و گوشیاش باتری نداشت. تحویل دوباره همین است: وقتی تأیید نمیرسد، فرق «انجام نشد» و «انجام شد ولی خبرش نرسید» را نمیدانی.You send your kid out for bread. Half an hour passes, no bread and no word. You send the younger one too. Now you may end up with two loaves, because the first was in the queue with a dead phone. Redelivery is that: when the ack never comes, you cannot tell “not done” from “done, but the news did not arrive”.
تشبیه و تصویر ذهنیThe analogy
دو راه داری و هر دو هزینه دارد: نفرستادن یعنی ممکن است نان نداشته باشی، فرستادن یعنی ممکن است دو نان داشته باشی. تنها راه بیرون آمدن از این دور، عوض کردن سؤال است: کاری کن که فرستادن دوباره بیخطر باشد. مثلاً بچه قبل از رفتن، روی تخته بنویسد «نان: در راه»؛ نفر دوم که میرود، اول تخته را میخواند، نه یخچال را. مسئلهٔ دوم مدت انتظار است: اگر خیلی زود نفر دوم را بفرستی، سر هر شلوغی دو نان میگیری؛ اگر خیلی صبر کنی، تا شب نان نداری. آن مدت، همان چیزی است که در سیستم به آن مهلت تأیید میگویند.Two options, both with a cost: not sending risks no bread, sending risks two loaves. The only way out is to change the question and make a repeat harmless — have the first kid chalk “bread: on the way” before leaving, so the second reads the board, not the fridge. The second problem is how long to wait: send the backup too soon and every busy evening gives you two loaves; wait too long and there is no bread tonight. That wait is what a system calls the ack timeout.
و بعدش، فنیAnd now, technically
تحویل دوباره از چند جا میآید و همهشان یک ریشه دارند: تأیید نرسیده. مصرفکننده وسط پردازش مرده و مهلت دیدهشدن پیام تمام شده؛ مصرفکننده از گروه بیرون افتاده و بخشش به عضو دیگری رسیده و آن عضو از آفست ثبتشدهٔ قبلی خوانده؛ تولیدکننده پس از یک مهلت دوباره فرستاده، در حالی که کارگزار بار اول پیام را ذخیره کرده بود. در هیچکدام از اینها سیستم نمیتواند بفهمد کار انجام شده یا نه، چون اثر جانبی و ثبت آفست دو چیز جدا هستند. راهحل استاندارد، حداقل یک بار تحویل بهعلاوهٔ idempotency است: هر عملیات یک کلید یکتا بگیرد و اثرش با همان کلید یک بار اعمال شود، یا نوشتن با upsert انجام شود که تکرارش بیاثر است. اگر ترتیب برای یک کلید مهم است، تحویل دوبارهٔ همزمان دو پیام همان کلید میتواند ترتیب را برگرداند، پس باید کلید را قفل کنی یا پردازش را تکرشتهای نگه داری.Redelivery comes from several places with one root: no ack arrived. A consumer died mid-processing and the message's visibility deadline expired; a consumer dropped out of the group, its partition went to another member, and that member resumed from the previous committed offset; a producer retried after a timeout even though the broker had stored the first attempt. In none of these can the system tell whether the work was done, because the side effect and the offset commit are two separate things. The standard answer is at-least-once plus idempotency: give each operation a unique key and apply its effect once per key, or write with an upsert so a repeat is a no-op. And if ordering matters for a key, two concurrent redeliveries of that key can invert the order, so lock the key or keep the processing single-threaded.
at-least-onceتحویل دوباره همان تضمین «حداقل یک بار» است، با تکرار بهعنوان هزینهredelivery is the at-least-once guarantee, with duplicates as the price
ack timeoutمدت انتظار برای تأیید؛ کوتاه یعنی تکرار، بلند یعنی معطلیthe wait for an ack: short means duplicates, long means stalls
تلهٔ رایجThe usual trap
مدرک شکست گرفتن مهلت. وقتی مهلت تمام میشود، تنها چیزی که میدانی این است که جوابی نیامده، نه اینکه کار انجام نشده؛ پرداختن دوبارهٔ پول به همین دلیل، کلاسیکترین شکل این خطاست. همینطور کلید dedup را از هش بدنه ساختن، در حالی که بدنه در هر تلاش یک مهر زمانی تازه دارد، یعنی هیچ تکراری تشخیص داده نمیشود.Treating a timeout as proof of failure. When the deadline passes, all you know is that no answer came, not that the work was not done; charging the card again on that basis is the classic form of this mistake. Equally common is deriving the dedup key from a hash of the body while the body carries a fresh timestamp on each attempt, so no duplicate is ever recognised.
کد باگ داشت؛ دادهٔ پارسال چه میشود؟The code had a bug. What happens to last year's data?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
آخر سال میفهمی اجارهٔ سه ماه را در حساب خانوادگی جا انداخته بودی. رسیدها را از جعبهٔ کفش درمیآوری و کل حساب سال را از نو میزنی. این کار فقط بهخاطر آن است که رسیدها را دور نریخته بودی؛ اگر فقط عدد نهایی را در دفتر نوشته بودی، راهی برای برگشت نبود. پردازش دوباره همین است: به شرط نگه داشتن ورودیها، خروجی از نو ساخته میشود.At the end of the year you find you left three months of rent out of the household accounts. You pull the receipts out of the shoebox and redo the whole year's sums. That is only possible because you kept the receipts; had you written down just the final number, there would be no way back. Reprocessing is that: keep the inputs and the output can be rebuilt.
تشبیه و تصویر ذهنیThe analogy
حساب دوباره سه دردسر دارد. اول، خروجی مشترک است: تا وقتی مشغولی، بعضی ماهها تازهاند و بعضی قدیم، و هر کسی که وسط کار نگاه کند عدد بیمعنی میبیند؛ پس باید حساب تازه را جدا بنویسی و یکجا جایگزین کنی. دوم، باید مطمئن شوی حساب تازه درست است: یک ماه را که خودت دقیق میدانی دوباره چک میکنی، وگرنه باگ تازه را با باگ قبلی عوض کردهای. سوم، ممکن است کسی از عدد قدیمی تصمیم گرفته باشد؛ عددی که عوض میشود باید به آنها هم گفته شود. و اگر جعبهٔ رسیدها را سالی یک بار خالی میکنی، پنجرهٔ برگشتت همان یک سال است.Redoing the sums has three annoyances. First, the output is shared: while you work, some months are new and some old, and anyone who looks mid-way sees nonsense, so you write the new account separately and swap it in one go. Second, you must be sure the new sum is right: you recheck a month you know exactly, or you have traded one bug for another. Third, someone may have decided something from the old number, and the change must reach them. And if you clear the shoebox once a year, your window for going back is that one year.
و بعدش، فنیAnd now, technically
پردازش دوباره یعنی همان منطق را روی دادهای که قبلاً پردازش شده دوباره اجرا کنی: بعد از یک باگ، بعد از عوض شدن یک قاعدهٔ کسبوکار، یا برای ساختن یک نمای تازه از داده. چهار شرط دارد. اول، ورودی مانده باشد: لاگ با نگهداشت کافی، یا آرشیو در ذخیرهسازی ارزان، وگرنه کاری نیست. دوم، منطق قطعی باشد: پنجره روی زمان رویداد، بدون now() و بدون تصادف، با قاعدهٔ ثابت برای مرتبسازی و تساویها، وگرنه دو اجرا دو جواب میدهد و مقایسه بیمعنی میشود. سوم، خروجی جایگزینی داشته باشد: جدول یا نمایهٔ تازه بسازی و بعد یکجا جایش بیندازی، یا با کلید پنجره upsert کنی تا تکرار بیاثر باشد. چهارم، با کار زندهٔ همزمان قاطی نشود: یا کار زنده را نگه داری، یا بازپخش را تا یک نقطه ببری و بعد عقبترها را جدا حساب کنی. معماری لامبدا همین را با دو مسیر جدا انجام میداد؛ با نگهداشت لاگ، یک مسیر کافی است.Reprocessing means running today's logic again over data that was already processed: after a bug, after a rule changes, or to build a new view of the data. It needs four things. First, the input must still exist: a log with enough retention, or an archive in cheap storage, or there is nothing to replay. Second, the logic must be deterministic: event-time windows, no now(), no randomness, a fixed rule for ordering and ties, or two runs give two answers and the comparison is meaningless. Third, the output needs a replacement path: build a new table or index and swap it in one step, or upsert by window key so repeats are harmless. Fourth, it must not collide with the live job: either pause the live path, or backfill up to a point and let the live path take over from there. The lambda architecture did this with two separate paths; with a retained log, one path is enough.
retention ≥ Tتا هر جا که لاگ نگه داشته شده میتوانی عقب بروی، نه بیشترyou can go back as far as retention goes, and no further
shadow → swapخروجی تازه را جدا بساز و یکجا جایگزین کن، نه در جاbuild the new output aside and swap it in one step, not in place
تلهٔ رایجThe usual trap
پردازش دوباره با منطقی که قطعی نیست. اگر پنجرهها روی زمان پردازش باشند یا جایی now() صدا زده شود، دو اجرا دو عدد میدهد و نمیتوانی بگویی کدام درست است، چه برسد به اینکه بفهمی باگ رفع شده. بههمین ترتیب، جایگزینی خروجی در حالی که مصرفکنندهها همان جدول را میخوانند، باعث میشود بعضی گزارشها عدد تازه ببینند و بعضی عدد قدیم.Reprocessing with logic that is not deterministic. If windows run on processing time or something calls now(), two runs give two numbers and you cannot say which is right, let alone whether the bug is fixed. Likewise, replacing the output in place while consumers read the same table leaves some reports on the new numbers and some on the old.
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
رسیدهای خرید را در کشو میریزی. رسیدداروی ماه پیش را نگه میداری چون ممکن است لازم شود، ولی رسید نان را همان شب دور میاندازی. هیچکدام را «برای همیشه» نگه نمیداری، چون کشو پر میشود و پیدا کردن چیزی که میخواهی سختتر میشود.You keep receipts in a drawer. The pharmacy receipt from last month stays because you might need it; the bread receipt goes out the same night. You keep neither forever, because the drawer fills up and finding the one you want gets harder.
تشبیه و تصویر ذهنیThe analogy
هر خانهای یک قاعدهٔ نگهداشت دارد که کسی هیچوقت ننوشته: قبضها تا آخر سال، ضمانتنامهها تا وقتی دستگاه هست، بلیتها تا وقتی سفر تمام شود. وقتی این قاعده نوشته نشود، همه چیز میماند. در سیستم داده هم همین است: اگر نگهداشت را عدد نکنی، عملاً بینهایت است و روزی میرسد که ذخیرهسازی گرانتر از سودِ نگهداشتن شود.Every home has an unwritten retention rule: bills until the end of the year, warranties as long as the appliance lives, tickets until the trip is over. When nobody writes it down, everything stays. The same holds for a data system: if retention is not a number, it is effectively infinite, and one day storage costs more than the value of keeping it.
و بعدش، فنیAnd now, technically
در لاگهای فقط-افزودنی، نگهداشت همان پنجرهٔ زمانی است که لاگ آن را پوشش میدهد؛ قدیمیتر از آن حذف یا فشرده میشود. این عدد تصمیمهای مهمی را قفل میکند:چه مدت میتوانی یک مصرفکنندهٔ خراب را بازپخش کنی، چه مدت یک نسخهٔ تازه از سرویس میتواند از آفست قدیمی شروع کند، و چه مدت پس از یک خرابی میتوانی حقیقت را بازسازی کنی. قاعدهٔ کلی این است که نگهداشت باید بلندتر از کندترین مصرفکنندهای باشد که تحمل خرابیاش میکنی، بهعلاوه حاشیهای برای تعطیلات و خرابیهای طولانی.In an append-only log, retention is the time window the log covers; anything older is deleted or compacted away. That number locks in real decisions: how long you can replay a broken consumer, how long a redeployed service may start from an old offset, and how long after an incident you can rebuild the truth. The working rule is that retention must exceed the slowest consumer you promise to tolerate, plus headroom for holidays and long outages.
> slowestنگهداشت باید از کندترین مصرفکننده بلندتر باشدretention must outlast the slowest consumer
تلهٔ رایجThe usual trap
کوتاه کردن نگهداشت برای صرفهجویی در دیسک، بدون شمردنِ کندترین مصرفکننده. یک تعطیلی چندروزه کافی است تا مصرفکننده از پنجره عقب بیفتد و وقتی برگردد، دادهاش رفته باشد و هیچ خطایی هم نگرفته باشد.Shortening retention to save disk without counting the slowest consumer. A long holiday is enough for a consumer to fall out of the window; when it comes back its data is gone and it never raised an error.
آب از کجا میآید و کجا میریزد؟Where does it come from, and where does it go?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
شلنگ آب را وصل میکنی به شیر و سرش را میگیری روی سطل. شیر «منبع» است، سطل «مقصد». اگر سطل سرریز کند، آب برمیگردد و مسیر را خیس میکند؛ درست همان اتفاقی که وقتی مقصدِ یک خط داده کند است میافتد.You connect a hose to the tap and point it into a bucket. The tap is the source, the bucket is the sink. If the bucket overflows, the water backs up and soaks the path — which is exactly what happens when a pipeline's destination is slow.
تشبیه و تصویر ذهنیThe analogy
در آشپزخانه، ظرفها از جایی میآیند و به جایی میروند: از سینک، به آبچکان، به کابینت. اگر کابینت جا نداشته باشد، آبچکان پر میشود و سینک هم پر میشود. خط دادهٔ سالم هم همین زنجیره را دارد و هر حلقهاش میتواند گلوگاه باشد؛ ولی بیشتر آدمها فقط ابتدای زنجیره را نگاه میکنند.In a kitchen, dishes travel from the sink to the rack to the cupboard. If the cupboard is full, the rack fills, and then the sink fills. A healthy data pipeline is the same chain and any link can be the bottleneck, yet most people only watch the first one.
و بعدش، فنیAnd now, technically
در اصطلاح خط داده، منبع چیزی را تولید میکند و مقصد آن را مصرف میکند: نوشتن در انبار داده، در ایندکس جستوجو، در یک صف دیگر، یا در یک سرویس بیرونی. مهمترین خصلت یک مقصد این است که بدانی کندیاش چه اثری میگذارد. اگر مقصد را با نوشتنِ دستهای و تکرارپذیر بسازی، کندی فقط تأخیر میسازد؛ اگر نوشتنش غیرتکرارپذیر باشد، هر بازتلاش داده را دوباره اضافه میکند و باید شناسهٔ یکتا داشته باشی. فشار برگشتی هم از همینجا شروع میشود: مقصدِ کند باید به لایههای بالاتر بگوید که کند است، وگرنه بافرها پر میشوند و حافظه میترکد.In pipeline terms, a source produces and a sink consumes: writes into a warehouse, a search index, another queue, or an external service. The most important property of a sink is knowing what its slowness does. If the sink is written in idempotent batches, slowness only costs latency; if its writes are not idempotent, every retry adds a duplicate and you need a unique key. Backpressure starts here too: a slow sink must tell the layers above it that it is slow, or the buffers fill and memory goes.
idempotentمقصد باید بیخطر تکرار شود، وگرنه هر بازتلاش یک ردیف تازه استa sink must be safe to retry, or each retry adds a row
تلهٔ رایجThe usual trap
طراحی مقصد بدون فکر کردن به کندیاش. خطی که با سرعت تولید میخواند و آرام مینویسد، تا روزی که بافرها پر شوند سالم به نظر میرسد؛ آن روز، خرابی از جایی میآید که کسی نگاهش نمیکرد.Designing a sink without thinking about its slowness. A pipeline that reads fast and writes slowly looks healthy until the buffers fill; on that day the failure comes from the place nobody was watching.
داده که تمام نمیشود؛ کِی جواب میدهی؟The data never ends. When do you answer?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
نانوایی دو جور میتواند حسابش را نگه دارد: شب همهٔ رسیدها را جمع کند و جمع روز را دربیاورد؛ یا همان لحظه هر فروش را در دفتری بزند و موجودی را بهروز نگه دارد. اولی دقیقتر و آرامتر است، دومی همان لحظه جواب میدهد. پردازش جریانی (stream processing) همان دفترِ لحظهای است.A bakery can keep its books two ways: gather all the receipts at night and total the day, or note every sale in a ledger as it happens and keep the stock current. The first is tidier and calmer; the second answers right now. Stream processing is the ledger.
تشبیه و تصویر ذهنیThe analogy
فرق اصلی سرعت نیست، شکل داده است. در کار دستهای مجموعهای بسته داری و میتوانی تا آخرش صبر کنی؛ در جریان پایانی نیست که صبر کنی، پس باید خودت برای «حالا» مرز بگذاری. همان مرز، پنجره است: هر دقیقه، هر ساعت، یا هر چند رویداد. این کار دو چیز تازه به سیستم اضافه میکند. اول، وضعیت: باید بین رویدادها چیزی یادت بماند، مثل جمع سی دقیقهٔ گذشته. دوم، زمان: باید تصمیم بگیری رویداد متعلق به کدام بازه است و چه وقت بازه را میبندی. و چون پایانی نیست، خرابی هم پایانی ندارد؛ هر بار باید بدانی از کجا ادامه میدهی. در عوض چیزی میگیری که دستهای نمیدهد: جوابی که وقتی رخداد هنوز تازه است میرسد.The difference is not speed but shape. In batch work you have a closed collection and can wait for the end; in a stream there is no end to wait for, so you draw the line yourself. That line is a window: a minute, an hour, a few events. It adds two new things to the system. State, because something must be remembered between events, such as the last thirty minutes' total. And time, because you must decide which period an event belongs to and when to close it. And since there is no end, failure has no end either; every restart needs to know where to resume. In exchange you get what batch cannot give: an answer while the event is still fresh.
جریان پایانی ندارد؛ پردازنده با پنجره و وضعیت، از رویدادهای بیپایان جوابهای محدود میسازد.A stream has no end; the processor turns unbounded events into bounded answers with windows and state.
و بعدش، فنیAnd now, technically
پردازش جریانی یعنی اجرای منطق روی رویدادها در لحظهٔ رسیدن، با تأخیری که معمولاً از چند ثانیه کمتر است. ورودی معمولاً از یک کارگزار لاگمحور میآید و سه وظیفهٔ اصلی روی دوش پردازنده است: پنجرهبندی، نگه داشتن وضعیت، و تحمل خرابی. پنجرهبندی بازهای را که روی آن حساب میکنی مشخص میکند؛ وضعیت همان چیزی است که بین رویدادها نگه میداری، مثل شمارش یا میانگین متحرک یا نتیجهٔ یک join؛ و تحمل خرابی از checkpoint و بازپخش میآید. تفاوت مهم با کار دستهای این است که در دسته میتوانی کل ورودی را دوباره اجرا کنی و نتیجهٔ یکسان بگیری، ولی در جریان نتیجه به ترتیب رسیدن و به لحظهٔ بستهشدن پنجره وابسته است. به همین دلیل جریانها معمولاً بین زمان رویداد و زمان پردازش فرق میگذارند و برای رویدادهای دیررس قاعده دارند. اگر جریان به یک سیستم بیرونی مینویسد، همانجا مرز ضمانت است: داخل پردازنده میتوانی وضعیت و آفست را اتمیک نگه داری، ولی اثر بیرونی تکرارپذیر نیست مگر اینکه مقصد هم همکاری کند.Stream processing runs logic over events as they arrive, usually with a delay below a few seconds. Input normally comes from a log-based broker, and the processor carries three jobs: windowing, keeping state, and surviving failure. Windowing decides the period you compute over; state is what you remember between events, such as a count, a moving average or the result of a join; fault tolerance comes from checkpoints and replay. The important difference from batch is that batch can re-run the whole input and get the same answer, while a stream's answer depends on arrival order and on when windows closed. That is why streams separate event time from processing time and have a policy for late events. When the stream writes to an external system, that is where the guarantee ends: inside the processor you can keep state and offset atomically, but an external effect is not repeatable unless the sink cooperates.
∞ورودی پایانی ندارد، پس «صبر تا آخر» ممکن نیستthe input has no end, so waiting for it is not an option
۲ زمانزمان رویداد و زمان پردازش، دو چیز متفاوتevent time and processing time are different clocks
< ۱ ثانیهتأخیر هدف یک پردازندهٔ جریانیthe latency a stream processor aims for
چرا جریان از دسته سختتر استWhy streaming is harder than batch
در کار دستهای، ورودی محدود است، پس میتوانی هر بار از صفر شروع کنی و همان نتیجه را بگیری؛ خطا را با اجرای دوباره حل میکنی. در جریان، سه چیز این سادگی را از بین میبرد. اول، پایانی نیست، پس «از صفر شروع کن» گران و بیمعناست. دوم، وضعیت بین رویدادها نگه داشته میشود و آن وضعیت باید جایی ذخیره شود، وگرنه با هر ریاستارت از دست میرود. سوم، نتیجه به زمان بستهشدن پنجره وابسته است، پس دو اجرا از یک ورودی ممکن است دو جواب بدهند. برای همین تیمها معمولاً جریان را برای چیزی میخواهند که تأخیرش ارزش این پیچیدگی را دارد، و بقیه را دستهای نگه میدارند.In batch the input is bounded, so you can start from zero every time and get the same answer; errors are fixed by re-running. In a stream three things destroy that simplicity. There is no end, so starting from zero is expensive and pointless. State is kept between events and must be stored somewhere, or every restart loses it. And the answer depends on when windows closed, so two runs over the same input may disagree. That is why teams reach for streaming when the delay is worth the complexity, and keep the rest in batch.
کِی دستهای بهتر استWhen batch is the better answer
اگر کسی عدد را فردا صبح میخواهد، جریان فقط هزینه است: وضعیت، پنجره، checkpoint و یک مسیر بازیابی که باید همیشه سالم بماند، برای گزارشی که کسی تا صبح نگاهش نمیکند. کار دستهای ارزانتر است، اجرای دوبارهاش ساده است، و با تغییر کد میتوانی کل تاریخ را بازسازی کنی، کاری که در جریان به backfill و هماهنگی نیاز دارد. جریان جایی میبرد که تأخیر خودش بخشی از محصول است: هشدار، تشخیص تقلب، بهروزرسانی لحظهای داشبورد، یا واکنشی که باید قبل از تمام شدن یک دقیقه رخ دهد. معیار خوب ساده است: اگر جواب شش ساعت دیرتر همان ارزش را دارد، دستهای بزن.If someone wants the number tomorrow morning, streaming is pure cost: state, windows, checkpoints and a recovery path that must stay healthy, all for a report nobody looks at until dawn. Batch is cheaper, re-running is trivial, and changing the code lets you rebuild all of history, which in streaming needs a backfill and careful coordination. Streaming earns its place where the delay is part of the product: alerts, fraud detection, a live dashboard, a reaction that must happen before the minute is out. The test is simple: if the answer is worth the same six hours later, use batch.
تلهٔ رایجThe usual trap
برخورد با جریان مثل یک کار دستهای که فقط زودتر اجرا میشود. با این ذهنیت وضعیت را جدی نمیگیری، checkpoint را لازم نمیدانی، و اولین ریاستارت نشان میدهد که همهٔ محاسباتت از صفر شروع میشود و گزارشها با هم نمیخوانند.Treating a stream like a batch job that runs sooner. With that mental model you neglect state, skip checkpoints, and the first restart reveals that all your aggregates start from zero and the reports no longer agree.
تا کجا مطمئنی دیگر خبری از گذشته نمیرسد؟How far back can you still be wrong?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
شام مهمانی است و همه دور میز نشستهاند جز دخترخالهات که همیشه دیر میرسد. ساعت نه، خاله میگوید «دیگر سفارش میدهیم» و غذا میآید؛ اگر دخترخالهات بعد از آن برسد، سهمش باید جدا حساب شود. آن «دیگر سفارش میدهیم» یک حدس است، نه یقین — و واترمارک هم همین است: حدسی دربارهٔ اینکه دیگر خبری از گذشته نمیرسد.Dinner is on the table and everyone is seated except your cousin, who is always late. At nine your aunt says “we are ordering now” and the food comes; if the cousin turns up after that, her portion has to be counted separately. That “we are ordering now” is a guess, not a certainty — and a watermark is the same guess, about the past no longer arriving.
تشبیه و تصویر ذهنیThe analogy
رستوران دفتر رزرو دارد و قاعدهاش این است که میز را تا یک ربع بعد از ساعت رزرو نگه میدارد. اگر این ربع کوتاه شود، مهمانی که در ترافیک مانده بیرون میماند و شکایتش هم حق دارد؛ اگر بلند شود، میز خالی میماند و آشپزخانه برای کسی که هیچوقت نمیآید غذا نگه میدارد. هیچکدام از این دو «درست» نیست، چون مدیر رستوران آینده را نمیداند. کاری که میکند این است که از شواهد حدس میزند: چند نفر رزرو کردهاند، شب چه شبی است، و کدام مشتریها همیشه سر وقت میآیند. واترمارک هم همان حدس است، فقط با یک تفاوت: عددش را از تأخیری میگیرد که واقعاً دیده، نه از یک قاعدهٔ ازلی.A restaurant keeps a reservation book with one rule: hold the table for a quarter of an hour past the booking. Shorten that quarter and the party stuck in traffic is left outside, and their complaint is fair. Stretch it and the table sits empty while the kitchen keeps food for someone who is never coming. Neither answer is “correct”, because the manager cannot see the future. What he does is guess from evidence: how many people booked, what kind of evening it is, which guests always arrive on time. A watermark is that guess, with one difference — the number comes from the lateness he actually observed, not from a rule handed down.
واترمارک روی زمان رویداد جلو میرود، نه روی ساعت دیوار؛ پنجره وقتی میرسد که از انتهایش بگذرد.The watermark advances on event time, not the wall clock; a window fires once the watermark passes its end.
و بعدش، فنیAnd now, technically
واترمارک یک زمان است، نه یک پیام: عددی که همراه جریان حرکت میکند و معنایش این است که «رویدادی با زمان رویداد کوچکتر از این عدد دیگر از این ورودی نمیآید». این عدد از خود داده میآید، معمولاً بزرگترین زمان رویدادی که تا حالا دیدهای منهای یک حاشیهٔ تأخیر که خودت انتخاب میکنی. در جریانِ چندبخشی، عملگر محافظهکارانه کمترین واترمارک بین ورودیها را برمیدارد، وگرنه یک بخشِ کند بقیه را گروگان میگیرد. کار اصلیاش این است که پنجره را از ساعت دیوار جدا کند: پنجره وقتی بسته میشود که واترمارک از انتهایش بگذرد، نه وقتی که ساعت تو به آن رسیده. دو نوعش را باید از هم جدا کرد. واترمارک کامل یک تضمین است و فقط وقتی به دست میآید که بدانی هیچ فرستندهای نمیتواند دیرتر پیام قدیمی بفرستد. واترمارک اکتشافی یک تخمین است و میتواند غلط باشد. وقتی غلط شد، رویدادِ دیررس میرسد و باید از قبل تصمیم گرفته باشی: دورش بیندازی، پنجره را با allowed lateness باز نگه داری و نتیجه را اصلاح کنی، یا آن را به خروجی جانبی بفرستی.A watermark is a time, not a message: a number that travels with the stream and means “no event with an event time earlier than this will arrive from this input”. The number comes from the data itself, usually the largest event time seen so far minus a delay you choose. On a stream with several partitions an operator takes the smallest watermark among its inputs, otherwise one slow partition holds the rest hostage. Its real job is to pry windows away from the wall clock: a window closes when the watermark passes its end, not when your clock reaches it. Keep two kinds apart. A perfect watermark is a guarantee, available only when you know no sender can deliver an older message later. A heuristic watermark is an estimate and can be wrong. When it is wrong a late event arrives, and you must have decided in advance whether to drop it, hold the window open with allowed lateness and emit a correction, or route it to a side output.
event < wmهر رویدادی که زمان رویدادش از واترمارک عقبتر باشد، دیررس حساب میشودany event older than the watermark counts as late
max − delayواترمارک اکتشافی: بزرگترین زمانِ دیدهشده منهای یک حاشیهa heuristic watermark: largest time seen minus a delay
min over inputsدر چند ورودی، کمترین واترمارک ملاک است تا امن بمانیacross inputs, the lowest watermark wins, so you stay safe
واترمارک دقیقاً چه چیزی را قول میدهد؟What a watermark actually promises
سه چیز را قول نمیدهد و آدمها همان سه را اشتباه میگیرند. اول، نمیگوید داده کامل است؛ میگوید از این به بعد چیزی با زمان قدیمیتر نمیرسد، و اگر تخمینی باشد حتی همین هم قطعی نیست. دوم، دربارهٔ ترتیب چیزی نمیگوید: رویدادهایی که رسیدهاند میتوانند به هر ترتیبی رسیده باشند و پنجره باید خودش مرتب کند. سوم، دربارهٔ وجود داشتن حرفی ندارد؛ قولش دربارهٔ رسیدن است، و اگر منبع یک رویداد را پیش از فرستادن گم کرده باشد، هیچ واترمارکی آن را برنمیگرداند. چیزی که واقعاً میدهد یک مرز تصمیم است: از اینجا به بعد، پنجرهای که انتهایش عقبتر است را میشود بست و نتیجه را منتشر کرد.Three things it does not promise, and those are the three people misread. First, it does not say the data is complete; it says nothing older will arrive from now on, and if it is heuristic even that is not certain. Second, it says nothing about order: the events that did arrive may have arrived in any order, and the window must sort them out itself. Third, it says nothing about existence; the promise is about arrival, and if the source lost an event before sending it, no watermark brings it back. What it really gives you is a decision boundary: past this point, a window whose end is behind the watermark can be closed and its result published.
وقتی واترمارک دروغ میگویدWhen the watermark lies
هر واترمارک اکتشافی بالاخره یک بار اشتباه میشود؛ سؤال این نیست که اشتباه میکند یا نه، این است که آن روز چه اتفاقی میافتد. اگر رویداد دیررس را بیصدا دور بریزی، عددت همیشه کمی کم است و کسی هم نمیفهمد، چون هیچ خطایی رخ نداده. راه سالمتر این است که پنجره را بعد از بسته شدن هم مدتی زنده نگه داری، رویداد دیررس را بپذیری و نتیجهٔ اصلاحشده را با همان کلید پنجره دوباره منتشر کنی؛ پاییندست باید بداند که این یک بهروزرسانی است نه یک رکورد تازه. بهایش حافظه و تأخیر است، پس با عدد تصمیم بگیر: توزیع تأخیر واقعی را اندازه بگیر، حاشیه را از صدک بالای آن بردار، و درصد رویدادهای دیررس را پیوسته رصد کن.Every heuristic watermark is wrong eventually; the question is not whether but what happens on that day. Drop the late event silently and your number is always a little low, and nobody notices because nothing raised an error. The healthier path keeps the window alive for a while after it closes, accepts the late event, and emits the corrected result under the same window key; downstream has to know this is an update, not a new record. That costs memory and latency, so decide with numbers: measure the real lateness distribution, set the delay from a high percentile of it, and watch the percentage of late events continuously.
تلهٔ رایجThe usual trap
ساختن واترمارک از ساعت دیوار، مثلاً «الان منهای پنج دقیقه». تا وقتی جریان بهروز است جواب میدهد؛ روزی که یک تولیدکننده یک ساعت عقب بیفتد، همین قاعده یک ساعت رویداد سالم را دیررس اعلام میکند و دور میریزد. واترمارک باید از زمان رویداد بیاید، وگرنه معیارت تأخیر سیستم است نه تأخیر داده. همینطور یک بخشِ بیکار میتواند واترمارک کل کار را متوقف کند تا وقتی کسی آن را idle اعلام کند.Building the watermark from the wall clock — “now minus five minutes”. It works while the stream is current; the day one producer falls an hour behind, that rule declares an hour of healthy events late and discards them. A watermark has to come from event time, or you are measuring system lag instead of data lateness. And one idle partition can stall the watermark of the whole job until someone marks it idle.
جریانی که تمام نمیشود را چطور جمع بزنی؟How do you total a stream that never ends?
مثال روزمره، بدون اصطلاحThe everyday one, no jargon
نانوایی سر کوچه هر نیم ساعت گچ را برمیدارد و تعداد مشتریهای آن نیم ساعت را روی تخته مینویسد، بعد تخته را پاک میکند و از نو شروع میکند. آخر روز از روی همین عددهای تیکهتیکه میفهمد چه ساعتی شلوغ بوده. پنجرهبندی در جریان هم همین است: جریان را به بازههای کوچک میبری تا هر بازه یک عدد داشته باشد.The bakery on the corner writes the number of customers of each half hour on a chalkboard, then wipes it and starts again. At closing time the day is a row of small numbers, and from them the owner sees which hours were busy. Windowing a stream is the same move: cut the flow into slices so that each slice owns one number.
تشبیه و تصویر ذهنیThe analogy
همین نانوایی سه جور عدد میخواهد. یک: تعداد مشتری هر نیم ساعت، که بازهها روی هم نمیافتند و تخته بعد از هر بازه پاک میشود. دو: میانگین سه ساعت گذشته که هر نیم ساعت یک بار بهروز شود؛ اینجا بازهها روی هم میافتند، پس یک مشتری در چند عدد شمرده میشود و باید چند تختهٔ همزمان نگه داری. سه: «یک خرید»، که از لحظهٔ ورود مشتری شروع میشود و ده دقیقه بعد از آخرین خریدش تمام میشود؛ این بازه را از قبل نمیشود روی تقویم کشید، چون نمیدانی مشتری کِی میآید و کِی میرود، پس باید بازهها را همانطور که میآیند بسازی و بعداً به هم بچسبانی. هر سه جواب یک سؤال متفاوت است و انتخابش با توست.That same bakery wants three different numbers. One: customers per half hour, where the slices never overlap and the board is wiped after each one. Two: the average of the last three hours, refreshed every half hour, where slices do overlap, so one customer is counted in several numbers and you must keep several boards at once. Three: “one visit”, which starts when a customer walks in and ends ten minutes after their last purchase; you cannot draw that slice on a calendar in advance, because you do not know when they arrive or leave, so you build slices as they happen and glue them together later. Three different questions, and the choice is yours.
یک جریان، سه برش: بازههای پشت سر هم، بازههای همپوشان، و نشستی که مرزش را خود رویدادها میسازند.One stream, three cuts: back-to-back slices, overlapping slices, and a session whose edges the events themselves draw.
و بعدش، فنیAnd now, technically
پنجرهبندی روی زمان رویداد تعریف میشود: هر رویداد بر اساس زمان رویدادش در پنجرههایی مینشیند که مرز پایینشان بسته و مرز بالایشان باز است، نه بر اساس لحظهای که به دستت رسیده. چهار شکل اصلی هست. پنجرهٔ جهشی طول ثابت و بدون همپوشانی دارد. پنجرهٔ لغزان طول ثابت دارد ولی گامش کوچکتر است، پس پنجرهها همپوشانی دارند و با گام یکثانیه روی بازهٔ یکساعته، هر کلید هزاران پنجرهٔ زنده میسازد. پنجرهٔ کشویی با هر رویداد یا با یک تایمر دوباره حساب میشود و معمولاً با نگه داشتن یک بافر مرتبشده و دور ریختن کهنهها پیاده میشود. پنجرهٔ نشست طول ثابت ندارد؛ با هر رویداد باز میشود، بعد از یک شکافِ بیرویداد بسته میشود، و اگر رویدادی وسط شکاف برسد دو پنجره باید در هم ادغام شوند. خروجی هر پنجره وقتی منتشر میشود که واترمارک از انتهایش بگذرد، یا با یک تریگر زودتر، و بعد با allowed lateness میشود اصلاحش کرد. کلید همه چیز را عوض میکند: پنجره برای هر کلید جدا نگه داشته میشود، وگرنه همهٔ ترافیک در یک حالت میافتد.Windowing is defined on event time: an event lands in the windows whose start is closed and end open around its event time, not around its arrival. There are four shapes. A tumbling window has a fixed length and no overlap. A hopping window has a fixed length with a smaller step, so windows overlap, and an hour-long window stepping every second gives each key thousands of live windows. A sliding window is recomputed on each event or on a timer, usually by keeping a time-sorted buffer and evicting what has aged out. A session window has no fixed length: it opens with an event, closes after a gap with no events, and merges two windows when an event lands inside the gap. A window emits when the watermark passes its end, or earlier from a trigger, and can be corrected afterwards within allowed lateness. The key changes everything: windows are kept per key, otherwise all the traffic lands in one piece of state.
3600 windowsپنجرهٔ یکساعته با گام یکثانیه، برای هر کلید ۳۶۰۰ پنجرهٔ زنده میسازدan hour window stepping every second keeps 3,600 live windows per key
[start, end)مرز پایین بسته و مرز بالا باز، تا هیچ رویدادی دو بار شمرده نشودstart closed, end open, so no event is counted twice
1 state / keyحالت پنجره به ازای هر کلید نگه داشته میشود، نه یک جا برای همهwindow state is held per key, not in one place for everything
طول پنجره را از تصمیم بگیر، نه از دادهChoose the length from the decision, not from the data
رایجترین اشتباه این است که طول پنجره را از فرکانس رویدادها برداری، چون داده هر ثانیه میآید. سؤال درست این است: چند وقت یک بار کسی از این عدد تصمیم میگیرد؟ اگر تیم پشتیبانی هر ساعت یک بار به نمودار نگاه میکند، پنجرهٔ یکدقیقهای فقط نویز تولید میکند و هزینهٔ ذخیرهسازی را بالا میبرد. پنجرهٔ خیلی بلند هم بد است: عیب را با چند ساعت تأخیر نشان میدهد و همین تأخیر، هزینهٔ واقعی است. گام، جواب یک سؤال دیگر است: با گام کوچکتر عدد نرمتر و تازهتر میشود، ولی تعداد پنجرههای زنده به نسبت بزرگتر شدن بازه به گام بالا میرود. برای رفتار کاربر، پنجرهٔ نشست تقریباً همیشه از پنجرهٔ ثابت بهتر است، چون نشستهای آدمها به تقویم کاری ندارند.The commonest mistake is to take the window length from the event rate, because data arrives every second. The right question is: how often does someone make a decision from this number? If support looks at the chart once an hour, a one-minute window only produces noise and storage. Too long is also bad: it shows the fault hours late, and that delay is a real cost. The step answers a different question: a smaller step makes the number smoother and fresher, while the number of live windows grows with the ratio of length to step. For user behaviour, a session window is almost always better than a fixed one, because human sessions do not care about the calendar.
حالت، حافظه و جاهایی که میشکندState, memory, and where it breaks
هر پنجرهٔ باز یعنی حالت: فهرست رویدادها یا جمعهای جزئی، به ازای هر کلید. هزینه تقریباً خطی است با تعداد پنجرههای زنده ضرب در نرخ رویداد، و همینجا سه چیز میشکند. اول، کلید داغ: اگر یک کلید بیشتر ترافیک را بکشد، همهٔ پنجرههایش روی یک تسک مینشینند و بقیه بیکار میمانند. دوم، بازههای همپوشان با گام ریز: حافظه بیآنکه کسی متوجه شود چند برابر میشود و بعد از چند ساعت کارگر میافتد. سوم، نگه داشتن پنجرهها برای allowed lateness: هر دقیقهای که پنجره بیشتر زنده میماند، هم حافظه میخورد هم چکپوینت را سنگین میکند. علاجش نگه داشتن جمعهای جزئی بهجای رویدادهای خام، و تعیین یک عمر سخت برای هر پنجره است.Every open window is state: a list of events or a set of partial sums, per key. Cost is roughly linear in live windows times event rate, and three things break here. First, a hot key: if one key carries most of the traffic, all of its windows sit on one task while the others idle. Second, overlapping windows with a tiny step: memory multiplies with nobody watching, and the worker dies a few hours later. Third, holding windows open for allowed lateness: every extra minute a window lives costs memory and makes the checkpoint heavier. The cure is keeping partial aggregates instead of raw events, and giving every window a hard expiry.
زمان رویداد در برابر زمان پردازشEvent time versus processing time
زمان رویدادEvent time
زمان پردازشProcessing time
چه چیزی را میگویدWhat it says
کِی اتفاق افتادwhen it happened
کِی دیدیمشwhen we saw it
روی بازپخشOn replay
همان عدد، همان پنجرهsame numbers, same windows
عددها عوض میشوندthe numbers change
تأخیر نتیجهResult delay
تا واترمارک نرسد، نهwaits for the watermark
سر ساعت دیوارon the wall clock
به چه کاری میآیدGood for
عدد کسبوکارbusiness numbers
سلامت خود سیستمthe system's own health
تلهٔ رایجThe usual trap
شمردن پنجرههای زمان پردازش بهعنوان عدد کسبوکار. یک بار که مصرفکننده دو ساعت عقب بیفتد، آن دو ساعت در پنجرهٔ ساعت دیوارِ بعدی میریزد و نمودار یا یک قلهٔ دروغین نشان میدهد یا یک چاله، در حالی که هیچ اتفاقی در دنیای واقعی نیفتاده. عدد فروش باید از زمان رویداد بیاید؛ زمان پردازش برای اندازهگیری خود سیستم است، نه برای مشتری.Reading processing-time windows as business numbers. The day a consumer falls two hours behind, those two hours land in the following wall-clock window and the chart shows a fake spike or a fake hole, while nothing happened in the real world. Revenue numbers belong on event time; processing time measures the system, not the customer.