System Design
System Design
Fundamentals
4. Caching Strategy
• Use Redis/Memcached to reduce database load.
• Define cache eviction policies (LRU, LFU, TTL).
• Determine what to cache (e.g., user sessions, API
responses, frequently accessed data).
9. Performance Optimization
• Reduce latency (optimize database queries, use CDNs).
• Minimize network bandwidth usage (compression,
efficient payloads).
• Optimize database writes (batch processing, eventual
consistency).
Monolithic Architecture
Microservices Architecture
Data Flow:
1. Database Bottlenecks
3. Caching Issues
5. Network Bottlenecks
7. Scalability Challenges
3. Proxy Servers
A proxy server acts as an intermediary between clients and
backend servers, improving security, performance, and
scalability.
• Forward Proxy
o Used by clients to access external servers.
o Common in corporate networks for filtering web
access.
o Example: A company’s proxy that blocks access to
social media sites.
• Reverse Proxy
o Sits in front of backend servers to distribute traffic.
o Helps with load balancing, caching, and security
(e.g., preventing direct exposure of backend servers).
o Example: Nginx or HAProxy acting as a reverse proxy
for multiple backend microservices.
4. Load Balancing
Distributes incoming network traffic across multiple servers to
prevent overload and improve reliability.
• Types of Load Balancing:
o DNS Load Balancing – Different IP addresses for a
domain, resolved randomly.
o Application Layer (L7) Load Balancer – Routes traffic
based on request content (e.g., API gateway).
o Network Layer (L4) Load Balancer – Routes traffic at
the transport level (IP, TCP, UDP).
• Example:
o A web application with millions of users can use a
load balancer to distribute traffic across multiple
backend servers, preventing downtime if one server
fails.
5. Caching
Stores frequently accessed data in memory to reduce database
load and improve response times.
• Types of Caching:
o Client-Side Caching – Browsers cache static assets
like images, CSS, and JavaScript.
o Server-Side Caching – Databases and APIs cache
responses (e.g., Redis or Memcached).
o CDN (Content Delivery Network) – Caches static
content at edge locations worldwide.
• Example:
o A social media app caches frequently viewed user
profiles in Redis to avoid repeatedly querying the
database.
6. Database Sharding
Splitting a large database into smaller, more manageable parts
(shards) to improve performance and scalability.
• Horizontal Sharding (Range-Based or Hash-Based)
o Example: A user database is divided based on user
IDs, where users with IDs 1-100K go to one shard, and
100K-200K go to another.
• Vertical Sharding
o Example: Storing user profiles in one database and
user transactions in another.
Challenge:
• Requires a shard key to efficiently route queries.
7. Event-Driven Architecture
Instead of synchronous request-response interactions,
components communicate asynchronously using events.
• Message Brokers (e.g., Kafka, RabbitMQ) handle event
distribution.
• Example:
o An e-commerce system publishes an "Order Placed"
event, which different services (inventory, shipping,
billing) consume asynchronously.
8. Rate Limiting
Controls the number of requests a user or client can make in a
given time period to prevent abuse (e.g., API rate limiting).
• Example:
o A login system limits failed attempts to 5 per minute
to prevent brute-force attacks.
9. Consistent Hashing
A technique for distributing data across multiple servers in a way
that minimizes key redistribution when adding or removing
servers.
• Example:
o A caching system (e.g., Redis cluster) uses consistent
hashing to distribute user sessions across multiple
nodes efficiently.
Messages are
Events may be stored for
Persistence removed once
replay
consumed
Event-driven architecture
Task processing (e.g.,
Use Case (e.g., real-time
background jobs)
notifications)