DDIA·واژه‌هاTERMS

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

۷7تقسیم (Sharding)Sharding

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

consistent hashing

هش سازگار فصل ۷Chapter 7 · دیدن در متنsee in context

یک گره تازه اضافه شد. چند کلید باید جابه‌جا شود؟A node joins. How many keys actually have to move?

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

در پارکینگ دوچرخهٔ ساختمان، قفسه‌ها را دور تا دور دیوار چیده‌اند و هر دوچرخه را با حرف اول فامیل صاحبش به نزدیک‌ترین قفسه می‌بندی. یک قفسهٔ تازه وسط دیوار نصب می‌شود و فقط دوچرخه‌های همان تکهٔ کوچک جابه‌جا می‌شوند، نه همهٔ دوچرخه‌های پارکینگ. هش سازگار همین حرکت را با کلیدها می‌کند: افزودن یک گره فقط بخشی از کلیدها را جابه‌جا می‌کند.In your building's bike room the racks run around the wall, and each bike goes to the rack nearest the first letter of its owner's surname. A new rack appears mid-wall, and only the bikes in that one short stretch move, not every bike in the room. Consistent hashing makes the same move with keys: one node joining shifts a slice of them, not all of them.

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

حالا همان پارکینگ را با یک نکتهٔ دیگر ببین. بعضی حروف پرجمعیت‌اند و بعضی خالی، پس یک قفسه ممکن است دو برابر دیگری دوچرخه بگیرد. راه‌حل این است که هر نفر چند نقطه روی دیوار داشته باشد، نه یکی؛ آن‌وقت قفسه‌های هر نفر پخش می‌شوند و سهمش به همان نسبت بزرگ می‌شود. بهایش این است که نقشهٔ پارکینگ شلوغ‌تر می‌شود و باید بدانی هر دوچرخه به کدام قفسه رفته است.Now look closer at the same bike room. Some letters are crowded and some are thin, so one rack may hold twice as many bikes as its neighbour. The fix is to give each person several spots along the wall instead of one: their racks end up spread out, and their share grows in proportion to how much they own. The price is a busier map and one more question to answer — which rack took which bike.

کلید به اولین گرهkey to first nodeگره ANode Aگره BNode Bگره CNode Cگره DNode Dگره تازهNew node
گره‌ها و کلیدها روی یک حلقه می‌نشینند؛ هر کلید در جهت ساعت به اولین گره می‌رسد و همان‌جا می‌ماند.Nodes and keys share one ring; each key walks clockwise to the first node and stays there.

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

در هش سازگار، هم گره‌ها و هم کلیدها را با یک تابع هش به یک فضای دایره‌ای می‌بری، مثلاً ۳۲ یا ۶۴ بیت. هر کلید روی حلقه می‌افتد و در جهت عقربه‌های ساعت می‌گردد تا به اولین گره برسد؛ همان گره صاحب کلید است. با افزودن یک گره، فقط کمانی که بین گره تازه و همسایهٔ قبلی‌اش است جابه‌جا می‌شود، یعنی حدود ۱/N کلیدها؛ در حالی که با hash % N تقریباً همهٔ کلیدها جایشان عوض می‌شود. یک نقطه به ازای هر گره کافی نیست، چون گره‌ها تصادفی روی حلقه پخش می‌شوند و فاصله‌ها ناهموار می‌مانند؛ به همین دلیل هر گره را به چند صد vnode می‌شکنند. هزینه‌اش این است که هر کلاینت باید نقشهٔ حلقه را بداند، و پیمایش بازه‌ای روی حلقه معنا ندارد، چون کلیدهای همسایه روی حلقه هیچ ربطی به ترتیب داده ندارند.Consistent hashing hashes both nodes and keys into a circular space, usually 32 or 64 bits wide. A key lands on the ring and walks clockwise to the first node ahead of it; that node owns the key. When a node joins, only the arc between it and its predecessor changes hands, roughly 1/N of the keys, whereas with hash % N almost every key moves. One point per node is not enough: random placement leaves very uneven arcs, so each physical node is split into a few hundred virtual nodes. The cost is that every client must know the ring map, and range scans are gone, because neighbouring keys on the ring have nothing to do with each other in the data.

1/Nسهم کلیدهایی که با افزودن یک گره جابه‌جا می‌شوندthe share of keys that move when one node joins
100–200vnode به ازای هر گره در پیاده‌سازی‌های رایجvnodes per node in common implementations
1 keyیک کلید داغ، هر قدر vnode هم باشد، یک گره را می‌بردone hot key pins to one node no matter how many vnodes

چرا یک نقطه به ازای هر گره کافی نیستWhy one point per node is not enough

اگر فقط یک نقطه به ازای هر گره روی حلقه بگذاری، فاصله‌ها تصادفی می‌شوند: ممکن است یک گره کمانی به اندازهٔ یک‌سوم حلقه بگیرد و گره‌ای دیگر چند درصد. آن‌وقت ظرفیتی که اضافه کرده‌ای عملاً هدر می‌رود و همان گره بزرگ گلوگاه می‌شود. راه‌حل vnode است: هر گره فیزیکی را به چند صد نقطهٔ کوچک می‌شکنی و آن نقطه‌ها را روی حلقه پخش می‌کنی. حالا میانگین کمان‌های هر گره به هم نزدیک می‌شود، می‌توانی به گره قوی‌تر vnode بیشتری بدهی، و وقتی یک گره می‌میرد بارش بین همهٔ گره‌های دیگر تقسیم می‌شود، نه فقط روی همسایه‌اش.With a single point per node, the arcs come out random: one node may own a third of the ring while another owns a few percent. The capacity you added is then wasted and the large node becomes the bottleneck. Virtual nodes fix this: every physical node is split into a few hundred small points spread around the ring. The average arc per node gets close to the others, a stronger machine can be given more vnodes, and when a node dies its load is shared by all the remaining nodes instead of only its neighbour.

کجا کم می‌آوردWhere it falls short

هش سازگار کجی داده را درمان نمی‌کند. اگر یک کلید داغ داشته باشی، همان کلید یک نقطه روی حلقه است و همهٔ درخواست‌هایش به همان گره می‌رسد؛ vnodeهای بیشتر فقط کلیدهای دیگر را پخش می‌کنند. پیمایش بازه‌ای هم دیگر معنا ندارد، چون کلیدهای همسایه روی حلقه هیچ ربطی به ترتیب داده ندارند. و هر بار که عضویت عوض می‌شود، بخشی از کش‌ها سرد می‌شود، چون مالک آن بخش از کلیدها عوض شده است؛ اگر جلوی این را نگیری، همان لحظهٔ اضافه کردن ظرفیت تأخیر بالا می‌رود.Consistent hashing does not cure skew. A hot key is one point on the ring, so all of its requests still reach one node; more vnodes only spread the other keys. Range scans are gone as well, because neighbouring keys on the ring have no relation to the order of the data. And every membership change cools part of the cache, since the owner of that slice has changed; left alone, the moment you add capacity is the moment latency rises.

تلهٔ رایجThe usual trap

فکر می‌کنی حلقه کجی را حل کرده است، پس یک کلید داغ را رها می‌کنی. آن کلید همچنان یک گره را می‌سوزاند؛ حلقه فقط تعداد زیادی کلید را پخش می‌کند و برای یک کلید هیچ کمکی نمی‌کند.Assuming the ring has solved skew, so you leave one hot key alone. That key still burns a single node; the ring spreads many keys, and for one key it does nothing.

coordinator

هماهنگ‌کننده فصل ۷Chapter 7 · دیدن در متنsee in context

کی درخواست را می‌شکند و جواب‌ها را جمع می‌کند؟Who splits the request and gathers the answers?

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

در بیمارستان، پرونده‌ات را به پذیرش می‌دهی. پذیرش تو را به بخش درست می‌فرستد، جواب آزمایش را از دو بخش دیگر می‌گیرد و آخر یک برگه به تو می‌دهد؛ خودش آزمایش نمی‌کند. هماهنگ‌کننده هم همین نقش را در یک سیستم تقسیم‌شده دارد.At the hospital you hand your file to reception. Reception sends you to the right department, collects the results from two other departments, and gives you one sheet at the end; it does not run the tests itself. A coordinator plays that part in a sharded system.

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

هماهنگ‌کننده داده را نگه نمی‌دارد؛ نقشه را می‌داند. برای همین می‌تواند ساده و بدون حالت باشد و هر تعداد که بخواهی از آن بگذاری، ولی هر درخواست یک پرش بیشتر می‌خورد. اگر هماهنگ‌کننده وسط کار خراب شود، مشتری دوباره تلاش می‌کند و ممکن است همان کار دو بار انجام شود؛ پس همه‌چیز باید تکرارپذیر باشد. و اگر هماهنگ‌کننده تصمیمی هم بگیرد، مثل کامیت دومرحله‌ای، دیگر بی‌دردسر نیست: باید تصمیمش را جایی بنویسد که با خرابی از دست نرود.The coordinator does not hold data, it knows the map. That is why it can be simple and stateless and you can run as many as you like, though every request pays one extra hop. If the coordinator dies mid-request the client retries, and the work may happen twice, so everything it touches must be repeatable. And once it also makes decisions, as in a two-phase commit, it stops being simple: it must record that decision where a crash cannot erase it.

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

هماهنگ‌کننده گره‌ای است که درخواست کلاینت را می‌گیرد، تشخیص می‌دهد کدام شاردها باید جواب بدهند، درخواست‌ها را می‌فرستد و جواب‌ها را ادغام می‌کند. در نقش مسیریابی، حالت نگه نمی‌دارد و می‌تواند افقی مقیاس بگیرد، ولی یک پرش اضافه به هر درخواست اضافه می‌کند و باید نقشهٔ مالکیت را تازه نگه دارد. در نقش تراکنشی، ماجرا جای دیگری است: هماهنگ‌کننده تصمیم commit یا abort را در لاگ خودش می‌نویسد و اگر بین نوشتن تصمیم و رساندن آن به بقیه خراب شود، شرکت‌کننده‌ها در حالت معلق می‌مانند تا برگردد. برای همین هماهنگ‌کنندهٔ تراکنش باید پایدار باشد و هماهنگ‌کنندهٔ مسیریابی نه.A coordinator takes the client request, works out which shards must answer, sends the sub-requests and merges the replies. As a router it holds no state and scales out, at the cost of one extra hop per request and the duty of keeping the ownership map fresh. As a transaction coordinator the story changes: it writes the commit or abort decision to its own log, and if it fails between writing that decision and telling the participants, they stay in doubt until it returns. A transaction coordinator must therefore be durable, and a routing coordinator need not be.

1 hopپرش اضافه‌ای که هر درخواست از هماهنگ‌کننده می‌خوردthe extra hop every request pays at the coordinator
2PCوقتی هماهنگ‌کننده تصمیم تراکنش را هم می‌گیردwhen the coordinator also owns the commit decision

تلهٔ رایجThe usual trap

هماهنگ‌کننده را با رهبر اشتباه می‌گیری. هماهنگ‌کننده فقط مسیر و جمع‌کردن را بلد است؛ رهبر صاحب نوشتن است. اگر همهٔ ترافیک را از یک هماهنگ‌کنندهٔ حالتمند رد کنی، هم سقف توانت یک ماشین می‌شود و هم یک خرابی کل مسیر را می‌خواباند.Mistaking the coordinator for the leader. A coordinator knows routes and merging; a leader owns writes. Funnel all traffic through one stateful coordinator and your throughput ceiling becomes one machine, while a single failure takes the whole path down.

کلید را که می‌چرخانی، چه چیزی هنوز در راه است؟When you flip the switch, what is still in flight?

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

