How to Identify Performance Bottlenecks
and Solving Them with the Ruleset Engine

Erfi Anugrah

Overview

What we’ll cover:

%%{init: {
  'theme': 'dark',
  'themeVariables': {
    'primaryColor': '#ff9800',
    'primaryTextColor': '#fff',
    'primaryBorderColor': '#fff',
    'lineColor': '#fff',
    'secondaryColor': '#006064',
    'tertiaryColor': '#4caf50',
    'mainBkg': '#2d3436',
    'nodeBorder': '#ff9800',
    'clusterBkg': '#2d3436',
    'clusterBorder': '#ff9800'
  },
  'themeCSS': '.edgePath path { stroke: #fff !important; }'
}}%%
graph TB
    Start[Key Performance Metrics] --> Bottlenecks[Common Bottlenecks]
    Bottlenecks --> Analysis[Analysis Methodology]
    Analysis --> Data[GraphQL Data Collection]
    Data --> Troubleshoot[Troubleshooting Workflow]
    Troubleshoot --> Solution[Implementation Solutions]
    
    classDef orange fill:#ff9800,stroke:#fff,stroke-width:2px,color:#fff;
    classDef blue fill:#006064,stroke:#fff,stroke-width:2px,color:#fff;
    classDef green fill:#4caf50,stroke:#fff,stroke-width:2px,color:#fff;

    class Start,Bottlenecks orange;
    class Analysis,Data blue;
    class Troubleshoot,Solution green;

Key Performance Metrics

%%{init: {
  'theme': 'dark',
  'themeVariables': {
    'primaryColor': '#ff9800',  /* Orange for main nodes */
    'primaryTextColor': '#fff',
    'primaryBorderColor': '#fff',
    'lineColor': '#fff',        /* Ensure lines are white */
    'secondaryColor': '#006064',  /* Blue-green for Response Times */
    'tertiaryColor': '#4caf50',   /* Green for Geographic Distribution */
    'quaternaryColor': '#8e44ad', /* Purple for Resource Utilization */
    'mainBkg': '#2d3436',
    'nodeBorder': '#fff',
    'clusterBkg': '#2d3436',
    'clusterBorder': '#fff'
  }
}}%%
graph LR
    Performance[Performance Metrics]
    Performance --> Response[Response Times]
    Performance --> Geographic[Geographic Distribution]
    Performance --> Resource[Resource Utilization]

    Response --> TTFB[Edge TTFB]
    Response --> Origin[Origin Response Time]
    Response --> Cache[Cache Performance]

    Geographic --> Latency[Regional Latency]
    Geographic --> Efficiency[Cache Efficiency]
    Geographic --> Errors[Error Distribution]

    Resource --> Load[Origin Load]
    Resource --> Bottlenecks[Application Bottlenecks]

    classDef orange fill:#ff9800,stroke:#fff,stroke-width:2px,color:#fff;
    classDef blue fill:#006064,stroke:#fff,stroke-width:2px,color:#fff;
    classDef green fill:#4caf50,stroke:#fff,stroke-width:2px,color:#fff;
    classDef purple fill:#8e44ad,stroke:#fff,stroke-width:2px,color:#fff;

    class Performance orange;
    class Response,TTFB,Origin,Cache blue;
    class Geographic,Latency,Efficiency,Errors green;
    class Resource,Load,Bottlenecks purple;

Common Performance Bottlenecks

