0
0
LLDsystem_design~25 mins

Pricing strategy (discounts, coupons) in LLD - System Design Exercise

Choose your learning style9 modes available
Design: Pricing Strategy System
Design covers discount and coupon management, validation, application logic, and usage tracking. Payment processing and product catalog are out of scope.
Functional Requirements
FR1: Support multiple types of discounts: percentage off, fixed amount off, buy-one-get-one (BOGO)
FR2: Allow creation and management of coupons with unique codes
FR3: Coupons can have constraints: expiration date, usage limits per user and overall
FR4: Apply discounts and coupons during checkout to calculate final price
FR5: Support stacking rules: which discounts can be combined or are exclusive
FR6: Track coupon usage for analytics and enforcement
FR7: Provide APIs for frontend to validate and apply discounts in real-time
Non-Functional Requirements
NFR1: Handle up to 10,000 concurrent users applying discounts
NFR2: API response time for discount validation must be under 100ms p99
NFR3: System availability target 99.9% uptime
NFR4: Ensure data consistency for coupon usage counts to prevent overuse
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
❓ Question 6
Key Components
Discount and coupon management service
Validation engine for applying rules
API gateway for frontend integration
Database for storing coupons, discounts, and usage data
Cache layer for fast lookup of active coupons
Analytics and reporting module
Design Patterns
Rule engine pattern for flexible discount logic
Cache aside pattern for coupon data caching
Circuit breaker for external dependencies
Event sourcing or transactional outbox for usage tracking
Rate limiting to prevent abuse
Reference Architecture
  +-------------+       +---------------------+       +----------------+
  |  Frontend   | <---> | API Gateway / Proxy | <---> | Discount Service|
  +-------------+       +---------------------+       +----------------+
                                                      |        |
                                                      |        v
                                                      |   +------------+
                                                      |   | Cache (Redis)|
                                                      |   +------------+
                                                      |        |
                                                      v        v
                                                +---------------------+
                                                |  Relational Database |
                                                | (Coupons, Discounts, |
                                                |  Usage Tracking)     |
                                                +---------------------+

  +---------------------+
  | Analytics & Reporting|
  +---------------------+
Components
Frontend
Any web/mobile client
User interface to enter coupon codes and see discounted prices
API Gateway / Proxy
Nginx, Envoy, or cloud API Gateway
Route requests, handle authentication, rate limiting
Discount Service
Microservice in preferred language (e.g., Node.js, Python)
Core logic for discount and coupon validation, application, and usage tracking
Cache (Redis)
Redis
Store active coupons and discount rules for fast validation
Relational Database
PostgreSQL or MySQL
Persist coupons, discount definitions, usage counts, and constraints
Analytics & Reporting
Data warehouse or BI tools
Analyze coupon usage patterns and effectiveness
Request Flow
1. User enters coupon code at checkout in frontend.
2. Frontend sends coupon code and cart details to API Gateway.
3. API Gateway forwards request to Discount Service.
4. Discount Service checks Redis cache for coupon validity and rules.
5. If not in cache, Discount Service queries database and updates cache.
6. Discount Service validates coupon constraints (expiration, usage limits).
7. Discount Service applies discount rules to calculate final price.
8. Discount Service updates usage count transactionally in database.
9. Discount Service returns discounted price and validation status to frontend.
10. Frontend displays final price and discount details to user.
Database Schema
Entities: - Coupon: id (PK), code (unique), type (percentage, fixed, BOGO), value, expiration_date, max_usage, max_usage_per_user, combinable (bool) - DiscountRule: id (PK), coupon_id (FK), rule_type, parameters (JSON) - CouponUsage: id (PK), coupon_id (FK), user_id, usage_count - User: id (PK), user details Relationships: - Coupon has many DiscountRules - CouponUsage tracks usage per user per coupon - Constraints enforced via Coupon fields and CouponUsage counts
Scaling Discussion
Bottlenecks
Database write contention on coupon usage counts under high concurrency
Cache invalidation delays causing stale coupon data
API Gateway becoming a bottleneck with many concurrent requests
Complex discount rule evaluation increasing response latency
Solutions
Use optimistic locking or atomic counters in database to handle concurrent usage updates
Implement cache expiration and event-driven cache invalidation on coupon updates
Scale API Gateway horizontally with load balancing
Precompute discount rules where possible and optimize rule engine for performance
Interview Tips
Time: 10 minutes for requirements and clarifications, 20 minutes for architecture and data flow, 10 minutes for scaling and trade-offs, 5 minutes for questions
Clarify discount types and stacking rules early
Explain how caching improves performance and reduces DB load
Discuss data consistency strategies for usage tracking
Highlight how the system prevents coupon abuse
Mention scalability considerations and bottleneck mitigation