DDIA·واژه‌هاTERMS

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

۵5کدگذاری و تکاملEncoding and evolution

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

اگر جلد جعبهٔ پازل گم شود، تکه‌ها چه می‌شوند؟If the puzzle box lid is lost, what are the pieces?

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

پازل هزارتکه را با تصویر روی جلد می‌چینی. تکه‌ها به‌تنهایی فقط مقوای رنگی‌اند؛ جلد است که می‌گوید کدام تکه کجاست. اگر جلد را گم کنی، پازل سالم است ولی دیگر معنا ندارد. آورو هم بایت‌ها را با نسخهٔ اسکیمایش می‌فرستد؛ بدون آن اسکیما، داده فقط بایت است.You build a thousand-piece puzzle from the picture on the lid. The pieces alone are coloured cardboard; the lid is what tells you where each one goes. Lose the lid and the puzzle is intact but meaningless. Avro sends its bytes together with the schema that wrote them; without that schema the data is just bytes.

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

در یک کارگاه، هر سفارش را با نسخهٔ همان نقشه‌ای می‌فرستند که با آن ساخته شده. کارگر جدید نقشهٔ خودش را دارد و دو نقشه را کنار هم می‌گذارد: هر قطعه‌ای که در نقشهٔ سفارش هست و در نقشهٔ او نیست دور ریخته می‌شود، و هر قطعه‌ای که او می‌خواهد و در سفارش نیست با مقدار پیش‌فرض پر می‌شود. به همین دلیل تغییر نقشه لازم نیست همهٔ سفارش‌های قدیمی را بشکند، به شرطی که پیش‌فرض‌ها را از قبل نوشته باشی.In a workshop every order ships with the version of the drawing it was built from. The new worker has his own drawing and puts the two side by side: a part that is on the order's drawing and not on his is dropped, and a part he needs that the order lacks is filled in from a default. That is why a revised drawing need not break old orders, provided the defaults were written down in advance.

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

در Avro داده به‌صورت دودویی و فشرده نوشته می‌شود و هیچ برچسبی کنار فیلد‌ها نمی‌نشیند؛ در عوض اسکیمای نویسنده همراه داده می‌رود، در سربرگ فایل، یا شناسه‌اش در یک رجیستری است. خواننده اسکیمای خودش را دارد و کتابخانه این دو را تطبیق می‌دهد: فیلد‌های مشترک با نام خوانده می‌شوند، فیلد تازه‌ای که در داده نیست از پیش‌فرض خواننده پر می‌شود، و فیلد ناشناخته دور ریخته می‌شود. چون تطبیق بر اساس نام است، اسم فیلد مهم می‌شود؛ عوض کردن نام بدون alias یعنی از دست دادن داده. برای فایل‌های بزرگ و جریان‌های دادهٔ تحلیلی که اسکیما کم عوض می‌شود، این قالب جمع‌وجور و مناسب است.Avro writes data in a compact binary form with no tags beside the fields; instead the writer's schema travels with the data, in the file header or as an id in a registry. The reader has its own schema and the library resolves the two: shared fields are read by name, a new field missing from the data is filled from the reader's default, and an unknown field is dropped. Because the match is by name, field names matter; renaming one without an alias loses the data. For large files and analytics streams whose schema changes rarely, it is a compact and comfortable fit.

0 tagهیچ برچسبی روی سیم نیست؛ اسکیما کار برچسب را می‌کندno tags on the wire; the schema does that job
by nameتطبیق فیلد‌ها با نام انجام می‌شود، نه با شمارهfields are matched by name, not by number

تلهٔ رایجThe usual trap

عوض کردن نام فیلد بدون alias و تکیه بر این‌که «فقط اسم است». در Avro اسم خودِ هویت فیلد است؛ دادهٔ قدیمی با نام قبلی نوشته شده و خوانندهٔ تازه آن را پیدا نمی‌کند، پس فیلد بی‌صدا خالی می‌شود. برخلاف protobuf که شماره لنگر است، اینجا نام لنگر است.Renaming a field without an alias and trusting that “it is only a name”. In Avro the name is the identity; old data was written under the old name, the new reader never finds it, and the field comes back empty without a word. Unlike Protocol Buffers, where the number is the anchor, here the name is.

backward compatibility

سازگاری با گذشته فصل ۵Chapter 5 · دیدن در متنsee in context

کد تازه‌ات می‌تواند دادهٔ دیروز را بخواند؟Can the new code read what yesterday's code wrote?

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

پیام‌رسان گوشی‌ات را به‌روز می‌کنی، نسخهٔ تازه بالا می‌آید و همهٔ چت‌های سه سال پیش همان‌جا هستند، با همان عکس‌ها و همان ترتیب. هیچ‌چیز از دست نرفته، چون نسخهٔ تازه قالب کهنه را می‌فهمد و همان را می‌خواند. سازگاری با گذشته همین است: ابزار تازه، دادهٔ کهنه را می‌خواند.You update the messaging app on your phone, the new version opens, and every chat from the last three years is still there, photos and all. Nothing was lost, because the new version understands the old storage format. Backward compatibility is that same thing: new code, old data.

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

ادارهٔ پست قالب نشانی تازه‌ای با کد پستی راه می‌اندازد. سامانهٔ تازه باید نامه‌های قدیمی را هم که فقط خیابان رویشان نوشته شده، بخواند، وگرنه یک روز صبح نصف نامه‌ها روی زمین می‌مانند. اما نامه‌رسان قدیمی که هنوز کد پستی را نمی‌شناسد، از نامهٔ تازه سر در نمی‌آورد. پس در دورهٔ گذار، فرستنده باید هم کد را بنویسد و هم خیابان را، و اداره باید برای نامه‌های بی‌کد یک مسیر قدیمی زنده نگه دارد. سازگاری یک‌طرفه است: تازه‌فهمیدن آسان است، کهنه‌فهمیدن گران.The post office introduces a new address style built around postal codes. The new sorting system still has to read the old envelopes that carry nothing but a street name, or half the mail sits on the floor one morning. But the old carrier who does not know the new codes cannot make sense of a newly written envelope. So during the changeover the sender writes both the code and the street, and the office keeps a slow old route alive for uncoded mail. Compatibility runs one way at a time: teaching the new system the old format is cheap, teaching the old system the new one is not.

داده با کد قدیمData from old codeقالب کهنه روی دیسکold format on diskاستقرار کد تازهNew code shipsیکی‌یکی، نه یک‌جاone node at a timeخواندن دادهٔ کهنهNew reads oldسازگاری با گذشتهbackward compatibleنوشتن با قالب تازهWrites go newنود قدیمی می‌خواندold node must cope
مسیر یک استقرار تدریجی: دادهٔ کهنه سر جایش می‌ماند، کد تازه اول می‌آید، و هر دو جهت سازگاری در یک پنجره آزموده می‌شوند.The path of a rolling upgrade: old data stays put, new code arrives first, and both directions of compatibility get tested in one window.

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

سازگاری با گذشته یعنی کد تازه بتواند داده یا پیامی را بخواند که کد قدیمی نوشته است. جهت را با یک جمله به یاد بسپار: تازه، کهنه را می‌خواند. این تعریف در استقرار تدریجی حیاتی می‌شود، چون در یک rolling upgrade مدتی هر دو نسخه با هم زنده‌اند و هر کدام ممکن است دادهٔ دیگری را ببیند؛ جهت دیگر، سازگاری با آینده است و می‌گوید کد قدیمی باید پیام تازه را بخواند یا دست‌کم فیلد ناشناخته را نادیده بگیرد. مکانیزم اصلی در قالب‌های تگ‌دار مثل protobuf این است که فیلد ناشناخته را دور نریزد و حفظ کند تا در عبور بعدی گم نشود؛ در Avro همین کار با مقایسهٔ اسکیمای نویسنده و خواننده و مقدار پیش‌فرض انجام می‌شود. بهایش این است که حذف فیلد تبدیل به یک پروژهٔ چندمرحله‌ای می‌شود و هر مسیر خواندن قدیمی باید تا آخرین نسخه تست شود.Backward compatibility means new code can read data or messages written by old code. Fix the direction in one phrase: new reads old. That definition becomes critical during a rolling upgrade, because for a while both versions are alive and either may see the other's data; the other direction, forward compatibility, says old code must be able to read a new message or at least ignore fields it does not know. Tagged formats such as Protocol Buffers get the first direction by keeping unknown fields instead of dropping them, so they survive one more hop; Avro gets it by resolving the writer's schema against the reader's and filling in defaults. The bill arrives when you want to delete a field, which turns into a multi-step project, and when every old read path has to stay in the test matrix until the last version is gone.

N-1کد تازه باید دادهٔ یک نسخهٔ عقب را بخواندnew code must read data from one version back
reservedشماره یا نام فیلد حذف‌شده را رزرو کن تا کسی دوباره به کارش نبردreserve a removed field's tag so nobody reuses it
3×3اندازهٔ ماتریس تست وقتی سه نسخه را زنده نگه می‌داریthe test matrix when three versions must stay alive

چرا استقرار تدریجی هر دو جهت را می‌خواهدWhy a rolling upgrade needs both directions

در استقرار تدریجی، نسخه‌ها مدتی با هم زندگی می‌کنند: چند نود تازه بالا آمده‌اند و بقیه هنوز قدیمی‌اند. در این پنجره، نود تازه ممکن است داده‌ای را که نود قدیمی نوشته بخواند، و این همان سازگاری با گذشته است؛ و نود قدیمی ممکن است پیامی از نود تازه بگیرد، که سازگاری با آینده است. پس در عمل هر دو جهت لازم است، نه یکی. ترتیب عاقلانه این است که اول کد تازه را طوری بنویسی که هر دو قالب را بفهمد، بعد نوشتن را عوض کنی، و حذف خواندن قدیمی را به نسخهٔ بعد بسپاری.During a rolling upgrade the versions live side by side for a while: a few nodes run the new code while the rest still run the old. In that window a new node may read data an old node wrote, which is backward compatibility, and an old node may receive a message from a new node, which is forward compatibility. Both directions are needed in practice, not one. The sane order is to ship new code that understands both formats first, then switch what you write, and leave the removal of the old read path for the version after that.

بهای نگه داشتن گذشتهThe rent you pay for the past

هر نسخهٔ عقبی که پشتیبانی می‌کنی، یک شاخهٔ کد و یک ردیف تست است. اگر سه نسخه را زنده نگه داری، ماتریس تست سه در سه می‌شود و هر تغییر کوچک باید در همهٔ خانه‌ها سبز شود. به همین دلیل تیم‌های جدی برای هر قالب یک سیاست مکتوب دارند: چه چیزی اضافه می‌شود، چه چیزی هرگز حذف نمی‌شود، و نام‌ها و شماره‌های بازنشسته کجا فهرست می‌شوند. سازگاری رایگان نیست؛ اجارهٔ ماهانه دارد.Every version back that you support is a branch of code and a row in the test matrix. Keep three versions alive and the matrix becomes three by three, and every small change has to go green in all of those cells. That is why serious teams keep a written policy per format: what may be added, what is never removed, and where retired names and numbers are listed. Compatibility is not free; it charges rent.

تلهٔ رایجThe usual trap

این‌که فقط یک جهت را تست کنی و خیالت راحت شود. فیلد تازه را بدون پیش‌فرض اضافه می‌کنی، تست‌های کد تازه با دادهٔ قدیمی سبز می‌شوند، اما در حین استقرار نود‌های قدیمی پیام تازه را می‌گیرند و در کدگشایی می‌ترکند؛ بدتر، برگشت به عقب هم ممکن نیست، چون دادهٔ تازه دیگر برای کد قدیمی خوانا نیست. سازگاری با گذشته نصف کار است.Testing one direction and calling it done. You add a field with no default, the new code's tests against old data go green, and then during the rollout the old nodes receive a new message and blow up in the decoder. Worse, rolling back is no longer possible, because the new data is unreadable to the old code. Backward compatibility is half the job.

چرا عکس در پیام متنی به حروف تبدیل می‌شود؟Why does a photo turn into letters inside a text message?

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

می‌خواهی عکس بلیت را جایی بفرستی که فقط متن قبول می‌کند. عکس را به رشته‌ای از حروف و رقم تبدیل می‌کنی، می‌فرستی، و طرف مقابل همان رشته را برمی‌گرداند به عکس. حجم حدود یک‌سوم بیشتر شده، ولی از مجرای متنی رد شده. بیس ۶۴ همین تبدیل است: هر سه بایت به چهار نویسه.You want to send a photo of your ticket somewhere that only accepts text. You turn the photo into a string of letters and digits, send it, and the other side turns that string back into the photo. It is about a third bigger, but it got through a text-only channel. Base64 is that conversion: three bytes into four characters.

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

مثل این است که برای فرستادن یک ظرف شیشه‌ای در پاکت‌نامه، آن را در کاغذ حباب‌دار بپیچی. پاکت جا برای شیشه ندارد و کاغذ جا برای شیشه دارد؛ پس چیزی وسط اضافه می‌شود که فقط برای عبور است. آن کاغذ نه شیشه را محکم‌تر می‌کند و نه کوچک‌تر؛ فقط قابل حملش می‌کند. و هر کسی پاکت را باز کند، شیشه را می‌بیند.It is like wrapping a glass jar in bubble wrap so it fits through a letter slot. The slot has no room for a jar, the wrap does; so something is added in the middle purely to get it through. The wrap does not make the jar stronger or smaller, only carriable. And anyone who opens the envelope sees the jar.

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

بیس ۶۴ هر سه بایت را به چهار نویسه از یک الفبای ۶۴تایی تبدیل می‌کند: ۲۶ حرف بزرگ، ۲۶ حرف کوچک، ده رقم و دو علامت. چون هر نویسه شش بیت را نگه می‌دارد و هر بایت هشت بیت است، خروجی حدود ۳۳ درصد بزرگ‌تر می‌شود و با علامت مساوی برای پر کردن تمام می‌شود. نسخهٔ URL-safe دو علامت آخر را عوض می‌کند تا در آدرس‌ها و توکن‌ها دردسر نسازد. کاربرد‌های رایجش جای‌گذاری بایت در JSON، بخش payload توکن‌های JWT، و data URI در HTML است. کدگشایی‌اش برای هر کسی ممکن است، پس هیچ رازی را پنهان نمی‌کند و هیچ حجمی را هم کم نمی‌کند.Base64 turns every three bytes into four characters from a 64-symbol alphabet: 26 capitals, 26 lowercase letters, ten digits and two signs. Because each character carries six bits and each byte eight, the output is about 33 percent larger, and it is padded with an equals sign to finish. The URL-safe variant swaps the last two symbols so links and tokens do not break. Common uses are embedding bytes in JSON, the payload segment of a JWT, and data URIs in HTML. Anyone can decode it, so it hides no secret and shrinks no payload.

تلهٔ رایجThe usual trap

گرفتن بیس ۶۴ به جای رمزنگاری یا فشرده‌سازی. کسی توکن را «رمز» می‌کند یا عکس را «فشرده» می‌کند و بعد تعجب می‌کند که حجم بیشتر شده و هر کسی با یک دستور محتوا را می‌خواند. بیس ۶۴ فقط لباس متن است، نه قفل و نه چلاندن.Mistaking Base64 for encryption or compression. Someone “encrypts” a token or “compresses” an image and is then surprised that it got bigger and that anyone with one command can read the contents. Base64 is a change of clothes, not a lock and not a press.

durable execution

اجرای پایدار فصل ۵Chapter 5 · دیدن در متنsee in context

وسط یک کار طولانی سرور می‌افتد؛ از کجا ادامه می‌دهی؟The server dies mid-job. Where do you resume?

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

دارویی که باید سه نوبت خورده شود: صبح، ظهر، شب. اگر ظهر یادت برود، شب از اول شروع نمی‌کنی؛ از نوبتی که مانده ادامه می‌دهی. برگهٔ یادداشت روی یخچال همان چیزی است که می‌گوید کدام نوبت انجام شده. اجرای پایدار هم گردش‌کار را قدم‌به‌قدم ثبت می‌کند تا بعد از هر خرابی از همان‌جا ادامه دهد.Medicine you have to take three times: morning, noon, night. If you miss the noon dose you do not start over at night, you continue from the dose that is left. The note stuck to the fridge is what tells you which dose is done. Durable execution records a workflow step by step so that after any crash it continues from there.

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

مثل سفارش ساخت یک کابینت است که شش مرحله دارد: اندازه‌گیری، برش، رنگ، مونتاژ، تحویل، نصب. اگر کارگاه وسط رنگ آتش بگیرد، نجار از اندازه‌گیری شروع نمی‌کند؛ دفتر کارگاه می‌گوید کدام مرحله تمام شده و از مرحلهٔ بعد ادامه می‌دهد. همان دفتر می‌گوید هر مرحله چقدر طول کشیده و چه کسی تحویلش داده. بدون دفتر، هر خرابی یعنی شروع از صفر و پرداخت دوبارهٔ همهٔ هزینه‌ها.It is like an order to build a cabinet with six stages: measure, cut, paint, assemble, deliver, install. If the workshop catches fire during painting, the carpenter does not start from measuring; the workshop book says which stage is finished and work continues from the next one. That same book says how long each stage took and who signed it off. Without the book, every failure means starting from zero and paying all the costs twice.

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

موتور اجرای پایدار، کد گردش‌کار را مثل یک برنامهٔ معمولی اجرا می‌کند، ولی هر قدم را در یک لاگ پایدار ثبت می‌کند: نتیجهٔ هر فعالیت، زمان‌سنج‌ها، و ورودی‌هایی که به کد رسیده. اگر پروسه بمیرد، موتور روی ماشین دیگری کد را از اول اجرا می‌کند و به‌جای انجام دوبارهٔ کار‌ها، نتیجه‌های ثبت‌شده را از لاگ برمی‌گرداند تا کد به همان نقطه برسد؛ به این می‌گویند replay. به همین دلیل کد گردش‌کار باید قطعی باشد: تصادفی‌سازی، زمان فعلی و تماس مستقیم با دنیای بیرون باید داخل فعالیت‌های ثبت‌شده بروند. اثر‌های بیرونی هم باید idempotent باشند، وگرنه یک replay دوباره ایمیل می‌فرستد یا دوباره پول جابه‌جا می‌کند.A durable execution engine runs the workflow code like an ordinary program but records every step in a durable log: the result of each activity, the timers, and the inputs that reached the code. If the process dies, the engine runs the code again on another machine and, instead of redoing the work, feeds back the recorded results so the code reaches the same point; that is called replay. That is why workflow code has to be deterministic: randomness, the current time and direct calls to the outside world belong inside recorded activities. External effects must be idempotent too, or a replay sends the email again or moves the money again.

1 stepهر قدم پیش از رفتن به قدم بعد در لاگ پایدار ثبت می‌شودevery step is recorded before the next one starts
replayبعد از خرابی، تاریخچه بازپخش می‌شود، نه کار‌هاafter a crash the history is replayed, not the work
0 اثرکد گردش‌کار نباید اثر بیرونی مستقیم داشته باشدworkflow code should have no direct external effects

تلهٔ رایجThe usual trap

گذاشتن یک اثر بیرونی مستقیم داخل کد گردش‌کار، به‌جای فعالیت. کد در replay دوباره اجرا می‌شود و آن خط دوباره اجرا می‌شود؛ نتیجه دو ایمیل یا دو پرداخت است، در حالی که تاریخچه می‌گوید همه‌چیز یک بار بوده.Putting an external effect directly in workflow code instead of an activity. The code runs again during replay and that line runs again; the result is two emails or two payments, while the history says everything happened once.

یک ساختار در حافظه چطور از سیم رد می‌شود؟How does a struct in memory get across a wire?

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

پشت تلفن می‌خواهی شمارهٔ کارت بانکی را برای خواهرت بخوانی. خط خش دارد، پس رقم‌ها را کلمه‌به‌کلمه می‌گویی: «صفر، نه، یک، دو...». او همان کلمه‌ها را برمی‌گرداند به رقم و روی کاغذ می‌نویسد. کدگذاری همین است: داده را به شکلی درمی‌آوری که از این کانال رد شود، و طرف مقابل دوباره از شکل به داده برمی‌گردد.You are reading your card number to your sister over the phone. The line is bad, so you say the digits as words: zero, nine, one, two. She turns the same words back into digits and writes them down. That is encoding: you put the data into a shape the channel can carry, and the other side turns the shape back into data.

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

فرستادن یک چراغ رومیزی به شهر دیگر را تصور کن. می‌توانی آن را با روزنامه بپیچی و روی جعبه با ماژیک بنویسی «شکستنی، این طرف بالا»، یا در فوم قالب‌گیری‌شده بگذاری و جعبه را با نوار و برچسب ببندی. اولی را هر کسی باز می‌کند و می‌فهمد چیست، ولی جا و وزن بیشتری می‌برد و اگر باران بیاید ماژیک پاک می‌شود. دومی محکم و جمع‌وجور است، ولی برای باز کردنش باید بدانی از کدام طرف شروع کنی؛ جعبهٔ بی‌راهنما فقط یک مکعب چسب‌خورده است. کدگذاری هم همین انتخاب را به تو می‌دهد: خوانایی در برابر حجم و استحکام، و هیچ‌کدام رایگان نیست.Picture shipping a desk lamp to another city. You can wrap it in newspaper and write “fragile, this side up” on the box with a marker, or seat it in moulded foam and seal the box with tape and a barcode label. Anyone can open the first one and see what it is, but it takes more room, weighs more, and one rainstorm erases the marker. The second is compact and survives handling, but to unpack it you have to know which end to start from; an unlabelled box is just a taped cube. Encoding offers you the same choice: readability against size and robustness, and neither is free.

شیء در برنامهObject in codeفیلد و نوعfields and typesکدگذاریEncodeبه بایتinto bytesروی سیمOn the wireقرارداد مشترکa shared contractکدگشاییDecodeشیء دوبارهobject again
کدگذاری پلی است بین ساختارِ درون برنامه و بایت‌هایی که شبکه یا دیسک می‌توانند حمل کنند.Encoding is the bridge between a structure inside the program and the bytes a network or a disk can carry.

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

کدگذاری یعنی قالب دقیق بایت‌ها: کدام بایت اول بیاید، عدد چطور نوشته شود، رشته کجا تمام شود. دو خانوادهٔ بزرگ داری. قالب‌های متنی مثل JSON و XML و CSV را انسان هم می‌خواند و هر زبانی پارسشان می‌کند، ولی پرحجم‌اند، نوع‌ها را دقیق نگه نمی‌دارند و عدد بزرگ را گاهی به اعشار شناور تبدیل می‌کنند. قالب‌های دودویی مثل protobuf و Avro و Thrift حجم کمتری می‌برند و مرز فیلد‌ها را با اسکیما می‌شناسند، به بهای این‌که بدون اسکیما فقط یک تودهٔ بایت‌اند. کدگذاری خودش فشرده‌سازی یا رمزنگاری نیست، هرچند هر دو روی خروجی‌اش می‌نشینند. قالب‌های وابسته به زبان، مثل سریال‌سازی جاوا و pickle، ارزان‌ترین راه شروع و بد‌ترین راه ماندن‌اند: به نسخهٔ کتابخانه و زبان گره می‌خورند و پارس کردن ورودی ناشناخته با آن‌ها سوراخ امنیتی است.Encoding is the exact byte layout: which byte comes first, how a number is written, where a string ends. There are two big families. Text formats such as JSON, XML and CSV are readable by humans and parsed by every language, but they are bulky, keep types loosely, and sometimes turn a large integer into a float. Binary formats such as Protocol Buffers, Avro and Thrift are smaller and know field boundaries from a schema, at the price of being an opaque pile of bytes without one. Encoding is not compression or encryption, though both can sit on top of its output. Language-specific formats, such as Java serialization and Python's pickle, are the cheapest way to start and the worst way to stay: they tie the data to a library version and a language, and decoding untrusted bytes with them is a security hole.

2–5×حجم معمول JSON در برابر قالب دودویی معادلhow much bigger JSON usually is than the equivalent binary
UTF-8نویسه را بایت می‌کند و بایت‌های ASCII را دست‌نخورده می‌گذاردturns characters into bytes, leaving ASCII bytes untouched
8 bitsواحد شمارش قالب؛ مرز فیلد‌ها با همین واحد سنجیده می‌شودthe unit every format counts in, which is how field boundaries are measured

خوانا یا فشردهReadable or compact

