1.. Licensed to the Apache Software Foundation (ASF) under one
2   or more contributor license agreements.  See the NOTICE file
3   distributed with this work for additional information
4   regarding copyright ownership.  The ASF licenses this file
5   to you under the Apache License, Version 2.0 (the
6   "License"); you may not use this file except in compliance
7   with the License.  You may obtain a copy of the License at
8
9   http://www.apache.org/licenses/LICENSE-2.0
10
11   Unless required by applicable law or agreed to in writing,
12   software distributed under the License is distributed on an
13   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14   KIND, either express or implied.  See the License for the
15   specific language governing permissions and limitations
16   under the License.
17
18.. include:: ../../common.defs
19
20.. _http-proxy-caching:
21
22HTTP Proxy Caching
23******************
24
25HTTP proxy caching enables you to store copies of frequently-accessed web
26objects (such as documents, images, and articles) and then serve this
27information to users on demand. It improves performance and frees up
28Internet bandwidth for other tasks.
29
30.. toctree::
31   :maxdepth: 2
32
33Understanding HTTP Web Proxy Caching
34====================================
35
36Internet users direct their requests to web servers all over the
37Internet. A caching server must act as a *web proxy server* so it can
38serve those requests. After a web proxy server receives requests for web
39objects, it either serves the requests or forwards them to the *origin
40server* (the web server that contains the original copy of the
41requested information). The Traffic Server proxy supports *explicit
42proxy caching*, in which the user's client software must be configured
43to send requests directly to the Traffic Server proxy. The following
44overview illustrates how Traffic Server serves a request.
45
46#. Traffic Server receives a client request for a web object.
47
48#. Using the object address, Traffic Server tries to locate the
49   requested object in its object database (*cache*).
50
51#. If the object is in the cache, then Traffic Server checks to see if
52   the object is fresh enough to serve. If it is fresh, then Traffic
53   Server serves it to the client as a *cache hit* (see the figure
54   below).
55
56   .. figure:: /static/images/admin/cache_hit.jpg
57      :align: center
58      :alt: A cache hit
59
60      A cache hit
61
62#. If the data in the cache is stale, then Traffic Server connects to
63   the origin server and checks if the object is still fresh (a
64   :term:`revalidation`). If it is, then Traffic Server immediately sends
65   the cached copy to the client.
66
67#. If the object is not in the cache (a *cache miss*) or if the server
68   indicates the cached copy is no longer valid, then Traffic Server
69   obtains the object from the origin server. The object is then
70   simultaneously streamed to the client and the Traffic Server local
71   cache (see the figure below). Subsequent requests for the object can
72   be served faster because the object is retrieved directly from cache.
73
74   .. figure:: /static/images/admin/cache_miss.jpg
75      :align: center
76      :alt: A cache miss
77
78      A cache miss
79
80Caching is typically more complex than the preceding overview suggests.
81In particular, the overview does not discuss how Traffic Server ensures
82freshness, serves correct HTTP alternates, and treats requests for
83objects that cannot or should not be cached. The following sections discuss
84these issues in greater detail.
85
86.. _ensuring-cached-object-freshness:
87
88Ensuring Cached Object Freshness
89================================
90
91When Traffic Server receives a request for a web object, it first tries
92to locate the requested object in its cache. If the object is in cache,
93then Traffic Server checks to see if the object is fresh enough to
94serve. For HTTP objects, Traffic Server supports optional
95author-specified expiration dates. Traffic Server adheres to these
96expiration dates; otherwise, it picks an expiration date based on how
97frequently the object is changing and on administrator-chosen freshness
98guidelines. Objects can also be revalidated by checking with the origin
99server to see if an object is still fresh.
100
101HTTP Object Freshness
102---------------------
103
104Traffic Server determines whether an HTTP object in the cache is fresh
105by checking the following conditions in order:
106
107-  **Checking the** ``Expires`` **or** ``max-age`` **header**
108
109   Some HTTP objects contain ``Expires`` headers or ``max-age`` headers
110   that explicitly define how long the object can be cached. Traffic
111   Server compares the current time with the expiration time to
112   determine if the object is still fresh.
113
114-  **Checking the** ``Last-Modified`` **/** ``Date`` **header**
115
116   If an HTTP object has no ``Expires`` header or ``max-age`` header,
117   then Traffic Server can calculate a freshness limit using the
118   following formula::
119
120      freshness_limit = ( date - last_modified ) * 0.10
121
122   where *date* is the date in the object's server response header
123   and *last_modified* is the date in the ``Last-Modified`` header.
124   If there is no ``Last-Modified`` header, then Traffic Server uses the
125   date the object was written to cache. The value ``0.10`` (10 percent)
126   can be increased or reduced to better suit your needs. Refer to
127   `Modifying Aging Factor for Freshness Computations`_.
128
129   The computed freshness limit is bound by a minimum and maximum value.
130   Refer to `Setting Absolute Freshness Limits`_ for more information.
131
132-  **Checking the absolute freshness limit**
133
134   For HTTP objects that do not have ``Expires`` headers or do not have
135   both ``Last-Modified`` and ``Date`` headers, Traffic Server uses a
136   maximum and minimum freshness limit. Refer to
137   `Setting Absolute Freshness Limits`_.
138
139-  **Checking revalidate rules in** :file:`cache.config`
140
141   Revalidate rules apply freshness limits to specific HTTP objects. You
142   can set freshness limits for objects originating from particular
143   domains or IP addresses, objects with URLs that contain specified
144   regular expressions, objects requested by particular clients, and so
145   on. Refer to :file:`cache.config`.
146
147Modifying Aging Factor for Freshness Computations
148-------------------------------------------------
149
150If an object does not contain any expiration information, then Traffic
151Server can estimate its freshness from the ``Last-Modified`` and
152``Date`` headers. By default, Traffic Server stores an object for 10% of
153the time that elapsed since it last changed. You can increase or reduce
154the percentage according to your needs.
155
156To modify the aging factor for freshness computations:
157
158#. Change the value for :ts:cv:`proxy.config.http.cache.heuristic_lm_factor`.
159
160#. Run the :option:`traffic_ctl config reload` command to apply the configuration changes.
161
162Setting Absolute Freshness Limits
163---------------------------------
164
165Some objects do not have ``Expires`` headers or do not have both
166``Last-Modified`` and ``Date`` headers. To control how long these
167objects are considered fresh in the cache, specify an *absolute
168freshness limit*.
169
170To specify an absolute freshness limit:
171
172#. Edit the variables :ts:cv:`proxy.config.http.cache.heuristic_min_lifetime`
173   and :ts:cv:`proxy.config.http.cache.heuristic_max_lifetime` in
174   :file:`records.config`.
175
176#. Run the :option:`traffic_ctl config reload` command to apply the configuration changes.
177
178Specifying Header Requirements
179------------------------------
180
181To further ensure freshness of the objects in the cache, configure
182Traffic Server to cache only objects with specific headers. By default,
183Traffic Server caches all objects (including objects with no headers);
184you should change the default setting only for specialized proxy
185situations. If you configure Traffic Server to cache only HTTP objects
186with ``Expires`` or ``max-age`` headers, then the cache hit rate will be
187noticeably reduced (since very few objects will have explicit expiration
188information).
189
190To configure Traffic Server to cache objects with specific headers:
191
192#. Change the value for :ts:cv:`proxy.config.http.cache.required_headers`
193   in :file:`records.config`.
194
195#. Run the :option:`traffic_ctl config reload` command to apply the configuration changes.
196
197Cache-Control Headers
198---------------------
199
200Even though an object might be fresh in the cache, clients or servers
201often impose their own constraints that preclude retrieval of the object
202from the cache. For example, a client might request that a object not
203be retrieved from a cache, or if it does allow cache retrieval, then it
204cannot have been cached for more than 10 minutes.
205
206Traffic Server bases the servability of a cached object on ``Cache-Control``
207headers that appear in both client requests and server responses. The following
208``Cache-Control`` headers affect whether objects are served from cache:
209
210-  The ``no-cache`` header, sent by clients, tells Traffic Server that
211   it should not serve any objects directly from the cache. When present in a
212   client request, Traffic Server will always obtain the object from the
213   origin server. You can configure Traffic Server to ignore client
214   ``no-cache`` headers. Refer to `Configuring Traffic Server to Ignore Client no-cache Headers`_
215   for more information.
216
217-  The ``max-age`` header, sent by servers, is compared to the object
218   age. If the age is less than ``max-age``, then the object is fresh
219   and can be served from the Traffic Server cache.
220
221-  The ``min-fresh`` header, sent by clients, is an *acceptable
222   freshness tolerance*. This means that the client wants the object to
223   be at least this fresh. Unless a cached object remains fresh at least
224   this long in the future, it is revalidated.
225
226-  The ``max-stale`` header, sent by clients, permits Traffic Server to
227   serve stale objects provided they are not too old. Some browsers
228   might be willing to take slightly stale objects in exchange for
229   improved performance, especially during periods of poor Internet
230   availability.
231
232Traffic Server applies ``Cache-Control`` servability criteria after HTTP
233freshness criteria. For example, an object might be considered fresh but will
234not be served if its age is greater than its ``max-age``.
235
236Revalidating HTTP Objects
237-------------------------
238
239When a client requests an HTTP object that is stale in the cache,
240Traffic Server revalidates the object. A *revalidation* is a query to
241the origin server to check if the object is unchanged. The result of a
242revalidation is one of the following:
243
244-  If the object is still fresh, then Traffic Server resets its
245   freshness limit and serves the object.
246
247-  If a new copy of the object is available, then Traffic Server caches
248   the new object (thereby replacing the stale copy) and simultaneously
249   serves the object to the client.
250
251-  If the object no longer exists on the origin server, then Traffic
252   Server does not serve the cached copy.
253
254-  If the origin server does not respond to the revalidation query, then
255   Traffic Server serves the stale object along with a
256   ``111 Revalidation Failed`` warning.
257
258By default, Traffic Server revalidates a requested HTTP object in the
259cache if it considers the object to be stale. Traffic Server evaluates
260object freshness as described in `HTTP Object Freshness`_.
261You can reconfigure how Traffic Server evaluates freshness by selecting
262one of the following options:
263
264*Traffic Server considers all HTTP objects in the cache to be stale:*
265    Always revalidate HTTP objects in the cache with the origin server.
266
267*Traffic Server considers all HTTP objects in the cache to be fresh:*
268    Never revalidate HTTP objects in the cache with the origin server.
269
270*Traffic Server considers all HTTP objects without* ``Expires`` *or* ``Cache-control`` *headers to be stale:*
271    Revalidate all HTTP objects without ``Expires`` or ``Cache-Control`` headers.
272
273To configure how Traffic Server revalidates objects in the cache, you
274can set specific revalidation rules in :file:`cache.config`.
275
276To configure revalidation options
277
278#. Edit the variable :ts:cv:`proxy.config.http.cache.when_to_revalidate`
279   in :file:`records.config`.
280
281#. Run the :option:`traffic_ctl config reload` command to apply the configuration changes.
282
283.. _pushing-content-into-the-cache:
284
285Pushing Content into the Cache
286==============================
287
288Traffic Server supports the HTTP ``PUSH`` method of content delivery.
289Using HTTP ``PUSH``, you can deliver content directly into the cache
290without client requests.
291
292Configuring Traffic Server for PUSH Requests
293--------------------------------------------
294
295Before you can deliver content into your cache using HTTP ``PUSH``, you
296must configure Traffic Server to accept ``PUSH`` requests.
297
298#. Edit :file:`ip_allow.yaml` to allow ``PUSH`` from the appropriate addresses.
299
300#. Update :ts:cv:`proxy.config.http.push_method_enabled` in
301   :file:`records.config`::
302
303        CONFIG proxy.config.http.push_method_enabled INT 1
304
305#. Run the command :option:`traffic_ctl config reload` to apply the configuration changes.
306
307Understanding HTTP PUSH
308-----------------------
309
310``PUSH`` uses the HTTP 1.1 message format. The body of a ``PUSH``
311request contains the response header and response body that you want to
312place in the cache. The following is an example of a ``PUSH`` request::
313
314   PUSH http://www.company.com HTTP/1.0
315   Content-length: 84
316
317   HTTP/1.0 200 OK
318   Content-type: text/html
319   Content-length: 17
320
321   <HTML>
322   a
323   </HTML>
324
325.. important::
326
327   Your ``PUSH`` headers must include ``Content-length``, the value for which
328   must include both headers and body byte counts. The value is not optional,
329   and an improper (too large or too small) value will result in undesirable
330   behavior.
331
332Tools that will help manage pushing
333-----------------------------------
334
335Traffic Server comes with a Perl script for pushing, :program:`tspush`,
336which can assist with understanding how to write scripts for pushing
337content yourself.
338
339Pinning Content in the Cache
340============================
341
342The *Cache Pinning Option* configures Traffic Server to keep certain
343HTTP objects in the cache for a specified time. You can use this option
344to ensure that the most popular objects are in cache when needed and to
345prevent Traffic Server from deleting important objects. Traffic Server
346observes ``Cache-Control`` headers and pins an object in the cache only
347if it is indeed cacheable.
348
349To set cache pinning rules:
350
351#. Enable :ts:cv:`proxy.config.cache.permit.pinning` in :file:`records.config`::
352
353        CONFIG proxy.config.cache.permit.pinning INT 1
354
355#. Add a rule in :file:`cache.config` for each URL you want Traffic Server to
356   pin in the cache. For example::
357
358      url_regex=^https?://(www.)?apache.org/dev/ pin-in-cache=12h
359
360#. Run the command :option:`traffic_ctl config reload` to apply the configuration changes.
361
362Caching HTTP Objects
363====================
364
365When Traffic Server receives a request for a web object that is not in
366the cache, it retrieves the object from the origin server and serves it
367to the client. At the same time, Traffic Server checks if the object is
368cacheable before storing it in its cache to serve future requests.
369
370Traffic Server responds to caching directives from clients and origin
371servers, as well as directives you specify through configuration options
372and files.
373
374Client Directives
375-----------------
376
377By default, Traffic Server does not cache objects with the following
378request headers:
379
380-  ``Authorization``
381
382-  ``Cache-Control: no-store``
383
384-  ``Cache-Control: no-cache``
385
386   To configure Traffic Server to ignore this request header, refer to
387   `Configuring Traffic Server to Ignore Client no-cache Headers`_.
388
389-  ``Cookie`` (for text objects)
390
391   By default, Traffic Server caches objects served in response to
392   requests that contain cookies (even if the object is text). You can
393   configure Traffic Server to not cache cookied content of any type,
394   cache all cookied content, or cache cookied content that is of image
395   type only. For more information, refer to `Caching Cookied Objects`_.
396
397Configuring Traffic Server to Ignore Client no-cache Headers
398~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
399
400By default, Traffic Server strictly observes client
401``Cache-Control: no-cache`` directives. If a requested object contains a
402``no-cache`` header, then Traffic Server forwards the request to the
403origin server even if it has a fresh copy in cache. You can configure
404Traffic Server to ignore client ``no-cache`` directives such that it
405ignores ``no-cache`` headers from client requests and serves the object
406from its cache.
407
408#. Edit :ts:cv:`proxy.config.http.cache.ignore_client_no_cache` in
409   :file:`records.config`. ::
410
411        CONFIG proxy.config.http.cache.ignore_client_no_cache INT 1
412
413#. Run the command :option:`traffic_ctl config reload` to apply the configuration changes.
414
415Origin Server Directives
416------------------------
417
418By default, Traffic Server does not cache objects with the following response
419headers:
420
421-  ``Cache-Control: no-store``
422
423-  ``Cache-Control: private``
424
425-  ``WWW-Authenticate``
426
427   To configure Traffic Server to ignore ``WWW-Authenticate`` headers,
428   refer to `Configuring Traffic Server to Ignore WWW-Authenticate Headers`_.
429
430-  ``Set-Cookie``
431
432-  ``Cache-Control: no-cache``
433
434   To configure Traffic Server to ignore ``no-cache`` headers, refer to
435   `Configuring Traffic Server to Ignore Server no-cache Headers`_.
436
437-  ``Expires`` header with a value of 0 (zero) or a past date.
438
439Configuring Traffic Server to Ignore Server no-cache Headers
440~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
441
442By default, Traffic Server strictly observes ``Cache-Control: no-cache``
443directives. A response from an origin server with a ``no-cache`` header
444is not stored in the cache and any previous copy of the object in the
445cache is removed. If you configure Traffic Server to ignore ``no-cache``
446headers, then Traffic Server also ignores ``no-store`` headers. The
447default behavior of observing ``no-cache`` directives is appropriate
448in most cases.
449
450To configure Traffic Server to ignore server ``no-cache`` headers:
451
452#. Edit :ts:cv:`proxy.config.http.cache.ignore_server_no_cache` in
453   :file:`records.config`. ::
454
455        CONFIG proxy.config.http.cache.ignore_server_no_cache INT 1
456
457#. Run the command :option:`traffic_ctl config reload` to apply the configuration changes.
458
459Configuring Traffic Server to Ignore WWW-Authenticate Headers
460~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
461
462By default, Traffic Server does not cache objects that contain
463``WWW-Authenticate`` response headers. The ``WWW-Authenticate`` header
464contains authentication parameters the client uses when preparing the
465authentication challenge response to an origin server.
466
467When you configure Traffic Server to ignore origin server
468``WWW-Authenticate`` headers, all objects with ``WWW-Authenticate``
469headers are stored in the cache for future requests. However, the
470default behavior of not caching objects with ``WWW-Authenticate``
471headers is appropriate in most cases. Only configure Traffic Server to
472ignore server ``WWW-Authenticate`` headers if you are knowledgeable
473about HTTP 1.1.
474
475To configure Traffic Server to ignore server ``WWW-Authenticate``
476headers:
477
478#. Edit :ts:cv:`proxy.config.http.cache.ignore_authentication` in
479   :file:`records.config`. ::
480
481        CONFIG proxy.config.http.cache.ignore_authentication INT 1
482
483#. Run the command :option:`traffic_ctl config reload` to apply the configuration changes.
484
485Configuration Directives
486------------------------
487
488In addition to client and origin server directives, Traffic Server
489responds to directives you specify through configuration options and
490files.
491
492You can configure Traffic Server to do the following:
493
494-  Not cache any HTTP objects. Refer to `Disabling HTTP Object Caching`_.
495
496-  Cache *dynamic content*. That is, objects with URLs that end in ``.asp`` or
497   contain a question mark (``?``), semicolon (``;``), or ``cgi``. For more
498   information, refer to `Caching Dynamic Content`_.
499
500-  Cache objects served in response to the ``Cookie:`` header. Refer to
501   `Caching Cookied Objects`_.
502
503-  Observe ``never-cache`` rules in :file:`cache.config`.
504
505Disabling HTTP Object Caching
506~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
507
508By default, Traffic Server caches all HTTP objects except those for
509which you have set ``never-cache`` as :ref:`action rules <cache-config-format-action>`
510in :file:`cache.config`. You can disable HTTP object caching so that all HTTP
511objects are served directly from the origin server and never cached, as
512detailed below.
513
514To disable HTTP object caching manually:
515
516#. Set :ts:cv:`proxy.config.http.cache.http` to ``0`` in :file:`records.config`. ::
517
518        CONFIG proxy.config.http.cache.http INT 0
519
520#. Run the command :option:`traffic_ctl config reload` to apply the configuration changes.
521
522Caching Dynamic Content
523~~~~~~~~~~~~~~~~~~~~~~~
524
525A URL is considered dynamic if it ends in ``.asp`` or contains a
526question mark (``?``), a semicolon (``;``), or ``cgi``. By
527default, Traffic Server caches dynamic content. You can configure the
528system to ignore dynamic looking content, although this is recommended
529only if the content is truly dynamic, but fails to advertise so with
530appropriate ``Cache-Control`` headers.
531
532To configure Traffic Server's cache behaviour in regard to dynamic
533content:
534
535#. Edit :ts:cv:`proxy.config.http.cache.cache_urls_that_look_dynamic` in
536   :file:`records.config`. To disable caching, set the variable to ``0``,
537   and to explicitly permit caching use ``1``. ::
538
539        CONFIG proxy.config.http.cache.cache_urls_that_look_dynamic INT 0
540
541#. Run the command :option:`traffic_ctl config reload` to apply the configuration changes.
542
543Caching Cookied Objects
544~~~~~~~~~~~~~~~~~~~~~~~
545
546.. XXX This should be extended to xml as well!
547
548By default, Traffic Server caches objects served in response to requests
549that contain cookies. This is true for all types of objects including
550text. Traffic Server does not cache cookied text content because object
551headers are stored along with the object, and personalized cookie header
552values could be saved with the object. With non-text objects, it is
553unlikely that personalized headers are delivered or used.
554
555You can reconfigure Traffic Server to:
556
557-  Not cache cookied content of any type.
558
559-  Cache cookied content that is of image type only.
560
561-  Cache all cookied content regardless of type.
562
563To configure how Traffic Server caches cookied content:
564
565#. Edit :ts:cv:`proxy.config.http.cache.cache_responses_to_cookies` in
566   :file:`records.config`.
567
568#. Run the command :option:`traffic_ctl config reload` to apply the configuration changes.
569
570Forcing Object Caching
571======================
572
573You can force Traffic Server to cache specific URLs (including dynamic
574URLs) for a specified duration, regardless of ``Cache-Control`` response
575headers.
576
577To force document caching:
578
579#. Add a rule for each URL you want Traffic Server to pin to the cache
580   :file:`cache.config`::
581
582       url_regex=^https?://(www.)?apache.org/dev/ ttl-in-cache=6h
583
584#. Run the command :option:`traffic_ctl config reload` to apply the configuration changes.
585
586Caching HTTP Alternates
587=======================
588
589Some origin servers answer requests to the same URL with a variety of
590objects. The content of these objects can vary widely, according to
591whether a server delivers content for different languages, targets
592different browsers with different presentation styles, or provides
593different document formats (HTML, XML). Different versions of the same
594object are termed *alternates* and are cached by Traffic Server based
595on ``Vary`` response headers. You can also limit the number of
596alternate versions of an object allowed in the cache.
597
598Limiting the Number of Alternates for an Object
599-----------------------------------------------
600
601You can limit the number of alternates Traffic Server can cache per
602object (the default is 3).
603
604.. important::
605
606   Large numbers of alternates can affect Traffic Server
607   cache performance because all alternates have the same URL. Although
608   Traffic Server can look up the URL in the index very quickly, it must
609   scan sequentially through available alternates in the object store.
610
611To alter the limit on the number of alternates:
612
613#. Edit :ts:cv:`proxy.config.cache.limits.http.max_alts` in :file:`records.config`. ::
614
615    CONFIG proxy.config.cache.limits.http.max_alts INT 5
616
617#. Run the command :option:`traffic_ctl config reload` to apply the configuration changes.
618
619.. _transaction-buffering-control:
620
621Using Transaction Buffering Control
622===================================
623
624By default, I/O operations are run at full speed, as fast as either Traffic
625Server, the network, or the cache can support. This can be problematic for
626large objects if the client side connection is significantly slower. In such
627cases the content will be buffered in ram while waiting to be sent to the
628client. This could potentially also happen for ``POST`` requests if the client
629connection is fast and the origin server connection slow. If very large objects
630are being used this can cause the memory usage of Traffic Server to become
631`very large <https://issues.apache.org/jira/browse/TS-1496>`_.
632
633This problem can be ameliorated by controlling the amount of buffer space used
634by a transaction. A high water and low water mark are set in terms of bytes
635used by the transaction. If the buffer space in use exceeds the high water
636mark, the connection is throttled to prevent additional external data from
637arriving. Internal operations continue to proceed at full speed until the
638buffer space in use drops below the low water mark and external data I/O is
639re-enabled.
640
641Although this is intended primarily to limit the memory usage of Traffic Server
642it can also serve as a crude rate limiter by setting a buffer limit and then
643throttling the client side connection either externally or via a transform.
644This will cause the connection to the origin server to be limited to roughly
645the client side connection speed.
646
647Traffic Server does network I/O in large chunks (32K or so) and therefore the
648granularity of transaction buffering control is limited to a similar precision.
649
650The buffer size calculations include all elements in the transaction, including
651any buffers associated with :ref:`transform plugins <developer-plugins-http-transformations>`.
652
653Transaction buffering control can be enabled globally by using configuration
654variables or by :c:func:`TSHttpTxnConfigIntSet` in a plugin.
655
656================= ================================================== =======================================================
657Value             Variable                                           :c:func:`TSHttpTxnConfigIntSet` key
658================= ================================================== =======================================================
659Enable buffering  :ts:cv:`proxy.config.http.flow_control.enabled`    :c:member:`TS_CONFIG_HTTP_FLOW_CONTROL_ENABLED`
660Set high water    :ts:cv:`proxy.config.http.flow_control.high_water` :c:member:`TS_CONFIG_HTTP_FLOW_CONTROL_HIGH_WATER_MARK`
661Set low water     :ts:cv:`proxy.config.http.flow_control.low_water`  :c:member:`TS_CONFIG_HTTP_FLOW_CONTROL_LOW_WATER_MARK`
662================= ================================================== =======================================================
663
664Be careful to always have the low water mark equal or less than the high water
665mark. If you set only one, the other will be set to the same value.
666
667If using :c:func:`TSHttpTxnConfigIntSet`, it must be called no later than
668:c:data:`TS_HTTP_READ_RESPONSE_HDR_HOOK`.
669
670.. _admin-configuration-reducing-origin-requests:
671
672Reducing Origin Server Requests (Avoiding the Thundering Herd)
673==============================================================
674
675When an object can not be served from cache, the request will be proxied to the
676origin server. For a popular object, this can result in many near simultaneous
677requests to the origin server, potentially overwhelming it or associated
678resources. There are several features in Traffic Server that can be used to
679avoid this scenario.
680
681.. _admin-config-read-while-writer:
682
683Read While Writer
684-----------------
685
686When Traffic Server goes to fetch something from origin, and upon receiving
687the response, any number of clients can be allowed to start serving the
688partially filled cache object once background_fill_completed_threshold % of the
689object has been received.
690
691While some other HTTP proxies permit clients to begin reading the response
692immediately upon the proxy receiving data from the origin server, ATS does not
693begin allowing clients to read until after the complete HTTP response headers
694have been read and processed. This is a side-effect of ATS making no
695distinction between a cache refresh and a cold cache, which prevents knowing
696whether a response is going to be cacheable.
697
698As non-cacheable responses from an origin server are generally due to that
699content being unique to different client requests, ATS will not enable
700read-while-writer functionality until it has determined that it will be able
701to cache the object.
702
703The following settings must be made in :file:`records.config` to enable
704read-while-writer functionality in ATS::
705
706    CONFIG proxy.config.cache.enable_read_while_writer INT 1
707    CONFIG proxy.config.http.background_fill_active_timeout INT 0
708    CONFIG proxy.config.http.background_fill_completed_threshold FLOAT 0.000000
709    CONFIG proxy.config.cache.max_doc_size INT 0
710
711All four configurations are required, for the following reasons:
712
713-  :ts:cv:`proxy.config.cache.enable_read_while_writer` being set to ``1`` turns
714   the feature on, as it is off (``0``) by default.
715
716.. _background_fill:
717
718-  The background fill feature (both
719   :ts:cv:`proxy.config.http.background_fill_active_timeout` and
720   :ts:cv:`proxy.config.http.background_fill_completed_threshold`) should be
721   allowed to kick in for every possible request. This is necessary in the event
722   the writer (the first client session to request the object, which triggered
723   ATS to contact the origin server) goes away. Another client session needs to
724   take over the writer.
725
726   As such, you should set the background fill timeouts and threshold to zero;
727   this assures they never time out and are always allowed to kick in.
728
729-  The :ts:cv:`proxy.config.cache.max_doc_size` should be unlimited (set to 0),
730   since the object size may be unknown, and going over this limit would cause
731   a disconnect on the objects being served.
732
733Once these are enabled, you have something that is very close, but not quite
734the same, to Squid's Collapsed Forwarding.
735
736In addition to the above settings, the settings :ts:cv:`proxy.config.cache.read_while_writer.max_retries`
737and :ts:cv:`proxy.config.cache.read_while_writer_retry.delay` allow to control the number
738of retries TS attempts to trigger read-while-writer until the download of first fragment
739of the object is completed::
740
741    CONFIG proxy.config.cache.read_while_writer.max_retries INT 10
742
743    CONFIG proxy.config.cache.read_while_writer_retry.delay INT 50
744
745
746Open Read Retry Timeout
747-----------------------
748
749The open read retry configurations attempt to reduce the number of concurrent
750requests to the origin for a given object. While an object is being fetched
751from the origin server, subsequent requests would wait
752:ts:cv:`proxy.config.http.cache.open_read_retry_time` milliseconds before
753checking if the object can be served from cache. If the object is still being
754fetched, the subsequent requests will retry
755:ts:cv:`proxy.config.http.cache.max_open_read_retries` times. Thus, subsequent
756requests may wait a total of (``max_open_read_retries`` x ``open_read_retry_time``)
757milliseconds before establishing an origin connection of its own. For instance,
758if they are set to ``5`` and ``10`` respectively, connections will wait up to
75950ms for a response to come back from origin from a previous request, until
760this request is allowed through.
761
762.. important::
763
764    These settings are inappropriate when objects are uncacheable. In those
765    cases, requests for an object effectively become serialized. The subsequent
766    requests would await at least ``open_read_retry_time`` milliseconds before
767    being proxied to the origin.
768
769It is advisable that this setting be used in conjunction with `Read While Writer`_
770for big (those that take longer than (``max_open_read_retries`` x
771``open_read_retry_time``) milliseconds to transfer) cacheable objects. Without
772the read-while-writer settings enabled, while the initial fetch is ongoing, not
773only would subsequent requests be delayed by the maximum time, but also, those
774requests would result in unnecessary requests to the origin server.
775
776Since ATS now supports setting these settings per-request or remap rule, you
777can configure this to be suitable for your setup much more easily.
778
779The configurations are (with defaults)::
780
781    CONFIG proxy.config.http.cache.max_open_read_retries INT -1
782    CONFIG proxy.config.http.cache.open_read_retry_time INT 10
783
784The defaults are such that the feature is disabled and every connection is
785allowed to go to origin without artificial delay. When enabled, you will try
786``max_open_read_retries`` times, each with an ``open_read_retry_time`` timeout.
787
788Open Write Fail Action
789----------------------
790
791In addition to the open read retry settings TS supports a new setting
792:ts:cv:`proxy.config.http.cache.open_write_fail_action` that allows to further
793reduce multiple concurrent requests hitting the origin for the same object by
794either returning a stale copy, in case of hit-stale or an error in case of cache
795miss for all but one of the requests.
796