Bird
Raised Fist0
LLDsystem_design~7 mins

DRY (Don't Repeat Yourself) in LLD - System Design Guide

Choose your learning style10 modes available
Problem Statement
When the same code or logic is copied in multiple places, it becomes hard to maintain. Fixing a bug or updating a feature requires changes in many spots, increasing the chance of errors and inconsistencies.
Solution
DRY solves this by ensuring every piece of knowledge or logic exists in only one place. Instead of copying code, you create reusable functions or classes and call them wherever needed, so updates happen in one spot only.
Architecture
Repeated
Code Block 1
↓
Single Function
(Reusable logic called by both places)

This diagram shows two places with repeated code replaced by calls to a single reusable function, centralizing the logic.

Trade-offs
✓ Pros
→
Reduces bugs by having a single source of truth for logic.
→
Simplifies maintenance since changes happen in one place.
→
Improves code readability by avoiding clutter from repeated code.
✗ Cons
→
May introduce extra function calls, slightly impacting performance in critical paths.
→
Over-abstraction can make code harder to follow if taken too far.
→
Requires upfront design effort to identify reusable parts.
Use DRY when the same logic or code appears in two or more places, especially in medium to large codebases where maintenance cost is high.
Avoid DRY in very small scripts or one-off code where abstraction adds unnecessary complexity and overhead.
Real World Examples
Amazon
Amazon uses DRY principles in their payment processing code to ensure all payment validation logic is centralized, reducing errors and speeding up updates.
Netflix
Netflix applies DRY in their streaming service code to reuse authentication checks across multiple microservices, ensuring consistent security.
Stripe
Stripe uses DRY to centralize billing calculations, so all products and services use the same pricing logic, avoiding discrepancies.
Code Example
The before code repeats the multiplication logic in two functions. The after code extracts multiplication into a single function 'multiply' and reuses it, following DRY.
LLD
### Before applying DRY (repeated code)
def calculate_area_rectangle(width, height):
    return width * height

def calculate_area_square(side):
    return side * side

# Both functions repeat multiplication logic

### After applying DRY (reusable function)
def multiply(a, b):
    return a * b

def calculate_area_rectangle(width, height):
    return multiply(width, height)

def calculate_area_square(side):
    return multiply(side, side)
OutputSuccess
Alternatives
WET (Write Everything Twice)
WET allows code duplication without abstraction, leading to repeated logic in multiple places.
Use when: Choose WET only for very small or experimental projects where speed of writing is more important than maintainability.
Copy-Paste Programming
Directly copying code blocks instead of abstracting them into reusable units.
Use when: Use only in quick prototyping or throwaway scripts where code reuse is not a concern.
Summary
DRY prevents bugs and eases maintenance by centralizing repeated logic into one place.
It encourages reusable functions or classes instead of copying code.
Applying DRY requires balancing abstraction with code clarity.

Practice

(1/5)
1. What is the main goal of the DRY principle in system design?
easy
A. To avoid repeating code or design to keep systems simple
B. To write as much code as possible for clarity
C. To duplicate modules for faster development
D. To create multiple versions of the same component

Solution

  1. Step 1: Understand the meaning of DRY

    DRY stands for "Don't Repeat Yourself," which means avoiding repeated code or design.
  2. Step 2: Identify the goal of DRY

    The goal is to keep systems simple, maintainable, and reduce bugs by reusing components.
  3. Final Answer:

    To avoid repeating code or design to keep systems simple -> Option A
  4. Quick Check:

    DRY = Avoid repetition [OK]
Hint: DRY means write once, use many times [OK]
Common Mistakes:
  • Thinking DRY means writing more code
  • Believing duplication speeds development
  • Confusing DRY with writing multiple versions
2. Which of the following is a correct way to follow DRY in system design?
easy
A. Creating reusable components or modules
B. Ignoring code reuse to save time
C. Writing separate code for each similar task
D. Copy-pasting the same code in multiple modules

Solution

  1. Step 1: Identify DRY best practice

    DRY encourages reuse by creating components or modules that can be used multiple times.
  2. Step 2: Evaluate options

    Copy-pasting or writing separate code repeats work, which breaks DRY. Ignoring reuse wastes effort.
  3. Final Answer:

    Creating reusable components or modules -> Option A
  4. Quick Check:

    Reuse = DRY [OK]
Hint: Reuse code with modules/components [OK]
Common Mistakes:
  • Copy-pasting code instead of reusing
  • Writing separate code for similar tasks
  • Ignoring reuse to save time
3. Consider this pseudocode snippet following DRY:
function calculateTax(amount) {
  return amount * 0.1;
}

function calculateTotal(amount) {
  return amount + calculateTax(amount);
}

print(calculateTotal(100));
What is the output?
medium
A. 10
B. 100
C. 110
D. Syntax Error

Solution

  1. Step 1: Calculate tax for amount 100

    Tax = 100 * 0.1 = 10
  2. Step 2: Calculate total using tax

    Total = 100 + 10 = 110
  3. Final Answer:

    110 -> Option C
  4. Quick Check:

    100 + 10 = 110 [OK]
Hint: Add tax to amount using reusable function [OK]
Common Mistakes:
  • Forgetting to add tax to amount
  • Confusing tax amount with total
  • Assuming syntax error without checking code
4. You see this code snippet violating DRY:
function sendEmailToUser1() {
  // send email logic
}

function sendEmailToUser2() {
  // send email logic
}
What is the best fix to follow DRY?
medium
A. Copy-paste send email logic in more functions
B. Create one function sendEmail(user) and reuse it
C. Keep both functions as is for clarity
D. Remove email sending completely

Solution

  1. Step 1: Identify repeated code

    Both functions have the same email sending logic repeated.
  2. Step 2: Refactor to reusable function

    Create one function with a parameter for user to reuse the logic.
  3. Final Answer:

    Create one function sendEmail(user) and reuse it -> Option B
  4. Quick Check:

    Refactor repeated code = DRY [OK]
Hint: Use parameters to reuse functions [OK]
Common Mistakes:
  • Leaving duplicate functions unchanged
  • Copy-pasting code more times
  • Removing needed functionality
5. You design a system with multiple services needing user authentication. To follow DRY, which approach is best?
hard
A. Implement authentication logic separately in each service
B. Skip authentication to reduce complexity
C. Copy authentication code into each service for speed
D. Create a shared authentication service used by all services

Solution

  1. Step 1: Identify repeated authentication logic

    Multiple services need the same authentication, so repeating code breaks DRY.
  2. Step 2: Use a shared authentication service

    Centralizing authentication logic in one service allows reuse and easier maintenance.
  3. Final Answer:

    Create a shared authentication service used by all services -> Option D
  4. Quick Check:

    Shared service = DRY in system design [OK]
Hint: Centralize common logic in shared services [OK]
Common Mistakes:
  • Duplicating authentication code in each service
  • Skipping authentication for simplicity
  • Copy-pasting code instead of sharing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard