|
| 1 | +--- |
| 2 | +myst: |
| 3 | + html_meta: |
| 4 | + description: Learn how to improve your Varnish cache hit rate on Hypernode by identifying automatic cache purges, analyzing hit/miss patterns, and optimizing URL normalization to boost performance and efficiency. |
| 5 | + title: Improving Varnish Cache Hit Rate on Hypernode |
| 6 | +--- |
| 7 | + |
| 8 | +# Improving Your Varnish Cache Hit Rate |
| 9 | + |
| 10 | +A higher Varnish cache hit rate means more requests are served directly from the cache. |
| 11 | +This reduces resource usage on your Hypernode, improves page load speed, and helps your shop handle more concurrent visitors without performance degradation. |
| 12 | + |
| 13 | +A low hit rate often indicates that cache is not being reused effectively, typically caused by misconfiguration, frequent invalidation, or too much variation in URLs. |
| 14 | + |
| 15 | +This guide takes you step-by-step from verifying that your cache is active to diagnosing and improving your hit ratio. |
| 16 | + |
| 17 | +## Before You Begin |
| 18 | + |
| 19 | +Typical cache hit rates: |
| 20 | +- **Below 10%** → Cache is barely reused |
| 21 | +- **30–70%** → Improvement possible (depends on shop type and traffic patterns) |
| 22 | +- **Above 80%** → Generally healthy for most shops |
| 23 | + |
| 24 | +Keep in mind: |
| 25 | +- Staging environments typically have low hit rates |
| 26 | +- B2B webshops often have lower hit rates due to personalization |
| 27 | +- Recently flushed caches temporarily reduce hit rates until the cache warms up |
| 28 | + |
| 29 | +Cache hit rate should always be evaluated in context. Traffic volume, personalization, and recent deployments directly affect cache reuse. |
| 30 | + |
| 31 | +## When a Low Hit Rate Is Expected |
| 32 | + |
| 33 | +A low hit rate does not always indicate a problem. It is normal when: |
| 34 | + |
| 35 | +- Traffic volume is low |
| 36 | +- The cache was recently flushed |
| 37 | +- Most visitors are logged in |
| 38 | +- The shop uses heavy personalization |
| 39 | +- You are working in a staging environment |
| 40 | + |
| 41 | +Investigate further only if traffic is stable, the cache is warmed up, and the hit rate remains consistently low. |
| 42 | + |
| 43 | +## Step 1 — Verify Varnish Is Enabled |
| 44 | + |
| 45 | +Ensure Varnish is properly enabled on your vhost and configured in your |
| 46 | +application (e.g. Magento 2). |
| 47 | + |
| 48 | +For Magento 2, verify: |
| 49 | +- That Varnish is enabled on the vhost |
| 50 | +- Varnish is selected as the caching application |
| 51 | +- The correct VCL is generated and loaded |
| 52 | +- Full Page Cache (FPC) is enabled |
| 53 | + |
| 54 | +For a step-by-step guide on activating and configuring Varnish in Magento 2, please refer to our [documentation here](https://docs.hypernode.com/ecommerce-applications/magento-2/how-to-configure-varnish-for-magento-2-x.html#how-to-configure-varnish-for-magento-2-x) |
| 55 | + |
| 56 | +```{tip} |
| 57 | +Tip: The [elgentos/magento2-varnish-extended](https://github.com/elgentos/magento2-varnish-extended) extension improves Magento’s default VCL configuration and marketing parameter handling. |
| 58 | +``` |
| 59 | + |
| 60 | +## Step 2 — Check if Pages Are Being Cached |
| 61 | + |
| 62 | +Using `varnishncsa` from the CLI to see in real time which pages are cached and which are not: |
| 63 | +```console |
| 64 | +varnishncsa -F '%U%q %{Varnish:hitmiss}x' |
| 65 | +``` |
| 66 | +Look for: |
| 67 | +- `hit` → Served from Varnish |
| 68 | +- `miss` → Served from backend |
| 69 | + |
| 70 | +Alternatively, you can use `curl` to inspect response headers: |
| 71 | + |
| 72 | +```console |
| 73 | +curl -I https://yourdomain.com |
| 74 | +``` |
| 75 | + |
| 76 | +Review the following headers: |
| 77 | + |
| 78 | +- **`Set-Cookie`** |
| 79 | + If a Set-Cookie header (such as PHPSESSID) is present, Varnish will typically not cache the response. |
| 80 | + |
| 81 | +- **`Cache-Control`** |
| 82 | + Should **not** contain `private`, `no-store`, or `no-cache`. |
| 83 | + |
| 84 | +- **`Age`** |
| 85 | + Indicates how long (in seconds) the object has been cached. |
| 86 | + |
| 87 | +- **`X-Magento-*`** |
| 88 | + Provides Magento cache/debug information (visible in developer mode). |
| 89 | + |
| 90 | +If most responses return `MISS` (for example in `X-Magento-Cache-Debug` or similar headers), caching is not being reused effectively. |
| 91 | + |
| 92 | +You can also inspect these headers in your browser via: |
| 93 | +Developer Tools → Network tab → Select request → Response Headers |
| 94 | + |
| 95 | +## Step 3 — Measure Your Cache Hit Rate |
| 96 | + |
| 97 | +Run: |
| 98 | + |
| 99 | +```console |
| 100 | +varnishstat -1 -f MAIN.cache_hit -f MAIN.cache_miss |
| 101 | +``` |
| 102 | + |
| 103 | +This shows: |
| 104 | +- `MAIN.cache_hit` → Cached responses served |
| 105 | +- `MAIN.cache_miss` → Requests sent to backend |
| 106 | + |
| 107 | +A high miss count relative to hits indicates room for improvement. |
| 108 | + |
| 109 | +For live monitoring of which requests are hitting Varnish, use: |
| 110 | + |
| 111 | +```console |
| 112 | +varnishlog |
| 113 | +``` |
| 114 | + |
| 115 | +Look for: |
| 116 | +- `VCL_call HIT` → Served from Varnish |
| 117 | +- `VCL_call MISS` → Served from backend |
| 118 | +- `Age` → Indicates how long (in seconds) the object has been cached. |
| 119 | +- `X-Magento-*` headers → Provides Magento cache/debug information (visible in developer mode). |
| 120 | + |
| 121 | +Alternatively, reuse the varnishncsa command from Step 2 for live hit/miss monitoring. |
| 122 | + |
| 123 | +## Step 4 — Common Causes of Low Hit Rates |
| 124 | + |
| 125 | +### 1. Pages Bypassing Varnish |
| 126 | + |
| 127 | +Some pages are intentionally not cached: |
| 128 | +- Checkout |
| 129 | +- Customer account pages |
| 130 | +- Requests containing `Set-Cookie` headers |
| 131 | + |
| 132 | +This is expected behavior. |
| 133 | + |
| 134 | +### 2. Frequent Cache Invalidations |
| 135 | + |
| 136 | +If cache clears happen frequently, cache reuse becomes nearly impossible. |
| 137 | + |
| 138 | +Common causes: |
| 139 | +- Stock or pricing integrations |
| 140 | +- Magento cron jobs performing full cache purges |
| 141 | +- Extensions invalidating excessive cache entries |
| 142 | + |
| 143 | +Best practice: |
| 144 | +Perform targeted purges (specific URLs or cache tags) instead of full cache |
| 145 | +flushes. |
| 146 | + |
| 147 | +### 3. Marketing & Tracking Parameters |
| 148 | + |
| 149 | +Tracking parameters create separate cache entries for identical content. |
| 150 | + |
| 151 | +Examples: |
| 152 | +- `utm_source` |
| 153 | +- `utm_medium` |
| 154 | +- `gclid` |
| 155 | +- `fbclid` |
| 156 | + |
| 157 | +Example problem: |
| 158 | +- /product-x |
| 159 | +- /product-x?utm_source=google |
| 160 | + |
| 161 | +These generate separate cache objects unless the URLs are normalized. |
| 162 | + |
| 163 | +Solution: |
| 164 | +Strip non-essential tracking parameters in VCL. |
| 165 | + |
| 166 | +```{tip} |
| 167 | +The [elgentos/magento2-varnish-extended](https://github.com/elgentos/magento2-varnish-extended) module improves this behavior. |
| 168 | +``` |
| 169 | + |
| 170 | +### 4. URL Normalization Issues |
| 171 | + |
| 172 | +Different URL formats fragment your cache. |
| 173 | + |
| 174 | +Examples: |
| 175 | +- `/category` vs `/category/` |
| 176 | +- `?Color=Red` vs `?color=red` |
| 177 | +- Unsorted query parameters |
| 178 | +- Session IDs in URLs |
| 179 | + |
| 180 | +Normalize URLs to ensure identical content maps to a single cache object in Varnish. |
| 181 | + |
| 182 | +### 5. Non-Cacheable Magento Blocks |
| 183 | + |
| 184 | +In Magento, a single block marked as non-cacheable can disable Full Page Cache (FPC) for the entire page. |
| 185 | + |
| 186 | +Search for non-cacheable blocks: |
| 187 | + |
| 188 | +```console |
| 189 | +grep -R "cacheable=\"false\"" app/code vendor |
| 190 | +``` |
| 191 | + |
| 192 | +If found: |
| 193 | +- Verify the block truly needs to be dynamic |
| 194 | +- Remove cacheable="false" if unnecessary |
| 195 | +- Use AJAX or Customer Data Sections for dynamic content |
| 196 | + |
| 197 | +Even one unnecessary non-cacheable block can severely impact hit rate. |
| 198 | + |
| 199 | +## Optional — Enable Magento Developer Mode for Debugging |
| 200 | + |
| 201 | +Enable developer mode temporarily for debugging purposes: |
| 202 | + |
| 203 | +```console |
| 204 | +magerun2 deploy:mode:set developer |
| 205 | +``` |
| 206 | + |
| 207 | +Or: |
| 208 | + |
| 209 | +```console |
| 210 | +php bin/magento deploy:mode:set developer |
| 211 | +``` |
| 212 | + |
| 213 | +## Debugging Tools |
| 214 | + |
| 215 | +### varnishlog |
| 216 | + |
| 217 | +Inspect detailed request handling: |
| 218 | +```console |
| 219 | +varnishlog |
| 220 | +``` |
| 221 | +Look for recurring MISS patterns on pages that should be cacheable. |
| 222 | + |
| 223 | +### varnishncsa |
| 224 | + |
| 225 | +Show hit/miss per URL: |
| 226 | +```console |
| 227 | +varnishncsa -F '%U%q %{Varnish:hitmiss}x' |
| 228 | +``` |
| 229 | +Filter for hits: |
| 230 | +```console |
| 231 | +varnishncsa -F '%U%q %{Varnish:hitmiss}x' | grep hit |
| 232 | +``` |
| 233 | + |
| 234 | +### Hypernode Insights (If Available) |
| 235 | + |
| 236 | +Use Hypernode Insights to: |
| 237 | +- Monitor hit/miss ratios |
| 238 | +- Detect purge spikes |
| 239 | +- Correlate cache drops with deployments or cron jobs |
0 commit comments