انتخاب بین متن و دودویی بیشتر از حجم، دربارهٔ این است که می‌خواهی موقع خرابی چه چیزی ببینی. با JSON می‌توانی پیام را در لاگ و ترمینال بخوانی و با یک ابزار ساده تست کنی؛ با protobuf باید ابزار و اسکیمای همان نسخه را داشته باشی. برای پیام‌های کوچک و سرویس‌های داخلی، خوانایی معمولاً می‌ارزد. وقتی پیام‌ها بزرگ‌اند یا تعدادشان زیاد است، چند برابر شدن حجم یعنی پهنای باند و هزینهٔ شبکه، و آن‌جاست که قالب دودویی خودش را پس می‌دهد. قاعدهٔ سرانگشتی این است: اول با متن شروع کن، و وقتی اندازه یا بار واقعاً درد شد، سنجیده جابه‌جا شو.The choice between text and binary is less about size than about what you want to see when something breaks. With JSON you can read the message in a log or a terminal and poke it with a simple tool; with Protocol Buffers you need the tool and the exact schema version. For small messages and internal services, readability usually wins. When messages are large or numerous, a few times the bytes means bandwidth and network cost, and that is where a binary format pays for itself. The rule of thumb: start with text, and move when size or volume actually hurts.

کدگذاری نویسه یا کدگذاری داده؟Character encoding or data encoding?

کدگذاری نویسهCharacter encodingکدگذاری دادهData encoding
چه چیزی را تبدیل می‌کندWhat it convertsنویسه به بایت، مثل UTF-8characters to bytes, as UTF-8 doesساختار به بایت، مثل protobufa structure to bytes, as protobuf does
کجا می‌بینی‌اشWhere you meet itفایل متنی، هدر HTTP، ترمینالa text file, an HTTP header, a terminalپیام بین سرویس‌ها، رکورد روی دیسکa message between services, a record on disk
اشتباه رایجThe usual mix-upفکر کردن UTF-8 همان سریال‌سازی استthinking UTF-8 is serializationفکر کردن این‌که قالب یعنی رمزنگاریthinking a format means encryption

تلهٔ رایجThe usual trap

این‌که قالب را در ذهن نگه داری و در قرارداد ننویسی. دو سرویس با JSON حرف می‌زنند و یکی زمان را ثانیه می‌فرستد و دیگری میلی‌ثانیه، یا یکی شناسه را رشته می‌فرستد و دیگری عدد؛ هیچ خطایی هم رخ نمی‌دهد، فقط داده هزار برابر یا خالی می‌شود. کدگذاری بدون قرارداد مکتوب توافق نیست، شانس است.Keeping the format in your head instead of in a contract. Two services speak JSON, one sends time in seconds and the other in milliseconds, or one sends an id as a string and the other as a number; nothing raises an error, the data just comes out a thousand times too large or empty. Encoding without a written contract is not agreement, it is luck.

اسم فیلد را عوض کردی؛ چرا داده سالم ماند؟You renamed a field and nothing broke. Why?

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

صندوق پست آپارتمان را نگاه کن. هر واحد یک شمارهٔ ثابت دارد و روی درِ صندوق، اسم ساکن نوشته شده. همسایه عوض می‌شود، اسم را پاک می‌کنی و اسم تازه می‌نویسی؛ ولی شمارهٔ صندوق همان می‌ماند و نامه‌رسان همچنان با شماره کار می‌کند. در پروتکل بافر هم فیلد‌ها با همین شماره شناخته می‌شوند، نه با اسمی که در کد گذاشته‌ای.Look at the mailboxes in an apartment building. Every flat has a fixed number, and the occupant's name is written on the door. A neighbour moves out, you wipe the name and write a new one, but the box keeps its number and the postman keeps working by number. Protocol Buffers works the same way: a field is known by its number, not by the name you gave it in code.

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

فکر کن مدیر ساختمان بخواهد شماره‌ها را مرتب کند و از رقم به حرف برود. هر فهرستی که در ساختمان هست، از انبار تا دفتر نامه‌رسان، یک‌شبه بی‌فایده می‌شود؛ برای همین شماره‌ها را دست نمی‌زنند. اسم روی برچسب را هر وقت بخواهند عوض می‌کنند، چون هیچ فهرست ماشینی به آن وابسته نیست. حالا خطر اصلی را ببین: اگر صندوق شمارهٔ ۷ خالی شود و تو همان شماره را به انباری بدهی، نامه‌های قدیمیِ صاحب‌خانهٔ قبلی هم می‌روند داخل انبار. شماره‌ای که یک بار به یک معنا داده شد، دیگر مال همان معناست.Now imagine the building manager wants to renumber the boxes and switch from digits to letters. Every list in the building, from the storeroom to the postman's notebook, is useless overnight; that is why nobody renumbers. The name on the label, on the other hand, can change whenever they like, because no machine list depends on it. Now watch the real danger: if box number 7 empties out and you hand that same number to a storeroom, the previous tenant's old letters go into the storeroom too. A number that has once meant one thing belongs to that thing forever.

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

در پروتکل بافر، هر فیلد روی سیم با یک شماره و یک نوع سیمی مشخص می‌شود؛ این جفت با هم در ابتدای هر مقدار می‌آید. شماره‌های ۱ تا ۱۵ با نوعشان در یک بایت جا می‌شوند، چون شماره بیت‌های بالا را می‌گیرد و نوع سیمی سه بیت پایین را؛ از ۱۶ به بعد دو بایت می‌خواهند. همان یک بایت است که باعث می‌شود عوض کردن نام فیلد هیچ اثری روی دادهٔ ذخیره‌شده نداشته باشد: کد تو اسم را عوض می‌کند، ولی بایت‌های روی دیسک همچنان شماره را حمل می‌کنند. افزودن فیلدی با شمارهٔ تازه که کسی استفاده نکرده سازگار است؛ خوانندهٔ قدیمی آن را نادیده می‌گیرد و خوانندهٔ تازه در نبودش مقدار پیش‌فرض می‌گذارد. حذف فیلد فقط وقتی بی‌خطر است که شماره‌اش را برای همیشه رزرو کنی. اگر شماره‌ای را به فیلدی با نوع دیگر بدهی، خوانندهٔ قدیمی بایت‌ها را با نوع قبلی می‌خواند و مقداری بی‌معنا می‌سازد، بی‌آنکه خطایی ببیند.In Protocol Buffers, each field is identified on the wire by a number plus a wire type, and that pair travels in front of every value. Tags 1–15 fit in a single byte with their type, because the number takes the high bits and the three-bit wire type the low ones; from 16 up they need two bytes. That byte is why renaming a field never touches stored data: your code changes the name, the bytes on disk still carry the number. Adding a field with a fresh number is compatible: an old reader skips it, and a new reader sees the default when it is absent. Removing a field is safe only if you reserve its number forever. Give that number to a field of another type and an old reader parses the bytes with the old type, producing a meaningless value without raising anything.

1–15شماره‌هایی که با نوعشان در یک بایت جا می‌شوندtags that share one byte with their wire type
16+از اینجا به بعد هر شماره دو بایت می‌خواهدfrom here on a tag costs two bytes
19000–19999بازه‌ای که خود پروتکل برای خودش رزرو کرده استthe range the protocol reserves for itself

تلهٔ رایجThe usual trap

حذف یک فیلد و استفادهٔ دوباره از شماره‌اش. کد کامپایل می‌شود، تست‌ها سبزند، و ماه‌ها بعد ردیف‌های قدیمی با نوع تازه خوانده می‌شوند و عددی بی‌معنا سر از گزارش‌ها درمی‌آورد. راهش reserved است: شماره و نام فیلد حذف‌شده را در همان تعریف نگه دار تا کامپایلر جلوی تو را بگیرد.Deleting a field and reusing its number. The code compiles, the tests are green, and months later old rows are read as the new type and a meaningless number surfaces in reports. The fix is reserved: keep the dead field's number and name in the definition so the compiler stops you.

idempotency key

کلید تکرارناپذیری فصل ۵Chapter 5 · دیدن در متنsee in context

دکمه را دو بار زدی؛ حالا دو بار پول کم می‌شود؟You pressed pay twice. Are you charged twice?

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

کاپشن را به خشکشویی می‌دهی و یک برگهٔ شماره‌دار می‌گیری. فردا دوباره سر می‌زنی و همان برگه را نشان می‌دهی؛ صاحب مغازه شماره را در دفترش پیدا می‌کند و می‌گوید «این را دیروز گرفتم»، پس دوباره نمی‌شویدش. کلید تکرارناپذیری همان شمارهٔ برگه است: با هر تلاش تازه، طرف مقابل می‌فهمد این همان درخواست قبلی است.You drop a coat at the dry cleaner and get a numbered slip. The next day you come back with the same slip; the owner finds the number in his book and says he already took that one in, so he does not wash it twice. An idempotency key is that number: on every new attempt the other side can tell this is the same request as before.

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

حالا تصور کن مغازه دو کارمند دارد و یک دفتر مشترک. مشتری برگه را می‌آورد، کارمند اول دفتر را نگاه می‌کند، اگر شماره هست همان جواب قبلی را می‌دهد، وگرنه شماره را می‌نویسد و کار را شروع می‌کند. دو چیز اینجا حیاتی است. اول، شماره باید از سمت مشتری بیاید؛ اگر مغازه خودش شماره بدهد، مشتریِ دوباره‌آمده شمارهٔ تازه‌ای می‌گیرد و دو بار حساب می‌شود. دوم، نوشتن در دفتر باید پیش از شروع کار باشد، وگرنه اگر وسط کار برق برود، کار انجام شده ولی ردی در دفتر نیست. و دفتر باید مدتی نگه داشته شود، چون مشتری ممکن است فردا برگردد.Now imagine the shop has two clerks and one shared book. The customer brings the slip, the clerk checks the book, hands back the earlier answer if the number is there, and otherwise writes the number down and starts the work. Two things matter here. First, the number has to come from the customer; if the shop issues its own number, a returning customer gets a fresh one and is charged twice. Second, the entry has to be written before the work starts, or a power cut mid-wash leaves the work done with no record of it. And the book has to be kept for a while, because the customer may come back tomorrow.

بدون کلیدNo keyتلاش دوباره، سفارش دومa retry is a second orderکارت دو بار کم می‌شودthe card is charged twiceسرور راهی برای تشخیص نداردthe server cannot tell them apartبا کلیدWith a keyکلید یکی، اثر یکیone key, one effectتلاش دوباره همان جواب اولthe retry gets the first answerنتیجه باید ذخیره بماندthe result must be stored
بدون کلید، هر تلاش دوباره یک عملیات تازه است؛ با کلید، تلاش دوباره همان نتیجهٔ اول را پس می‌دهد.Without a key, every retry is a new operation; with one, the retry gets the first result back.

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

کلید تکرارناپذیری یک رشتهٔ یکتا است که کلاینت می‌سازد و همراه درخواست می‌فرستد؛ معمولاً یک UUID که یک بار برای هر عملیات منطقی تولید می‌شود. سرور پیش از انجام کار، کلید را در جدولی با ایندکس یکتا درج می‌کند و نتیجه یا نشانهٔ «در حال انجام» را کنارش نگه می‌دارد؛ این درج و اثر باید در یک تراکنش باشند وگرنه بین چک و نوشتن شکافی می‌ماند که دو درخواست همزمان از آن رد می‌شوند. درخواست تکراری همان پاسخ ذخیره‌شده را می‌گیرد، یا اگر کار هنوز تمام نشده یک پاسخ «در حال پردازش» با کد مناسب. کلید باید به دامنه بسته شود، یعنی معلوم باشد به کدام حساب و کدام endpoint تعلق دارد، و پنجرهٔ نگه‌داری داشته باشد، چون جدول بی‌پایان رشد می‌کند. با این کار تحویل at-least-once به اثر exactly-once می‌رسد، بدون این‌که لازم باشد شبکه معجزه کند.An idempotency key is a unique string the client generates and sends with the request, usually a UUID created once per logical operation. Before doing the work, the server inserts the key into a table with a unique index and stores the result, or a marker that the work is in progress, beside it; that insert and the effect must share one transaction, otherwise a gap opens between the check and the write and two concurrent requests pass through it. A duplicate request gets the stored response, or a “still processing” answer with a sensible status code while the first one finishes. The key should be scoped, so it is clear which account and which endpoint it belongs to, and it should have a retention window, because an unbounded table grows forever. Done this way, at-least-once delivery produces exactly-once effects without asking the network to perform a miracle.

