What is CDN in System Design: Definition and Use Cases
CDN (Content Delivery Network) is a system of distributed servers that deliver web content to users based on their geographic location. It helps reduce latency and load on the main server by caching content closer to users, improving speed and reliability.How It Works
Imagine you want to send a letter to a friend who lives far away. Instead of sending it directly from your home every time, you use local post offices near your friend to deliver the letter faster. A CDN works similarly by placing copies of website content on servers around the world.
When a user requests a website, the CDN finds the closest server to that user and delivers the content from there. This reduces the time it takes for the data to travel, making the website load faster. It also reduces the traffic on the original server, preventing overload during high demand.
Example
import math def distance(lat1, lon1, lat2, lon2): # Calculate simple Euclidean distance for example return math.sqrt((lat1 - lat2)**2 + (lon1 - lon2)**2) # CDN servers with their locations (latitude, longitude) cdn_servers = { 'New York': (40.7128, -74.0060), 'London': (51.5074, -0.1278), 'Tokyo': (35.6895, 139.6917), 'Sydney': (-33.8688, 151.2093) } # User location user_location = (48.8566, 2.3522) # Paris # Find nearest CDN server nearest_server = min(cdn_servers.items(), key=lambda item: distance(user_location[0], user_location[1], item[1][0], item[1][1])) print(f"Nearest CDN server to user is in {nearest_server[0]}")
When to Use
Use a CDN when you want to improve website speed and reliability for users spread across different locations. It is especially useful for websites with heavy static content like images, videos, or scripts.
Real-world use cases include streaming services, e-commerce sites, and news portals that need to serve content quickly to a global audience. CDNs also help reduce server costs and protect against traffic spikes or attacks.
Key Points
- A CDN caches content on servers worldwide to reduce latency.
- It improves user experience by delivering content faster.
- CDNs reduce load on the origin server and increase availability.
- They are ideal for static content and global user bases.