
الـ exactly-once delivery خرافة: إزاي تصمم idempotent consumers
- كاتب موثّق
محتويات المقال
OrderPaid. بعد deploy يوم التلات، شوية عملاء بقى معاهم نقط مضاعفة على نفس الـ order، وتقرير الـ finance الشهري مش بيقفل. الفريق مستغرب: من شهور شغّلوا الـ duplicate detection بتاع الـ broker. والـ logs بتقول إن نفس الـ message id اتعالج مرتين، من 2 consumer instances بينهم كام ثانية، بالظبط والـ instance القديمة بتقفل.
الفكرة الغلط الشائعة
اللي بيحصل فعلًا
المعالجة والـ acknowledge خطوتين
التكرار بييجي من الناحيتين
مميزات الـ "exactly-once" بتغطي إيه
الـ idempotent producer في Kafka بيشيل التكرار اللي بييجي من إعادة الـ producer بـ producer id و sequence numbers، فـ "resending will not result in duplicate entries in the log." ومالوش أي علاقة بالـ consumer بتاعك. الـ transactions في Kafka بتدّيك نتايج exactly-once لما تقرا من Kafka وتكتب في Kafka تاني. لأي حاجة غير كده، "Exactly-once delivery for other destination systems generally requires cooperation with such systems". وإلا، "Kafka guarantees at-least-once delivery by default". الـ duplicate detection في Azure Service Bus بيرمي الرسالة اللي اتبعتت تاني بنفس الـ MessageIdاللي التطبيق حاطه جوه فترة زمنية، 10 دقايق default ولحد 7 أيام. يعني بيحمي الـ queue من الـ sender، مش الـ handler بتاعك من إعادة التوصيل.الـ redelivered flag في RabbitMQ مجرد تلميح إن الـ consumer "may have seen this message before". وهو بيتابع إعادة التوصيل اللي الـ broker نفسه عملها، فالرسالة اللي الـ publisher بعتها مرتين بتوصل كرسالتين جداد.
الـ idempotent consumers
الحل: صف الـ dedupe والأثر في transaction واحدة
using Microsoft.EntityFrameworkCore;public sealed class OrderPaidHandler(LoyaltyDb db){ public async Task HandleAsync(OrderPaid msg, CancellationToken ct) { var account = await db.Accounts.SingleAsync( a => a.CustomerId == msg.CustomerId, ct); account.Points += msg.Points; // runs again on every redelivery await db.SaveChangesAsync(ct); // A crash here, before the ack, delivers the message again }}using Microsoft.EntityFrameworkCore;public sealed class OrderPaidHandler(LoyaltyDb db){ public async Task HandleAsync(OrderPaid msg, CancellationToken ct) { var id = msg.MessageId; // set by the producer, the same on every copy if (await db.ProcessedMessages.AnyAsync(p => p.Id == id, ct)) return; // already applied: just acknowledge it var account = await db.Accounts.SingleAsync( a => a.CustomerId == msg.CustomerId, ct); account.Points += msg.Points; db.ProcessedMessages.Add(new ProcessedMessage(id, DateTime.UtcNow)); // One SaveChanges is one transaction: points and id commit together. // A concurrent copy fails on the primary key and rolls back its points await db.SaveChangesAsync(ct); }}AnyAsync مجرد طريق سريع. الحماية الحقيقية هي الـ primary key على ProcessedMessages: لما 2 instances يستلموا نفس الرسالة في نفس اللحظة، واحد منهم الـ save بتاعه يفشل والنقط بتاعته تعمل rollback، والنسخة اللي هتتعاد هتلاقي الـ id وتتعمل لها acknowledge.
MessageId اتبعت تاني جوه الفترة | ||
تطبقها إزاي في مشروعك
اكتب الـ handlers اللي مش idempotent دوّر على الـ consumers اللي آثارهم بتتجمع لما تتكرر: زيادة أرقام، و inserts، و emails، و payments، و calls لـ services تانية. ادّي كل رسالة id ثابت حط الـ id في المكان اللي الرسالة بتتعمل فيه، زي id صف الـ outbox أو business key، وعمره ما يتعمل مع كل توصيل. خزّن الـ ids المتعالجة مع الأثر ضيف table للرسايل المتعالجة بـ primary key، واكتب فيه في نفس الـ transaction بتاعة التغيير، وامسح الصفوف القديمة بشكل دوري. اعمل test بالتكرار في الـ integration tests، وصّل نفس الرسالة مرتين، ومرتين في نفس اللحظة، واتأكد إن النتيجة زي ما تكون وصلت مرة واحدة.
الخلاصة
الـ brokers بتوصّل at least once؛ والـ crash بين المعالجة والـ acknowledge دايمًا بيسمح بالتكرار. مميزات الـ exactly-once أضيق من أساميها: dedup للـ producer، و transactions من Kafka لـ Kafka، وفترات على ناحية البعت. الـ idempotent consumer بيدّي نفس النتيجة سواء الرسالة اتعالجت مرة أو كذا مرة. سجّل الـ message id في نفس الـ transaction بتاعة الأثر، والـ primary key هو الحارس. الآثار اللي برّه الداتابيز بتاعتك محتاجة idempotency key عند الطرف التاني أو outbox.
التعليقات (0)