1 keyبه ازای هر درخواست منطقی، نه هر تلاشper logical request, not per attempt
unique indexهمان چیزی که دو درخواست همزمان را از هم جدا می‌کندwhat keeps two concurrent requests from both getting through
24 hپنجرهٔ نگه‌داری معمول؛ بعد از آن کلید پاک می‌شودa common retention window; after it the key is dropped

چرا کلید باید از کلاینت بیایدWhy the key has to come from the client

اگر سرور خودش شناسه بسازد، هر تلاش تازه یک شناسهٔ تازه می‌گیرد و تکرار هرگز دیده نمی‌شود؛ شناسه فقط وقتی معنا دارد که در دو تلاش یکسان بماند، و تنها کسی که می‌داند دو تلاش یکی هستند، کلاینت است. همین موضوع یک قرارداد با کلاینت می‌سازد: او باید کلید را پیش از اولین ارسال بسازد و در همهٔ تلاش‌های دوباره همان را بفرستد. اگر کتابخانهٔ HTTP خودش هنگام retry کلید تازه بگذارد، کل کار بی‌اثر می‌شود و بد‌ترین حالتش این است که فکر کنی محافظت داری.If the server mints the id itself, every fresh attempt gets a fresh id and the duplicate is never seen; an id only means something if it stays the same across two attempts, and the only party that knows the two attempts are the same is the client. That turns into a contract: the client must create the key before the first send and reuse it on every retry. If the HTTP library quietly generates a new key while retrying, the whole mechanism is void, and the worst version of that is believing you are protected.

پنجرهٔ نگه‌داری و اندازهٔ جدولThe retention window and table size

جدول کلید‌ها بی‌نهایت رشد می‌کند، پس هر کلید یک تاریخ انقضا دارد و بعد از آن پاک می‌شود؛ پنجره را بر اساس این انتخاب کن که یک کلاینت حداکثر چقدر می‌تواند دیر دوباره تلاش کند. اگر کلید را خیلی زود پاک کنی، تلاش دیرهنگام دوباره اثر می‌گذارد؛ اگر خیلی دیر، جدول و ایندکس یکتایش به یکی از بزرگ‌ترین جدول‌های سیستم تبدیل می‌شود. بعضی سیستم‌ها به‌جای نگه‌داشتن همهٔ کلید‌ها، فقط نتیجهٔ عملیات حساس را نگه می‌دارند و بقیه را با یک سیاست ساده پاک می‌کنند.The key table grows without bound, so every key carries an expiry and is deleted after it; pick the window from how late a client could possibly retry. Delete the key too early and a late retry takes effect a second time; delete it too late and the table plus its unique index becomes one of the largest tables in the system. Some systems store only the results of the sensitive operations instead of every key, and clear the rest with a simple policy.

تلهٔ رایجThe usual trap

ساختن کلید در سمت سرور یا داخل حلقهٔ retry. اگر کلاینت با هر تلاش یک UUID تازه بسازد، سرور هرگز تکرار را نمی‌بیند و همان کار دو بار انجام می‌شود؛ و اگر چک کردن کلید و ثبت اثر در یک تراکنش نباشند، دو درخواست همزمان هر دو از چک رد می‌شوند. کلید باید از بیرون بیاید، یک بار ساخته شود و همراه همان درخواست تکرار شود.Minting the key on the server, or inside the retry loop. If the client makes a fresh UUID per attempt the server never sees a duplicate and the work happens twice; and if the key check and the effect are not in one transaction, two concurrent requests both pass the check. The key has to come from outside, be created once, and travel with that same request.

Protocol Buffers

بافرهای پروتکل فصل ۵Chapter 5 · دیدن در متنsee in context

چرا اسم فیلد را عوض می‌کنی و هیچ‌چیز نمی‌شکند؟Rename a field and nothing breaks. Why?

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

فرم واریز بانکی را پر می‌کنی. روی فرم خانه‌های شماره‌دار است: خانهٔ ۱ مبلغ، خانهٔ ۲ شمارهٔ حساب. مهم نیست کدام شعبه بروی یا کارمند فرم را با چه لهجه‌ای بخواند؛ او شمارهٔ خانه را می‌بیند، نه برچسبش را. اگر نسخهٔ تازهٔ فرم خانهٔ ۵ را اضافه کند، کارمند قدیمی نگاهش نمی‌کند و بقیه را می‌خواند. بافر‌های پروتکل همان فرم شماره‌دار است، با یک فایل قرارداد که از رویش کد می‌سازند.You fill in a bank deposit slip. The boxes are numbered: box 1 is the amount, box 2 the account number. It does not matter which branch you visit or how the clerk pronounces the labels; the clerk reads the box number, not its title. If a new version of the form adds box 5, the old clerk skips it and reads the rest. Protocol Buffers is that numbered form, plus a contract file that generates the code for you.

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

حالا فرض کن همهٔ شعبه‌ها فرم را از یک چاپخانه می‌گیرند و چاپخانه یک دفتر دارد که می‌گوید خانهٔ ۲ همیشه شمارهٔ حساب است. اگر یک روز دفتر را عوض کنند و خانهٔ ۲ را مبلغ بگذارند، هیچ چاپخانه‌ای خطا نمی‌دهد، ولی هر کارمندی که فرم قدیمی را بلد است، مبلغ را به جای شمارهٔ حساب می‌خواند و پول به جای اشتباه می‌رود. فرم جمع‌وجور و سریع است و سال‌ها بدون تغییر می‌ماند، به شرطی که شماره‌ها را مقدس بدانی و هیچ‌وقت معنایشان را عوض نکنی.Now suppose every branch gets its forms from one printer, and the printer keeps a ledger saying box 2 is always the account number. If someone edits the ledger and makes box 2 the amount, no press jams, but every clerk who knows the old form reads the amount as an account number and the money goes to the wrong place. The form is compact and fast and lasts for years, as long as you treat the numbers as sacred and never change what they mean.

فایل قرارداد.proto fileنام و شمارهnames and numbersتولید کدCodegenکلاس هر زبانa class per languageکدگذاریEncodeتگ و مقدارtag and valueکدگشاییDecodeتگ ناشناخته، ردunknown tag skipped
از فایل قرارداد تا کد تولیدی و پیام روی سیم: نام فیلد در مسیر حذف می‌شود و فقط شماره می‌ماند.From the contract file to generated code and the bytes on the wire: field names drop out of the path and only numbers remain.

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

در protobuf هر فیلد یک شماره دارد و روی سیم، اسم فیلد اصلاً نوشته نمی‌شود؛ آنچه می‌رود شماره به‌همراه نوع سیم و مقدار است. عدد‌ها به‌صورت varint کد می‌شوند، یعنی عدد کوچک یک بایت و عدد بزرگ چند بایت می‌برد؛ رشته‌ها و پیام‌های تودرتو با طول مشخص و پشت سر هم می‌آیند. کد خواندن و نوشتن را ابزار از فایل قرارداد می‌سازد، پس هر دو طرف یک قرارداد تایپ‌دار دارند و اشتباه نوع در زمان کامپایل گرفته می‌شود. فیلدی که خواننده نمی‌شناسد نادیده گرفته یا حفظ می‌شود، و همین است که اضافه کردن فیلد تازه را بی‌خطر می‌کند. تغییر‌های امن سه‌تاست: فیلد تازه با شمارهٔ نو، حذف فیلد با رزرو کردن شماره‌اش، و عوض کردن نام که روی سیم اثری ندارد. در نسخهٔ سوم زبان، فیلد required وجود ندارد و هر فیلد غایب مقدار پیش‌فرض می‌گیرد، پس کد باید غیبت را حالت عادی بداند.In Protocol Buffers every field has a number, and the field name never travels on the wire; what goes out is the number plus a wire type and the value. Integers are encoded as varints, so a small number costs one byte and a large one costs several; strings and nested messages arrive length-prefixed and back to back. The encoder and decoder are generated from the contract file, so both sides share a typed contract and a type mistake is caught at compile time. A field the reader does not recognise is skipped or preserved, which is what makes adding a new field safe. Three changes are safe: a new field with a new number, deleting a field while reserving its number, and renaming a field, which has no effect on the wire. In proto3 there is no required field and a missing field takes its default, so code has to treat absence as normal.

1 byteبرچسب معمول یک فیلد: شماره و نوع سیم با همthe usual tag for a field: number and wire type together
19000–19999بازهٔ رزروشده؛ شمارهٔ فیلد از این محدوده برندارa reserved range; never take a field number from it
1–5 Bطول یک varint برای عدد ۳۲بیتی؛ عدد کوچک‌تر، بایت کمترthe length of one varint, depending on the number

شماره می‌ماند، اسم عوض می‌شودThe number stays, the name moves

چون روی سیم فقط شماره هست، اسم فیلد یک تصمیم محلی در کد است: می‌توانی account_id را به accountNumber تغییر بدهی و هیچ داده‌ای نشکند. اما شماره، هویت ابدی فیلد است. اگر فیلدی را حذف کنی و شماره‌اش را آزاد بگذاری، نفر بعدی ممکن است همان شماره را برای معنای دیگری بردارد و رکورد‌های قدیمی با معنای تازه خوانده شوند، بی‌آنکه خطایی ببینی. به همین دلیل نسخه‌های تازهٔ زبان یک کلمهٔ reserved دارند که شماره‌ها و نام‌های بازنشسته را برای همیشه قفل می‌کند. این قفل ارزان است و از گران‌ترین نوع خرابی جلوگیری می‌کند.Because only the number is on the wire, the field name is a local decision in the code: rename account_id to accountNumber and no data breaks. The number, though, is the field's permanent identity. Delete a field and leave its number free, and the next person may take that number for something else, so old records get read with the new meaning and no error appears anywhere. That is why newer versions of the language have a reserved keyword that locks retired numbers and names forever. It is cheap, and it prevents the most expensive kind of breakage.

کجا به درد نمی‌خوردWhere it does not pay off

پیام protobuf را نمی‌توانی با چشم در لاگ بخوانی یا با curl دستی بسازی؛ برای دیباگ کردن باید ابزار و همان نسخهٔ اسکیما را داشته باشی. برای یک سرویس کوچک داخلی که ماهی یک بار عوض می‌شود، JSON ساده‌تر و ارزان‌تر است. برای تحلیل دادهٔ خام هم مناسب نیست، چون داده خودتوصیف نیست و بدون اسکیما فقط بایت است. جایی که protobuf می‌درخشد پیام‌های پرتعداد و کوچک بین سرویس‌های تایپ‌دار است، نه فایل‌هایی که آدم‌ها باید باز کنند و بخوانند.You cannot read a protobuf message in a log with your eyes or build one by hand with curl; debugging needs a tool and the exact schema version. For one small internal service that changes once a month, plain JSON is simpler and cheaper. It is a poor fit for raw analytics too, since the data is not self-describing and without the schema it is just bytes. It shines for high-volume, small messages between typed services, not for files a human has to open and read.

تلهٔ رایجThe usual trap

استفادهٔ دوباره از شمارهٔ یک فیلد بازنشسته، یا عوض کردن نوعش از عدد به رشته. هیچ خطایی رخ نمی‌دهد؛ رکورد‌های قدیمی با همان شماره خوانده می‌شوند و مقدار بی‌معنا یا صفر بیرون می‌آید، در حالی که همهٔ تست‌های نسخهٔ تازه سبزند. شماره‌ها قفل‌های دائمی‌اند: هر شماره‌ای که یک بار استفاده شد، تا ابد مال همان معناست.Reusing the number of a retired field, or changing its type from a number to a string. Nothing errors out; old records are read under that number and a meaningless value or a zero comes back, while every test of the new code is green. Numbers are permanent locks: once a number has been used, it belongs to that meaning forever.

هر آدرسی که JSON برمی‌گرداند، REST است؟Does every URL that returns JSON count as REST?

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

در کتابخانهٔ محله، هر قفسه یک آدرس دارد و هر کتاب روی جای مشخصی نشسته. کتابدار فقط چهار کار بلد است: بیاور، بگذار، عوض کن، بردار؛ همین چهار حرکت برای کتاب و مجله و پرونده یکی است. لازم نیست برای هر نوع شیء یک زبان جدا یاد بگیرد. REST هم همین قرار را با HTTP می‌گذارد: هر چیز یک آدرس دارد و همه با چند فعل یکسان صدا زده می‌شوند.In the neighbourhood library every shelf has an address and every book sits in a known spot. The librarian knows four moves: fetch, put, replace, remove; the same four work for books, magazines and files. She never learns a separate language per kind of object. REST makes that deal with HTTP: everything gets an address, and everything is called with the same few verbs.

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