%%{init: {
  'theme': 'dark',
  'themeVariables': {
    'primaryColor': '#ff9800',  /* Orange for main categories */
    'primaryTextColor': '#fff',
    'primaryBorderColor': '#fff',
    'lineColor': '#fff',        /* Ensure lines are white */
    'secondaryColor': '#4caf50',   /* Green for Cache Issues */
    'tertiaryColor': '#006064',    /* Blue for Network Problems */
    'quaternaryColor': '#8e44ad',  /* Purple for Origin Issues */
    'mainBkg': '#2d3436',
    'nodeBorder': '#fff',
    'clusterBkg': '#2d3436',
    'clusterBorder': '#fff'
  }
}}%%
graph TB
    Performance[Performance Bottlenecks]

    %% First Level
    Performance --> Cache[Cache Issues]
    Performance --> Network[Network Problems]
    Performance --> Origin[Origin Issues]

    %% Cache Issues (Compact Layout)
    Cache --> LowCache[Low Cache Hit Ratios]
    Cache --> TTL[Ineffective/Low TTLs]
    Cache --> Rules[Missing Cache Rules]
    Cache --> Design[Poor Cache Key Design]

    %% Network Problems (Compact Layout)
    Network --> Latency[High Origin Latency]
    Network --> Routing[Poor Routing Paths]
    Network --> Connectivity[Regional Connectivity]
    Network --> Limits[Connection Limits]

    %% Origin Issues (Compact Layout)
    Origin --> Overload[Server Overload]
    Origin --> Database[Database Bottlenecks]
    Origin --> Resources[Resource Exhaustion]
    Origin --> Timeouts[Timeout Configuration]

    %% Class Definitions
    classDef orange fill:#ff9800,stroke:#fff,stroke-width:2px,color:#fff;
    classDef green fill:#4caf50,stroke:#fff,stroke-width:2px,color:#fff;
    classDef blue fill:#006064,stroke:#fff,stroke-width:2px,color:#fff;
    classDef purple fill:#8e44ad,stroke:#fff,stroke-width:2px,color:#fff;

    class Performance orange;
    class Cache,LowCache,TTL,Rules,Design green;
    class Network,Latency,Routing,Connectivity,Limits blue;
    class Origin,Overload,Database,Resources,Timeouts purple;

Analysis Workflow

%%{init: {
  'theme': 'dark',
  'themeVariables': {
    'primaryColor': '#ff9800',
    'primaryTextColor': '#fff',
    'primaryBorderColor': '#fff',
    'lineColor': '#fff',
    'secondaryColor': '#006064',
    'tertiaryColor': '#4caf50',
    'mainBkg': '#2d3436',
    'nodeBorder': '#ff9800',
    'clusterBkg': '#2d3436',
    'clusterBorder': '#ff9800'
  }
}}%%
graph TB
    Start[Performance Issue] --> Identify{Identify Problem Type}
    Identify --> Cache[Cache Analysis]
    Identify --> Network[Network Analysis]
    Identify --> Origin[Origin Analysis]
    
    Cache --> CacheMetrics[Cache Metrics]
    Cache --> HitRatio[Hit Ratio]
    Cache --> CacheRules[Cache Rules]
    
    Network --> Latency[Latency Analysis]
    Network --> Routes[Route Analysis]
    Network --> Geo[Geographic Distribution]
    
    Origin --> Server[Server Metrics]
    Origin --> Database[Database Servers]
    Origin --> Application[Application Servers]
    
    CacheMetrics --> Solution{Solution}
    HitRatio --> Solution
    CacheRules --> Solution
    Latency --> Solution
    Geo --> Solution
    Routes --> Solution
    Server --> Solution
    Database --> Solution
    Application --> Solution

    classDef orange fill:#ff9800,stroke:#fff,stroke-width:2px,color:#fff;
    classDef blue fill:#006064,stroke:#fff,stroke-width:2px,color:#fff;
    classDef green fill:#4caf50,stroke:#fff,stroke-width:2px,color:#fff;

    class Start,Identify,Solution orange;
    class Cache,Network,Origin blue;
    class CacheMetrics,HitRatio,CacheRules,Latency,Routes,SSL,Server,Database,Application,Geo green;

Initial Investigation Steps

1. Baseline Metrics

  • Current cache hit ratio
  • Average response times
  • Quantiles for response times
  • Error rate patterns
  • Geographic distribution
  • Resource utilization

2. Impact Assessment

  • Affected regions
  • User experience impact
  • Origin load
  • Bandwidth implications

3. Root Cause Analysis

  • Timeline correlation
  • Configuration review
  • Log analysis
  • Performance profiling

