github.com/outbrain/consul@v1.4.5/website/source/docs/guides/dns-cache.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "DNS Caching"
     4  sidebar_current: "docs-guides-dns-cache"
     5  description: |-
     6    One of the main interfaces to Consul is DNS. Using DNS is a simple way to integrate Consul into an existing infrastructure without any high-touch integration.
     7  ---
     8  
     9  # DNS Caching
    10  
    11  One of the main interfaces to Consul is DNS. Using DNS is a simple way to
    12  integrate Consul into an existing infrastructure without any high-touch
    13  integration.
    14  
    15  By default, Consul serves all DNS results with a 0 TTL value. This prevents
    16  any caching. The advantage is that each DNS lookup is always re-evaluated,
    17  so the most timely information is served. However, this adds a latency hit
    18  for each lookup and can potentially exhaust the query throughput of a cluster.
    19  For this reason, Consul provides a number of tuning parameters that can
    20  customize how DNS queries are handled.
    21  
    22  In this guide, we will review important parameters for tuning
    23  stale reads, negative response caching, and TTL. All of the DNS config
    24  parameters must be set in set in the agent's configuration file.
    25  
    26  <a name="stale"></a>
    27  ## Stale Reads
    28  
    29  Stale reads can be used to reduce latency and increase the throughput
    30  of DNS queries. The [settings](/docs/agent/options.html) used to control stale reads
    31  are:
    32  
    33  * [`dns_config.allow_stale`](/docs/agent/options.html#allow_stale) must be
    34  set to true to enable stale reads.
    35  * [`dns_config.max_stale`](/docs/agent/options.html#max_stale) limits how stale results
    36  are allowed to be when querying DNS.
    37  
    38  With these two settings you can allow or prevent stale reads. Below we will discuss
    39  the advanatages and disadvatages of both.
    40  
    41  ### Allow Stale Reads
    42  
    43  Since Consul 0.7.1, `allow_stale` is enabled by default and uses a `max_stale`
    44  value that defaults to a near-indefinite threshold (10 years).
    45  This allows DNS queries to continue to be served in the event
    46  of a long outage with no leader. A new telemetry counter has also been added at
    47  `consul.dns.stale_queries` to track when agents serve DNS queries that are stale
    48  by more than 5 seconds.
    49  
    50  ```javascript
    51  "dns_config" {
    52    "allow_stale" = true
    53    "max_stale" = "87600h"
    54  }
    55  ```
    56  
    57  ~> NOTE: The above example is the default setting. You do not need to set it explicitly.
    58  
    59  Doing a stale read allows any Consul server to
    60  service a query, but non-leader nodes may return data that is
    61  out-of-date. By allowing data to be slightly stale, we get horizontal
    62  read scalability. Now any Consul server can service the request, so we
    63  increase throughput by the number of servers in a cluster.
    64  
    65  ### Prevent Stale Reads
    66  
    67  If you want to prevent stale reads or limit how stale they can be, you can set `allow_stale`
    68  to false or use a lower value for `max_stale`. Doing the first will ensure that
    69  all reads are serviced by a [single leader node](/docs/internals/consensus.html).
    70  The reads will then be strongly consistent but will be limited by the throughput
    71  of a single node.
    72  
    73  ```javascript
    74  "dns_config" {
    75    "allow_stale" = false
    76  }
    77  ```
    78  
    79  ## Negative Response Caching
    80  
    81  Some DNS clients cache negative responses - that is, Consul returning a "not
    82  found" style response because a service exists but there are no healthy
    83  endpoints. In practice, this could mean that the cached negative responses may
    84  cause that service to appear "down" for longer than they are actually unavailable
    85  when using DNS for service discovery.
    86  
    87  ### Configure SOA
    88  
    89  In Consul 1.3.0 and newer, it is now possible to tune SOA
    90  responses and modify the negative TTL cache for some resolvers. It can
    91  be achieved using the [`soa.min_ttl`](/docs/agent/options.html#soa_min_ttl)
    92  configuration within the [`soa`](/docs/agent/options.html#soa) configuration.
    93  
    94  ```javascript
    95  "dns_config" {
    96    "soa" {
    97     "min_ttl" = "60s"
    98    }
    99  }
   100  ```
   101  
   102  One common example is that Windows will default to caching negative responses
   103  for 15 minutes. DNS forwarders may also cache negative responses, with the same
   104  effect. To avoid this problem, check the negative response cache defaults for
   105  your client operating system and any DNS forwarder on the path between the
   106  client and Consul and set the cache values appropriately. In many cases
   107  "appropriately" simply is turning negative response caching off to get the best
   108  recovery time when a service becomes available again.
   109  
   110  <a name="ttl"></a>
   111  ## TTL Values
   112  
   113  TTL values can be set to allow DNS results to be cached downstream of Consul. Higher
   114  TTL values reduce the number of lookups on the Consul servers and speed lookups for
   115  clients, at the cost of increasingly stale results. By default, all TTLs are zero,
   116  preventing any caching.
   117  
   118  ```javascript
   119  {
   120    "dns_config": {
   121      "service_ttl" = "0s"
   122      "node_ttl" = "0s"
   123    }
   124  }
   125  ```
   126  
   127  ### Enable Caching
   128  
   129  To enable caching of node lookups (e.g. "foo.node.consul"), we can set the
   130  [`dns_config.node_ttl`](/docs/agent/options.html#node_ttl) value. This can be set to
   131  "10s" for example, and all node lookups will serve results with a 10 second TTL.
   132  
   133  Service TTLs can be specified in a more granular fashion. You can set TTLs
   134  per-service, with a wildcard TTL as the default. This is specified using the
   135  [`dns_config.service_ttl`](/docs/agent/options.html#service_ttl) map. The "*"
   136  is supported at the end of any prefix and a lower precedence than strict match,
   137  so 'my-service-x' has precedence over 'my-service-*', when performing wildcard
   138  match, the longest path is taken into account, thus 'my-service-*' TTL will
   139  be used instead of 'my-*' or '*'. With the same rule, '*' is the default value
   140  when nothing else matches. If no match is found the TTL defaults to 0.
   141  
   142  For example, a [`dns_config`](/docs/agent/options.html#dns_config) that provides
   143  a wildcard TTL and a specific TTL for a service might look like this:
   144  
   145  ```javascript
   146  {
   147    "dns_config": {
   148      "service_ttl": {
   149        "*": "5s",
   150        "web": "30s",
   151        "db*": "10s",
   152        "db-master": "3s"
   153      }
   154    }
   155  }
   156  ```
   157  
   158  This sets all lookups to "web.service.consul" to use a 30 second TTL
   159  while lookups to "api.service.consul" will use the 5 second TTL from the wildcard.
   160  All lookups matching "db*" would get a 10 seconds TTL except "db-master"
   161  that would have a 3 seconds TTL.
   162  
   163  ### Prepared Queries
   164  
   165  [Prepared Queries](/api/query.html) provide an additional
   166  level of control over TTL. They allow for the TTL to be defined along with
   167  the query, and they can be changed on the fly by updating the query definition.
   168  If a TTL is not configured for a prepared query, then it will fall back to the
   169  service-specific configuration defined in the Consul agent as described above,
   170  and ultimately to 0 if no TTL is configured for the service in the Consul agent.
   171  
   172  ## Summary
   173  
   174  In this guide we covered several of the parameters for tuning DNS queries. We reviewed
   175  how to enable or disable stale reads and how to configure the amount of time when stale
   176  reads are allowed. We also looked at the minimum TTL configuration options
   177  for negative responses from services. Finally, we reviewed how to setup TTLs
   178  for service lookups.