وسط اسباب‌کشی، تا وقتی همه خبردار نشده‌اند، نامه‌هایی به آدرس قبلی می‌آید. اگر همان روز کلید را تحویل بدهی و صندوق قدیمی را برداری، آن چند نامه گم می‌شوند. برش انتقال هم همان روز است: عوض کردن آدرس، در حالی که نامه هنوز در راه است.In the middle of a move, letters keep arriving at the old address until everyone has heard. Hand over the keys and take the old mailbox down the same day, and those few letters are gone. Cutover is that day: changing the address while the mail is still on its way.

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

آدم‌های باتجربه این کار را یک‌شبه نمی‌کنند. اول صندوق جدید را می‌گیرند، بعد نامه‌های هر دو صندوق را می‌خوانند، بعد مدتی نامه‌های قدیمی را به جدید پست می‌کنند، و فقط وقتی مطمئن شدند چیزی جا نمانده صندوق قدیمی را برمی‌دارند. این میانهٔ کار بهای خودش را دارد: دو جا باید هماهنگ بمانند و هر کدام می‌تواند عقب بماند. پس تا وقتی مطمئن نشده‌ای که هیچ نامه‌ای در راه نیست، صندوق قدیمی سر جایش می‌ماند.People who have done it do not finish in one night. They open the new box first, read both boxes, forward old mail to the new one for a while, and only when nothing is missing do they take the old box down. The middle has its own bill: two places must stay in step, and either can lag. So the old box stays up until you are sure no letter is still in the post.

نوشتن دوگانهDual writeهر دو سیستمboth systemsپر کردن گذشتهBackfillدادهٔ تاریخیthe old historyخواندن سایهShadow readsمقایسهٔ جواب‌هاcompare answersخاموشی سیستم قدیمTurn off the oldبرگشت گران استrollback is costly
برش یک لحظه نیست: اول هر دو طرف می‌نویسند، بعد گذشته پر می‌شود، بعد خواندن سایه، و آخر خاموشی.Cutover is not one moment: both sides write, history is backfilled, reads are shadowed, and the old side goes dark last.

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

برش انتقال آخرین مرحلهٔ مهاجرت است و پیش از آن سه کار آمده: نوشتن دوگانه یا دنبال کردن تغییرها، پر کردن دادهٔ تاریخی، و مقایسه. در نوشتن دوگانه هر تغییر در هر دو سیستم ثبت می‌شود، ولی این کار اتمی نیست؛ اگر یکی از دو نوشتن شکست بخورد، دو طرف از هم جدا می‌شوند و کسی خبردار نمی‌شود. پر کردن گذشته باید طوری بنویسد که نوشتن‌های تازهٔ سیستم نو را لگدمال نکند، وگرنه رکوردی که کاربر همین الان عوض کرده با نسخهٔ دیشب جایگزین می‌شود. مقایسه معمولاً با خواندن سایه انجام می‌شود: همان درخواست به هر دو سیستم می‌رود و جواب‌ها و تأخیر‌ها مقایسه می‌شوند. خودِ برش می‌تواند یک‌جا باشد یا درصدی و به‌تفکیک مشتری؛ برش درصدی ریسک را کم می‌کند ولی پنجرهٔ دوحالته را طولانی‌تر می‌کند. تا وقتی سیستم قدیم گرم و قابل نوشتن است برگشت ممکن است؛ بعد از خاموشی، برگشت یعنی مهاجرت دوباره.Cutover is the last step of a migration, and three jobs come before it: dual writes or change capture, backfilling history, and comparison. With dual writes every change is recorded in both systems, but that is not atomic; if one write fails the two sides drift apart and nothing tells you. A backfill must write in a way that does not trample fresh writes on the new system, or a record the user changed a minute ago is replaced by last night's copy. Comparison usually runs as shadow reads: the same request goes to both systems and the answers and latencies are compared. The switch itself can be all at once or by percentage and by tenant; a gradual switch lowers risk but stretches the two-way window. Rollback is possible while the old system is warm and writable; after it is switched off, rollback means migrating again.

2 writesتا پایان مهاجرت، هر تغییر در دو جا ثبت می‌شودuntil the end, every change is recorded twice
T-0لحظهٔ برش؛ بعد از آن برگشت یعنی مهاجرت دوبارهthe switch moment; after it, going back means migrating again
100%برش یک‌جا در برابر درصدی؛ ریسک در برابر پنجرهٔ دوحالتهall at once versus gradual: risk against a longer two-way window

تلهٔ رایجThe usual trap

برش بدون نوشتن دوگانه و بدون مقایسه. دو سیستم مدتی جواب می‌دهند، هیچ خطایی بالا نمی‌آید، و سه هفته بعد مشتری می‌گوید سفارشش نیست. سکوت نشانهٔ موفقیت نیست؛ نشانهٔ نبودِ اندازه‌گیری است.Cutting over without dual writes and without comparison. Both systems answer for a while, nothing errors, and three weeks later a customer says their order is missing. Silence is not success; it is the absence of measurement.

gossip protocol

پروتکل شایعه فصل ۷Chapter 7 · دیدن در متنsee in context

خبر چطور بدون رئیس به همه می‌رسد؟How does news reach everyone with no boss?

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

خبر تعطیلی مدرسه را در حیاط به دو نفر می‌گویی. هر کدام هم به دو نفر دیگر می‌گویند. تا زنگ آخر تقریباً همه می‌دانند، بی‌آنکه کسی فهرست کلاس را خوانده باشد. پروتکل شایعه هم همین‌طور کار می‌کند: هر گره دوره‌ای چند گره دیگر را صدا می‌زند و آخرین خبر‌ها را رد و بدل می‌کند.You tell two friends in the schoolyard that classes are cancelled. Each of them tells two more. By the last bell almost everyone knows, and nobody read the class list. A gossip protocol works like that: every node calls a few others on a timer and swaps the latest news.

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

سرعتش از همین تکرار می‌آید: با هر دور تعداد کسانی که می‌دانند تقریباً دو برابر می‌شود، پس چند دور برای پوشاندن کل ساختمان کافی است. ولی همین تکرار آشغال هم تولید می‌کند؛ یک نفر خبر را ده بار می‌شنود و باید بفهمد کدام تازه‌تر است، وگرنه خبر کهنه خبر تازه را می‌بلعد. برای همین هر گره شمارهٔ نسخهٔ خبر را با خودش می‌آورد و فقط تازه‌تر را نگه می‌دارد. کسی هم که در راهروی بسته بوده تا در باز شود بی‌خبر می‌ماند؛ بعد از باز شدن چند دور کافی است تا عقب‌افتادگی جبران شود.The speed comes from doubling: each round roughly doubles the number of people who know, so a few rounds cover the building. The same repetition makes noise; one person hears the news ten times and has to tell which version is newer, or old news swallows new. So each node carries a version number and keeps only the fresher copy. Anyone stuck behind a closed corridor stays ignorant until it opens; after that, a few rounds close the gap.

یک گره می‌داندOne node knowsدور ۱round 1دو همسایهTwo peersدور ۲round 2چهار همسایهFour peersدور ۳round 3همه می‌دانندEveryone knowsچند دور بعدa few rounds on
پخش خبر: هر دور چند همسایهٔ تصادفی، و تعداد کسانی که می‌دانند تقریباً دو برابر می‌شود.How news spreads: a few random peers per round, and the informed set roughly doubles.

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

در هر دور، گره یک مجموعهٔ کوچک و تصادفی از همسایه‌ها را انتخاب می‌کند و وضعیتش را با آن‌ها رد و بدل می‌کند؛ این تبادل می‌تواند push باشد، pull باشد، یا هر دو. چون هر گره در هر دور چند نفر را می‌بیند، تعداد کسانی که خبر را دارند تقریباً نمایی بالا می‌رود و با احتمال زیاد بعد از O(log n) دور همه می‌دانند؛ در عوض هر دور حدود n × f پیام روی شبکه می‌فرستد. برای این‌که پیام تکراری بی‌ضرر باشد، وضعیت باید idempotent و نسخه‌دار باشد: شمارهٔ نسخه، incarnation، یا بردار نسخه. gossip برای عضویت و تشخیص خرابی هم به کار می‌رود؛ SWIM با ping و ping غیرمستقیم و حالت مشکوک، خرابی را با چند گره دیگر چک می‌کند تا یک تأخیر شبکه‌ای کوتاه گرهٔ سالم را مرده اعلام نکند. چیزی که gossip نمی‌دهد ترتیب است: دو خبر متناقض می‌توانند همزمان پخش شوند و همه به یک جواب نرسند؛ برای تصمیم‌هایی که اجماع می‌خواهند لایهٔ دیگری لازم است.Each round a node picks a small random set of peers and swaps state with them; the exchange can be push, pull, or both. Because every node meets a few others per round, the informed set grows roughly exponentially and with high probability everyone knows after O(log n) rounds; in exchange, each round puts about n × f messages on the network. For duplicates to be harmless the state must be idempotent and versioned: a version number, an incarnation, or a version vector. Gossip also carries membership and failure detection; SWIM pings a node, pings it indirectly through others, and marks it suspect, so a short network delay does not declare a healthy node dead. What gossip does not give you is order: two conflicting stories can spread at once and not everyone lands on the same answer, so decisions that need agreement require another layer.

O(log n)دور‌ها تا رسیدن خبر به همه، با احتمال زیادrounds until everyone hears, with high probability
f peersهمسایه‌های تصادفی هر گره در هر دورrandom peers each node picks per round
n × fپیام‌های هر دور؛ با هزار گره و سه همسایه، سه هزار پیامmessages per round: a thousand nodes and three peers means three thousand

تلهٔ رایجThe usual trap

استفاده از gossip برای چیزی که ترتیب می‌خواهد، مثل انتخاب لیدر یا قفل. gossip فقط «چه کسی را می‌شناسم» و «آخرین وضعیت چیست» را پخش می‌کند؛ دو گره می‌توانند همزمان یک چیز را متفاوت ببینند. و تشخیص خرابی خیلی حساس، گره‌ای را که فقط مکث کرده مرده اعلام می‌کند و بعد از برگشتنش می‌بیند داده جابه‌جا شده است.Using gossip for something that needs order, such as leader election or a lock. It spreads “who I know” and “what I last heard”; two nodes can see the same thing differently at the same moment. And an over-eager failure detector declares a paused node dead, then finds its data already moved when it returns.

چرا با باز شدن یک باجهٔ تازه، صف همه عوض می‌شود؟One new counter opens. Why does everyone's queue change?

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

در کتاب‌فروشی سه صندوق داری و هر مشتری را بر اساس باقی‌ماندهٔ رقم آخر شمارهٔ تلفنش به یکی از آن‌ها می‌فرستی. تا وقتی سه صندوق است همه‌چیز مرتب است؛ صندوق چهارم که باز شود، تقریباً هر مشتری باید صفش را عوض کند. تقسیم هشی هم همین قانون را دارد، فقط با تابع هش و باقی‌مانده.Your bookshop has three tills, and you send each customer to one of them by the last digit of their phone number, modulo three. While there are three tills that is perfectly fair. Open a fourth and almost every customer has to change queue. Hash sharding runs on the same rule, with a hash instead of a phone number.

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

چرا این قانون عادلانه است؟ چون رقم آخر شمارهٔ تلفن تصادفی است، پس مشتری‌ها به‌طور یکنواخت بین صندوق‌ها پخش می‌شوند و هیچ صندوقی بیکار نمی‌ماند. نقطهٔ ضعفش این است که تعداد صندوق‌ها داخل خود قاعده است: یک مشتری با یک شمارهٔ ثابت، تا وقتی تعداد صندوق‌ها ثابت است همیشه به همان صندوق می‌رود، ولی به‌محض عوض شدن تعداد، تقریباً همه به صندوق دیگری می‌افتند. هیچ مشتری‌ای خودش را جابه‌جا نکرده؛ فقط قاعده عوض شده است. در پایگاه داده هم اگر شاردها را از روی N حساب کنی، هر تغییر در N یعنی جابه‌جایی تقریباً همهٔ کلیدها.Why is the rule fair? Because the last phone digit is random, customers spread evenly across the tills and none stands idle. The rule has one weakness, though: the number of tills is inside the rule itself. While there are three, everything is fine; open a fourth and the remainder modulo four puts almost every customer somewhere else. In a database, if shards are computed from N, any change in N means moving almost every key.

