الانتقال إلى المحتوى
Software ArchitectureSystem Design· 9 دقائق للقراءة

ليه الـ retries الكتير ممكن توقّع السيستم؟ الـ backoff والـ jitter والـ retry budgets

الـ retries بتتضاعف على الطبقات، وبتيجي على دفعات، وبتزوّد الحمل على اللي تعبان أصلًا. اعمل retry في طبقة واحدة، بـ jitter، وجوه budget.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

وقت عروض، الداتابيز بتاعة المخزون بتبطّأ تحت ضغط orders مفاجئ، وشوية queries بتعمل timeout. والـ gateway و service الـ Orders و service الـ Inventory كل واحد فيهم بيعمل retry للـ call الفاشلة 3 مرات، policy كل فريق ضافها لوحده "عشان الـ resilience". وفي دقايق الداتابيز بقت بتستقبل queries أكتر بكتير من ذروة الضغط نفسه، فتبطّأ أكتر، و calls أكتر تتعاد. الفريق بيزوّد resources ومفيش حاجة بتتحسن. والحمل مابينزلش غير لما حد ينزّل hotfix يقفل الـ retries.

الفكرة الغلط الشائعة

الفكرة بتقول إن كل ما تعيد الـ calls الفاشلة مرات أكتر، السيستم بيبقى موثوق أكتر. لو retry واحد كويس، يبقى 3 أحسن، وكل طبقة بتكلم الـ network لازم يبقى ليها retries بتاعتها.

والمطورين الشاطرين بيصدّقوا كده لأنه صح أغلب الوقت. الـ Builders' Library بتاعة Amazon بتشرح السبب: "Often, trying the same request again causes the request to succeed." السيستمات نادرًا ما بتقع حتة واحدة؛ اللي بيحصل فشل جزئي أو قصير. وأغلب الـ client libraries بتعمل retry لوحدها، وفي الـ testing الأعطال عشوائية ومستقلة، فالـ retry دايمًا تقريبًا بيكسب. نص الحقيقة إن الـ retries بتفيد لما الأعطال نادرة ومستقلة؛ لكن لما تكون جاية من الـ overload، الـ retry بيزوّد الحمل على نفس الحاجة اللي بتفشل. ومقال أخطاء Claude API بيشرح أنهي أخطاء من API واحدة تستاهل retry؛ إنما هنا بنشوف الـ retries بتعمل إيه في السيستم كله.

اللي بيحصل فعلًا

الـ retries أنانية

الـ Builders' Library بتقولها صريحة: "Retries are “selfish.” In other words, when a client retries, it spends more of the server's time to get a higher chance of success." وده كويس طالما الأعطال نادرة. لكن "When failures are caused by overload, retries that increase load can make matters significantly worse. They can even delay recovery by keeping the load high long after the original issue is resolved."

وكتاب الـ SRE بتاع Google بيوصف نفس الدوامة: backend فوق طاقته بيفشّل requests، والـ clients بيعيدوها فوق الـ traffic العادي، فـ requests أقل بتنجح من أول مرة و retries أكتر، و "the retries themselves may be keeping the backend in an overloaded mode."

الطبقات بتتضرب في بعض

الـ retries بتاعة كل طبقة بتتطبق على كل محاولة من الطبقة اللي فوقها. الـ simulation القصيرة دي بتعد الـ calls اللي بتوصل لداتابيز بتفشّل كل حاجة:

C# RetryMath.cs
int dbCalls = 0;bool Database() { dbCalls++; return false; } // overloaded: every call fails// A layer calls the one below it up to `attempts` timesFunc<bool> Layer(Func<bool> below, int attempts) =>    () => Enumerable.Range(0, attempts).Any(_ => below());// Gateway -> Orders -> Inventory -> Database, 3 attempts per hopLayer(Layer(Layer(Database, 3), 3), 3)();Console.WriteLine($"Every layer retries: {dbCalls} database calls");dbCalls = 0;Layer(Layer(Layer(Database, 3), 1), 1)(); // only Inventory retriesConsole.WriteLine($"One layer retries:   {dbCalls} database calls");

لما تشغّلها على .NET 10 بتطبع 27 database call لـ action واحد من المستخدم لما كل طبقة تعمل retry، و 3 لما طبقة واحدة بس تعمل. والقاعدة العامة في كتاب الـ SRE إن الـ request الواحد "may produce a number of attempts as large as the product of the number of attempts at each layer to the lowest layer"، يعني 3 طبقات بـ 4 محاولات لكل واحدة يوصلوا 64.

الـ retries اللي بتستنى نفس الوقت بتيجي مع بعض

الـ backoff بيساعد، بس جزئيًا. "If all the failed calls back off to the same time, they cause contention or overload again when they are retried." والـ simulations بتاعة Marc Brooker على AWS Architecture Blog بتوضّح إن الـ لوحده لسه بيسيب "clusters of calls". والحل عنده هو الـ : في الـ "Full Jitter"، كل client بينام وقت عشوائي بين الصفر والـ delay الـ exponential بعد الحد الأقصى. ومع 100 client بيتزاحموا، ده قلّل عدد الـ calls لأقل من النص. ونصيحة كتاب الـ SRE نفس الكلام: "Always use randomized exponential backoff when scheduling retries."

الحد لكل request مش حد للسيستم

حتى 3 محاولات لكل request ممكن تقرّب الحمل من 3 أضعاف: في أسوأ حالة في فصل الـ overload في كتاب الـ SRE، الـ requests بتزيد "to somewhere just below 3X." والحل عند Google هو لكل client: الـ request مايتعادش غير لو الـ retries أقل من 10% من requests الـ client، وده "reduces the growth to just 1.1x in the general case". والـ Builders' Library بتوصف نفس الفكرة كـ token bucket بيـ "allows all calls to retry as long as there are tokens, and then retry at a fixed rate when the tokens are exhausted."

الحل: طبقة واحدة، و jitter، و budget

C# InventoryClient.cs
// Orders service. Gateway and Inventory have the same looppublic sealed class InventoryClient(HttpClient http){    public async Task<Stock> GetStockAsync(string sku, CancellationToken ct)    {        for (int attempt = 1; ; attempt++)        {            try { return (await http.GetFromJsonAsync<Stock>($"/stock/{sku}", ct))!; }            catch (Exception) when (attempt < 3) // retries 404s too            {                await Task.Delay(1000, ct); // every client wakes up together            }        }    }}

3 تغييرات. طبقة واحدة بس بتعمل retry: الـ Orders، اللي فوق الـ dependency الفاشلة على طول، زي قاعدة كتاب الـ SRE إن الـ requests "should only be retried at the layer immediately above the layer that is rejecting them." والـ retry بس للأعطال اللي ممكن تنجح: مفيش رد أو 5xx، وعمره ما يبقى 404. والـ RetryBudget هو token bucket واحد لكل الـ calls اللي رايحة للـ Inventory: كل request بيضيف عُشر token، وكل retry بيصرف token كامل، فالـ retries بتفضل حوالين 10% من الـ traffic وبتقف لما عطل يفضّي الـ bucket. والـ call دي قراية، فتكرارها آمن؛ أما إعادة الكتابات فمحتاجة الـ idempotency اللي في مقال الـ idempotent consumers.

الطريقةلما الـ dependency تبقى overloaded
retries فورية في كل طبقةالحمل بيتضرب في عدد المحاولات في كل طبقة
exponential backoff من غير jittercalls أقل، بس لسه جاية على دفعات متزامنة
backoff مع full jitterالـ retries بتتوزع، بس مفيش سقف لمجموعها
retry budget لكل dependencyالـ retries بتفضل نسبة صغيرة من الـ traffic
circuit breakerالـ calls بتقف خالص؛ و AWS بتحذّر إنه بيضيف modal behavior وممكن يأخّر الـ recovery

تطبقها إزاي في مشروعك

  1. عِد طبقات الـ retryلـ action واحد من المستخدم، اكتب كل طبقة بتعمل retry: الـ loops بتاعتك والـ HTTP handlers والـ SDKs والـ database drivers وإعادة توصيل الرسايل، واضرب المحاولات في بعض.
  2. سيب طبقة واحدةاعمل retry فوق الـ dependency اللي بتفشل على طول، وخلّي الباقي يفشل بسرعة أو يقدّم نسخة أخف، زي صفحة من غير الـ recommendations.
  3. اعمل backoff بـ jitter، جوه deadlineاستخدم exponential backoff ليه حد أقصى مع full jitter، ومحاولات قليلة، ووقت كلي على قد استنا المستخدم.
  4. ضيف budget وراقبهحط سقف للـ retries لكل dependency، مثلًا عُشر الـ requests، واعمل alert لما نسبة الـ retries تعلى.

الخلاصة

  • الـ retries بتفيد مع الأعطال النادرة المستقلة، وبتضر لما الأعطال جاية من الـ overload.
  • الـ retries في كذا طبقة بتتضرب في بعض: 3 طبقات بـ 3 محاولات بيبعتوا 27 call للآخر.
  • الـ backoff من غير jitter لسه بيطلّع دفعات متزامنة؛ والـ full jitter بيوزّعها.
  • الحد لكل request مش سقف للحمل الكلي؛ الـ retry budget لكل dependency هو السقف.
  • اعمل retry في طبقة واحدة وبس للأخطاء اللي ممكن تنجح، وشوف الـ libraries بتاعتك بتعيد إيه أصلًا.

📚 المصادر

  1. Timeouts, retries, and backoff with jitter — Marc Brooker, Amazon Builders' Library (2019)
  2. Exponential Backoff And Jitter — Marc Brooker, AWS Architecture Blog (2015)
  3. Addressing Cascading Failures — Site Reliability Engineering, Google
  4. Handling Overload — Site Reliability Engineering, Google
  5. Build resilient HTTP apps: Key development patterns — .NET, Microsoft Learn

التعليقات (0)