الـ CAP مش «اختار 2 من 3»: نظرية CAP من غير خرافات
الـ partitions مش اختيار، والـ C والـ A في CAP أضيق من أساميهم، والاختيار الحقيقي بيتعمل لكل operation، وقت الـ partition، وقصاد الـ latency في باقي الوقت.
في design review، حد بيسأل الفريق اختار database معينة ليه للـ service الجديدة بتاعة حجز الطيران. والرد بييجي بسرعة: "عشان هي AP، واحنا محتاجين availability." والكل بيهز راسه. بعد كام شهر، مشكلة في الـ network بين 2 regions بتخلّي مسافرين في الـ region-ين يحجزوا نفس الكرسي، والـ support بيقعد أسبوع يعيد حجز الناس. الـ label كان صح في حدوده. بس جاوب على السؤال الغلط، وللسيستم كله مرة واحدة.
الفكرة الغلط الشائعة
الفكرة بتقول إن الـ CAP بتسيب اللي بيصمم السيستم يختار أي 2 من consistency و availability و partition tolerance. اختار C و A لـ database تقليدية، و A و P لـ NoSQL store، و C و P لـ consensus system، وخلاص كده.
ودي مش خرافة متألفة من العدم. Eric Brewer، صاحب الفكرة، لخّصها بنفسه كده: "any networked shared-data system can have at most two of three desirable properties". و diagrams كتير بترسمها مثلث تختار منه ضلعين. بس سنة 2012 Brewer كتب: "The '2 of 3' formulation was always misleading because it tended to oversimplify the tensions among properties."
اللي بيحصل فعلًا
الـ partition tolerance مش اختيار
الـ معناها إن الـ network بين شوية nodes بتقع والـ nodes نفسها شغالة. و Martin Kleppmann بيوصف الـ partition tolerance إنها "communicating over an asynchronous network that may delay or drop messages. The internet and all our datacenters have this property, so you don't really have any choice in this matter." و Brewer بيقول نفس الكلام عن اختيار "CA": "If the choice is CA, and then there is a partition, the choice must revert to C or A."
يعني الكلام الحقيقي أضيق: لما partition تحصل، ماتقدرش تاخد C و A مع بعض. ولما مفيش partition، الـ CAP مش بتقول إن فيه حاجة تمنعك تاخد الاتنين.
الإثبات بيكفيه 2 nodes
Seth Gilbert و Nancy Lynch أثبتوا فرضية Brewer سنة 2002. والـ بتاعتهم بتقول إنه "impossible in the asynchronous network model to implement a read/write data object" يضمن الـ availability والـ atomic consistency مع بعض في كل الحالات، "including those in which messages are lost." الفكرة: قسّم الـ nodes لمجموعتين وضيّع كل الرسايل اللي بينهم. كتابة بتخلص في ناحية، وقراية بعدها بتوصل للناحية التانية. لو ردّت، هترجّع القيمة القديمة؛ ولو عايزة تفضل صح، لازم ماتردش. و Brewer بيقول: "Allowing at least one node to update state will cause the nodes to become inconsistent, thus forfeiting C. Likewise, if the choice is to preserve consistency, one side of the partition must act as if it is unavailable, thus forfeiting A."
الـ C والـ A معناهم أقل من أساميهم
الـ C في CAP هي الـ : ببساطة، أول ما كتابة تخلص، أي قراية تبدأ بعدها تشوفها أو تشوف حاجة أحدث. و Kleppmann بيأكد إنها "has got nothing to do with the C in ACID". والـ A في CAP صارمة برضه. Gilbert و Lynch بيعرّفوها إن "every request received by a non-failing node in the system must result in a response"، ومن غير حد للوقت. سيستم بيرد في دقيقتين يعتبر CAP-available؛ وسيستم الـ nodes اللي في الجزء الأصغر من الـ partition فيه بترفض الكتابة مش CAP-available، حتى لو الـ uptime بتاعك ممتاز.
وبالتعريفات دي، سيستمات حقيقية كتير لا ده ولا ده. مثال Kleppmann هو الـ database المعتادة اللي ليها leader واحد: الـ clients اللي اتقطعوا عن الـ leader مايقدروش يكتبوا، والقراية من الـ followers الـ asynchronous ممكن تبقى قديمة. "They are neither 'CP' nor 'AP', they are just 'P'".
الاختيار لكل operation، وأغلبه على الوقت
Brewer بيقول: "the choice between C and A can occur many times within the same system at very fine granularity"، وممكن يتغير كمان "according to the operation or even the specific data or user involved." وعمليًا الاختيار بيحصل وقت الـ timeout، واللي بيسميه الـ partition decision: "cancel the operation and thus decrease availability, or proceed with the operation and thus risk inconsistency."
و Daniel Abadi بيضيف الجزء اللي الـ CAP سايباه. الـ PACELC بتاعه بيسأل سؤالين: وقت الـ partition، تختار availability ولا consistency؟ وإلا (الـ E، اختصار else)، يعني والسيستم شغال عادي من غير partitions، بيوازن إزاي بين الـ latency (الـ L) والـ consistency (الـ C)؟ والقراية من replica قريبة هي الصفقة دي بالظبط، بتتعمل كل يوم من غير أي عطل.
الحل: قرّر لكل operation
C# · Seats.cs
// "We picked AP", so every call goes to the nearest regionpublic sealed class Seats(ISeatStore nearest){ // During a partition, two regions can each sell the same seat public Task<bool> BookAsync(Guid flight, string seat, CancellationToken ct) => nearest.TryAssignAsync(flight, seat, ct); public Task<string[]> FreeSeatsAsync(Guid flight, CancellationToken ct) => nearest.GetFreeSeatsAsync(flight, ct);}
C# · Seats.cs
public sealed class Seats(ISeatStore primary, ISeatStore nearest){ // A seat must never be sold twice, so only the primary region decides. // If it can't answer in time, fail and let the user try again (C over A) public async Task<bool> BookAsync( Guid flight, string seat, CancellationToken ct) { using var cts = CancellationTokenSource.CreateLinkedTokenSource(ct); cts.CancelAfter(TimeSpan.FromSeconds(2)); // the time bound return await primary.TryAssignAsync(flight, seat, cts.Token); } // The seat map may be seconds old: the nearest region answers fast, // even during a partition (A and low latency over C) public Task<string[]> FreeSeatsAsync(Guid flight, CancellationToken ct) => nearest.GetFreeSeatsAsync(flight, ct);}
نفس الـ service بقت بتاخد قرارين، كل واحد على قد اللي الـ operation تستحمله. الحجز بيدفع تمن الـ consistency من الـ latency، ومن الفشل لما الـ primary region مايتوصلش. والتصفح بيفضل سريع ومتاح، والكرسي اللي كان باين فاضي واتباع حالًا بتمسكه خطوة الحجز. والحجز اللي عمل timeout نتيجته مش معروفة، زي ما شرحنا في مقال سابق في نفس المسار، فشيّك على حجز المسافر قبل ما تحاول تاني.
الفكرة الغلط
الحقيقة
تقدر تختار أي 2 من C و A و P
الـ partitions بتحصل كده كده؛ ووقتها كل operation بتتنازل عن C أو A
الـ database الموزعة ممكن تبقى "CA"
Brewer: لو فيه partition، "the choice must revert to C or A"
الـ C في CAP هي الـ C في ACID
دي linearizability: القراية بتشوف آخر كتابة خلصت
الـ A في CAP معناها uptime عالي
كل node شغالة لازم ترد، ومن غير حد للوقت
كل database يا CP يا AP
كتير منهم لا ده ولا ده، والاختيار بيختلف حسب الـ operation والـ settings
تطبقها إزاي في مشروعك
اكتب الـ operations مش الـ databasesاكتب كل قراية وكتابة مهمة، زي حجز كرسي أو عرض خريطة الكراسي أو تعديل profile، بدل ما تحط label على السيستم كله.
قرّر كل واحدة تعمل إيه لما ماتوصلش للطرف التانيلكل operation اختار واحدة: تستنى وتفشل (consistency)، أو ترد من الداتا المحلية (availability)، أو تقبل دلوقتي وتعمل reconcile بعدين.
حط حد الوقت عن قصدBrewer بيسمّي الـ partition "a time bound on communication". اختار الـ timeout لكل operation من وقت استنا المستخدم، وخد بالك إن الحد الأضيق معناه إنك هتدخل الـ partition mode أكتر.
اتأكد الـ database بتاعتك بتضمن إيه فعلًااقرا الـ documentation والـ configuration بتوع الـ consistency، زي القراية من الـ replicas أو الـ quorum settings أو الـ consistency levels، بدل ما تثق في label زي CP أو AP.
الخلاصة
الـ CAP مش "اختار أي 2": الـ partitions بتحصل، ووقتها كل operation بتتنازل عن الـ consistency أو الـ availability.
الـ C في CAP هي الـ linearizability والـ A معناها "كل node شغالة ترد"، والاتنين أضيق من الاستخدام العادي.
سيستمات حقيقية كتير لا CP ولا AP، فالـ labels بتخبّي أكتر ما بتوضّح.
قرّر لكل operation لوحدها، واعتبر الـ timeout هو لحظة القرار.
برّه الـ partitions، الصفقة اليومية هي الـ consistency قصاد الـ latency، ودي الـ CAP مش بتغطيها والـ PACELC بتغطيها.
التعليقات (0)