کلیدKeyمثلاً شناسهٔ کاربرsay a user idتابع هشHash functionپخش تصادفیuniform spreadباقی‌ماندهModuloh(key) % Nh(key) % NشاردShardهمیشه همان یکیalways the same one
کلید از تابع هش می‌گذرد و باقی‌مانده می‌گوید کدام شارد؛ ترتیب کلیدها در این مسیر گم می‌شود.The key goes through a hash and the remainder names the shard; the ordering of keys is lost on the way.

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

در تقسیم هشی، از کلید یک مقدار عددی می‌سازی و آن را بر تعداد شاردها تقسیم می‌کنی؛ باقی‌مانده می‌گوید رکورد کجاست. اگر تابع هش خوب باشد، کلیدهای شبیه به هم به جاهای پراکنده می‌روند و بار یکنواخت پخش می‌شود. مشکل از جایی شروع می‌شود که N عوض شود: با اضافه شدن یک شارد، باقی‌ماندهٔ تقریباً همهٔ کلیدها تغییر می‌کند و باید تقریباً همهٔ داده جابه‌جا شود. دو راه برای فرار از این هست: تعداد پارتیشن‌ها را از تعداد ماشین‌ها جدا کنی و پارتیشن‌ها را ثابت نگه داری، یا از هش سازگار استفاده کنی. بهای اصلی تقسیم هشی جای دیگری است: ترتیب کلیدها از بین می‌رود، پس پرس‌وجوی بازه‌ای به یک fan-out روی همهٔ شاردها تبدیل می‌شود، و یک کلید داغ هم همچنان روی یک شارد می‌ماند.Hash sharding turns the key into a number with a hash function and takes that number modulo the shard count; the remainder names the shard. A good hash scatters similar keys, so the load spreads evenly. Trouble starts when N changes: with one more shard almost every remainder changes and almost all of the data has to move. There are two escapes: decouple the partition count from the machine count and keep the partitions fixed, or use consistent hashing. The other bill is ordering. Range queries become a fan-out over every shard, and one hot key still sits on one shard.

h(key) % Nقاعدهٔ ساده‌ای که ماشین را انتخاب می‌کندthe simple rule that picks the machine
1 − 1/Nسهم کلیدهایی که با عوض شدن N جابه‌جا می‌شوندthe share of keys that move when N changes
N shardهزینهٔ یک پرس‌وجوی بازه‌ای: fan-out به همهa range query fans out to all of them

قانون mod N و صورت‌حسابشThe mod N rule and its bill

هر وقت ظرفیت کم بیاوری، اولین فکری که می‌رسد این است که N را زیاد کنی. ولی N داخل خود قاعده است: با N+1، باقی‌ماندهٔ هر کلید دوباره حساب می‌شود و کلیدها جای تازه‌ای پیدا می‌کنند. این یعنی یک مهاجرت کامل، درست در لحظه‌ای که به ظرفیت نیاز داری. راه‌حل این است که قاعده را از تعداد ماشین‌ها جدا کنی: یک بار تعداد زیادی پارتیشن بسازی و بعد فقط بگویی کدام پارتیشن روی کدام ماشین است.When capacity runs short, the first instinct is to raise N. But N sits inside the rule: with N+1 every key's remainder is recomputed and the keys land somewhere new. That is a full migration, exactly when you need capacity. The fix is to separate the rule from the machine count: create many partitions once, then only decide which partition lives on which machine.

بهای از دست دادن ترتیبThe price of losing order

وقتی کلید را هش می‌کنی، دیگر نمی‌دانی کلیدهای نزدیک کجا هستند. هر پرس‌وجویی که بازه بخواهد، از «سفارش‌های این ماه» تا «کاربران الف تا م»، باید به همهٔ شاردها برود و جواب‌ها را ادغام کند. این کار هم تأخیر را بالا می‌برد، چون کندترین شارد جواب را تعیین می‌کند، و هم بار را چند برابر می‌کند. اگر بیشتر پرس‌وجوهایت بازه‌ای است، تقسیم هشی مشکل را جابه‌جا می‌کند نه حل.Once keys are hashed, you no longer know where nearby keys are. Any range query, from this month's orders to users whose names start with A through M, has to visit every shard and merge the answers. That raises latency, because the slowest shard sets the pace, and multiplies the load. If most of your queries are range queries, hash sharding moves the problem rather than solving it.

تلهٔ رایجThe usual trap

قانون mod N را مستقیم در کد اپلیکیشن می‌نویسی و بعد یک ماشین اضافه می‌کنی. حالا تقریباً همهٔ کلیدها جابه‌جا می‌شوند و کش‌ها سرد؛ همان ساعتی که برای ظرفیت اضافه آمده بودی، تأخیر دو برابر می‌شود.You hard-code mod N in the application and later add one machine. Almost every key now moves and the caches go cold, so the hour you added capacity for doubles your latency instead.

همه یک کلید را صدا می‌زنند. کدام ماشین می‌سوزد؟Everyone wants the same key. Which machine melts?

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

زنگ تفریح مدرسه، همهٔ بچه‌ها سراغ همان یک بوفه می‌روند، چون فقط آن‌جا ساندویچ گرم دارد. صف تا وسط راهرو می‌رسد و بوفهٔ روبه‌رو خالی می‌ماند. هیچ‌کس دادهٔ بیشتری ندارد؛ فقط همه یک‌جا را می‌خواهند، و در پایگاه داده هم یک شارد می‌تواند همین بوفه باشد.At school break every kid runs to the same canteen window, because it is the only one with hot sandwiches. The queue reaches halfway down the corridor while the window opposite stands empty. Nobody holds more data; everyone just wants the same place, and in a database one shard can be that window.

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

بوفه ساندویچ کافی دارد، ولی دست کافی ندارد؛ مشکل کمبود غذا نیست، مشکل این است که همهٔ سفارش‌ها به یک نفر می‌رسد. حالا فرض کن همان ساندویچ محبوب را بوفهٔ دوم هم بفروشد: صف نصف می‌شود، بدون اینکه غذا بیشتر شود. ولی اگر فقط یک بوفه بلد باشد آن را بپزد، اضافه کردن بوفهٔ سوم هیچ کمکی نمی‌کند. نقطهٔ داغ هم همین‌طور است: تا وقتی همهٔ درخواست‌های یک کلید به یک جا برسد، ماشین‌های دیگر تماشاچی‌اند.The canteen has enough sandwiches, but not enough hands; the shortage is not food, it is that every order goes to one person. Now suppose the second window also sells the popular sandwich: the queue halves without any extra food. But if only one window knows how to make it, adding a third window changes nothing. A hot spot behaves the same way: while every request for one key lands in one place, the other machines are spectators.

شارد ۱Shard 1شارد ۲Shard 2شارد ۳Shard 3بار معمولNormal loadاوج ظهرNoon peakاوج شبNight peakیک شارد در هر دو ساعت اوج، همهٔ فشار را می‌گیردone shard takes all of it at both peaks
بار یکنواخت نیست: یک شارد در ساعت‌های اوج همهٔ فشار را می‌گیرد و بقیه بیکار می‌مانند.Load is not uniform: one shard takes all the pressure at peak hours while the others sit idle.

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

نقطهٔ داغ شاردی است که بارش به‌طور نامتناسبی از بقیه بیشتر است؛ نه چون داده‌اش بیشتر است، بلکه چون درخواست‌های بیشتری به آن می‌رسد. سه علت رایج دارد. اول، یک کلید داغ: حساب یک کاربر مشهور یا محصولی که همه همزمان می‌خواهند. دوم، بازهٔ داغ در تقسیم بازه‌ای: اگر کلید زمان باشد، همهٔ نوشتن‌های امروز روی آخرین بازه می‌افتند. سوم، کلید با تنوع کم، مثل وضعیت سفارش، که همهٔ رکوردهای یک مقدار را یک‌جا جمع می‌کند. درمان‌ها به علت بستگی دارد: کش کردن خواندن کلید داغ، شکستن کلید با پسوند تصادفی، جدا کردن مستأجر بزرگ روی شارد خودش، یا تقسیم همان بازه. هیچ‌کدام جادو نیست: ظرفیت یک کلید از ظرفیت یک شارد بیشتر نمی‌شود.A hot spot is a shard whose load is far above the others, not because it stores more data but because more requests reach it. Three causes are common. A hot key: a celebrity account or a product everyone wants at once. A hot range under range partitioning: if the key is a timestamp, today's writes all land on the last range. A low-cardinality key, such as order status, piles every record with one value in one place. The treatment follows the cause: cache reads of the hot key, split the key with a random suffix, isolate the large tenant on its own shard, or split that range. None of it is magic: one key can never get more capacity than one shard.

1 shardسقف ظرفیت یک کلید، ظرفیت یک شارد استthe ceiling for one key is one shard
QPSبار هر شارد را جدا بسنج؛ میانگین خوشه داغی را پنهان می‌کندmeasure load per shard; the cluster average hides the heat
p99تأخیر دم، اولین چیزی که از شارد داغ ضربه می‌خوردtail latency is the first casualty of a hot shard

کلید داغ را چطور بشکنیمHow to break a hot key

اگر یک کلید داغ داری، می‌توانی همان کلید را به K کلید بشکنی: نوشتن با یک پسوند تصادفی به یکی از K شارد می‌رود و خواندن هر K را می‌خواند و جمع می‌کند. این کار ظرفیت نوشتن را K برابر می‌کند، ولی خواندن را K برابر پرهزینه‌تر و پیچیده‌تر. برای خواندن معمولاً کش جلوی این هزینه را می‌گیرد. اگر ترافیک بیشتر خواندن است، شاید شکستن ارزشش را نداشته باشد و همان کش کافی باشد.With one hot key you can split it into K keys: a write goes to one of K shards behind a random suffix, and a read visits all K and combines them. That multiplies write capacity by K, and makes reads K times more expensive and more complicated. A cache usually absorbs that read cost. If your traffic is mostly reads, splitting may not be worth it and the cache alone is enough.

بازهٔ داغ، وقتی زمان کلید استA hot range when time is the key

در تقسیم بازه‌ای، کلید زمان بدترین انتخاب برای نوشتن است: هر نوشتن تازه به آخرین بازه می‌رسد، پس یک شارد همهٔ نوشتن‌ها را می‌گیرد و بقیه فقط دادهٔ قدیمی را نگه می‌دارند. یک راه این است که کلید را ترکیب کنی: بخشی از کلید هش‌شده و بخشی زمان، تا نوشتن‌ها پخش شوند و پیمایش بازه‌ای هم دست‌کم در یک بازهٔ کوتاه ممکن بماند. راه دیگر این است که بازهٔ آخر را زودتر تقسیم کنی، ولی تقسیم هم خودش داده جابه‌جا می‌کند.Under range partitioning a timestamp is the worst write key: every new write reaches the last range, so one shard takes all writes and the others only hold old data. One answer is a composite key, part hash and part time, so writes spread out while a scan over a short window still works. Another is to split the newest range early, though splitting moves data as well.

تلهٔ رایجThe usual trap

می‌بینی یک شارد داغ است و شارد اضافه می‌کنی. اگر داغی از یک کلید باشد، شارد تازه هیچ درخواستی نمی‌گیرد؛ آن کلید هنوز یک‌جاست. اول باید بفهمی داغی از کلید است یا از بازه، بعد درمان را انتخاب کنی.You see one hot shard and add another. If the heat comes from a single key, the new shard gets no traffic; the key is still in one place. Find out whether the heat is a key or a range before choosing the cure.