ولی همین یک‌دستی بهای خودش را دارد. اگر برای پر کردن یک صفحه باید ده چیز را از ده آدرس بگیری، ده بار راه می‌افتی؛ در حالی که یک انباردار می‌توانست همه را یک‌جا روی میزت بگذارد. بعضی کار‌ها هم با چهار فعل جور درنمی‌آیند: «این سفارش را لغو کن» نه ساختن است نه خواندن، و آدم‌ها فعل را می‌چپانند داخل آدرس؛ از آن لحظه دیگر سبک رعایت نشده و فقط ظاهرش مانده. در عوض، چون «بیاور» یعنی هیچ چیز را عوض نکن، مرورگر و کش‌های میانی می‌توانند جواب را نگه دارند و دوباره بدهند.That uniformity has a bill. If filling one screen means fetching ten things from ten addresses, you walk over ten times, where a warehouseman could have put everything on your desk at once. Some actions also refuse to fit the four verbs: “cancel this order” is neither creating nor reading, so people stuff the verb into the URL; from that moment the style is not being followed, only imitated. In exchange, because “fetch” promises to change nothing, browsers and middleboxes can keep the answer and hand it back.

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

REST یک سبک معماری است، نه یک کتابخانه و نه یک قالب داده. چند چیز را با هم می‌خواهد: منابعی که با URI شناسایی می‌شوند، رابط یکسان یعنی همان چند فعل HTTP با معنای استانداردشان، بازنمایی جدا از منبع، و بی‌حالتیِ هر درخواست تا هر گره‌ای بتواند جوابش را بدهد. وقتی این‌ها رعایت شود، GET و PUT و DELETE باید تکرارپذیر باشند و POST نباشد؛ همین تفاوت است که به کش و ابزار شبکه اجازه می‌دهد بدون شناختن برنامه تصمیم بگیرد. در عمل بیشتر APIهایی که REST نامیده می‌شوند فقط JSON روی HTTP‌اند: فعل داخل مسیر، کد وضعیت همیشه ۲۰۰، و خطا در بدنه. برای API عمومی این سبک برنده است چون قابل کشف و قابل آزمون با curl است؛ برای فراخوانی‌های ریز و پرحجم بین سرویس‌ها، gRPC با اسکیمای صریح و فریم دودویی معمولاً کم‌هزینه‌تر است.REST is an architectural style, not a library and not a data format. It asks for several things at once: resources identified by URIs, a uniform interface (the HTTP verbs with their standard meaning), a representation separate from the resource, and stateless requests so any node can answer. When those hold, GET, PUT and DELETE must be repeatable and POST must not be; that difference is what lets caches and network tools decide without knowing your app. In practice most APIs called REST are just JSON over HTTP: the verb sits in the path, status is always 200, and errors ride in the body. For a public API the style wins because it is discoverable and testable with curl; for high-volume service-to-service calls, gRPC with an explicit schema and binary framing is usually cheaper.

GETباید امن و تکرارپذیر باشد، وگرنه کش معنا نداردmust be safe and repeatable, or caching means nothing
200 OKهمیشه ۲۰۰ یعنی کش و مانیتور نمی‌فهمند چه شدalways 200 means caches and monitors learn nothing
N resourcesیک صفحهٔ معمول چند منبع می‌خواهد و چند رفت‌وبرگشتa normal screen wants several resources, so several round trips

REST در برابر RPCREST versus RPC

RESTRESTRPCRPC
واحد کارUnit of workمنبعی با آدرس ثابتa resource at a fixed addressتابعی با آرگومانa function with arguments
قالبFormatمعمولاً JSON، خوانا با چشمusually JSON, readable by eyeدودویی و فشردهbinary and compact
کشCachingبا معنای GET کار می‌کندworks through GET semanticsکم، چون هر فراخوانی فرق داردrare, since calls differ
اسکیماSchemaاختیاری، معمولاً OpenAPIoptional, usually OpenAPIبخشی از قراردادpart of the contract

تلهٔ رایجThe usual trap

صدا زدن هر چیزی که JSON برمی‌گرداند «REST». بعد تعجب می‌کنی که چرا کش کاری نمی‌کند، پراکسی نمی‌فهمد چه چیزی را می‌تواند تکرار کند، و مانیتورینگ هیچ خطایی نمی‌بیند.Calling anything that returns JSON “REST”. Then wondering why the cache does nothing, why the proxy cannot tell what is safe to retry, and why monitoring sees no errors at all.

RPC

فراخوانی از راه دور فصل ۵Chapter 5 · دیدن در متنsee in context

صدا زدن تابعی که آن طرف شهر است چه فرقی دارد؟What is different about calling a function across the city?

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

از اتاقت به همکارت صدا می‌زنی: «پروندهٔ آقای رضایی را بیاور». او می‌رود، قفسه را باز می‌کند و برمی‌گردد؛ پنج دقیقه طول می‌کشد و اگر وسط راه تلفن قطع شود، نمی‌دانی پرونده را برداشت یا نه، و اگر دوباره صدا بزنی ممکن است دو بار برود. صدا زدنِ کارِ دیگری همان فراخوانی از راه دور است: ساده به نظر می‌رسد، ولی رفت‌وآمد و ابهام دارد.You call out from your room to a colleague: bring me Mr Rezaei's file. He walks over, opens the cabinet, comes back; it takes five minutes, and if the phone dies midway you do not know whether he picked the file up, and calling again may send him twice. Asking someone else to do the work is what a remote call is: it looks simple, but it has travel time and doubt built in.

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

تفاوت وقتی روشن می‌شود که همان کار را با دست خودت انجام دهی. قلم روی میزت است؛ دست دراز می‌کنی و برمی‌داری، بدون فکر، بدون انتظار، و اگر قلم نبود فوراً می‌فهمی. حالا همان قلم در اتاق همکار است. باید بگویی، او باید بشنود، باید بلند شود، و ممکن است سرش شلوغ باشد یا اصلاً در اتاق نباشد. دو چیز عوض شده: زمان، و یقین. هر طراحی RPC که این دو را نادیده بگیرد، در دفتر کار خوب به نظر می‌رسد و در ساعت شلوغی می‌شکند.The difference shows when you do the same job yourself. The pen is on your desk: you reach out, pick it up, no thought, no wait, and if it is missing you know at once. Now the pen is in a colleague's room. You have to ask, he has to hear, he has to get up, and he may be busy or not there at all. Two things changed: time, and certainty. Any RPC design that ignores those two looks fine in the design doc and breaks at peak hour.

فراخوانی متدCall the methodمثل کد خودیlooks localبسته‌بندیMarshalپارامتر‌هاargumentsشبکهNetworkتأخیر و قطعیdelay and lossاجرا در سرورRun on serverشاید دو بارmaybe twiceجواب برمی‌گرددReply returnsیا گم می‌شودor gets lost
یک RPC از دید کلاینت مثل صدا زدن تابع است؛ از دید شبکه، سفری با تأخیر و ابهام است.From the client's side an RPC looks like a function call; from the network's side it is a trip with delay and doubt.

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

در RPC، کلاینت یک stub دارد که پارامتر‌ها را کدگذاری می‌کند، از شبکه می‌فرستد، سرور کدگشایی می‌کند و متد را صدا می‌زند، و جواب همان مسیر را برمی‌گردد. تفاوت با صدا زدن تابع محلی سه چیز است. اول تأخیر: یک فراخوانی محلی نانوثانیه است و یک RPC داخل دیتاسنتر حدود نیم تا یک میلی‌ثانیه، و بین دو قاره حدود صد میلی‌ثانیه. دوم، خرابی جزئی: بسته می‌تواند گم شود، سرور می‌تواند وسط کار بمیرد، و timeout یعنی «نمی‌دانم انجام شد یا نه»، نه «انجام نشد». سوم، نسخه: کلاینت و سرور ممکن است دو نسخهٔ مختلف باشند. فریمورک‌هایی مثل gRPC و Thrift این کار را با اسکیما و کد تولیدی جدی می‌گیرند، ولی هیچ‌کدام تأخیر و ابهام را حذف نمی‌کنند.In an RPC the client holds a stub that encodes the arguments and sends them over the network, the server decodes them and invokes the method, and the reply comes back the same way. Three things differ from a local call. First, latency: a local call is nanoseconds, an RPC inside a datacenter is a fraction of a millisecond to about one, and across continents it is around a hundred. Second, partial failure: a packet can be lost, the server can die mid-work, and a timeout means “I do not know whether it ran”, not “it did not run”. Third, versioning: client and server may be different builds. Frameworks such as gRPC and Thrift take the schema and generated code seriously, but none of them removes the delay or the doubt.

0.5–1 msرفت‌وبرگشت معمول RPC داخل یک دیتاسنترa typical round trip inside one datacenter
≈ 100 msهمان فراخوانی بین دو قارهthe same call between two continents
0/1/2یک RPC ممکن است صفر بار، یک بار یا دو بار اجرا شودan RPC may run zero times, once, or twice

timeout یعنی نمی‌دانمA timeout means I do not know

وقتی کلاینت بعد از دو ثانیه جواب نمی‌گیرد، فقط این را می‌داند که جواب نرسیده؛ نمی‌داند سرور درخواست را نگرفته، گرفته و نصفه انجام داده، یا تمام کرده و جوابش در راه گم شده. پس timeout یک «شکست» نیست، یک «نامعلوم» است. تصمیم درست به عملیات بستگی دارد: اگر خواندن است، تکرار بی‌خطر است؛ اگر نوشتن است، تکرار می‌تواند دو بار پول کم کند. راه‌حل این است که یا عملیات را idempotent کنی یا کلید تکرارناپذیری بگذاری، و بعد با خیال راحت retry کنی.When the client gets no answer after two seconds, all it knows is that no answer arrived; it does not know whether the server never received the request, received it and did half the work, or finished and lost the reply on the way back. So a timeout is not a failure, it is an unknown. The right move depends on the operation: retrying a read is safe, retrying a write can charge the card twice. Either make the operation idempotent or attach an idempotency key, and then retry with a clear head.

چرا شبیه صدا زدن تابع محلی نیستWhy it is not like a local call

صدا زدن تابع محلی مقرون به صرفه و قابل پیش‌بینی است: یا جواب می‌دهد یا بلافاصله می‌ترکد. RPC نه. پارامتر‌ها کپی می‌شوند، پس تغییری که سرور روی شیء می‌دهد به کلاینت نمی‌رسد؛ اشاره‌گر معنا ندارد؛ هر فراخوانی یک اتصال، یک بافر و یک مهلت می‌خواهد؛ و خطاهایش از جنس دیگری‌اند. این شباهت ظاهری خطرناک است، چون کد را طوری می‌نویسی که انگار تابع محلی است و بعد شبکه با بار و تأخیر و خرابی غافلگیرت می‌کند.A local call is cheap and predictable: it returns or it throws, immediately. An RPC is neither. Arguments are copied, so a change the server makes to an object never reaches the client; pointers mean nothing; each call costs a connection, a buffer and a deadline; and its errors are of a different species. The surface similarity is the danger, because you write code as if it were local and the network surprises you with load, delay and failure.

تلهٔ رایجThe usual trap

خواندن timeout به‌عنوان شکست و retry کردن یک نوشتن غیرتکرارناپذیر. سرور درخواست اول را کامل انجام داده، جواب در راه گم شده، و تلاش دوم همان کار را دوباره می‌کند؛ نتیجه دو رزرو، دو پرداخت یا دو ایمیل است، در حالی که متریک‌هایت یک خطای گذرا نشان می‌دهند.Reading a timeout as a failure and retrying a non-idempotent write. The server finished the first request, the reply was lost on the way back, and the second attempt does the same work again: two reservations, two charges or two emails, while your metrics show one transient error.

این ستون دقیقاً چه معنایی دارد، و برای کی؟What does this column actually mean, and to whom?

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

برای سفر خانوادگی یک صفحهٔ اکسل می‌سازی: ستون اول اسم، دوم سهم، سوم «پرداخت شد». حالا هر کسی صفحه را باز کند، بدون توضیح می‌فهمد چه خبر است. اگر همان اطلاعات را در چت بفرستی، «۳۵۰» و «علی» بی‌معنا روی صفحه می‌مانند تا کسی توضیحشان بدهد. اسکیما همان سرستون‌ها است: نام و ترتیب و معنای هر تکه از داده.You set up a spreadsheet for the family trip: first column the name, second the share, third “paid”. Now anyone who opens it understands it without an explanation. Send the same facts in a chat and “350” and “Ali” sit there meaning nothing until someone explains them. A schema is that header row: the name, order and meaning of every piece of data.

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

