الفريق قسّم الـ shop لـ 3 services: Orders و Billing و Shipping، كل واحدة ليها repository و pipeline و container. بعد سنة، Orders بتقسم CustomerName لاسم أول واسم أخير. الـ migration بتشتغل، و Orders بتنزل، وبالليل الـ label job بتاع Shipping بيبتدي يفشل، لأنه بيقرا الـ column ده مباشرة. والحل؟ خطة release بتنزّل 3 services بترتيب معيّن، وده بالظبط اللي التقسيم كان المفروض ينهيه.
الفكرة الغلط الشائعة
الفكرة بتقول إن تقسيم الكود لـ services منفصلة بيديك deployments مستقلة، حتى لو كلهم فضلوا على database واحدة. الداتا نفصلها بعدين؛ المهم الكود.
وده اختصار مفهوم. الكود بيتقسم بسهولة؛ الداتا لأ. والـ database الواحدة بتحافظ على الـ joins والـ foreign keys و backup واحد و reports سهلة. والـ guide بتاع Microsoft للـ microservices على .NET بيعترف بالجاذبية دي: الـ database المركزية "initially looks simpler and seems to enable reuse of entities in different subsystems to make everything consistent." و Martin Fowler بيسمّي الميزة الحقيقية كمان: مع الـ ، "Any changes to data made in a single application are made available to all applications at the time of database commit."
النص الصح في الكلام: الـ deploy بتاع الـ service بقى مستقل فعلًا. إنما تغييرها لأ.
اللي بيحصل فعلًا
الـ schema بتبقى هي الـ contract
لما كذا service يستخدموا نفس الـ tables، كل اسم column و type وقيمة status بيبقى جزء من الـ interface بتاعتهم. Fowler بيقول إن الـ integration database "needs a schema that takes all its client applications into account. The resulting schema is either more general, more complex or both - because it has to unify what should be separate BoundedContexts." وحكمه: "the database becomes a point of coupling between the applications that access it. This is usually a deep coupling that significantly increases the risk involved in changing those applications and making it harder to evolve them." ده مش هتشوفه في الكود، لأنه قاعد في الـ SQL وفي معنى قيم زي 'paid'.
الـ releases بترجع تتنسّق
الـ guide بتاع Microsoft بيوضّح النتيجة: "If multiple services were accessing the same data, schema updates would require coordinated updates to all the services. This would break the microservice lifecycle autonomy." و Fowler بيروح أبعد: بما إن الـ deployment المستقل جزء من التعريف، فـ "a suite of services that must have its deployments coordinated is not a microservice architecture"، وفرق كتير بتقع في مشاكل لأنها "they end up having to coordinate service deployments."
والنتيجة : عندك network calls، و pipelines أكتر، وحاجات بتتحرك أكتر، ولسه بتنزّلوا مع بعض.
المشكلة مش في السيرفر، المشكلة في الـ tables
الـ Azure Architecture Center بيحط الخط بدقة: "Services can safely share the same physical database server. Problems occur when services share the same schema, or they read and write to the same set of database tables." والقاعدة عنده قصيرة: "Two services shouldn't share a data store. Each service manages its own private data store, and other services can't access it directly." والـ store المشترك كمان "limits each team's ability to optimize data storage for its specific service."
اللي بتتنازل عنه، وإيه اللي بييجي مكانه
فصل الداتا ليه تمن حقيقي، و Microsoft بيسمّيه: الـ database الواحدة بتديك "ACID transactions and the SQL language, both working across all the tables"، أما الداتا المنفصلة فمعناها "you can't make a single ACID transaction across microservices." وبدل كده، الـ service اللي محتاجة داتا service تانية بتحتفظ بنسخة فيها "only the attributes you need"، بتتحدّث بالـ events أو بتتجاب من الـ API بتاع المالك. والنسخة دي عايشة في الـ بتاعها وتقدر تستخدم أسامي خاصة بيها.
الحل: امتلك الداتا بتاعتك، وانشر contract
C# · Shipping/LabelJob.cs
// Shipping queries Orders' tables through the shared databasepublic sealed class LabelJob(ShopDb db){ public Task<List<Label>> RunAsync(CancellationToken ct) => db.Orders .Where(o => o.Status == "paid" && o.ShippedAt == null) .Select(o => new Label(o.Id, o.CustomerName, o.Street, o.City)) .ToListAsync(ct);}// Orders renames a status or splits CustomerName: this breaks at// runtime, and only a coordinated release of both services fixes it
C# · Shipping/OnOrderPaid.cs
// Orders publishes this contract and promises to keep it stablepublic sealed record OrderPaid(Guid OrderId, string Recipient, Address ShipTo);// Shipping keeps what it needs, in tables only Shipping can touchpublic sealed class OnOrderPaid(ShippingDb db){ public async Task HandleAsync(OrderPaid e, CancellationToken ct) { db.Shipments.Add(new Shipment(e.OrderId, e.Recipient, e.ShipTo)); await db.SaveChangesAsync(ct); }}public sealed class LabelJob(ShippingDb db){ public Task<List<Shipment>> PendingAsync(CancellationToken ct) => db.Shipments.Where(s => s.ShippedAt == null).ToListAsync(ct);}
دلوقتي Shipping معتمدة على OrderPaid، contract صغير Orders اختارت تنشره، مش على الـ tables بتاعة Orders. و Orders تقدر تقسم الأسامي، أو تغيّر أسامي الـ statuses، أو تنقل لـ database تانية من غير ما تستأذن حد، طالما بتنشر نفس الـ . و Shipping بتخزّن المستلم والعنوان بأسامي خاصة بيها، فالـ label job بتاعها مابقاش بيقرا schema حد تاني. أما إزاي الـ event ده يوصل من غير ما يضيع، وإزاي تتعايش مع التأخير لحد ما يوصل، فدي مشاكل تانية هنتكلم عنها في مقالات جاية في نفس المسار.
الفكرة الغلط
الحقيقة
الـ repos والـ pipelines المنفصلة بتخلّي الـ services مستقلة
الـ tables المشتركة بتخلّي الـ schema contract كل الـ services معتمدة عليه
المشكلة في مشاركة سيرفر الـ database
مشاركة السيرفر عادي؛ مشاركة الـ schemas والـ tables هي الـ coupling
القراية بس من tables service تانية مش بتضر
اللي بيقرا معتمد على أسامي ومعاني المالك مابقاش يقدر يغيّرها براحته
هنفصل الداتا بعدين
كل query جديدة على الـ tables المشتركة بتكبّر الفصل ده
الـ database المشتركة بتحافظ على الداتا متسقة ببلاش
ده صح، وده بالظبط اللي لازم تلاقي بديله لما تفصل
تطبقها إزاي في مشروعك
اعرف مين بيلمس أنهي tableلكل service، اكتب الـ tables اللي بتقراها وبتكتب فيها، من الكود ومن الـ logs بتاعة الـ database. وأي table بتستخدمه 2 services يبقى contract مشترك.
ادّي كل table مالك واحدسلّم كل table للـ service اللي الـ business rules بتاعتها هي اللي بتكتب فيه. المالك بس هو اللي يكتب، وخطّط تشيل أي وصول تاني.
بدّل الوصول بين الـ services بـ contractsلما service تانية تحتاج الداتا، اعرضها من الـ API بتاع المالك أو انشر events، وخلّي اللي بيستهلك يحتفظ بس بالـ attributes اللي محتاجها.
ثبّتها بالصلاحياتانقل كل service للـ schema والـ login بتوعها، عشان الـ database هي اللي تشيّك على الحدود، مش الذاكرة.
الخلاصة
كود منفصل مع tables مشتركة بيديك distributed monolith: تكاليف distributed و releases متنسّقة.
الـ tables المشتركة بتحوّل الـ schema لـ contract محدش مالكه، فأي تغيير في column بيبقى مفاوضات.
مشاركة سيرفر الـ database عادي؛ مشاركة الـ schemas والـ tables هي الـ coupling.
كل service المفروض تمتلك الداتا بتاعتها وتشاركها بـ APIs أو events منشورة، وتحتفظ بس بالـ attributes اللي محتاجاها.
فصل الداتا بيكلّفك الـ transactions والـ joins بين الـ services، فخطّط لده بدل ما تهرب من الفصل.
التعليقات (0)