key-range sharding

تقسیم بازه‌ای فصل ۷Chapter 7 · دیدن در متنsee in context

کلیدها را مرتب نگه می‌داری. چه چیزی را می‌فروشی؟Keep the keys sorted. What are you selling?

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

در خانه، فاکتورها و کاغذهای اداری را به ترتیب ماه در پوشه‌های برچسب‌دار می‌گذاری: فروردین، اردیبهشت، خرداد و همین‌طور تا اسفند. پیدا کردن کاغذهای یک ماه یعنی باز کردن یک پوشه، نه همه. آخر سال می‌بینی پوشهٔ اسفند کلفت شده و بقیه نازک، چون هر کاغذ تازه به همان پوشه رفته است. تقسیم بازه‌ای هم همین کار را با کلیدها می‌کند.At home you file bills and paperwork by month in labelled folders: January, February, March and on to December. Finding one month's papers means opening one folder, not all of them. By December the folder for the last month has swollen while the others are thin, because every new piece of paper went there. Key-range sharding files keys the same way.

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

این چیدمان یک هدیه و یک صورت‌حساب دارد. هدیه‌اش این است که نزدیک‌ها کنار هم می‌مانند: هر چیزی که به یک ماه مربوط است در یک پوشه است، و پیمایش یک بازه یک پوشه را می‌خواند. صورت‌حسابش این است که پوشهٔ آخر همیشه در حال رشد است، پس همان پوشه‌ای که باید جواب بدهد بیشترین کار را هم دارد. راه‌حل موقت این است که پوشهٔ آخر را زودتر نصف کنی، ولی نصف کردن یعنی جابه‌جا کردن کاغذها؛ اگر کاغذها زیاد باشند، این کار خودش یک پروژه است.The filing has one gift and one bill. The gift is that neighbours stay together: everything about one month is in one folder, and scanning a range reads one folder. The bill is that the last folder keeps growing, so the folder that must answer has the most work. The quick fix is to split the last folder early, but splitting means moving paper; when there is a lot of it, that is a project of its own.

کلیدهای مرتبKeys in orderبازهٔ ۱ · a–fRange 1 · a–fبازهٔ ۲ · g–mRange 2 · g–mبازهٔ ۳ · n–zRange 3 · n–zمرزها را تو می‌گذاری؛ هر بازه روی یک شارد و پیمایش بازه‌ای در یک شاردYou choose the boundaries: one range per shard, and a scan stays inside one
کلیدها مرتب می‌مانند و به بازه‌های پیوسته بریده می‌شوند؛ همین ترتیب پیمایش را ارزان و نوشتن تازه را یک‌جا می‌کند.Keys stay ordered and are cut into contiguous ranges; that order makes scans cheap and new writes land in one place.

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

در تقسیم بازه‌ای، کلیدها را در ترتیب خودشان نگه می‌داری و فضای کلید را به بازه‌های پیوسته می‌بری: از a تا f یک شارد، از g تا m شارد بعدی. مزیتش این است که کلیدهای نزدیک کنار هم می‌مانند، پس پیمایش بازه‌ای ارزان است و معمولاً فقط یک شارد را می‌خواند. قیمتش دو چیز است. اول، نوشتن‌هایی که کلیدشان همیشه بزرگ‌تر می‌شود همه به آخرین بازه می‌رسند؛ اگر کلید زمان باشد، هر نوشتن امروز روی یک شارد می‌افتد. دوم، مرزها را کسی باید تعیین کند: یا اپراتور بازهٔ اولیه را می‌چیند، یا شارد وقتی بزرگ شد خودش نصف می‌شود. تقسیم خودکار یعنی کپی کردن نیمی از داده در همان لحظه، که برای یک شارد خیلی بزرگ گران است.Range sharding keeps keys in their own order and maps the key space to contiguous ranges: a through f in one shard, g through m in the next. Nearby keys stay together, so range scans are cheap and usually read one shard. The price is twofold. First, writes whose key grows monotonically all reach the last range; if the key is time, every write today lands on one shard. Second, someone has to set the boundaries: either an operator picks the initial ranges, or a shard splits itself when it grows. Automatic splitting copies half the data right then, which is expensive for a very large shard.

1 shardپیمایش یک بازه معمولاً فقط یک شارد را می‌خواندa range scan usually reads one shard
log Nیافتن بازه از روی مرزها، با جست‌وجوی دودوییfinding the range from the boundaries, by binary search
½با تقسیم خودکار یک بازه، نیمی از داده جابه‌جا می‌شودan automatic split moves half the data

چرا نوشتن‌های امروز یک‌جا می‌افتندWhy today's writes land in one place

ترتیب کلید همان چیزی است که پیمایش را ارزان می‌کند، و همان چیزی است که نوشتن را به یک سمت می‌راند. کلیدهایی که با زمان یا شمارهٔ سفارش ساخته می‌شوند همیشه بزرگ‌تر از کلیدهای قبلی‌اند، پس همیشه به آخرین بازه می‌رسند. نتیجه این است که یک شارد تمام نوشتن‌های تازه را می‌گیرد و بقیه فقط خواندن را جواب می‌دهند. راه‌حل معمول این است که کلید را ترکیب کنی: یک پیشوند هش‌شده و بعد زمان. با این کار نوشتن‌ها پخش می‌شوند، ولی پیمایش یک بازهٔ زمانی به یک پرس‌وجوی چند-شاردی تبدیل می‌شود و باید جواب‌ها را ادغام کنی.The order of keys is what makes scans cheap, and the same order pushes writes to one side. Keys built from time or an order number are always larger than the previous ones, so they always reach the last range. One shard then takes every new write while the others answer reads only. The usual answer is a composite key: a hashed prefix, then time. Writes spread out, but scanning a time window becomes a multi-shard query that has to merge its answers.

مرزها را کی می‌گذاردWho sets the boundaries

دو سبک رایج هست. در اولی، اپراتور از قبل می‌داند داده چطور پخش می‌شود و مرزها را دستی می‌چیند؛ نتیجه معمولاً متعادل‌تر است، ولی با تغییر الگوی داده کهنه می‌شود. در دومی، شارد وقتی از یک آستانه گذشت خودش نصف می‌شود و نیمهٔ بالایی به گره دیگری می‌رود؛ این سبک خودکار است، ولی هر تقسیم یک کپی کامل از نیمی از داده است و اگر بازه‌ها بد تقسیم شوند، دوباره همان شارد داغ را می‌سازی.Two styles dominate. In the first, an operator knows how the data is distributed and places boundaries by hand; the result is usually better balanced but goes stale as the data pattern changes. In the second, a shard splits itself when it crosses a threshold and the upper half moves to another node. That is automatic, but each split copies half the data, and badly chosen split points simply rebuild the same hot shard.

تلهٔ رایجThe usual trap

کلید زمان را انتخاب می‌کنی چون مرتب و خوانا است، و بعد تعجب می‌کنی که توان نوشتن از یک ماشین بالاتر نمی‌رود. مرتب بودن کلید همان چیزی است که همهٔ نوشتن‌های تازه را به یک بازه می‌فرستد.You pick a timestamp because it sorts and reads nicely, then wonder why write throughput never exceeds one machine. The very order that helps you scan is what sends every new write to one range.

کدام ستون را کلید می‌کنی؟ جواب همه‌چیز را عوض می‌کند.Which column becomes the key? That answer changes everything.

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

در انبار خانه، کارتن‌ها را یا بر اساس اتاقی که وسایلش داخلشان است می‌چینی، یا بر اساس تاریخی که بسته‌بندی شده‌اند. اولی وقتی دنبال یک وسیله می‌گردی فقط یک کارتن به تو می‌دهد؛ دومی موقع اسباب‌کشی مرتب است، ولی پیدا کردن وسیله یعنی باز کردن چند کارتن. کلید تقسیم همان انتخابی است که می‌کنی، و همان انتخاب تعیین می‌کند کدام پرس‌وجو ارزان باشد.In your storage room you pack boxes either by the room their contents belong to, or by the day you packed them. The first hands you one box when you look for something; the second is tidy on moving day, but finding a thing means opening several boxes. The partition key is that choice, and that choice decides which query is cheap.

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

هر انتخاب یک چیز را ارزان و چیز دیگری را گران می‌کند، و هیچ انتخابی هر دو را ارزان نمی‌کند. اگر کلید را از روی چیزی انتخاب کنی که زیاد تکرار می‌شود، مثل تاریخ یا وضعیت، همهٔ کارتن‌ها در یک قفسه جمع می‌شوند. اگر کلید را از روی چیزی انتخاب کنی که یکتاست، قفسه‌ها خوب پخش می‌شوند ولی برای پیدا کردن گروهی باید همه را بگردی. انتخاب درست از روی این می‌آید که بدانی بیشتر وقت‌ها چه می‌پرسی، نه از روی این‌که کدام ستون قشنگ‌تر است.Every choice makes one thing cheap and another expensive, and no choice makes both cheap. Pick a key that repeats a lot, such as a date or a status, and every box ends up on one shelf. Pick a key that is unique, and the shelves spread nicely, but any group search means walking all of them. The right choice comes from knowing what you ask most often, not from which column looks tidier.

کلید: شناسهٔ کاربرKey: user idپخش یکنواخت بین شاردهاeven spread across shardsهر پرس‌وجوی کاربر: یک شاردone shard per user queryبازهٔ زمانی: fan-outtime range: fan-outکلید: زمان ثبتKey: timestampنوشتن امروز: یک شاردtoday's writes: one shardپیمایش بازه: یک شاردrange scan: one shardکجی: همهٔ نوشتن‌ها یک‌جاskew: all writes in one place
انتخاب کلید تعیین می‌کند کدام پرس‌وجو یک‌جا جواب بگیرد و کدام یکی به همهٔ شاردها برود.The key decides which query is answered in one place and which one has to visit every shard.

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

کلید تقسیم تصمیم می‌گیرد هر رکورد کجا بنشیند و کدام پرس‌وجوها بتوانند یک‌جا جواب بگیرند. سه چیز را با هم باید بسنجی: تنوع کلید، یکنواختی دسترسی، و هم‌جهتی با پرس‌وجوی غالب. کلیدی با تنوع بالا مثل شناسهٔ کاربر همه را پخش می‌کند؛ کلیدی مثل وضعیت سفارش که فقط چند مقدار دارد، همه را روی چند شارد جمع می‌کند. اگر همهٔ پرس‌وجوها با شناسهٔ کاربر فیلتر می‌شوند، همان کلید بهترین است، حتی اگر پخشش کامل نباشد. نکتهٔ سخت این است که کلید بعد از نوشتن داده قابل تغییر نیست: عوض کردنش یعنی نوشتن دوبارهٔ همهٔ رکوردها در جای تازه. کلید مرکب هم همین منطق را دارد؛ ترتیب اجزا تعیین می‌کند پیمایش‌های پیشوندی کار کنند یا نه.The partition key decides where each record sits and which queries can be answered in one place. Weigh three things together: cardinality, evenness of access, and alignment with the dominant query. A high-cardinality key such as a user id spreads everything; a key like order status, with only a few values, piles everything onto a few shards. If every query filters by user id, that key is the best one even when the spread is imperfect. The hard part is that the key cannot change after the data is written: changing it means rewriting every record somewhere new. A composite key follows the same logic, and the order of its parts decides whether prefix scans work.

1 keyهر رکورد دقیقاً یک کلید تقسیم دارد و همان سرنوشتش را می‌سازدeach record has exactly one partition key, and it sets its fate
1 shardپرس‌وجویی که کلید را دارد، یک شارد را می‌خواندa query that carries the key reads one shard
N shardsپرس‌وجویی که کلید را ندارد، به همه می‌رودa query without the key visits all of them

