0
0
LLDsystem_design~10 mins

Composition over inheritance in LLD - Scalability & System Analysis

Choose your learning style9 modes available
Scalability Analysis - Composition over inheritance
Growth Table: Composition over Inheritance
Users / ScaleDesign ComplexityCode ReuseFlexibilityMaintenance Effort
100 usersSimple class hierarchies, few componentsBasic reuse via inheritanceLow flexibility, rigid structuresLow effort, easy to track
10,000 usersMore components, inheritance chains growReuse limited by tight couplingHarder to change behavior without side effectsModerate effort, risk of bugs
1,000,000 usersComplex hierarchies cause code duplication and rigidityInheritance limits reuse, causes fragilityLow flexibility, difficult to extendHigh effort, frequent regressions
100,000,000 usersInheritance leads to brittle, unmaintainable codeReuse blocked by deep couplingVery low flexibility, hard to scale featuresVery high effort, costly fixes

Using composition instead allows building flexible, reusable components that can be combined without deep inheritance chains, improving scalability and maintainability at all scales.

First Bottleneck

As the system grows, the first bottleneck is the rigid class hierarchy caused by inheritance. It limits flexibility and code reuse because changes in a parent class can unexpectedly affect many child classes. This leads to increased bugs and slows down development, especially when new features or changes are needed.

Scaling Solutions
  • Use Composition: Build small, focused components that can be combined. This reduces tight coupling and makes code easier to change.
  • Favor Interfaces or Protocols: Define clear contracts for components to interact without depending on concrete implementations.
  • Apply Dependency Injection: Inject dependencies to increase flexibility and testability.
  • Modular Design: Organize code into modules or services that encapsulate behavior and state.
  • Refactor Inheritance: Replace deep inheritance trees with composition to avoid fragile base class problems.
Back-of-Envelope Cost Analysis

While composition itself is a design principle and not a direct resource consumer, its impact on system scalability is significant:

  • Development Speed: Composition reduces bugs and rework, saving developer hours as user base grows.
  • Maintenance Cost: Easier to update components independently, lowering long-term costs.
  • System Performance: Flexible components can be optimized or replaced without affecting entire system.
  • Example: At 1M users, a rigid inheritance system might cause 20% more bug fixes and slower feature rollout, increasing costs by thousands of developer hours annually.
Interview Tip

When discussing scalability, explain how inheritance can cause tight coupling and rigidity, leading to maintenance challenges as the system grows. Then describe how composition allows building flexible, reusable components that scale better. Use simple examples like "instead of a deep family tree of classes, we build small parts that can be mixed and matched." This shows you understand both design principles and their impact on system scalability.

Self Check

Question: Your system uses deep inheritance hierarchies and the codebase is hard to maintain as traffic grows 10x. What do you do first?

Answer: Refactor the design to use composition over inheritance. Break down large classes into smaller, reusable components that can be combined. This reduces coupling and makes the system easier to extend and maintain under higher load.

Key Result
Inheritance causes rigid, tightly coupled code that breaks as user scale grows; adopting composition creates flexible, reusable components that scale and maintain better.