سرستون‌ها فقط اسم نیستند. با خودشان یک قرارداد می‌آورند: ستون دوم عدد است نه متن، خالی نمی‌ماند، و جمعش باید با ستون چهارم جور باشد. تا وقتی همه از همین یک صفحه استفاده می‌کنند، هر اشتباهی همان اول دیده می‌شود. اما لحظه‌ای که کسی بخواهد ستون تازه‌ای اضافه کند یا معنای ستون دوم را عوض کند، همان قرارداد به یک مذاکره و یک کار تبدیل می‌شود: باید همه را قانع کنی، صفحه را مهاجرت بدهی، و مطمئن شوی هیچ فرمول قدیمی نشکسته. اسکیما همین معامله است: نظم امروز، در ازای هزینهٔ تغییر فردا.The header row is more than names. It carries an agreement: the second column is a number, not text, it is not left empty, and its total must match the fourth column. While everyone uses that one sheet, every mistake shows up early. The moment someone wants to add a column or change what the second one means, the agreement turns into a negotiation and a job: convince everyone, migrate the sheet, and make sure no old formula broke. A schema is that trade: order today, paid for when you change it tomorrow.

نوشتن با اسکیماSchema on writeدرج فقط اگر با قید‌ها جور باشدan insert must satisfy the constraintsداده تمیز و یکدستclean, uniform dataتغییر ستون یعنی مهاجرتa column change means migrationخواندن با اسکیماSchema on readبایت خام روی دیسک می‌نشیندraw bytes land on diskمعنا موقع خواندن ساخته می‌شودmeaning appears at read timeهر خواننده تفسیر خودشeach reader interprets
اسکیما را می‌توان موقع نوشتن اعمال کرد یا موقع خواندن تفسیر؛ هر کدام هزینهٔ خودش را جای دیگری می‌گذارد.A schema can be enforced on write or interpreted on read; either way the cost moves somewhere else.

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

اسکیما تعریف صریح ساختار داده است: نام هر فیلد، نوعش، این‌که می‌تواند خالی باشد یا نه، و قید‌هایی مثل یکتا بودن یا بازهٔ مجاز. در پایگاه دادهٔ رابطه‌ای، اسکیما داخل خود پایگاه تعریف می‌شود و نوشتن ناسازگار همان‌جا رد می‌شود؛ در یک فایل Avro یا قرارداد protobuf، اسکیما قرارداد سیم است؛ در یک پایگاه سندی معمولاً اسکیمایی روی نوشتن نداری و تفسیر را به خواننده می‌سپاری. اسکیما سه چیز می‌خرد: معنای مشترک بین تیم‌ها، اعتبارسنجی در نقطهٔ ورود، و امکان ساختن ابزار و ایندکس از روی ساختار. بهایش تغییر است: اضافه کردن ستون به جدولی با صدها میلیون ردیف یک عملیات چندساعته است و برداشتن یا عوض کردن نوع ستون می‌تواند مهاجرت و دو نسخه خواندن بخواهد.A schema is the explicit definition of a structure: each field's name, its type, whether it may be empty, and constraints such as uniqueness or an allowed range. In a relational database the schema lives in the database and a mismatched write is rejected there; in an Avro file or a protobuf contract the schema is the wire contract; in a document store you often have no write-time schema and push the interpretation to the reader. A schema buys three things: a shared meaning across teams, validation at the point of entry, and the ability to build tools and indexes from the structure. It charges for change: adding a column to a table with hundreds of millions of rows is a multi-hour operation, and dropping a column or changing its type can require a migration and a period of reading both shapes.

DDLتعریف صریح ستون‌ها و قید‌ها، یک بار در پایگاه دادهexplicit column and constraint definitions, once in the database
0 قیددر خواندن با اسکیما، هیچ چیز موقع نوشتن جلوی دادهٔ بد را نمی‌گیردwith schema on read, nothing stops bad data at write time
N shapesشکل‌هایی که یک ستون JSON بدون اسکیما به خود می‌گیردthe shapes one unconstrained JSON column grows into

اسکیما جلوی چه چیزی را می‌گیردWhat a schema actually prevents

بدون اسکیما، اولین چیزی که از دست می‌رود معناست، نه ساختار. یک ستون متنی می‌تواند کد ملی باشد، شمارهٔ موبایل، یا یک یادداشت؛ همه در یک قالب ذخیره می‌شوند و هیچ‌کس نمی‌داند کدام. نفر بعدی برای اطمینان، هر سه حالت را با if بررسی می‌کند و کد پر از حدس می‌شود. اسکیما این حدس را حذف می‌کند: نوع می‌گوید چه چیزی مجاز است، قید می‌گوید چه چیزی ممکن نیست، و ابزار می‌تواند از روی همان تعریف، اعتبارسنجی و مستندات بسازد. چیزی که اسکیما نمی‌گیرد، درستی معنایی داده است؛ ستون درست پر شده، ولی با عدد اشتباه.Without a schema the first thing lost is meaning, not structure. A text column can hold a national id, a mobile number or a note; all three are stored the same way and nobody knows which is which. The next person checks all three cases with an if and the code fills up with guesses. A schema removes the guessing: the type says what is allowed, the constraint says what is impossible, and tooling can generate validation and docs from the same definition. What a schema cannot catch is semantic wrongness: the column is filled correctly, with the wrong number.

تغییر اسکیما در جدول بزرگChanging a schema on a large table

در جدول کوچک، اضافه کردن ستون یک دستور است. در جدول بزرگ، همان دستور می‌تواند ساعت‌ها طول بکشد، قفل بگیرد و فضای دیسک بخورد، چون موتور باید هر ردیف را بازنویسی کند. راه‌حل رایج این است که ستون را سبک و خالی اضافه کنی، مقدار پیش‌فرض را در کد بگذاری نه در جدول، و بعد آرام‌آرام و تکه‌تکه عقب‌رو پر کنی. برای تغییر نوع یا معنا، الگوی امن‌تر ستون موازی است: ستون تازه را پر کن، هر دو را مدتی بخوان، و ستون قدیمی را وقتی هیچ‌کس نمی‌خواندش بردار.On a small table, adding a column is one statement. On a large one the same statement can run for hours, take locks and eat disk, because the engine has to rewrite every row. The usual answer is to add the column empty and cheaply, put the default in the code rather than in the table, and backfill slowly in chunks. To change a type or a meaning, the safer pattern is a parallel column: fill the new one, read both for a while, and drop the old one once nothing reads it any more.

تلهٔ رایجThe usual trap

این‌که ستون را اختیاری بگذاری و بعد در کد فرض کنی همیشه پر است. شش ماه بعد یک‌سوم ردیف‌ها خالی‌اند و کوئری‌ها بی‌صدا آن‌ها را از دست می‌دهند؛ هیچ خطایی رخ نداده، فقط هیچ‌کس تعریف نکرده بود که این ستون از کِی باید پر باشد.Leaving a column nullable and then assuming in code that it is always filled. Six months later a third of the rows are empty and the queries drop them without a word; no error ever fired, nobody had defined from when that column must be populated.

schema evolution

تحول اسکیما فصل ۵Chapter 5 · دیدن در متنsee in context

فیلد تازه اضافه شد؛ پرونده‌های قدیمی چه می‌شوند؟You added a field. What happens to last year's rows?

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

ادارهٔ ثبت‌نام فرم امسال را عوض کرده و یک خانهٔ تازه به آخرش اضافه کرده: «گروه خونی». پرونده‌های سال قبل آن خانه را ندارند و پرونده‌های جدید دارند. کارمند باید بتواند هر دو را بخواند: از پروندهٔ قدیمی خانهٔ خالی را با «نامعلوم» پر کند و بقیه را همان‌طور بخواند. تحول اسکیما همین است، فقط با کد و دادهٔ زنده.The registration office has changed this year's form and added one box at the end: blood type. Last year's files have no such box, this year's do. The clerk has to read both: for an old file, fill the empty box with “unknown” and read the rest as usual. Schema evolution is exactly that, with code and live data instead of paper.

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

چند قاعده در همان اداره کار را ممکن می‌کند. خانهٔ تازه همیشه آخر اضافه می‌شود و شمارهٔ خانه‌های قبلی را جابه‌جا نمی‌کند. هیچ خانه‌ای بی‌سروصدا معنایش عوض نمی‌شود؛ اگر «مبلغ» از ریال به تومان تغییر کند، روی فرم نوشته می‌شود، وگرنه پروندهٔ قدیمی هزار برابر اشتباه خوانده می‌شود. و فرم قدیمی تا وقتی آخرین پروندهٔ قدیمی بسته نشده، در کشو می‌ماند. اداره نمی‌تواند یک روز صبح همهٔ پرونده‌ها را از نو بنویسد، پس ناچار است با دو قالب همزمان زندگی کند.A few rules in that office make it work. A new box always goes at the end and never shifts the numbers of the old ones. No box quietly changes meaning; if the amount switches from rial to toman, the form says so, otherwise an old file is read a thousand times wrong. And the old form stays in the drawer until the last old file is closed. The office cannot rewrite every file one morning, so it has to live with two formats at once.

اسکیمای نسخهٔ ۱Schema v1فیلد a و bfields a and bافزودن فیلد cField c addedبا مقدار پیش‌فرضwith a defaultکد تازه، دادهٔ کهنهNew code, old databackwardbackwardکد کهنه، دادهٔ تازهOld code, new dataforwardforward
تحول اسکیما یک لحظه نیست: فیلد اضافه می‌شود، هر دو نسخه مدتی زنده‌اند، و دو جهت خواندن باید جواب بدهند.Schema evolution is not one moment: a field is added, both versions live for a while, and both read directions must keep working.

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

تحول اسکیما یعنی تغییر قالب در حالی که داده و کد قدیمی هنوز زنده‌اند. تغییر‌های امن، آن‌هایی‌اند که خوانندهٔ قدیمی را کور نمی‌کنند: افزودن فیلد تازه با مقدار پیش‌فرض، برداشتن فیلدی که دیگر هیچ‌کس نمی‌خواند، و عوض کردن نام. تغییر‌های خطرناک، آن‌هایی‌اند که معنای بایت‌ها را عوض می‌کنند: شمارهٔ فیلد را دوباره به کار بردن، واحد را از میلی‌ثانیه به ثانیه بردن، نوع را از عدد ۶۴بیتی به ۳۲بیتی باریک کردن، یا اختیاری را اجباری کردن. در protobuf شماره‌ها لنگرند و اسم‌ها آزاد؛ در Avro اسم‌ها لنگرند و اسکیمای نویسنده با خواننده تطبیق داده می‌شود. در هر دو، ترتیب انتشار مهم است: اول کدی که هر دو قالب را می‌خواند، بعد نوشتن تازه، بعد پر کردن عقب‌رو، و در آخر حذف مسیر قدیمی.Schema evolution means changing the format while old data and old code are still alive. The safe changes are the ones that do not blind an old reader: add a new field with a default, remove a field nobody reads any more, rename a field. The dangerous ones change what the bytes mean: reusing a field number, switching the unit from milliseconds to seconds, narrowing a type from 64-bit to 32-bit, or turning an optional field into a required one. In Protocol Buffers the numbers are the anchor and the names are free; in Avro the names are the anchor and the writer's schema is resolved against the reader's. In both, the release order matters: code that reads both formats first, then the new writes, then the backfill, then removal of the old path.

N-1دادهٔ یک نسخهٔ عقب باید خوانده شود، نه بیشترdata from one version back must stay readable, not more
reservedشمارهٔ فیلد حذف‌شده را رزرو کن تا دوباره استفاده نشودreserve a dropped field's number so it is never reused
defaultفیلد تازه بدون پیش‌فرض، خوانندهٔ قدیمی را می‌شکندa new field without a default breaks old readers

تغییر‌هایی که بی‌صدا می‌شکنندThe changes that break in silence