چطور انتخابش کنیHow to choose it

اول سه پرس‌وجوی پرتکرارت را بنویس و ببین کدام ستون در همه‌شان هست. بعد تنوع آن ستون را نگاه کن و بعد بدترین حالت را: بزرگ‌ترین مشتری یا پرترافیک‌ترین کاربر چه سهمی از بار یک شارد را می‌گیرد. اگر یک مشتری بزرگ داری، کلید مرکب با شناسهٔ مشتری در ابتدا پرس‌وجوهای او را ارزان می‌کند، ولی همان مشتری می‌تواند یک شارد را اشغال کند؛ در آن حالت شاید بهتر باشد مشتری‌های بزرگ را جدا کنی.Start by writing down your three most frequent queries and see which column appears in all of them. Then check that column's cardinality, and then the worst case: how much of one shard's load the largest customer or the busiest user takes. With one big customer, a composite key led by customer id makes their queries cheap, but that same customer can occupy a shard; isolating large customers may be the better answer.

عوض کردنش بعد از تولید دادهChanging it after the data exists

اگر انتخاب اول اشتباه بود، راهی جز مهاجرت نیست: مدتی هر دو کلید را می‌نویسی، دادهٔ قدیمی را در پس‌زمینه بازنویسی می‌کنی، هر دو مسیر را می‌خوانی و مقایسه می‌کنی، و آخر مسیر قدیمی را می‌بندی. هزینه‌اش با حجم داده بالا می‌رود و در طول مهاجرت دو نسخه از حقیقت داری، پس باید بدانی کدام معتبر است. به همین دلیل انتخاب کلید را قبل از اولین رکورد انجام می‌دهند، نه بعد از اینکه سیستم بزرگ شد.If the first choice was wrong, there is no route but migration: write both keys for a while, rewrite the old data in the background, read both paths and compare, then close the old one. The cost grows with the data, and during the migration you hold two versions of the truth, so you must know which one wins. That is why the key is chosen before the first record, not after the system has grown.

تلهٔ رایجThe usual trap

کلیدی را انتخاب می‌کنی که فقط یکتاست، مثل ایمیل، و بعد می‌بینی همهٔ گزارش‌ها بر اساس تاریخ است. حالا هر گزارش باید به همهٔ شاردها برود؛ یکتایی کلید هیچ ربطی به الگوی دسترسی ندارد.You choose a key because it is unique, such as an email address, and then notice every report is filtered by date. Now each report visits every shard; uniqueness has nothing to do with access pattern.

ماشین تازه آماده است. چطور داده را بی‌وقفه جابه‌جا می‌کنی؟The new machine is ready. Move the data without an outage?

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

دو خواهر یک کمد مشترک دارند. وقتی خواهر سوم بزرگ می‌شود، کمد را یک‌شبه خالی نمی‌کنند؛ قفسه‌به‌قفسه جابه‌جا می‌کنند و هر شب کمد قابل استفاده می‌ماند. بازتوازن هم همین است: داده جابه‌جا می‌شود، ولی سرویس همان شب باید جواب بدهد.Two sisters share a wardrobe. When the third grows old enough for her own shelf, they do not empty the wardrobe overnight; they move it shelf by shelf, and the wardrobe stays usable every evening. Rebalancing is the same: data moves, and the service still has to answer that night.

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

حالا جزئیات را ببین. اول باید بفهمی کدام قفسه‌ها باید جابه‌جا شوند و کدام‌ها سر جایشان بمانند؛ اگر همه را جابه‌جا کنی، کار سه برابر می‌شود و هیچ‌کس چیزی پیدا نمی‌کند. بعد یک نقشه روی در کمد می‌چسبانی که بگوید چه چیزی کجاست، و نقشه را همزمان با جابه‌جایی به‌روز می‌کنی. اگر نقشه عقب بماند، آدم‌ها سراغ قفسهٔ اشتباه می‌روند؛ اگر جابه‌جایی را سریع و بی‌نظم انجام دهی، یک شب همه‌چیز گم می‌شود. بازتوازن هم بین این دو سختی گیر می‌کند: آرام و درست، یا سریع و پرهزینه.Now the details. First you work out which shelves move and which stay; move everything and the work triples while nobody can find anything. Then you tape a map to the wardrobe door saying what is where, and you keep the map current as you move. If the map falls behind, people walk to the wrong shelf; if you move fast and carelessly, everything is lost in one night. Rebalancing is stuck between those two: slow and correct, or fast and expensive.

شارد تازه اضافه شدA shard is addedظرفیت خالی، بدون دادهempty, no data yetانتقال در پس‌زمینهBackground copyبا محدودیت پهنای باندthrottled on purposeمسیر عوض می‌شودRouting flips overاتمیک، در متادیتاatomic, in metadataنسخهٔ قدیمی حذفOld copy droppedبعد از تأییدafter the check
بازتوازن یک لحظه نیست: ظرفیت اضافه می‌شود، داده در پس‌زمینه منتقل می‌شود، مسیر عوض می‌شود و بعد نسخهٔ قدیمی می‌رود.Rebalancing is not one moment: capacity appears, data copies in the background, routing flips, and only then does the old copy go.

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

بازتوازن یعنی وقتی گره یا ظرفیت عوض می‌شود، داده و بار را دوباره بین گره‌ها پخش کنی، بدون اینکه سرویس بایستد. سه راه رایج هست. اول، تعداد زیادی پارتیشن ثابت از قبل بسازی و روی گره‌ها بچینی؛ با افزودن گره فقط چند پارتیشن کامل جابه‌جا می‌شود و متادیتا تغییر کوچکی دارد. دوم، تقسیم پویا: بازهٔ بزرگی که از حدی گذشت نصف می‌شود و نیمه‌اش به گره دیگری می‌رود. سوم، تخصیص متناسب با ظرفیت، که در آن هر گره به نسبت توانش سهم می‌گیرد. در هر سه راه، انتقال باید محدود شود؛ کپی کردن یک بازهٔ بزرگ با تمام پهنای باند، همان شبکه‌ای را می‌خورد که باید به درخواست‌ها جواب بدهد.Rebalancing spreads data and load across nodes again when capacity or membership changes, without stopping the service. Three approaches dominate. First, create many fixed partitions up front and assign them to nodes; adding a node moves a few whole partitions and changes metadata only slightly. Second, dynamic splitting: an oversized range is halved and half moves to another node. Third, proportional assignment, where each node takes a share matching its capacity. In all three the transfer must be throttled: copying a large range at full bandwidth eats the network that has to answer requests.

1/Nسهم داده‌ای که با افزودن یک گره جابه‌جا می‌شودthe share of data that moves when one node joins
10+پارتیشن به ازای هر گره، تا فضای رشد بماندpartitions per node, so there is room to grow
1 rangeواحد جابه‌جایی: یک بازهٔ کامل، نه یک کلیدthe unit of movement is one whole range, not one key

در حین انتقال چه اتفاقی می‌افتدWhat happens during the move

مالکیت یک بازه در متادیتا ثبت می‌شود و کلاینت‌ها و لایهٔ مسیریابی از همان می‌خوانند. انتقال سه مرحله دارد: کپی دادهٔ قدیمی، گرفتن تغییرات تازه تا وقتی دو نسخه همزمان‌اند، و بعد عوض کردن مالکیت در یک قدم. اگر این قدم اتمیک نباشد، مدتی دو گره خودشان را صاحب بازه می‌دانند و نوشتن‌ها دو جا می‌افتند. برای همین سیستم‌های جدی مالکیت را با یک توافق توزیع‌شده ثبت می‌کنند و برای هر بازه یک شمارهٔ نسل نگه می‌دارند.Ownership of a range lives in metadata, and clients and routing tiers read it from there. A move has three stages: copy the old data, tail the new changes while both copies are live, then switch ownership in one step. If that step is not atomic, two nodes consider themselves the owner for a while and writes land in two places. Serious systems therefore record ownership through a distributed agreement and keep a generation number per range.

چرا تعداد پارتیشن مهم استWhy the partition count matters

اگر تعداد پارتیشن‌ها را دقیقاً برابر تعداد گره‌ها بگیری، با اضافه شدن یک گره هیچ پارتیشنی برای جابه‌جایی آزاد نمی‌ماند و باید پارتیشن‌ها را دوباره تقسیم کنی. قاعدهٔ ساده این است که چند برابر گره‌ها پارتیشن داشته باشی تا فضای رشد بماند؛ آن‌وقت افزودن گره یعنی جابه‌جایی چند پارتیشن، نه بازنویسی کل نقشه. ولی پارتیشن زیاد هم رایگان نیست: هر پارتیشن متادیتا و حافظهٔ خودش را می‌خواهد و تعداد فایل‌های باز و کارهای پس‌زمینه را بالا می‌برد.If the partition count equals the node count, adding a node leaves nothing to hand over and the partitions must be re-split. The simple rule is several partitions per node, so there is room to grow; adding a node then means moving a few partitions, not rewriting the map. Partitions are not free either: each carries its own metadata and memory and raises the number of open files and background jobs.

تلهٔ رایجThe usual trap

انتقال را در ساعت اوج و بدون محدودیت پهنای باند انجام می‌دهی. شبکه اشباع می‌شود، تأخیر می‌پرد، و همان شبی که برای اضافه کردن ظرفیت انتخاب کرده‌ای بدترین شب هفته می‌شود.You move the data at peak hour with no bandwidth limit. The network saturates, latency jumps, and the night you picked to add capacity becomes the worst night of the week.

request routing

مسیریابی درخواست فصل ۷Chapter 7 · دیدن در متنsee in context

درخواست رسید. از کجا بفهمی کدام شارد جواب دارد؟The request is in. Which shard owns it?

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

در رستوران شلوغ، خودت سالن را نمی‌گردی. اسمت را به میزبان می‌گویی، او دفتر را نگاه می‌کند و می‌گوید کدام میز، و تو مستقیم همان‌جا می‌نشینی. مسیریابی درخواست هم همین است: کسی باید بداند هر کلید کدام شارد را دارد، تا درخواست به همه‌جا نرود.In a busy restaurant you do not wander the dining room. You give your name to the host, who checks the book and tells you which table, and you walk straight there. Request routing is the same: someone has to know which shard holds each key so a request does not go everywhere.

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

سه راه برای پیدا کردن میز هست، و هر سه یک بهای متفاوت دارند. می‌توانی از هر گارسن بپرسی و او از نفر بعدی بپرسد تا کسی جواب را بداند؛ ساده است، ولی چند پرش طول می‌کشد. می‌توانی میزبان داشته باشی که همه‌چیز را بداند؛ سریع است، ولی خودش یک صف و یک نقطهٔ شکست می‌شود. یا می‌توانی نقشه را کنار در بچسبانی و خودت بخوانی؛ از همه سریع‌تر است، ولی به شرطی که نقشه تازه باشد. نقشه‌ای که دیشب عوض شده و هنوز به‌روز نشده، بدتر از نداشتن نقشه است.There are three ways to find a table, and each has its own price. You can ask any waiter, who asks the next one until somebody knows; simple, but several hops long. You can keep a host who knows everything; fast, but the host becomes a queue and a single point of failure. Or you can tape the map to the door and read it yourself; fastest of all, provided the map is current. A map that changed last night and has not been updated is worse than no map.

کلاینتClientکلید را می‌فرستدsends the keyلایهٔ مسیریابیRouting tierنقشه را می‌داندknows the mapشارد صاحب کلیدOwning shardمتادیتا، نه حدسmetadata, no guessپاسخResponseادغام‌شدهmerged
درخواست از مسیریاب می‌گذرد تا به شاردی برسد که کلید را دارد؛ جواب‌ها همان‌جا ادغام می‌شوند.The request passes a router to reach the shard holding the key; answers are merged on the way back.

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