GraphQL Demo

  • Performance analysis walkthrough
  • Common issue resolution
  • Implementation guidance

GraphQL Performance Analysis

query ZoneMetricsBasic($zoneTag: String!, $filter: ZoneHttpRequestsAdaptiveGroupsFilter!) {
  viewer {
    zones(filter: { zoneTag: $zoneTag }) {
      httpRequestsAdaptiveGroups(
        limit: 5000
        filter: $filter
        orderBy: [datetimeMinute_DESC]
      ) {
        dimensions {
          datetimeMinute
          clientCountryName
          clientDeviceType
          clientRequestHTTPProtocol
          edgeResponseContentTypeName
          edgeResponseStatus
          cacheStatus
          coloCode
          upperTierColoName
        }
        avg {
          sampleInterval
          edgeTimeToFirstByteMs
          originResponseDurationMs
        }
        sum {
          visits
          edgeResponseBytes
        }
        count
        ratio {
          status4xx
          status5xx
        }
      }
    }
  }
}


query ZoneMetricsDetailed($zoneTag: String!, $filter: ZoneHttpRequestsAdaptiveGroupsFilter!) {
  viewer {
    zones(filter: { zoneTag: $zoneTag }) {
      httpRequestsAdaptiveGroups(
        limit: 5000
        filter: $filter
        orderBy: [datetimeMinute_DESC]
      ) {
        dimensions {
          datetimeMinute
          clientAsn
          clientIP
          clientRefererHost
          clientRequestHTTPHost
          clientRequestPath
          clientRequestHTTPMethodName
        }
        quantiles {
          edgeTimeToFirstByteMsP50
          edgeTimeToFirstByteMsP95
          edgeTimeToFirstByteMsP99
          originResponseDurationMsP50
          originResponseDurationMsP95
          originResponseDurationMsP99
        }
        sum {
          edgeTimeToFirstByteMs
          originResponseDurationMs
        }
      }
    }
  }
}


Take a Break ☕

Ruleset Engine with Terraform

  • Dashboard demo into rule building
  • Implementation guidance

Cache Optimization Solutions

1. Static Content Caching

resource "cloudflare_ruleset" "static_cache" {
  zone_id = var.zone_id
  name    = "Static Content Optimization"
  phase   = "http_request_cache_settings"

  rules {
    action = "set_cache_settings"
    action_parameters {
      cache = true
      edge_ttl {
        default = 31536000  # 1 year
        mode    = "override_origin"
      }
      browser_ttl {
        default = 86400    # 1 day
        mode    = "override_origin"
      }
      cache_key {
        custom_key {
          query_string {
            exclude = ["*"]  # Ignore all query params
          }
        }
      }
    }
    expression = "(http.request.uri.path matches \"\\.(jpg|jpeg|gif|png|ico|css|js|svg|woff2)$\")"
  }
}


2. API Response Caching

resource "cloudflare_ruleset" "api_cache" {
  zone_id = var.zone_id
  name    = "API Cache"
  phase   = "http_request_cache_settings"

  rules {
    action = "set_cache_settings"
    action_parameters {
      cache = true
      cache_key {
        custom_key {
          header {
            include = ["Authorization"]
          }
          query_string {
            include = ["version", "locale"]
          }
        }
      }
      edge_ttl {
        default = 300  # 5 minutes
        mode    = "respect_origin"
      }
      serve_stale {
        disable_stale_while_updating = false
      }
    }
    expression = "(http.host eq \"api.example.com\" and http.request.uri.path contains \"/v1/\")"
  }
}

Network Optimization

1. Argo Smart Routing

resource "cloudflare_argo" "argo" {
  zone_id        = var.cloudflare_zone_id
  smart_routing  = "on"
}


2. Tiered Cache

resource "cloudflare_tiered_cache" "tc" {
  zone_id        = var.cloudflare_zone_id
  cache_type     = "smart"
}

resource "cloudflare_regional_tiered_cache" "regional_tc" {
  zone_id        = var.cloudflare_zone_id
  value          = "on"
}


