Applications best practices
Match device groups on precise words, set a TTL per rate of change, vary the cache key only by what changes the response, and cache derived images long.
Most mistakes in the layer between your users and your origin stay invisible until a user meets one. A visitor sees a page you already replaced, or the origin answers requests that a stored copy could have answered. A session cookie reaches a user it was never issued to, or an image arrives heavier or cropped differently than the page asked.
The cause is rarely one wrong value. More often, a cache key, the string Azion files a stored copy under, varies by something the response ignores. Or one time to live (TTL) covers content that changes at different rates, or a change is judged before anyone reads the response.
These practices apply to an Application and its device groups. They also cover the Rules Engine for Applications rules that act on its traffic, and the cache settings those rules apply. How Applications works explains the mechanisms behind them, and Applications limits holds the value of every bound. For the practices of the Functions that an Application runs, refer to Functions best practices.
The first two practices apply to the Application itself: the words a device group matches, and the requests that send the WebSocket upgrade headers. Sections for Cache, Application Accelerator, and Image Processor follow, one per Platform Resource nested in Applications, each in the order you meet its decisions.
Match a device group on the words that only that device sends
A device group is a name and a regular expression that an Application compares with the User-Agent header of each request. A rule can match on the group through ${device_group}, and a cache setting can vary by it. iPhone and iPod share iP, so an expression on iP puts both in one group.
Write each expression on the words that set its class of device apart. This device group, in the form the API accepts, catches most mobile devices: { "name": "Mobile", "user_agent": "(Mobile|iP(hone|od)|BlackBerry|IEMobile)" }.
The cost is coverage: a device of the class whose header carries none of the words falls outside the group. Where two classes share a word, the first matching group in the list wins, as Device Groups shows. With Debug Rules on, the log of a request from that class of device shows which rules that test ${device_group} ran on it. To create a group, refer to Create device groups.
Send the WebSocket upgrade headers only on requests that open a connection
By default, an Application with WebSocket Proxy turned on proxies every request that carries Upgrade: websocket and Connection: upgrade to the origin as a WebSocket connection, whatever its path. Control those headers in the application you build, and send them only where the WebSocket protocol should be used.
The decision then lives in your client code. The same client also reopens a connection that closes. Azion recycles keepalive connections approximately every 15 minutes, which can close an active WebSocket connection.
A connection that opens returns 101 Switching Protocols, and any other status, even a 2xx or a 3xx, means the upgrade did not complete. For the headers and the statuses, refer to WebSocket Proxy.
Cache
A cache setting belongs to one Application and holds how long Azion and the browser keep a response, and what makes two requests different. Kept too long, a copy shows a visitor content the origin already replaced. Kept too briefly, it sends the origin requests that a stored copy could have answered.
These practices apply to the cache settings of an Application and to the rules that apply those cache settings through the Set Cache Policy behavior. They cover, in order, the TTL, the name of an object, the purge of its variations, Tiered Cache, stale cache, and the cache status of a response. The first practice shows a complete cache setting in the form the API accepts, and later practices name only the field they change. The last practice shows how to confirm each change in the response.
Match each TTL to how often its content changes
Under the override behavior, the Max Age of a cache setting sets the TTL of a copy, in its modules.cache.max_age field. A longer TTL answers more requests but lags further behind the origin. Assets can change only when a deployment replaces them, while a page changes through the day, so one TTL misfits part of the content.
Give each group of paths that changes at one rate its own cache setting and rule. This setting keeps the assets 86,400 seconds in the browser and 300 seconds at Azion:
Pages edited during the day get a second setting and rule, with a lower Max Age. A Max Age under 60 seconds needs Application Accelerator, or the API refuses it with 21021. For every bound, refer to Applications limits. For the steps, refer to Create a cache setting.
Change an object’s name when its content changes
An object that keeps its name needs a purge of every cache layer and variation each time its content changes. Put a version in the file name instead. The cache treats each version as a separate object, and the first request for it fetches the current bytes from the origin. Any scheme serves, such as a counter, a timestamp, or a content hash, provided different content always gets a different name: https://static.example.com/assets/image_1.jpg becomes https://static.example.com/assets/image_2.jpg.
An update reaches users without a purge, and a browser that holds the earlier name keeps a valid object. Both versions stay available, so a rollback points your pages at the previous name. The cost is that every reference to the object changes with it, which a build step can automate and a hand edit cannot.
Purge an object that varies by cache key or wildcard
Real-Time Purge finds a copy by its cache key, and an object that varies has one key per variation. A URL purge converts the URL into one key. A copy that varies by cookie, device group, or image format survives it, while the purge reports success.
Name those keys in a cache key purge, up to 50 per request, or reach them with a wildcard. These Azion CLI commands send both, the wildcard ending in @@* for cookie variations:
Each command prints Purge carried out successfully. A variation with many values costs more to purge and to store, so choose its purge when you choose the variation. For the purge that reaches each variation, refer to Real-Time Purge. For the steps, refer to Purge cached content.
Keep Tiered Cache for long-lived objects with a stable body
Tiered Cache adds a second cache layer between the data center that answers a request and your origin. A valid copy of an object in that layer answers a miss in the first layer, and the origin receives no request. Turn Tiered Cache on for long-lived objects with a stable body, with modules.cache.tiered_cache set to { "enabled": true, "topology": "nearest-region" }.
The setting needs override, or the API refuses it with 21001. With Application Accelerator on, the Max Age floor is 3 seconds, and a lower value gets 21020. The cost is one extra hop on a first-layer miss, and the layer misfits content that changes every few seconds.
The layer is outside the reach of a Bypass Cache rule, as Rule out Tiered Cache before you rely on Bypass Cache explains. To remove an object from both layers, purge Tiered Cache before Cache, as Tiered Cache shows.
Turn on stale cache where an old page is better than an error
With stale cache on in a cache setting, Azion serves a copy past its TTL when revalidating it with the origin fails. It does so for the origin’s stale-while-revalidate value under the honor behavior, and for 300 seconds otherwise. The visitor gets the last good version rather than an error page. Set modules.cache.stale_cache.enabled to true for an article, a listing, or a product page. Keep it off for a price or an availability figure that a visitor decides on.
A response served this way reports STALE in x-cache. For the length of the window, what makes a revalidation fail, and when a purge serves better than expiry, refer to Expiration and freshness.
Read the cache status after every cache change
A setting states what Azion should store, and only the response shows what it stored. Send a request with Pragma: azion-debug-cache each time you create a setting, change a TTL, or run a purge:
The response carries x-cache: MISS from 192.0.2.10 with HTTP/2.0 and x-cache-key: httpswww.example.com/static/site.js. x-cache opens with the status, HIT for a stored copy and MISS for a trip to the origin. x-cache-key holds the key a cache key purge takes, with the variations the setting adds.
One response describes one copy on one server, so repeat the request before you conclude anything. For every status value, refer to Cache keys. For the steps, refer to Check the cache status of a response.
Application Accelerator
Every attribute a cache key varies by multiplies the copies Azion keeps of one URL, one copy per value. When the attribute leaves the response unchanged, those copies carry identical bytes, split the traffic between them, and add keys a purge has to reach.
Application Accelerator unlocks Rules Engine behaviors such as Bypass Cache and Forward Cookies, and it lets a cache setting change the fields of its modules.application_accelerator object. While it is off in the Application, the API refuses with 21013 a variation in those fields, a behavior other than ignore or a method in cache_vary_by_method.
The first four practices shape the key: the query string, its order, the cookies, and the session cookies under Forward Cookies. x-cache-key shows what each of them adds, as Read the cache status after every cache change explains. The last one concerns Bypass Cache and the Tiered Cache layer it leaves untouched.
Key the cache only on the arguments the response depends on
By default, ignore leaves the query string out of the cache key, so ?category=shoes and the bare path share one copy. Under all, every argument joins the key, so a link with a campaign argument gets an identical copy of its own.
Under allowlist, only the listed arguments vary the key, so the count of copies tracks the content, not the traffic. List exactly the arguments that change the response:
An empty list is refused with 21018. Leave off an argument that changes the response, and one visitor can see content meant for another. For the values of every variation, refer to Cache settings. For the steps, refer to Configure Advanced Cache Key for an application.
Sort the query string before it enters the key
Unsorted, the listed arguments join the cache key in the order the client wrote them. ?category=shoes&page=2 and ?page=2&category=shoes then become two copies of the same response. With sort_enabled set to true in the same cache_vary_by_querystring object, the arguments enter the key in alphabetical order, and both requests land on one copy.
The cost shows at purge time: a URL purge reaches the copy only when it names the arguments in alphabetical order. When the Azion CLI creates a cache setting, only --file and a JSON body set sort_enabled. On an update, --enable-caching-string-sort sets it. To see which purge reaches each variation, refer to Real-Time Purge.
List only the cookies that segment content
Browsers send every cookie they hold for a domain, and few of them change what the origin returns. Under all, an analytics identifier or a consent flag joins the key, and copies multiply with cookie values rather than with content. Under allowlist, only the cookies you name vary the key, which is how an Application segments content by user profile or another grouping.
Use the allowlist when cookies manage user sessions: behavior set to allowlist and session_id in cookie_names, under modules.application_accelerator.cache_vary_by_cookies. The cost is copies: a cookie unique per visitor means one copy per visitor. For the steps, refer to Configure Advanced Cache Key for an application.
Allowlist session cookies where Forward Cookies runs
The Forward Cookies behavior passes the origin’s Set-Cookie header on to users, cache hits included. A cached response can then hand one user the Set-Cookie of another user’s session. Name the session cookie in an allowlist, so each session gets its own key: behavior set to allowlist, with session_id in cookie_names. A denylist that names session_id leaves the cookie out of the key, and the sessions share one object.
Put the allowlist on the cache setting that the Forward Cookies rule applies through Set Cache Policy. For the steps, refer to Configure cache policies for an application.
Rule out Tiered Cache before you rely on Bypass Cache
The Bypass Cache behavior keeps Azion’s cache from storing the origin’s response, but the Tiered Cache layer is outside the rule’s reach. While the rule is active, a layer that the cache settings turn on keeps caching objects for the Max Age of the cache setting, whose floor is 3 seconds with Tiered Cache on. The layer shows only in the cache settings, not in the rule, so a rule that looks right can still leave content cached.
When freshness is the requirement, look for modules.cache.tiered_cache.enabled set to true in the cache settings that cover the rule’s paths. If no layer may keep a copy, decide on Tiered Cache together with the rule. For the symptom a missed layer produces and its fix, refer to Content is still served from cache after a Bypass Cache rule.
Image Processor
A transformed image can fail a page in three ways. The file outweighs what the page needs, or the crop removes something nobody meant to remove. Or the image arrives in a format the client cannot display. Around the transformation, the cache setting decides how derived images are stored and how often the same request reaches Image Processor again.
These practices apply to Image Processor: the ims query string, the rule whose Optimize Images behavior acts on that query string, and that rule’s cache setting. The first three practices concern the request: its quality, its resize, and the Accept header. The last two concern the cache setting: its allowlist and its TTL. x-cache and x-cache-key show the result of those two, as Read the cache status after every cache change explains.
Request quality 85 unless one image needs otherwise
The quality filter controls how hard Image Processor compresses a derived image, exchanging bytes for visual fidelity. Its argument is a whole number from 0 to 100. Request ?ims=filters:quality(85), which optimizes the file without a noticeable loss of visual quality.
A lower value makes the file smaller still, and the delivered image shows the loss. A higher one adds weight that a viewer cannot perceive. Depart from 85 only for an image that must look sharper, or weigh less, than 85 delivers. For the argument and its range, refer to Image Processor URL parameters.
Use fit-in when the image must keep its proportions
?ims=WidthxHeight fills an exact box. When the requested shape differs from the source, a centered autocrop trims the axis that overflows. Part of the subject can go with it. fit-in places the image inside the same box instead: it keeps the aspect ratio, and the result is at most the size of the original.
On a landscape photograph, ?ims=400x400 crops the picture to fill the square. ?ims=fit-in/400x400 keeps the whole picture and falls short of the square on one side. Use the plain resize when the layout needs the box filled, and fit-in when no edge may be lost. For each resize form, refer to Image Processor URL parameters.
Add the Accept header only where every client displays WEBP
An Add Request Header behavior with Accept: image/webp makes the automatic conversion deliver WEBP to every request the rule matches, including one whose client asked for PNG. A filters:format() filter does not need it: the response carries the named format whatever Accept header the request carries. Add the header only when every client of those paths displays WEBP.
The API takes the behavior as { "type": "add_request_header", "attributes": { "value": "Accept: image/webp" } } in the Request Phase of the rule, and it rejects the type add_header with 10039. For the conversion filter, refer to Image Processor URL parameters. For the criteria that limit a rule to image requests, refer to Image Processor settings.
Put ims in the allowlist of the cache setting that serves images
ims carries the transformation, so it is an argument the response depends on. Key the cache only on the arguments the response depends on applies to it. The cache setting that the image rule applies sets fields to ["ims"] under an allowlist.
The difference is a second Platform Resource: cache_vary_by_querystring belongs to modules.application_accelerator, so this allowlist needs Application Accelerator, even though transforming an image does not. For the Azion Console controls that set the field, refer to Image Processor settings.
Keep derived images cached as long as their sources allow
Image Processor adds one image to the monthly Images meter for each request it processes, whether the request resizes, crops, converts, or filters it. A request the cache answers runs no transformation and adds nothing. A short Max Age sends the same request to Image Processor again on a timer unrelated to changes at the source.
Give the cache setting for derived images the longest modules.cache.max_age its source content tolerates, up to 31,536,000 seconds. The cost is freshness. A replaced source image leaves its derived versions, one key per processing and per format, cached until they expire or a purge removes them. For the images each plan includes, refer to Azion pricing.