مسیریابی درخواست یعنی از کلید درخواست به گره‌ای برسی که آن بازه را دارد. سه الگوی رایج هست. در اولی، کلاینت به هر گره‌ای وصل می‌شود و اگر آن گره صاحب کلید نبود، درخواست را به گره درست می‌فرستد و جواب را برمی‌گرداند. در دومی، یک لایهٔ مسیریابی جدا وجود دارد که فقط نقشه را می‌داند و درخواست را به شارد درست می‌رساند. در سومی، خود کلاینت نقشه را نگه می‌دارد و مستقیم به گره درست وصل می‌شود. هر سه به یک چیز وابسته‌اند: نقشهٔ مالکیت. آن نقشه با بازتوازن، خرابی و برگشتن گره عوض می‌شود؛ اگر کلاینت نسخهٔ قدیمی را نگه دارد، درخواست را به صاحب قبلی می‌فرستد. برای پرس‌وجویی که کلید ندارد، مسیریابی معنا عوض می‌کند: درخواست باید به همهٔ شاردها برود و جواب‌ها ادغام شوند، و آن‌وقت کندترین شارد تعیین می‌کند کل پاسخ چقدر طول بکشد.Request routing means getting from a request's key to the node that owns its range. Three patterns are common. In the first, a client connects to any node, and a node that does not own the key forwards the request and returns the answer. In the second, a separate routing tier holds the map and sends each request to the right shard. In the third, the client keeps the map and connects directly to the right node. All three depend on one thing: the ownership map, which changes on rebalancing, failure and recovery. A client holding an old copy sends the request to the previous owner. For a query with no key, routing changes meaning: the request must visit every shard and merge the answers, and the slowest shard then sets the total latency.

1 hopپرش اضافه‌ای که لایهٔ مسیریابی به هر درخواست می‌دهدthe extra hop a routing tier adds to every request
N shardپرس‌وجوی بدون کلید: fan-out به همهa query without the key fans out to all
1 slow shardکندترین شارد، تأخیر کل پاسخ را تعیین می‌کندthe slowest shard sets the whole response time

نقشه را از کجا بدانیمWhere the map comes from

نقشهٔ مالکیت خودش یک مسئلهٔ توزیع‌شده است. یک راه این است که گره‌ها با شایعه‌پرانی وضعیت هم را بدانند و هر گره نقشهٔ کامل را در حافظه داشته باشد؛ ساده است، ولی نقشه با تأخیر به همه می‌رسد و در لحظهٔ تغییر ممکن است دو گره یک چیز متفاوت فکر کنند. راه دیگر یک سرویس هماهنگی متمرکز است که مرجع نقشه است؛ تازه‌تر است، ولی هر خطای آن به همهٔ مسیرها می‌رسد. راه سوم، همان نقشه در خود کلاینت است که باید با نسخه و انقضا نگه داشته شود.The ownership map is a distributed problem in itself. One way is for nodes to gossip membership so every node holds the whole map in memory; simple, but the map reaches nodes late and two nodes may briefly disagree during a change. Another is a central coordination service as the source of truth; fresher, but its failure reaches every route. The third is the map inside the client, which must be cached with a version and an expiry.

وقتی کلید را نداری: fan-outWhen there is no key: fan-out

هر پرس‌وجویی که کلید تقسیم را ندارد، یا باید به همهٔ شاردها برود یا به ایندکس سراسری تکیه کند. در حالت اول، جواب وقتی آماده است که کندترین شارد جواب بدهد، پس تأخیر دم بدتر می‌شود: اگر هر شارد یک درصد درخواست‌ها را کند جواب بدهد، با ده شارد احتمال اینکه یکی کند باشد به حدود ده درصد می‌رسد. راه‌های عملی این است که تعداد شاردهای درگیر را کم کنی، شارد کند را جدا کنی، یا برای این پرس‌وجوها یک مسیر کش جدا بسازی.Any query without the partition key must either visit every shard or lean on a global index. In the first case the answer is ready when the slowest shard replies, so tail latency gets worse: if each shard is slow on one percent of requests, ten shards make a slow one likely about a tenth of the time. The practical answers are to reduce the number of shards involved, isolate the slow shard, or build a separate cached path for those queries.

تلهٔ رایجThe usual trap

نقشه را یک بار در کلاینت کش می‌کنی و دیگر به‌روزش نمی‌کنی. بعد از یک failover، نوشتن‌ها به گره‌ای می‌روند که دیگر صاحب آن بازه نیست؛ بعضی‌شان بی‌صدا رد می‌شوند و تو تازه موقع خواندن می‌فهمی.You cache the map in the client once and never refresh it. After a failover, writes go to a node that no longer owns the range; some are silently dropped and you only find out when you read.

secondary index

ایندکس ثانویه فصل ۷Chapter 7 · دیدن در متنsee in context

کلید اصلی جواب نمی‌دهد. حالا کجا بگردی؟The primary key is no help. Where do you look now?

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

در یخچال، غذاها بر اساس طبقه چیده شده‌اند: لبنیات یک طبقه، سبزیجات طبقهٔ دیگر. ولی روی در یخچال کاغذی چسبانده‌ای که می‌گوید غذای هر کس کدام طبقه است. برای پیدا کردن غذای علی به کاغذ نگاه می‌کنی، نه به همهٔ ظرف‌ها. ایندکس ثانویه هم همان کاغذ است: فهرستی روی چیزی جز کلید اصلی.In the fridge, food is arranged by shelf: dairy on one, vegetables on another. But a note on the door says which shelf holds whose food. To find Ali's lunch you read the note instead of opening every container. A secondary index is that note: a listing keyed by something other than the primary key.

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

این کاغذ دو شکل می‌تواند داشته باشد. یا هر طبقه کاغذ خودش را دارد و هر کس غذایش را می‌گذارد، همان‌جا کاغذ طبقه را هم به‌روز می‌کند؛ به‌روز کردن آسان است، ولی برای پیدا کردن «همهٔ لبنیات» باید کاغذ همهٔ طبقه‌ها را بخوانی. یا یک کاغذ مرکزی روی در داری که همه‌چیز در آن است؛ پیدا کردن یک‌جاست، ولی با گذاشتن هر غذا باید دو جا را به‌روز کنی، و اگر یکی را فراموش کنی کاغذ دروغ می‌گوید. ایندکس محلی و سراسری دقیقاً همین دو انتخاب‌اند.That note can take two shapes. Either each shelf keeps its own note, and whoever puts food away updates that shelf's note; updating is easy, but finding all the dairy means reading every shelf's note. Or one master note hangs on the door; lookup is one place, but putting food away means updating two places, and if you forget one the note lies. Local and global indexes are exactly those two choices.

ایندکس محلیLocal indexهر شارد فهرست خودشeach shard keeps its ownنوشتن: همان یک شاردwrite: that one shardخواندن: همهٔ شاردهاread: every shardایندکس سراسریGlobal indexفهرست جدا شارد شدهthe index is sharded tooنوشتن: داده و ایندکسwrite: data and indexخواندن: یک شاردread: one shard
ایندکس محلی نوشتن را ارزان می‌کند، ایندکس سراسری خواندن را؛ هیچ‌کدام هر دو را ارزان نمی‌کند.A local index makes writes cheap, a global index makes reads cheap; neither makes both cheap.

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

ایندکس ثانویه فهرستی روی ستونی جز کلید تقسیم است. دو راه برای شارد کردنش وجود دارد. در ایندکس محلی، یا document-partitioned، هر شارد ایندکس رکوردهای خودش را نگه می‌دارد؛ نوشتن فقط همان شارد را لمس می‌کند، ولی هر خواندن باید به همهٔ شاردها برود و جواب‌ها را جمع کند. در ایندکس سراسری، یا term-partitioned، خود ایندکس بر اساس مقدار ایندکس‌شده شارد می‌شود؛ خواندن یک شارد را می‌خواند، ولی نوشتن باید هم داده و هم ایندکس را به‌روز کند، و آن دو معمولاً روی دو شارد مختلف‌اند. همین دو نوشتن است که کار را سخت می‌کند: یا باید با یک پروتکل اتمیک هماهنگ شوند، یا ایندکس با تأخیر به‌روز شود و خواندن کمی کهنه باشد. حذف هم به همان اندازه مهم است؛ اگر رکوردی پاک شود و مدخل ایندکس بماند، جست‌وجو به جای خالی اشاره می‌کند.A secondary index is a listing on a column other than the partition key, and there are two ways to shard it. In a local, or document-partitioned, index each shard indexes its own records: a write touches only that shard, but every read must visit all shards and merge. In a global, or term-partitioned, index the index itself is sharded by the indexed value: a read touches one shard, but a write must update both the data and the index, usually on two different shards. Those two writes are the hard part: they either need an atomic protocol, or the index is updated late and reads go slightly stale. Deletes matter just as much; if a record is removed and its index entry stays, the search points at nothing.

N shardخواندن با ایندکس محلی: به همهٔ شاردهاreading a local index: every shard
2 shardنوشتن در ایندکس سراسری: داده و ایندکسwriting a global index: data and index
1 shardخواندن با ایندکس سراسری: یک شاردreading a global index: one shard

صورت‌حساب fan-outThe fan-out bill

با ایندکس محلی، هر جست‌وجو به تعداد شاردها پرس‌وجو می‌فرستد. این یعنی بار هر جست‌وجو N برابر می‌شود و تأخیرش به کندترین شارد گره می‌خورد؛ با بیست شارد، یک کندی کوچک در یکی از آن‌ها کافی است تا کل جست‌وجو کند شود. راه‌های عملی این است که نتایج را محدود کنی و از هر شارد فقط چند ردیف اول را بخواهی، تعداد شاردها را کم نگه داری، یا نتیجهٔ این جست‌وجوهای پرهزینه را کش کنی.With a local index every search sends one query per shard. That multiplies the work by N and ties latency to the slowest shard; with twenty shards, a small hiccup in one is enough to slow the whole search. The practical answers are to ask each shard for only the top few rows, keep the shard count modest, or cache the results of the expensive searches.

ایندکس سراسری: دو نوشتن، یک ترتیبThe global index: two writes, one order

در ایندکس سراسری، نوشتن یک رکورد به دو جای مختلف می‌رسد. اگر اول داده را بنویسی و بعد ایندکس را، بین این دو یک پنجره هست که جست‌وجو رکورد را پیدا نمی‌کند؛ اگر برعکس، مدتی به رکوردی اشاره می‌شود که هنوز نیست. برای همین بیشتر سیستم‌ها ایندکس را غیرهمزمان و با صف به‌روز می‌کنند و پنجرهٔ کهنگی را می‌پذیرند. اگر به‌روزرسانی اتمیک لازم باشد، باید یک تراکنش دوتایی روی دو شارد بزنی، که هم کندتر است و هم وقتی هماهنگ‌کننده می‌میرد شرکت‌کننده‌ها را معلق می‌کند.With a global index, writing one record reaches two different places. Write the data first and the index second, and there is a window where a search cannot find the record; reverse the order and for a moment the index points at a record that does not exist yet. Most systems therefore update the index asynchronously through a queue and accept a staleness window. If an atomic update is required, you need a two-shard transaction, which is both slower and leaves participants in doubt when the coordinator dies.

تلهٔ رایجThe usual trap

ایندکس سراسری را غیرهمزمان به‌روز می‌کنی و بعد در کد فرض می‌کنی جست‌وجو تازه است. کاربر رکوردی را ویرایش می‌کند و بلافاصله در نتایج نسخهٔ قدیمی را می‌بیند.You update the global index asynchronously and then assume in code that search is fresh. A user edits a record and immediately sees the old version in the results.

service discovery

کشف سرویس فصل ۷Chapter 7 · دیدن در متنsee in context

