Defeat the speed of light by moving data physically closer to the user.
If your server is in New York, a user in Tokyo has to wait ~200ms just for light to travel back and forth through fiber optic cables. You can't optimize physics.
A Content Delivery Network (CDN) places "Edge Servers" (Points of Presence / PoPs) in hundreds of cities worldwide. When the Tokyo user requests an image, they hit the Tokyo PoP. If it's cached (Hit), it returns in 10ms. If not (Miss), the PoP fetches it from the NY Origin, caches it, and serves it. This shields the origin from massive traffic.
# The CDN intercepts the DNS request and routes the user
# to the physically closest IP address via Anycast.
def handle_edge_request(request):
edge_cache = local_pop_storage()
# Check if we have a fresh copy
if edge_cache.has(request.url) and not is_expired(edge_cache.ttl):
return edge_cache.get(request.url) # FAST HIT (10ms)
# MISS (or expired). Fetch from Origin over long-haul network.
response = fetch_from_origin(request.url) # SLOW MISS (200ms)
# Store for next time based on Cache-Control headers
edge_cache.set(request.url, response, ttl=3600)
return response