| Users / Scale | Codebase Size | Bug Rate | Development Speed | Maintenance Effort |
|---|---|---|---|---|
| 100 users | Small, some repetition | Moderate bugs due to copy-paste | Moderate speed | Manageable |
| 10,000 users | Medium, repetition grows | Increased bugs, inconsistent fixes | Slower due to duplicated logic | Higher effort, more errors |
| 1,000,000 users | Large, many repeated code blocks | High bug rate, hard to fix | Slow, complex merges | Very high, costly maintenance |
| 100,000,000 users | Very large, unmanageable repetition | Critical bugs, system instability | Very slow, risky deployments | Extremely high, technical debt |
DRY (Don't Repeat Yourself) in LLD - Scalability & System Analysis
As the system grows, the main bottleneck caused by ignoring DRY is the maintenance overhead. Repeated code means bugs appear in multiple places. Fixing one place but missing others causes inconsistent behavior. This slows down development and increases risk of failures.
At small scale, this is manageable. But at medium to large scale, duplicated logic causes high bug rates and slow feature delivery. The team spends more time fixing repeated bugs than adding value.
- Refactor common logic: Extract repeated code into reusable functions, classes, or modules.
- Use libraries and frameworks: Leverage shared components to avoid rewriting code.
- Automate testing: Ensure changes in shared code do not break dependent parts.
- Code reviews and standards: Enforce DRY principles to prevent new duplication.
- Modular design: Design system components with clear responsibilities to reduce overlap.
These solutions reduce bugs, speed up development, and make scaling easier.
- At 1,000 users: ~10,000 lines of code with ~10% duplication -> ~1,000 lines repeated.
- At 1,000,000 users: ~1,000,000 lines with ~30% duplication -> ~300,000 lines repeated.
- Bug fixing time increases linearly with duplicated code; fixing one bug may require multiple fixes.
- Maintenance cost grows significantly due to duplicated logic, increasing developer hours and delaying releases.
- Bandwidth and storage are less affected directly by DRY, but indirect costs rise due to slower deployments and more bugs.
1. Explain DRY: Define the principle simply as avoiding repeated code to reduce bugs and improve maintainability.
2. Identify bottleneck: Show how duplication causes maintenance overhead and slows development as users grow.
3. Discuss scaling impact: Use examples of bug multiplication and slower feature delivery at larger scale.
4. Propose solutions: Refactoring, modular design, testing, and code reviews to enforce DRY.
5. Summarize benefits: Faster development, fewer bugs, easier scaling.
Your database handles 1000 QPS. Traffic grows 10x. What do you do first?
Note: This question is unrelated to DRY principle. For DRY, a better self-check is:
Self Check: You notice repeated code causing bugs in multiple places. How do you fix it to improve scalability?
Practice
DRY principle in system design?Solution
Step 1: Understand the meaning of DRY
DRY stands for "Don't Repeat Yourself," which means avoiding repeated code or design.Step 2: Identify the goal of DRY
The goal is to keep systems simple, maintainable, and reduce bugs by reusing components.Final Answer:
To avoid repeating code or design to keep systems simple -> Option AQuick Check:
DRY = Avoid repetition [OK]
- Thinking DRY means writing more code
- Believing duplication speeds development
- Confusing DRY with writing multiple versions
Solution
Step 1: Identify DRY best practice
DRY encourages reuse by creating components or modules that can be used multiple times.Step 2: Evaluate options
Copy-pasting or writing separate code repeats work, which breaks DRY. Ignoring reuse wastes effort.Final Answer:
Creating reusable components or modules -> Option AQuick Check:
Reuse = DRY [OK]
- Copy-pasting code instead of reusing
- Writing separate code for similar tasks
- Ignoring reuse to save time
function calculateTax(amount) {
return amount * 0.1;
}
function calculateTotal(amount) {
return amount + calculateTax(amount);
}
print(calculateTotal(100));
What is the output?Solution
Step 1: Calculate tax for amount 100
Tax = 100 * 0.1 = 10Step 2: Calculate total using tax
Total = 100 + 10 = 110Final Answer:
110 -> Option CQuick Check:
100 + 10 = 110 [OK]
- Forgetting to add tax to amount
- Confusing tax amount with total
- Assuming syntax error without checking code
function sendEmailToUser1() {
// send email logic
}
function sendEmailToUser2() {
// send email logic
}
What is the best fix to follow DRY?Solution
Step 1: Identify repeated code
Both functions have the same email sending logic repeated.Step 2: Refactor to reusable function
Create one function with a parameter for user to reuse the logic.Final Answer:
Create one function sendEmail(user) and reuse it -> Option BQuick Check:
Refactor repeated code = DRY [OK]
- Leaving duplicate functions unchanged
- Copy-pasting code more times
- Removing needed functionality
Solution
Step 1: Identify repeated authentication logic
Multiple services need the same authentication, so repeating code breaks DRY.Step 2: Use a shared authentication service
Centralizing authentication logic in one service allows reuse and easier maintenance.Final Answer:
Create a shared authentication service used by all services -> Option DQuick Check:
Shared service = DRY in system design [OK]
- Duplicating authentication code in each service
- Skipping authentication for simplicity
- Copy-pasting code instead of sharing
Ready to prove it?
Pass the test and this pattern moves unknown