سرویس کجاست؟ آدرسش عوض شد و کسی خبر نداشت.Where is the service now that its address changed?

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

می‌خواهی به مطب دندان‌پزشک زنگ بزنی. شماره‌ای که پارسال روی یخچال چسبانده‌ای جواب نمی‌دهد، چون مطب عوض شده. این بار، اول تابلو اعلانات ساختمان را نگاه می‌کنی که شمارهٔ امروز رویش نوشته شده. کشف سرویس هم همان تابلو است: یک جای مرکزی که می‌گوید الان کدام نمونه زنده است و کجا.You want to call the dentist. The number you stuck on the fridge last year does not answer, because the practice moved. This time you check the noticeboard in the lobby, where today's number is written. Service discovery is that board: a place that says which instances are alive right now and where.

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

تابلو فقط وقتی به کار می‌آید که کسی مرتب به‌روزش کند. اگر مطب اسباب‌کشی کند و شمارهٔ قدیمی پاک نشود، زنگ می‌زنی و کسی جواب نمی‌دهد؛ بدتر، اگر آن شماره به مطب دیگری برسد، اطلاعاتت را جای اشتباه می‌گویی. طرف دیگرش این است که هر بار پرسیدن از تابلو هم وقت و بار دارد؛ اگر هر ثانیه سراغش بروی، خود تابلو گلوگاه می‌شود. راه معمول این است که جواب را مدتی نگه داری: نه آن‌قدر طولانی که کهنه شود، نه آن‌قدر کوتاه که هر بار از صفر شروع کنی.The board only helps if someone keeps it current. If the practice moves and the old number is not erased, you call and nobody answers; worse, if that number is reassigned, you give your details to the wrong place. Asking the board costs something too: check it every second and the board itself is the bottleneck. The usual answer is to keep the answer for a while: long enough to be worth it, short enough not to go stale.

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

کشف سرویس دو نیمه دارد: ثبت و جست‌وجو. نمونه‌ها هنگام بالا آمدن خودشان را با یک lease یا TTL ثبت می‌کنند و با heartbeat تمدید می‌کنند؛ وقتی heartbeat قطع شود، ثبت منقضی می‌شود و آدرس از فهرست می‌افتد. جست‌وجو دو مدل دارد: سمت کلاینت، که خود کلاینت فهرست را می‌گیرد و بین نمونه‌ها یکی را انتخاب می‌کند، و سمت سرور، که یک متعادل‌کننده یا پروکسی انتخاب را انجام می‌دهد. DNS هم می‌تواند همین کار را بکند، ولی TTL و کش شدنش با سرعت انتشار آدرس‌ها می‌جنگد. نکته‌ای که بیشتر از همه اشتباه می‌شود تفاوت liveness و readiness است: پروسه‌ای که زنده است ولی به دیتابیس نرسیده نباید ترافیک بگیرد. سیستم‌های جدی فهرست را در حافظهٔ محلی کش می‌کنند تا قطع شدن خودِ سرویس کشف، اتصال‌های موجود را نخواباند.Service discovery has two halves: registration and lookup. Instances register themselves on startup with a lease or TTL and renew it with a heartbeat; when the heartbeat stops the registration expires and the address drops out of the list. Lookup comes in two models: client-side, where the client fetches the list and picks an instance, and server-side, where a load balancer or proxy makes the choice. DNS can do the job too, but its TTL and caching fight against how fast addresses change. The detail people get wrong most often is the difference between liveness and readiness: a process that is up but has not reached its database must not take traffic. Serious systems cache the list locally so an outage of the discovery service itself does not break existing connections.

TTLمدتی که یک ثبت معتبر می‌ماند و بعد منقضی می‌شودhow long a registration stays valid before it expires
2 modelsکشف سمت کلاینت یا سمت سرورclient-side or server-side lookup
2 probesliveness و readiness؛ دومی تعیین می‌کند ترافیک برسدliveness and readiness; the second decides who gets traffic

تلهٔ رایجThe usual trap

جدا نکردن liveness از readiness. نمونه‌ای که بالا آمده ولی هنوز به دیتابیس وصل نشده ترافیک می‌گیرد و هر درخواست timeout می‌شود؛ از بیرون به‌نظر می‌رسد سرویس هست، فقط کند است. و کش کردن DNS با TTL طولانی، بعد از هر دیپلوی چند دقیقه خطا می‌دهد.Not separating liveness from readiness. An instance that is up but not yet connected takes traffic and times out every request; from outside the service looks present, only slow. And a long DNS TTL turns every deploy into a few minutes of errors.

یه ماشین دیگه جواب نمی‌ده. بعدش چی؟One machine isn't enough anymore. Now what?

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

گوشی‌ات ۶۴ گیگ است و پر شده. عکس‌های پارسال را می‌ریزی روی کارت حافظه، بقیه می‌ماند روی گوشی. حالا دو سطل داری به‌جای یکی، هر فایل هم فقط یک جا است، و گوشی‌ات جا باز کرده. تقسیمِ دادهٔ یک سیستم هم دقیقاً همین حرکت است، فقط با کلید و قاعده.Your phone has 64 GB and it is full. You move last year's photos to an SD card and keep the rest on the phone. Now you have two buckets instead of one, every file still lives in exactly one place, and your phone has room again. Sharding a database is the same move, done with a rule instead of your thumb.

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

سوپرمارکت بزرگ را در نظر بگیر. مدیر فروشگاه محصولات را به راهروها تقسیم می‌کند: لبنیات راهروی دو، نان راهروی پنج، نوشیدنی راهروی نه. سه چیز با همین کار درست می‌شود. اول، هر راهرو فقط بخشی از کالا را نگه می‌دارد، پس هیچ راهرویی به اندازهٔ کل فروشگاه بزرگ نمی‌شود. دوم، چند مشتری می‌توانند همزمان در راهروهای مختلف خرید کنند و به هم برنخورند. سوم، برای پیدا کردن ماست باید بدانی کدام راهرو؛ فروشگاه بدون آن قاعدهٔ ساده فقط یک انبار به‌هم‌ریخته است. تقسیم داده هم همین قرار را با تو می‌گذارد: یک قاعده که بگوید هر رکورد کدام تیکه است.Picture a large supermarket. The manager splits products across aisles: dairy in aisle two, bread in aisle five, drinks in aisle nine. That split buys three things. Each aisle holds only a slice of the stock, so no aisle has to be as big as the whole shop. Several shoppers can fill their carts in different aisles at the same time without colliding. And to find yoghurt you have to know the aisle; without that simple rule the shop is just a warehouse in chaos. Sharding makes the same deal with you: one rule that says which piece a record belongs to.

همهٔ رکوردهاAll recordsتیکه ۱ · کلیدهای a–fShard 1 · keys a–fتیکه ۲ · کلیدهای g–mShard 2 · keys g–mتیکه ۳ · کلیدهای n–zShard 3 · keys n–zهر تیکه روی یک ماشین جدا، و هر ماشین می‌تواند چند تیکه را هم نگه داردEach piece on its own machine; one machine may also hold several pieces
همهٔ رکوردها با یک قاعده به چند تیکه می‌شکنند؛ هر رکورد فقط در یک تیکه.One rule splits all records into pieces, and every record belongs to exactly one piece.

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

در سطح فیزیکی، sharding یعنی مجموعهٔ داده و بار نوشتن را بین چند ماشین پخش کنی، طوری که هر رکورد دقیقاً در یک shard باشد. برای این کار دو تصمیم لازم است: یک partition key انتخاب کنی، و یک الگوریتم که از آن کلید به shard برسد. دو خانوادهٔ اصلی وجود دارد. تقسیم بازه‌ای کلیدها را مرتب نگه می‌دارد، پس پیمایش بازه‌ای مثل «همهٔ سفارش‌های این ماه» ارزان است، ولی اگر داده بر اساس زمان نوشته شود، همهٔ نوشتن‌های امروز روی یک shard می‌افتد و آن shard داغ می‌شود. تقسیم هشی این کجی را پخش می‌کند، ولی ترتیب را از دست می‌دهی و پیمایش بازه‌ای به پرس‌وجوی fan-out تبدیل می‌شود. نکتهٔ مهم این است که sharding و replication دو کار جدا هستند: sharding ظرفیت را زیاد می‌کند، replication تحمل خرابی را، و در عمل هر shard خودش چند replica دارد.Concretely, sharding spreads the data set and the write load across machines so that each record sits in exactly one shard. That needs two decisions: a partition key, and an algorithm that maps that key to a shard. There are two main families. Key-range sharding keeps keys ordered, so a range scan such as “all orders this month” is cheap, but if rows are written in time order every write of the day lands on one shard and that shard goes hot. Hash sharding spreads the skew away, at the cost of ordering: a range query becomes a fan-out query. The point people miss is that sharding and replication solve different problems: sharding adds capacity, replication adds fault tolerance, and in production each shard is itself replicated.

1 shardهر رکورد دقیقاً در یک shard؛ نه بیشترevery record in exactly one shard, never two
N → N+1با hash % N، افزودن یک ماشین تقریباً همهٔ کلیدها را جابه‌جا می‌کندwith hash % N, adding one machine moves almost every key
≈ 10–20%سهم معمول یک shard داغ از کل بار وقتی کلید بد انتخاب شودa badly chosen key can put this much of total load on one shard

چرا shard کردن سخت است، وقتی خودِ تیکه‌کردن آسان است؟Why sharding is hard when splitting is easy

تیکه‌کردن داده در یک جمله تمام می‌شود؛ چیزی که سال‌ها وقت می‌برد سه چیز دیگر است. اول، کلید. کلید بد یعنی یک shard همهٔ بار را می‌کشد و بقیه بیکار می‌نشینند، و عوض کردن کلید بعد از تولید داده یعنی مهاجرت دادن همه چیز. دوم، جابه‌جایی. با افزودن یک ماشین باید داده جابه‌جا شود، و جابه‌جایی هم شبکه می‌خورد هم دیسک هم زمان، و در همان حین سیستم باید جواب بدهد. سوم، پرس‌وجوهایی که کلید ندارند. هر query که partition key را نمی‌داند باید به همهٔ shardها برود و جواب‌ها را جمع کند، و همین جاست که هزینهٔ پنهان تقسیم صورت‌حساب می‌شود.Splitting data takes one sentence; the three things that take years are elsewhere. First, the key. A bad key means one shard carries all the load while the rest idle, and changing the key after data exists means migrating everything. Second, movement. Adding a machine has to move data, and moving data costs network, disk and time while the system keeps serving. Third, queries without the key. Any query that does not know the partition key has to visit every shard and merge the answers, and that is where the hidden bill for sharding arrives.

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

اگر داده‌ات جا می‌شود و بارت با یک ماشین بزرگ جواب می‌دهد، sharding فقط پیچیدگی می‌خرد. یک سرور قوی با دیسک NVMe و ۲۵۶ گیگ رم، تا حجم زیادی از کارهای واقعی را راحت انجام می‌دهد، و هزینهٔ نگهداری‌اش از هزینهٔ یک خوشهٔ سه‌نودی با همهٔ مسائلش کمتر است. sharding را وقتی برو که یا داده از یک ماشین بزرگ‌تر شده، یا نوشتن از توان یک ماشین گذشته، یا قانونی می‌گوید داده باید در چند منطقه بماند. قبل از آن، ایندکس بهتر، کش و پاک‌سازی کوئری‌های کند، معمولاً ارزان‌تر جواب می‌دهد.If your data fits and one large machine serves your load, sharding buys you complexity and nothing else. A single strong server with NVMe storage and 256 GB of RAM handles a surprising amount of real work, and costs less to operate than a three-node cluster with all of its failure modes. Reach for sharding when the data no longer fits one machine, when writes exceed what one machine can take, or when a regulation says data must stay in several regions. Before that, a better index, a cache and fixing the slow queries is usually the cheaper answer.

تلهٔ رایجThe usual trap

قاطی کردن sharding با replication. آدم‌ها می‌گویند «سه نسخه داریم، پس تقسیم شدیم» در حالی که سه نسخه از یک داده، ظرفیت نوشتن را سه برابر نمی‌کند. برعکسش هم رایج است: تیکه‌کردن بدون نسخهٔ دوم، ظرفیت می‌دهد ولی یک خرابی دیسک کل آن تیکه را می‌برد.Confusing sharding with replication. People say “we have three copies, so we are partitioned” when three copies of the same data do not triple write capacity. The mirror error is just as common: splitting without a second copy adds capacity but one disk failure takes the whole piece with it.

میانگین خوب است. چرا یک شارد نفسش بند آمده؟The average looks fine. So why is one shard gasping?

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

بشقاب‌های یک مهمانی را بین سه میز بر اساس رنگ تقسیم می‌کنی: سفید، آبی، گل‌دار. رنگ سفید نیمی از بشقاب‌هاست، پس میز سفیدها دو برابر بقیه کار دارد. هیچ‌کس هجوم نیاورده و هیچ میزی خراب نیست؛ فقط تقسیم‌بندی ناهموار است. کجی یعنی همین: نابرابری از جنس تقسیم، نه از جنس هجوم.You split the plates for a party across three tables by colour: white, blue, patterned. White is half the plates, so the white table has twice the work. Nobody rushed anywhere and no table is broken; the split itself is uneven. Skew is exactly that: imbalance that comes from the division, not from a rush.

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

حالا هزینه‌اش را ببین. میز سفیدها باید زودتر شروع کند و دیرتر تمام کند؛ اگر سه میز را با یک اندازه بخری، یکی از آن‌ها برای این مهمانی کوچک است و دو تای دیگر جای خالی دارند. میانگین می‌گوید هر میز ۴۰ بشقاب دارد و همه‌چیز خوب است؛ ولی میز سفیدها ۶۰ بشقاب دارد و همان میز سرعت کل مهمانی را تعیین می‌کند. کجی همیشه با میانگین پنهان می‌شود؛ چیزی که لو می‌رود بیشترین مقدار است، نه متوسط.Now the cost. The white table has to start earlier and finish later; buy three identical tables and one is too small for the party while the other two sit half empty. The average says every table has forty plates and all is well, but the white table has sixty and it sets the pace of the whole evening. Skew always hides behind the average; the number that gives it away is the maximum.

12kشارد ۱Shard 111kشارد ۲Shard 213kشارد ۳Shard 348kشارد ۴Shard 4
حجم شاردها یکسان نیست: یکی چند برابر بقیه است و همان یکی سقف واقعی سیستم را تعیین می‌کند.The shards are not the same size: one is several times the others, and that one sets the real ceiling.

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

کجی یعنی توزیع داده یا بار بین بخش‌ها ناهموار است. برخلاف نقطهٔ داغ که معمولاً گذرا و مربوط به دسترسی است، کجی ساختاری است: یک شارد از اول بزرگ‌تر است یا کلیدها به‌طور طبیعی به یک سمت می‌چسبند. اندازه‌گیری‌اش ساده است: نسبت بیشترین بار یا حجم یک شارد به میانگین، و توزیع کامل شاردها نه فقط میانگین. میانگین می‌تواند سالم به نظر برسد در حالی که یک شارد دو برابر بقیه است و همان شارد ظرفیت واقعی سیستم را تعیین می‌کند. کجی سه هزینه دارد: برنامه‌ریزی ظرفیت باید بر اساس بیشترین باشد نه میانگین، جابه‌جایی آن شارد بزرگ طولانی‌تر است، و نسخهٔ پشتیبانش هم دیرتر تمام می‌شود. درمانش به منشأ بستگی دارد: کلید بهتر، پارتیشن‌های بیشتر، یا شکستن مستأجر بزرگ.Skew means data or load is distributed unevenly across the pieces. Unlike a hot spot, which is usually transient and about access, skew is structural: one shard is larger from the start, or the keys naturally clump to one side. Measuring it is straightforward: the ratio of the busiest shard's load or size to the mean, and the full distribution rather than the average. The mean can look healthy while one shard is twice the others, and that shard sets the real capacity. Skew costs three things: capacity planning must use the maximum, moving that large shard takes longer, and its backup finishes later. The cure depends on the source: a better key, more partitions, or splitting the large tenant.

max/meanنسبت شارد پرکار به میانگین؛ معیار واقعی ناهمواریbusiest shard over mean: the real measure of imbalance
≈ 1.1نسبت قابل قبول در توزیع خوب؛ بیشتر از این یعنی کجیa healthy ratio; above this it is skew
σپراکندگی حجم شاردها؛ میانگین تنهایی دروغ می‌گویدthe spread of shard sizes; the mean alone lies

چطور کجی را ببینیHow to see the skew

هر شارد را جدا اندازه بگیر: بایت‌های ذخیره‌شده، تعداد درخواست در ثانیه، و تأخیر. بعد بیشترین را با میانگین مقایسه کن و همین نسبت را روی یک نمودار در طول زمان ببین؛ کجی‌ای که آرام رشد می‌کند، ماه‌ها بعد به شکل یک شارد داغ و یک هشدار ظرفیت برمی‌گردد. برای کلیدها هم شمارندهٔ سنگین‌ها نگه دار تا بفهمی کدام مستأجر یا کدام کلید سهم بزرگ را می‌سازد؛ بدون آن، فقط می‌دانی یکی بزرگ است، نه چرا.Measure each shard separately: bytes stored, requests per second, latency. Then compare the maximum with the mean and watch that ratio over time; skew that grows quietly comes back months later as one hot shard and a capacity alert. Keep a heavy-hitters counter for keys as well, so you learn which tenant or key builds the large share; without it you only know one piece is big, not why.

کجی در برابر نقطهٔ داغSkew versus hot spot

کجیSkewنقطهٔ داغHot spot
منشأSourceتوزیع نابرابر کلید و دادهuneven key and data distributionتمرکز درخواست روی یک کلید یا بازهrequests piling onto one key or range
زمانTimingاز اول، پایدارthere from the start, stableگذرا، با ترافیک می‌آید و می‌رودtransient, follows the traffic
اثرEffectظرفیت هدر و جابه‌جایی کندwasted capacity, slow movesتأخیر و خطا در زمان اوجlatency and errors at peak
درمانFixکلید بهتر، پارتیشن بیشترbetter key, more partitionsکش، شکستن کلید داغ، جدا کردن شاردcache, split the hot key, isolate the shard

تلهٔ رایجThe usual trap

ظرفیت را بر اساس میانگین برنامه‌ریزی می‌کنی. میانگین می‌گوید هر شارد ۲۰۰۰ درخواست دارد، ولی یکی ۸۰۰۰ دارد و همان یکی سقف واقعی است.You plan capacity from the mean. It says every shard takes two thousand requests, but one takes eight thousand, and that one is the real ceiling.

یک ماشین قوی‌تر چطور سهم بیشتری بگیرد؟How does a stronger machine earn a bigger share?

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

کیک تولد را برای شش نفر به شش تیکهٔ بزرگ نمی‌بری؛ به دوازده تیکهٔ کوچک می‌بری و به هر کس یکی دو تیکه می‌دهی؛ حالا کسی که کمتر می‌خورد یک تیکه می‌گیرد و بقیه دو تیکه. گرهٔ مجازی هم همین کار را با حلقهٔ داده می‌کند: به‌جای یک نقطهٔ بزرگ برای هر سرور، چند نقطهٔ کوچک به آن می‌دهی.You do not cut a birthday cake for six into six big slices. You cut twelve small ones and give each person one or two; now the one who eats less takes one and the others take two. Virtual nodes do that to the data ring: instead of one big point per server, a server gets several small ones.

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

تیکه‌های کوچک دو سود دارند. اول، تعادل: با تیکه‌های بزرگ یک نفر ممکن است سهم ناعادلانه بگیرد؛ با تیکه‌های کوچک، خطا‌ها روی هم می‌افتند و سهم‌ها نزدیک هم می‌شوند. دوم، جابه‌جایی: اگر یک نفر مهمانی را ترک کند، تیکه‌هایش بین همه پخش می‌شود، نه این‌که یک نفر مجبور شود کل یک تیکهٔ بزرگ را بلع کند. بهایش این است که باید تعداد بیشتری تیکه را بشماری و به خاطر بسپاری؛ هر تیکه یک خط در فهرست است. تیکه‌ها را بیش از حد ریز کنی، فهرست از خودِ کیک بزرگ‌تر می‌شود.Small slices buy two things. Balance: with big slices one guest can end up with an unfair share, while small ones average out. And movement: when a guest leaves, their slices go to everyone, instead of one person having to swallow a whole slab. The bill is bookkeeping: more slices to track, one line each. Make them too fine and the list outgrows the cake.

۳ سرور، ۶ نقطه3 servers, 6 spotsA · ۱A · 1C · ۱C · 1B · ۱B · 1A · ۲A · 2C · ۲C · 2B · ۲B · 2
سه سرور، شش نقطه: سهم هر سرور از چند تیکهٔ کوچک ساخته می‌شود، نه یک کمان بزرگ.Three servers, six points: each server's share is many small arcs, not one big one.

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

در هش سازگار، هر گرهٔ فیزیکی به‌جای یک نقطه روی حلقه، k نقطه یا token می‌گیرد و هر کلید به اولین token در جهت ساعت می‌رسد. با یک نقطه در هر گره، طول کمان‌ها تصادفی است و ممکن است یکی دو برابر بقیه سهم بگیرد؛ با k نقطه، نوسان سهم‌ها تقریباً به اندازهٔ یک تقسیم بر رادیکال k کوچک می‌شود، پس با چند صد نقطه سهم‌ها به هم نزدیک می‌شوند. همان نقطه‌ها جابه‌جایی را هم ارزان می‌کنند: گرهٔ تازه از خیلی از گره‌ها کمی برمی‌دارد و گرهٔ رفته سهمش بین همه پخش می‌شود. با دادن تعداد بیشتری vnode به ماشین قوی‌تر، ناهمگنی سخت‌افزار هم جبران می‌شود. در عوض، جدول مسیریابی بزرگ‌تر می‌شود، تعداد اتصال‌ها بالا می‌رود، و هر رویداد کوچک دادهٔ بیشتری را جابه‌جا می‌کند.In consistent hashing a physical node holds k points, or tokens, on the ring instead of one, and each key belongs to the first token clockwise. With one point per node the arc lengths are random and one node may take twice its share; with k points the load variance falls roughly as one over the square root of k, so a few hundred points bring shares close together. Those points also make movement cheap: a joining node takes a little from many nodes, and a leaving node's share is spread across all of them. Giving more vnodes to a stronger machine compensates for uneven hardware. In exchange the routing table grows, the number of connections rises, and every small event moves more pieces of data.

k tokensهر سرور چند نقطه روی حلقه دارد، نه یکیeach server holds several ring points, not one
1/√kکاهش ناهمترازی سهم‌ها با زیاد شدن khow fast share imbalance falls as k grows
~100sتعداد معمول vnode برای هر سرور در سیستم‌های واقعیa usual vnode count per server in real systems

تلهٔ رایجThe usual trap

گذاشتن دو replica روی یک ماشین. وقتی برای پیدا کردن replica بعدی روی حلقه راه می‌روی، باید vnodeهایی را که مال همان گرهٔ فیزیکی‌اند رد کنی؛ وگرنه نسخهٔ دوم هم روی همان ماشین می‌نشیند و یک خرابی، هر دو نسخه را با هم می‌برد.Putting two replicas on one machine. Walking the ring for the next replica means skipping the vnodes that belong to the same physical node; otherwise the second copy lands on the same box, and one failure takes both.