بعضی تغییر‌ها خطا نمی‌دهند، فقط داده را اشتباه می‌کنند، و همین آن‌ها را خطرناک می‌کند. عوض کردن واحد بدون عوض کردن نام، کلاسیک‌ترین نمونه است: عدد ۳۰۰۰ که قبلاً میلی‌ثانیه بود حالا ثانیه است و یک تأخیر نیم‌ساعته از آن بیرون می‌آید. باریک کردن نوع، مقادیر بزرگ را می‌برد یا سرریز می‌کند. اجباری کردن فیلدی که نیمِ ردیف‌های قدیمی آن را ندارند، نوشتن را می‌شکند. و استفادهٔ دوباره از یک شماره یا نام بازنشسته، دادهٔ قدیمی را زیر معنای تازه می‌خواند. در همهٔ این‌ها کامپایلر ساکت است و تست‌ها سبزند، چون فقط کد تازه با دادهٔ تازه آزموده شده.Some changes raise no error, they just make the data wrong, which is what makes them dangerous. Changing the unit without changing the name is the classic: a value of 3000 that used to be milliseconds is now seconds, and a half-hour delay falls out of it. Narrowing a type drops or overflows large values. Making a field required when half the old rows lack it breaks writes. Reusing a retired number or name reads old data under a new meaning. In all of these the compiler stays quiet and the tests are green, because only new code against new data was ever exercised.

ترتیب انتشارThe order of release

ترتیب، بخشی از تغییر است. اول کدی را منتشر کن که قالب قدیمی و تازه را با هم می‌فهمد؛ تا این نسخه روی همهٔ نود‌ها ننشسته، هیچ نوشتن تازه‌ای را شروع نکن. بعد نوشتن را عوض کن و بگذار دادهٔ تازه جریان پیدا کند. بعد ردیف‌های قدیمی را عقب‌رو پر کن، آرام و تکه‌تکه، تا جدول و لاگ قفل نشوند. در آخر، وقتی مطمئن شدی هیچ نسخهٔ قدیمی و هیچ مصرف‌کنندهٔ قدیمی نمانده، خواندن قالب قدیمی و فیلد‌های مرده را بردار. هر جا این ترتیب را به هم بزنی، یک پنجرهٔ خرابی ساخته‌ای که ممکن است همان شب اتفاق بیفتد.The order is part of the change. Ship code that understands both the old and the new format first; do not start writing the new shape until that build is on every node. Then switch the writes and let new data flow. Then backfill the old rows, slowly and in chunks, so the table and the log do not lock up. Finally, once you are sure no old build and no old consumer remains, remove the old read path and the dead fields. Break that order anywhere and you have created a failure window that may well open that same night.

تلهٔ رایجThe usual trap

عوض کردن معنا یا واحد یک فیلد بدون عوض کردن نامش. تیم می‌گوید «فقط واحد را تغییر دادیم»، ولی ردیف‌های قدیمی همان عدد را با واحد قبلی نگه داشته‌اند و گزارش‌ها هزار برابر جابه‌جا می‌شوند؛ هیچ استثنایی هم پرتاب نمی‌شود که کسی را بیدار کند.Changing a field's meaning or unit without changing its name. The team says “we only changed the unit”, but old rows still hold the same number in the old unit and the reports are off by a thousand; no exception is thrown to wake anybody up.

schema registry

ثبت اسکیما فصل ۵Chapter 5 · دیدن در متنsee in context

اسکیمای تازه را چه کسی تأیید می‌کند؟Who approves the new schema before it ships?

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

در یک اداره، هر نامه باید روی سربرگ رسمی نوشته شود. سربرگ‌ها در بایگانی نگه داشته می‌شوند و کارمند پیش از مهر زدن، سربرگ تو را با نسخهٔ رسمی مقایسه می‌کند. اگر سربرگ تازه‌ای بسازی که ستون «شمارهٔ پرونده» را حذف کرده باشد، نامه‌ات برمی‌گردد. ثبت اسکیما هم همان بایگانی است، فقط برای قالب داده.In an office every letter must go out on the official letterhead. The blanks live in the registry, and the clerk compares yours with the official version before stamping it. Bring a new letterhead that has dropped the case-number column and your letter comes back. A schema registry is that registry, for data formats.

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

بهای این سخت‌گیری این است که حالا یک اداره وسط کار روزانه‌ات نشسته. اگر بایگانی تعطیل باشد و تو نتوانی سربرگ بگیری، نامه نوشتن هم می‌خوابد؛ برای همین نسخهٔ تأییدشده را روی میز خودت نگه می‌داری و تا وقتی قالب عوض نشده به بایگانی کاری نداری. طرف دیگرش هم مهم است: بایگانی‌ای که همهٔ نسخه‌های قبلی را نگه می‌دارد اجازه می‌دهد نامه‌های پارسال هنوز خوانده شوند. اگر نسخه‌های قدیمی را دور بریزی، دیگر نمی‌توانی بگویی نامهٔ دیروز با قالب امروز خوانده می‌شود یا نه.The bill for that strictness is a clerk standing in your daily path. If the registry is closed and you cannot get a letterhead, writing stops too; so you keep the approved version on your own desk and ignore the registry until the format changes. The other side matters just as much: a registry that keeps every earlier version lets last year's letters still be read. Throw the old versions away and you can no longer say whether yesterday's letter reads under today's template.

تولیدکنندهProducerپیام تازهnew messageبررسی سازگاریCompatibility checkپیش از انتشارbefore releaseنسخهٔ ثبت‌شدهRegistered versionبا شناسهwith an idمصرف‌کنندهConsumerمی‌خواندreads it
مسیر یک پیام: اسکیما پیش از انتشار تأیید و ثبت می‌شود، بعد شناسه‌اش با پیام می‌رود.One message's path: the schema is checked and registered before release, then its id travels with the data.

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

ثبت اسکیما یک سرویس است که برای هر subject فهرستی از نسخه‌های اسکیما نگه می‌دارد: Avro، Protobuf یا JSON Schema. تولیدکننده پیش از فرستادن پیام، اسکیما را ثبت می‌کند و شناسه‌ای می‌گیرد؛ در Avro همان شناسه با یک بایت جادویی در ابتدای هر پیام می‌نشیند و مصرف‌کننده با آن، اسکیمای نویسنده را از انبار می‌آورد. سرویس‌های جدی سیاست سازگاری را هنگام ثبت اعمال می‌کنند: backward یعنی اسکیمای تازه دادهٔ قدیمی را می‌خواند، forward یعنی اسکیمای قدیمی دادهٔ تازه را، full یعنی هر دو، none یعنی هیچ. بررسی با مقایسهٔ تعریف تازه با نسخهٔ قبلی یا با همهٔ نسخه‌ها انجام می‌شود، نه در زمان اجرا؛ پس تغییر شکننده همان‌جا رد می‌شود. مقاومت در برابر خرابی هم شرط است: اسکیمای تأییدشده باید در خود سرویس کش شود تا قطعی ثبت، نوشتن را نخواباند.A schema registry is a service that keeps a list of schema versions per subject: Avro, Protobuf or JSON Schema. A producer registers its schema before sending and gets an id; in Avro that id rides behind a magic byte at the front of every message, and the consumer uses it to fetch the writer's schema from the store. Serious registries enforce compatibility at registration time: backward means the new schema can read old data, forward means the old can read new, full means both, none means neither. The check compares the new definition with the previous version, or with all of them, rather than at runtime; a breaking change is rejected right there. Resilience is part of the deal too: the approved schema must be cached inside the service so a registry outage does not stop writes.

5 bytesیک بایت جادویی و چهار بایت شناسه در ابتدای هر پیام Avroone magic byte and a four-byte id at the front of an Avro message
backwardاسکیمای تازه، دادهٔ قدیمی را می‌خواندthe new schema reads the old data
N versionsهمهٔ نسخه‌ها می‌مانند تا دادهٔ قدیمی خوانا بماندevery version stays so old data remains readable

تلهٔ رایجThe usual trap

ثبت اسکیما را روشن کردن و سیاست سازگاری را روی none گذاشتن. انبار داری، ولی نگهبان نداری؛ اولین تغییر شکننده بی‌صدا رد می‌شود و سر وقت خواندن دادهٔ قدیمی، یا بدتر سر وقت خواندن دادهٔ تازه توسط مصرف‌کنندهٔ قدیمی، می‌فهمی.Turning the registry on and leaving compatibility at none. You get a store without a guard; the first breaking change sails through, and you find out when an old consumer reads new data, or new code reads old rows.

serialization

سریال‌سازی فصل ۵Chapter 5 · دیدن در متنsee in context

یک شیء زنده چطور در یک فایل جا می‌شود؟How does a live object fit inside a file?

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

برای اسباب‌کشی، قفسهٔ کتاب را باز می‌کنی: پیچ‌ها در پاکت، تخته‌ها روی هم، و روی هر پاکت می‌نویسی «پیچ‌های قفسه». در خانهٔ تازه از همان نوشته‌ها و همان ترتیب، قفسه را دوباره می‌سازی. اگر شماره‌گذاری پاکت‌ها را اشتباه کنی، قفسه سر جایش می‌ایستد ولی یک تخته اضافه می‌ماند. سریال‌سازی همان باز کردن و بسته‌بندی یک شیء است، با همان ریسک.To move house you take the bookshelf apart: screws into one bag, boards stacked together, and on every bag you write “shelf screws”. In the new place you rebuild it from those notes and that order. Get the bag labels wrong and the shelf still stands, with one board left over. Serialization is that same taking apart and packing of an object, with the same risk.

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

قفسه در خانهٔ قدیم یک چیز بود؛ در جعبه‌ها چند چیز شد. اسباب‌کشی فقط جابه‌جایی نیست، تبدیل است: از یک ساختار سه‌بعدی به یک انبوه تخت و مرتب. دو چیز در این تبدیل گم می‌شود مگر این‌که خودت نگهش داری: رابطهٔ قطعه‌ها با هم، و این‌که کدام پیچ به کدام سوراخ می‌خورد. اگر روی پاکت‌ها ننویسی، در خانهٔ تازه همهٔ قطعه‌ها را داری و هیچ‌کدام را نمی‌شناسی. سریال‌زدایی هم اگر قالبش رابطه‌ها را نگه ندارد، همان وضع را می‌سازد: داده کامل است، ساختار نیست.The shelf was one thing in the old house; in the boxes it became several. Moving is not just transport, it is a conversion: from a three-dimensional structure to a flat, ordered heap. Two things are lost in that conversion unless you keep them yourself: how the pieces relate to each other, and which screw goes into which hole. Skip the notes and in the new place you have every piece and recognise none of them. Deserialization without a format that preserves relations leaves you in the same state: all the data, no structure.

ساختار در حافظهStruct in memoryفیلد و نوعfields and typesسریال‌سازیSerializeبه بایتinto bytesذخیره یا ارسالStore or sendدیسک یا شبکهdisk or wireدی‌سریال‌سازیDeserializeساختار دوبارهstruct rebuilt
سریال‌سازی یک ساختار را خطی می‌کند تا جابه‌جا شود؛ سریال‌زدایی باید بتواند همان ساختار را از نو بسازد.Serialization flattens a structure so it can travel; deserialization has to rebuild that same structure on the other side.

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

سریال‌سازی یک ساختار در حافظه را به دنبالهٔ خطی بایت تبدیل می‌کند و سریال‌زدایی همان را برمی‌گرداند. مسئلهٔ اصلی این است که حافظه پر از اشاره‌گر است و بایت‌ها جایی برای اشاره‌گر ندارند: دو فیلد که به یک شیء اشاره می‌کنند، بعد از رفت‌وبرگشت دو شیء جدا می‌شوند، و حلقه‌ها باید شکسته یا با شناسه جایگزین شوند. نوع‌ها هم باید بازسازی شوند؛ اگر قالب نوع را با خود نبرد، خواننده باید از قبل بداند چه انتظاری داشته باشد. سریال‌زدایی کامل و قابل‌اعتماد سخت‌تر از آن است که به نظر می‌رسد: ترتیب فیلد‌ها، مقادیر پیش‌فرض، نسخهٔ کلاس و پلتفرم همه روی نتیجه اثر می‌گذارند. به همین دلیل قالب‌های جدی، ساختار را صریح و مستقل از زبان تعریف می‌کنند و سریال‌زدایی را از یک قرارداد مشترک انجام می‌دهند.Serialization turns an in-memory structure into a linear stream of bytes, and deserialization turns it back. The core problem is that memory is full of pointers and bytes have nowhere to put them: two fields pointing at one object come back as two separate objects, and cycles have to be broken or replaced with identifiers. Types have to be rebuilt too; if the format does not carry them, the reader must already know what to expect. Reliable, complete deserialization is harder than it looks: field order, default values, class version and platform all affect the result. That is why serious formats define the structure explicitly and independently of any language, and deserialize against a shared contract.

