0
0
LLDsystem_design~10 mins

Law of Demeter in LLD - Scalability & System Analysis

Choose your learning style9 modes available
Scalability Analysis - Law of Demeter
Growth Table: Law of Demeter Impact
UsersCode ComplexityModule CouplingMaintenance EffortBug Rate
100LowLowLowLow
10,000ModerateModerateModerateModerate
1,000,000High if violatedHigh if violatedHigh if violatedHigh if violated
100,000,000Very High if violatedVery High if violatedVery High if violatedVery High if violated

The Law of Demeter helps keep code loosely coupled and easier to maintain as user base grows.

First Bottleneck

When the Law of Demeter is violated, the first bottleneck is increased code coupling. This leads to modules depending too much on internal details of others.

As users and features grow, this causes maintenance difficulty and bug introduction because changes ripple through many modules.

Scaling Solutions
  • Strict adherence: Ensure each module only talks to its immediate friends, not friends of friends.
  • Use interfaces: Hide internal details behind clear interfaces to reduce coupling.
  • Refactor: Break down large modules into smaller, focused ones to reduce dependencies.
  • Automated tests: Use tests to catch unintended coupling and side effects early.
  • Code reviews: Enforce the Law of Demeter during reviews to maintain clean design.
Back-of-Envelope Cost Analysis

Violating the Law of Demeter increases maintenance cost exponentially with user growth.

  • At 1,000 users, small coupling issues cause minor delays.
  • At 10,000 users, fixing bugs from tight coupling can cost weeks of developer time.
  • At 1 million users, downtime or bugs due to coupling can cause significant revenue loss.
  • At 100 million users, system instability from poor design can cause catastrophic failures.
Interview Tip

When discussing scalability, explain how following the Law of Demeter reduces coupling and improves maintainability.

Describe how this prevents cascading failures and makes the system easier to extend as users grow.

Use simple examples like "talk only to your immediate friends" to show understanding.

Self Check

Question: Your codebase is tightly coupled violating the Law of Demeter. As traffic grows 10x, bugs increase and maintenance slows. What do you do first?

Answer: Refactor code to reduce coupling by enforcing the Law of Demeter. Introduce interfaces and limit module knowledge to immediate neighbors to improve scalability and maintainability.

Key Result
Following the Law of Demeter keeps code loosely coupled, preventing maintenance and bug issues as user scale grows from thousands to millions.