0
0
LLDsystem_design~25 mins

Proxy pattern in LLD - System Design Exercise

Choose your learning style9 modes available
Design: Proxy Pattern Implementation
Design and implement the proxy pattern for a generic service object. Out of scope are specific business logic of the real object and network communication details.
Functional Requirements
FR1: Provide a surrogate or placeholder for another object to control access to it
FR2: Support lazy initialization to delay object creation until needed
FR3: Allow access control to restrict or log requests to the real object
FR4: Enable caching of expensive operations to improve performance
FR5: Maintain the same interface as the real object for seamless substitution
Non-Functional Requirements
NFR1: The proxy should not change the interface of the real object
NFR2: Must handle concurrent access safely if used in multithreaded environments
NFR3: Should add minimal overhead to method calls
NFR4: Support scalability for multiple proxy instances
NFR5: Latency for proxy operations should be under 50ms for typical calls
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
Subject interface defining common methods
Real subject implementing actual business logic
Proxy class implementing the same interface and controlling access
Client interacting only with the subject interface
Design Patterns
Decorator pattern for adding responsibilities dynamically
Adapter pattern for interface compatibility
Singleton pattern if proxy manages a single resource
Lazy initialization for deferred object creation
Reference Architecture
Client
  |
  v
Proxy (implements Subject interface)
  |
  v
Real Subject (implements Subject interface)
Components
Subject Interface
Abstract Interface or Base Class
Defines common methods for Real Subject and Proxy to implement
Real Subject
Concrete Class
Implements actual business logic and operations
Proxy
Concrete Class
Controls access to Real Subject, adds lazy loading, caching, or access control
Client
Any
Uses Subject interface to interact with Real Subject via Proxy transparently
Request Flow
1. Client calls method on Proxy object via Subject interface
2. Proxy checks if Real Subject is initialized; if not, creates it (lazy initialization)
3. Proxy optionally checks permissions or logs the request (access control)
4. Proxy forwards the method call to Real Subject
5. Real Subject executes the method and returns result
6. Proxy optionally caches the result before returning to Client
7. Client receives the result as if directly from Real Subject
Database Schema
Not applicable for Proxy pattern design as it focuses on object interaction and control rather than data storage.
Scaling Discussion
Bottlenecks
Proxy object becoming a performance bottleneck if it adds heavy processing
Concurrency issues if multiple clients access the proxy and real subject simultaneously
Memory overhead if many proxy instances hold references to real objects
Latency increase due to additional proxy layer
Complexity in managing lifecycle of real objects behind proxies
Solutions
Optimize proxy logic to minimize overhead and avoid blocking operations
Use thread-safe patterns or synchronization inside proxy for concurrent access
Implement proxy pooling or reuse to reduce memory footprint
Use asynchronous calls or caching to reduce latency impact
Clearly define ownership and lifecycle management policies for real objects
Interview Tips
Time: Spend 10 minutes explaining the problem and proxy pattern concept, 15 minutes designing the components and data flow, and 10 minutes discussing scaling and trade-offs.
Explain the purpose of the proxy pattern as a control layer
Describe different types of proxies and their use cases
Show how proxy and real subject share the same interface
Discuss lazy initialization and access control benefits
Highlight potential bottlenecks and how to mitigate them
Emphasize clean separation of concerns and interface transparency