فريق من 4 أفراد بيبدأ product جديد للحجوزات. وقبل أول عميل ما يسجل، الـ diagram فيه 8 services و message broker و API gateway و Kubernetes cluster. بعد 3 شهور، أغلب الـ features بتلمس 3 repositories، وأشهر bug في الـ production إن service تنزّل تغيير في الـ contract قبل الـ service اللي بتكلمها. ولا حتة من دول غلط لوحدها. الفريق ببساطة دفع من أول يوم تمن مشاكل لسه ماعندوش.
الفكرة الغلط الشائعة
الفكرة بتقول إن الـ microservices هي الطريقة اللي السيستمات الحديثة بتتبني بيها، وإن الـ monolith حاجة قديمة هترميها في الآخر لأنها مش بتعمل scale. فلو بدأت بـ services من الأول، توفّر على نفسك rewrite مؤلم بعدين.
وسهل تفهم ليه ناس شاطرة بتصدّق كده. أشهر قصص النجاح قصص microservices. والـ guide بتاع Microsoft على Azure بيعدّ فوايد حقيقية: deployment مستقل، وفرق صغيرة، وعزل الأعطال، و scale لكل service لوحدها. ومطورين كتير عانوا من monolith متلخبط؛ Fowler بيلاحظ إن الـ Big Ball of Mud هو أشهر architectural pattern، وإن الإحباط ده هو اللي زقّ فرق كتير ناحية الـ microservices.
النص الصح في الكلام: الفوايد دي حقيقية. بس ليها تمن، واتنين من أكبر الأسباب اللي الناس بتقولها، الـ scaling والـ modularity، مش محتاجين services أصلًا.
اللي بيحصل فعلًا
الـ services ليها premium
بوست Fowler سنة 2015 بيقولها من غير تجميل: الـ microservices "introduce complexity on their own account. This adds a premium to a project's cost and risk - one that often gets projects into serious trouble." وبيعدّ اللي بتشيله على كتفك: "automated deployment, monitoring, dealing with failure, eventual consistency, and other factors that a distributed system introduces." ومن هنا القاعدة بتاعة الـ : "don't even consider microservices unless you have a system that's too complex to manage as a monolith."
ومقاله عن الـ trade-offs بيفصّل التكاليف. الـ distribution: "Distributed systems are harder to program, since remote calls are slow and are always at risk of failure." والـ operations: "You need a mature operations team to manage lots of services, which are being redeployed regularly." والـ guide بتاع Microsoft متفق: "Each service is simpler, but the entire system as a whole is more complex."
الـ monolith بيعمل scale
مقال Lewis و Fowler الأصلي عن الـ microservices بيقولها صريحة: "You can horizontally scale the monolith by running many instances behind a load-balancer." والقيد الحقيقي أضيق: الـ scaling في الـ monolith معناه إنك تكبّر التطبيق كله، مش بس الجزء اللي محتاج resources. ده بيفرق مع سيستمات معينة، بس دي مشكلة محددة، مش سبب إنك تبدأ distributed. والـ continuous delivery كمان مش محتاج services، و Fowler بيستشهد بـ Facebook و Etsy.
الحدود أصعب ما يكون تظبطها في الأول
الـ services "only work well if you come up with good, stable boundaries between the services"، زي ما Fowler بيكتب في MonolithFirst، ودي بالظبط مهمة إنك تلاقي الـ الصح. "Any refactoring of functionality between services is much harder than it is in a monolith. But even experienced architects working in familiar domains have great difficulty getting boundaries right at the beginning." في الـ monolith، الحد الغلط تمنه refactoring. بين الـ services، تمنه تغيير في الـ contract و migration للداتا و release متنسّق.
وعشان كده بيحكي عن pattern شافه: "Almost all the successful microservice stories have started with a monolith that got too big and was broken up"، في حين إن تقريبًا كل سيستم سمع عنه اتبنى microservices من الصفر "has ended up in serious trouble."
اللي محتاجه الأول هو الـ modularity
الميزة اللي الناس عايزاها فعلًا هي حدود قوية بين الـ modules، والـ monolith يقدر يبقى فيه كده. Fowler بيقول: "It's perfectly possible to have firm module boundaries with a monolith, but it requires discipline." والانضباط ده هو اللي بيخلّي التقسيم بعدين ممكن. والـ وحدة deploy واحدة، كل module فيها مالك الداتا بتاعته وبيتكلم مع الباقيين من خلال contracts صغيرة بس.
الحل: modules ليها حدود حقيقية
C# · Ordering/Checkout.cs
public sealed class Checkout(ShopDb db){ public async Task<string> PlaceAsync(Order order, CancellationToken ct) { db.Orders.Add(order); // Ordering writes Billing's table and copies Billing's tax rule var invoice = new Invoice(order.Id, order.Net * 1.14m); db.Invoices.Add(invoice); await db.SaveChangesAsync(ct); return invoice.Number; }}// Every module reaches into every table: Billing can't change alone,// and splitting it out later means untangling all of this first
C# · Billing/BillingModule.cs
// The module's public contract: one coarse call, no tables exposedpublic interface IBilling{ Task<string> InvoiceAsync(Guid orderId, decimal net, CancellationToken ct);}// Tax rules and Billing's tables stay internal to this projectinternal sealed class BillingModule(BillingDb db) : IBilling{ public async Task<string> InvoiceAsync( Guid orderId, decimal net, CancellationToken ct) { var invoice = Invoice.For(orderId, net); // the VAT rule lives here db.Invoices.Add(invoice); await db.SaveChangesAsync(ct); return invoice.Number; }}public static class BillingSetup{ public static IServiceCollection AddBilling(this IServiceCollection s) => s.AddScoped<IBilling, BillingModule>();}
دلوقتي الـ Ordering بينادي billing.InvoiceAsync(order.Id, order.Net, ct) ومش شايف Invoice ولا الـ tables بتاعة Billing، لأن الـ compiler بيمنعه. و Billing يقدر يغيّر قاعدة الضريبة أو الـ schema من غير ما يلمس Ordering. والاتنين لسه في process واحدة و database واحدة، فـ transaction واحدة بينهم لسه ممكنة؛ و Fowler بيقول إن مع الـ monolith "you can update a bunch of things together in a single transaction". ولو Billing بقى service في يوم، نقطة الفصل جاهزة. بس خلّي الـ contract خشن: Lewis و Fowler بيحذّروا إن "a good in-process interface is usually not a good service interface."
الفكرة الغلط
الحقيقة
الـ microservices هي الـ default الحديث
Fowler: "The majority of software systems should be built as a single monolithic application."
الـ monolith مش بيعمل scale
بيعمل scale out بكذا instance ورا load balancer، بس مايقدرش يكبّر جزء لوحده
البداية بـ services بتوفّر rewrite
الحدود الأولانية غالبًا غلط، ونقلها بين services أصعب بكتير
الـ services بتديك modularity ببلاش
بتخلّي الحدود أثبت، بس الحدود الغلط بتبقى عائق
الـ continuous delivery محتاج services
Facebook و Etsy بيسلّموا monoliths كبيرة بشكل continuous
تطبقها إزاي في مشروعك
اكتب ليه عايز servicesاكتب الوجع المحدد اللي كل تقسيم هيحلّه. لو الإجابة "عشان دي الحاجة الحديثة" أو "عشان الـ scale"، فالـ monolith ورا load balancer مغطّيها.
ارسم الـ modules حوالين الـ bounded contextsكل module في project لوحده، و internal هو الأساس، وكل module مالك الـ tables بتاعته. ومفيش module يقرا tables بتاعة module تاني.
خلّي الـ contracts خشنةصمّم كل public interface كأنها ممكن تبقى remote في يوم: calls قليلة، و requests ونتايج كاملة، ومفيش entities بتطلع برّه.
قسّم لما وجع محدد يظهرلما module يحتاج release لوحده أو فريق لوحده أو scaling لوحده، اقطعه من الأطراف، الطريق التدريجي اللي Fowler بيوصفه، وسيب الباقي مع بعض.
الخلاصة
الـ microservices صفقة ليها premium حقيقي في الـ distribution والـ consistency والـ operations، مش الـ default الحديث.
الـ monolith بيعمل scale out ورا load balancer، واللي مايقدرش يعمله هو إنه يكبّر جزء لوحده.
حدود الـ services أصعب ما يكون تظبطها بدري، ونقلها بعد ما تبقى remote أصعب بكتير.
الحدود القوية بين الـ modules ممكنة جوه الـ monolith بالانضباط، وهي اللي بتخلّي التقسيم بعدين ممكن.
قسّم لما تعقيد محدد يفرض ده، وابدأ من modular monolith، مش من diagram.
التعليقات (0)