Bird
0
0
LLDsystem_design~7 mins

Fine calculation in LLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
When fines are calculated without a clear, consistent method, errors occur leading to unfair penalties or revenue loss. Manual or ad-hoc fine calculations cause confusion and disputes among users and administrators.
Solution
A fine calculation system applies predefined rules to compute penalties automatically based on input parameters like delay duration or violation type. This ensures consistent, transparent, and error-free fine amounts every time.
Architecture
┌───────────────┐      ┌───────────────┐      ┌───────────────┐
│   User Input  │─────▶│ Fine Calculation│─────▶│ Fine Output   │
│ (violation,  │      │   Module       │      │ (amount,     │
│  delay, etc) │      │ (rules engine) │      │  details)    │
└───────────────┘      └───────────────┘      └───────────────┘

This diagram shows the flow from user input through the fine calculation module applying rules, producing the fine output.

Trade-offs
✓ Pros
Ensures consistent and repeatable fine calculations.
Reduces manual errors and disputes over fines.
Easily update rules without changing core logic.
Improves transparency and auditability of fines.
✗ Cons
Requires upfront design of fine rules and edge cases.
May need frequent updates if regulations change often.
Complex rules can increase system complexity and testing effort.
Use when fines must be calculated automatically for many users or transactions, especially if rules are complex or frequently updated.
Avoid if fines are rare, simple, or manually handled with low volume, where automation overhead outweighs benefits.
Real World Examples
Uber
Automates penalty fees for late cancellations or no-shows to ensure consistent charges across millions of rides.
Amazon
Calculates fines for sellers violating marketplace policies, applying different rules per violation type.
Airbnb
Applies fines for guest damages or late check-outs based on predefined rules to protect hosts fairly.
Code Example
The before code uses simple if-else logic directly, which is hard to extend or maintain. The after code encapsulates fine rules in a class with a list of condition-action pairs, making it easy to add or modify rules without changing the calculation method.
LLD
### Before: naive fine calculation without clear structure

def calculate_fine(days_late):
    if days_late <= 0:
        return 0
    elif days_late <= 5:
        return days_late * 10
    else:
        return 100


### After: structured fine calculation using a class and rules

class FineCalculator:
    def __init__(self):
        self.rules = [
            (lambda days: days <= 0, lambda days: 0),
            (lambda days: days <= 5, lambda days: days * 10),
            (lambda days: True, lambda days: 100)
        ]

    def calculate(self, days_late):
        for condition, action in self.rules:
            if condition(days_late):
                return action(days_late)


# Usage
calculator = FineCalculator()
fine = calculator.calculate(3)  # returns 30
OutputSuccess
Alternatives
Manual fine calculation
Fines are computed by humans case-by-case without automation.
Use when: Use only when volume is very low and rules are simple or frequently changing.
Rule engine integration
Uses a dedicated external rule engine to manage and execute fine calculation rules.
Use when: Choose when fine rules are very complex and need non-developer management.
Summary
Automated fine calculation prevents errors and disputes by applying consistent rules.
Designing fine calculation as a flexible, rule-based system improves maintainability.
Automation is essential when fines are frequent, complex, or impact many users.