3. Load Balancing

resource "cloudflare_load_balancer" "revista" {
  default_pool_ids = [cloudflare_load_balancer_pool.revista_k3s_nl.id,  cloudflare_load_balancer_pool.revista_ipsec_k3s_nl.id]
  enabled          = true
  fallback_pool_id = cloudflare_load_balancer_pool.revista_sg.id
  name             = var.domain_name
  proxied          = true
  session_affinity = "none"
  steering_policy  = "geo"
  zone_id          = var.cloudflare_zone_id
  pop_pools {
    pop      = "AMS"
    pool_ids = [cloudflare_load_balancer_pool.revista_k3s_nl.id, cloudflare_load_balancer_pool.revista_ipsec_k3s_nl.id]
  }
  pop_pools {
    pop      = "SIN"
    pool_ids = [cloudflare_load_balancer_pool.revista_sg.id]
  }
  adaptive_routing {
    failover_across_pools = true
  }
  location_strategy {
    mode       = "pop"
    prefer_ecs = "proximity"
  }
  random_steering {
    default_weight = 1
  }
  session_affinity_attributes {
    samesite               = "Auto"
    secure                 = "Auto"
    zero_downtime_failover = "temporary"
  }
}


4. Regional Cache Configuration

resource "cloudflare_ruleset" "regional_cache" {
  zone_id = var.zone_id
  name    = "Regional Cache Settings"
  phase   = "http_request_cache_settings"

  rules {
    action = "set_cache_settings"
    action_parameters {
      cache = true
      cache_key {
        custom_key {
          user {
            geo = true
            device_type = true
          }
        }
      }
      edge_ttl {
        default = 3600
        mode    = "override_origin"
      }
    }
    expression = "(ip.src.country in {\"US\" \"CA\" \"GB\"} and http.request.uri.path contains \"/content/\")"
  }
}

Things to Look Out For

%%{init: {
  'theme': 'dark',
  'themeVariables': {
    'primaryColor': '#ff9800',  /* Orange for main categories */
    'primaryTextColor': '#fff',
    'primaryBorderColor': '#fff',
    'lineColor': '#fff',        /* Ensure lines are white */
    'secondaryColor': '#4caf50',   /* Green for Cache Optimization */
    'tertiaryColor': '#006064',    /* Blue for Network Optimization */
    'quaternaryColor': '#8e44ad',  /* Purple for Origin Optimization */
    'mainBkg': '#2d3436',
    'nodeBorder': '#fff',
    'clusterBkg': '#2d3436',
    'clusterBorder': '#fff'
  }
}}%%
graph TB
    Solutions[Optimization Solutions]

    %% Categories
    Solutions --> Cache[Cache Optimization]
    Solutions --> Network[Network Optimization]
    Solutions --> Origin[Origin Optimization]

    %% Cache Optimization (Compact Layout)
    Cache --> TTL[Appropriate TTLs]
    Cache --> Keys[Custom Cache Keys]
    Cache --> Tiered[Tiered Caching]

    %% Network Optimization (Compact Layout)
    Network --> Argo[Argo Smart Routing]
    Network --> Balancing[Load Balancing]
    Network --> Monitoring[Latency Monitoring]

    %% Origin Optimization (Compact Layout)
    Origin --> Errors[Error Handling]
    Origin --> Services[Service Monitoring]
    Origin --> Stale[Serve Stale Content]

    %% Class Definitions
    classDef orange fill:#ff9800,stroke:#fff,stroke-width:2px,color:#fff;
    classDef green fill:#4caf50,stroke:#fff,stroke-width:2px,color:#fff;
    classDef blue fill:#006064,stroke:#fff,stroke-width:2px,color:#fff;
    classDef purple fill:#8e44ad,stroke:#fff,stroke-width:2px,color:#fff;

    class Solutions orange;
    class Cache,TTL,Keys,Tiered green;
    class Network,Argo,Balancing,Monitoring blue;
    class Origin,Errors,Services,Stale purple;

Survey