CDN Sequence Flow - rahul7838/quora.clone GitHub Wiki

Let me help explain the request flow and address your questions.

sequenceDiagram
    participant Client
    participant DNS
    participant CDN
    participant API Gateway
    participant Mapping Service
    participant Redis Cache
    participant Database

    Client->>DNS: 1. DNS lookup
    DNS-->>Client: 2. Returns CDN IP
    Client->>CDN: 3. HTTP request
    
    alt Mapping found in CDN
        CDN-->>Client: 4a. Returns mapping (~10-50ms)
    else Mapping not in CDN
        CDN->>API Gateway: 4b. Forwards request
        API Gateway->>Mapping Service: 5. Routes request
        Mapping Service->>Redis Cache: 6. Check cache
        
        alt Found in Redis
            Redis Cache-->>Mapping Service: 7a. Returns mapping (~1ms)
        else Not in Redis
            Redis Cache-->>Mapping Service: 7b. Cache miss
            Mapping Service->>Database: 8. Query DB
            Database-->>Mapping Service: 9. Returns data (~10-100ms)
        end
        
        Mapping Service-->>CDN: 10. Returns mapping
        CDN-->>Client: 11. Returns response
    end

Let me address your specific questions:

  1. When a client sends a request:
  • The client first performs a DNS lookup to resolve the domain name
  • The DNS system returns the IP address of the nearest CDN edge server
  • The client then directly sends the HTTP request to that CDN IP address
  • There's no intermediate service - modern CDNs work by becoming the first point of contact through DNS resolution
  1. Typical CDN response times:
  • CDN cache hit (when mapping is found in CDN): ~10-50ms
  • CDN cache miss (when request goes to origin):
    • Total time = CDN processing (~5-10ms) + Network latency to origin + Origin processing time + Return journey
    • Full round trip usually takes 100-500ms depending on:
      • Geographic distance between CDN and origin
      • Origin processing time
      • Network conditions
      • Whether data needs to be fetched from Redis (~1ms) or Database (~10-100ms)

Note that these times are approximate and can vary based on:

  • Geographic location of client and servers
  • Network conditions and server load
  • Size of the data being transferred
  • CDN provider and configuration

Would you like me to explain any specific part of this flow in more detail?