4–8 Bاندازهٔ معمول یک عدد صحیح یا یک اشاره‌گر در حافظهthe usual size of one integer or one pointer in memory
N copiesهر بار سریال‌زدایی یک نسخهٔ تازه در حافظه می‌سازد، نه همان شیء قبلیeach deserialization builds a fresh object, not the one you sent
cycleگراف حلقه‌دار را قالب ساده نمی‌تواند بنویسد؛ باید شناسه بدهیa simple format cannot write a graph with cycles; you need ids

چیزی که در رفت‌وبرگشت گم می‌شودWhat is lost in the round trip

سه چیز معمولاً از دست می‌رود و همه‌شان بعداً گران می‌شوند. هویت: دو فیلد که در حافظه یک شیء بودند، بعد از خواندن دو شیء مستقل‌اند و تغییر یکی روی دیگری اثر ندارد. اشتراک: اگر همان داده را دو جا نگه می‌داشتی تا حافظه کم بخورد، بعد از بازسازی دو کپی داری و حجم دو برابر می‌شود. تنبلی: فیلدی که در حافظه هنوز خوانده نشده بود، در بایت‌ها معمولاً یک مقدار محاسبه‌شدهٔ کامل است. این‌ها باگ نیستند؛ خاصیت تبدیل‌اند، و کدی که روی نسخهٔ درون‌حافظه‌ای حساب باز کرده، بعد از سریال‌زدایی غافلگیر می‌شود.Three things usually vanish, and all of them cost later. Identity: two fields that were one object in memory become two independent objects after a read, and changing one no longer affects the other. Sharing: if you kept one copy in two places to save memory, you now have two copies and twice the size. Laziness: a field that had not been loaded yet is usually stored as a fully computed value. These are not bugs, they are properties of the conversion, and code that relied on the in-memory shape is surprised after a round trip.

تلهٔ رایجThe usual trap

سریال‌زدایی ورودی ناشناخته با قالب‌های زبان‌محور. وقتی bytes از شبکه را با pickle یا سریال‌سازی جاوا باز می‌کنی، مهاجم می‌تواند کلاسی را انتخاب کند که موقع ساخت، کد اجرا می‌کند؛ یعنی یک فیلد در یک درخواست، معادل اجرای دستور روی سرورت است. این قالب‌ها برای دادهٔ خودی و داخلی‌اند، نه برای هر چیزی که از بیرون می‌رسد.Deserializing untrusted input with language-native formats. When you unpack bytes from the network with pickle or Java serialization, an attacker can choose a class whose construction runs code; one field in one request becomes command execution on your server. Those formats are for your own internal data, not for anything that arrives from outside.

هر سرویس خودش TLS و retry را دوباره بنویسد؟Should every service rewrite its own TLS and retries?

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

در ساختمان اداری، کنار درِ هر واحد یک باجهٔ کوچک گذاشته‌اند. هر نامه‌ای که می‌آید اول به باجه می‌رود: کارت کنترل می‌شود، فرستنده ثبت می‌شود، و اگر واحد جواب نداد باجه یک بار دیگر امتحان می‌کند. کارمندان دیگر لازم نیست برای هر نامه این کار‌ها را بلد باشند. شبکهٔ سرویس هم همین باجه است، چسبیده به هر سرویس.In an office building they put a small reception desk beside every door. Every letter goes there first: the badge is checked, the sender is logged, and if the office does not answer the desk tries once more. The staff no longer have to know those steps for every letter. A service mesh is that desk, attached to each service.

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

بهایش این است که هر نامه یک ایستگاه اضافه دارد؛ وقت می‌گیرد و هر واحد باید یک باجهٔ کوچک را هم بچرخاند. سود بزرگش یک‌دستی است: ده واحد، ده سیاست مختلف ندارند. ولی همین یک‌دستی یک تله دارد. اگر باجه برای نامه‌ای که جواب نمی‌گیرد سه بار تلاش کند و هر واحد هم همین کار را بکند، یک نامه به بیست نامه تبدیل می‌شود و مقصد زیر بار همان نامه‌های تکراری خفه می‌شود. باجه هم خودش می‌تواند خراب شود؛ آن‌وقت همهٔ نامه‌ها می‌ایستند، در حالی که دیروز فقط یک نامه گم می‌شد.The bill is an extra stop for every letter: it takes time, and every office has to keep a small desk running. The big win is uniformity: ten offices, not ten different policies. But uniformity hides a trap. If the desk retries a silent office three times and each office does the same, one letter becomes twenty and the destination suffocates under its own duplicates. The desk can also break, and then every letter stops, where yesterday only one went missing.

درخواستRequestاز کدfrom codeپروکسی محلیLocal proxysidecarsidecarسیاست مشترکShared policymTLS، retrymTLS, retryسرویس مقصدTarget serviceپروکسی آن طرفwith its proxy
مسیر یک فراخوانی: کد فقط درخواست می‌دهد؛ پروکسی کنارش سیاست‌های مشترک شبکه را اعمال می‌کند.One call's path: the code just asks; the proxy beside it applies the shared network policy.

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

یک mesh دو نیمه دارد: data plane که همان پروکسی‌های کنار سرویس‌اند و ترافیک واقعی از آن‌ها رد می‌شود، و control plane که پیکربندی را می‌سازد و به آن‌ها می‌رساند. پروکسی با یک قاعدهٔ مسیریابی در network namespace سرویس، ترافیک ورودی و خروجی را بی‌آنکه کد خبر داشته باشد برمی‌دارد. چیزی که اضافه می‌کند سیاست است، نه جادو: هویت سرویس با mTLS، timeout، retry با بودجه، قطع‌کنندهٔ مدار، تقسیم ترافیک بین نسخه‌ها، و متریک یکسان برای همه. هزینه‌اش یک hop محلی اضافه است، معمولاً کمتر از یک میلی‌ثانیه، به‌علاوهٔ یک پروسه و کمی حافظه برای هر pod. نکتهٔ عملی این است که control plane در مسیر داده نیست؛ قطع شدنش ترافیک را نمی‌خواباند، ولی تغییر بد را در چند ثانیه به همه می‌رساند، و همان‌جاست که یک اشتباه پیکربندی کل شبکه را می‌خواباند.A mesh has two halves: the data plane, the proxies beside each service that real traffic passes through, and the control plane, which builds configuration and delivers it. A routing rule in the service's network namespace takes over inbound and outbound traffic without the code knowing. What it adds is policy, not magic: service identity through mTLS, timeouts, retries with a budget, circuit breaking, traffic splitting between versions, and one set of metrics for all. The cost is one extra local hop, usually under a millisecond, plus a process and some memory per pod. The practical point is that the control plane is not in the data path: its outage does not stop traffic, but it pushes a bad change everywhere in seconds, and that is where one misconfiguration takes the whole mesh down.

+1 hopهر درخواست از یک پروکسی محلی رد می‌شودevery request goes through one local proxy
< 1 msهزینهٔ معمول همان hop روی همان ماشینthe usual cost of that hop on the same machine
mTLSهویت هر سرویس، بدون یک خط کد در برنامهservice identity without a line of code in the app

تلهٔ رایجThe usual trap

retry را در سه لایه روشن گذاشتن: کلاینت، mesh، و خود سرویس. هر لایه به‌تنهایی منطقی است، جمعشان طوفان retry می‌سازد؛ سرویسی که فقط کند شده بود، زیر بار همان تلاش‌های تکراری می‌افتد. retry فقط برای عملیات تکرارپذیر معنا دارد.Leaving retries on at three layers: the client, the mesh, and the service itself. Each is reasonable alone; together they make a retry storm, and a service that was merely slow goes down under the duplicates. Retries only make sense for repeatable operations.

چرا عدد ۵ یک بایت است و ۳۰۰ هزار سه بایت؟Why is 5 one byte and 300,000 three?

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

کتاب‌فروشی برای فرستادن کتاب، بسته را به اندازهٔ کتاب انتخاب می‌کند: رمان نازک داخل پاکت، اطلس قطور داخل جعبه. هیچ‌وقت برای رمان نازک جعبهٔ بزرگ برنمی‌دارد. عدد متغیرطول هم همین کار را با عدد می‌کند: عدد کوچک در یک بایت جا می‌شود، عدد بزرگ چند بایت می‌برد.A bookshop picks the parcel to fit the book: a thin novel goes in an envelope, a heavy atlas in a box. Nobody ships a thin novel in a crate. A varint does that with a number: a small value fits in one byte, a large one takes several.

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

قاعده‌اش ساده است: هر بایت هفت بیت داده دارد و یک بیت می‌گوید «باز هم هست». پس عدد تا ۱۲۷ در یک بایت جا می‌شود و هر بایت اضافه دامنه را ۱۲۸ برابر می‌کند. بهایش این است که تا بایت آخر را نخوانی نمی‌دانی عدد کجا تمام می‌شود؛ نمی‌توانی بپری سراغ عدد دهم، باید نُه تای قبلی را هم بخوانی. برای عدد‌های بزرگ هم بدتر می‌شود: یک عدد سی‌ودو بیتی می‌تواند پنج بایت شود، بیشتر از چهار بایت ثابت. پس این طرح وقتی برنده است که بیشترِ عدد‌هایت کوچک باشند.The rule is simple: each byte carries seven bits of data and one bit that says “more follows”. So anything up to 127 fits in one byte, and each extra byte multiplies the range by 128. The bill is that you cannot know where a number ends until you read its last byte; you cannot jump to the tenth value, you decode the nine before it. Large values get worse: a 32-bit integer can take five bytes, more than a fixed four. The scheme wins when most of your numbers are small.

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

هر بایت هفت بیت عدد را حمل می‌کند و بیت هشتم می‌گوید بایت بعدی هم مال همین عدد است؛ گروه‌ها از کم‌ارزش به پرارزش چیده می‌شوند. برای عدد‌های علامت‌دار، اول با zigzag علامت را جابه‌جا می‌کنند تا عدد‌های کوچک منفی هم مثل عدد‌های کوچک مثبت یک بایت بخواهند، وگرنه یک عدد منفی کوچک در نمایش مکمل دو ده بایت می‌خواهد. پروتکل بافر همین طرح را برای شمارنده‌ها و طول رشته‌ها به کار می‌برد؛ Avro هم int و long را با همین روش می‌نویسد. هزینه‌اش در دو جا ظاهر می‌شود: هر عدد یک حلقهٔ خواندن بایت‌به‌بایت می‌خواهد، پس رمزگشایی از عدد ثابت‌عرض کندتر است، و دسترسی تصادفی به عضو n‌ام فهرست ممکن نیست. اگر کلید‌هایت تصادفی و ۶۴ بیتی باشند، هر کدام ۹ یا ۱۰ بایت می‌شود و این طرح فقط ضرر است.Each byte carries seven bits of the number and the eighth bit says the next byte belongs to it; groups run from least to most significant. Signed values go through zigzag first, so small negatives cost what small positives cost, otherwise a small negative number in two's complement wants ten bytes. Protocol Buffers uses the scheme for counters and string lengths; Avro writes int and long the same way. The cost shows up twice: every value needs a byte-by-byte decode loop, so it is slower than fixed width, and you cannot jump to the n-th entry of a list. If your keys are random 64-bit values, each becomes nine or ten bytes and the scheme is pure loss.

7 + 1هفت بیت داده و یک بیت ادامه در هر بایتseven data bits and one continuation bit per byte
127بزرگ‌ترین عددی که در یک بایت جا می‌شودthe largest value that fits in one byte
≤ 5 bytesبدترین حالت یک عدد ۳۲ بیتیthe worst case for a 32-bit integer

تلهٔ رایجThe usual trap

فرض کردن این‌که این طرح همیشه جا صرفه‌جویی می‌کند. با شناسه‌های تصادفی ۶۴ بیتی هر مقدار ۹ یا ۱۰ بایت می‌شود؛ هم بزرگ‌تر از عدد ثابت، هم کندتر در رمزگشایی. برای چنین کلیدهایی عدد ثابت‌عرض یا کلید کوتاه‌تر بهتر است.Assuming the scheme always saves space. With random 64-bit identifiers every value takes nine or ten bytes: larger than fixed width and slower to decode. For keys like that, fixed width or a shorter key is the better deal.