负缓存 (在 DNS 中较为常见)

摘要

负缓存,也被称为负值缓存或负面缓存,指的是在域名系统(DNS)中记录和重用之前失败查询结果的机制。当DNS服务器无法解析一个域名时,它会返回一个特定错误码(例如NXDOMAIN),指示该域名不存在或无法解析。负缓存会将该错误码与请求的域名相关联,并在一段时间内记住此结果。

设计原理:

  1. 健康检查:负缓存机制会首先检查之前失败的查询结果是否仍然有效。它会向该域名的DNS服务器发送一个健康检查请求,以确认该域名是否解析成功。如果成功解析,则该域名将从负缓存中移除,并将新的解析结果返回给客户端。如果仍然无法解析,则继续保留错误码,并继续使用负缓存。
  2. 缓存时间:负缓存的设计原理是在一定时间内重用错误码。这个时间段(通常称为“负缓存时间”或“负TTL”)由DNS服务器设置,并告知客户端。在此期间内,如果再次请求相同的域名,DNS服务器会直接返回负缓存中的错误码,而无需进行新的查询。这样可以减少服务器负载,并提高响应速度。
  3. 动态更新:负缓存具有动态更新的能力。一旦过了负缓存时间,DNS服务器会再次尝试解析该域名。如果解析成功,则负缓存将被删除,并返回新的解析结果给客户端。如果仍然无法解析,则错误码会继续保留,并继续使用负缓存。

负缓存是一种有效的机制,可以在一定时间内避免重复查询无法解析的域名。它可以提高DNS服务器性能和响应速度,并减少对上游DNS服务器的负载。然而,负缓存机制也可能导致过期的错误码被重用,导致客户端无法及时获得最新的解析结果。因此,在设置负缓存时间时应权衡好性能和实时性的要求。

Simply put

Negative caching, which is commonly found in DNS, refers to the mechanism of recording and reusing previously failed query results for a certain period of time, typically several seconds. The concept and design principles of negative caching can be explained as follows:

Concept: Negative caching is a mechanism used by DNS servers to improve performance and reduce the load on upstream servers when resolving domain names that have previously failed to be resolved. When a DNS server encounters a domain name that cannot be resolved, it stores the error code associated with the requested domain and remembers it for a certain amount of time. During this time, if the same domain name is queried again, the DNS server returns the stored error code from the negative cache instead of performing a new query.

Design Principles:

  1. Health Checking: The negative caching mechanism first checks if the previously failed query result is still valid. It sends a health check request to the DNS server of the domain name to confirm if the domain name can be resolved successfully. If the resolution is successful, the domain name is removed from the negative cache, and the new resolution result is returned to the client. If the resolution still fails, the error code is retained, and the negative caching continues.
  2. Cache Time: The negative caching mechanism has a designated time period, often referred to as “negative TTL”, during which the stored error code is reused. The DNS server sets this time period and informs the clients. Within this timeframe, if the same domain name is requested again, the DNS server directly returns the error code from the negative cache without performing a new query. This reduces server load and improves response speed.
  3. Dynamic Update: Negative caching has the capability of dynamic updates. Once the negative TTL has expired, the DNS server attempts to resolve the domain name again. If the resolution is successful, the negative cache is cleared, and the new resolution result is returned to the client. If the resolution still fails, the error code is retained, and the negative caching continues.

Negative caching is an effective mechanism that helps avoid repeated queries for domain names that have previously failed to resolve within a certain time period. It improves DNS server performance, reduces the load on upstream servers, and enhances response speed. However, negative caching may cause expired error codes to be reused, resulting in clients not receiving the latest resolution results in a timely manner. Hence, it is important to strike a balance between performance and real-time requirements when setting the negative cache time.

On K8s

在Kubernetes(K8s)中,DNS服务通常由CoreDNS或kube-dns提供。这些服务可以配置负缓存来记录先前的查询结果,以便在一定的时间内重用。下面是一个示例:

假设我们有一个Kubernetes集群,其中有两个命名空间:defaultservices

我们可以在CoreDNS的配置文件中添加负缓存配置。对于CoreDNS,配置文件通常位于/etc/coredns/Corefile

. {
    forward . /etc/resolv.conf
    cache 30
    errors
}

在上述配置中,我们添加了cache 30行来配置一个缓存时间为30秒的负缓存。这意味着如果一个DNS查询失败,查询结果将被记录并在接下来的30秒内重用。

接下来,假设有一个Pod在default命名空间中,试图解析服务名为my-service.services的DNS记录。

如果首次解析失败,由于我们配置了负缓存,CoreDNS将记录该失败的结果,并将其缓存。接下来的30秒内,如果有其他Pod尝试解析相同的服务名,CoreDNS将直接返回先前记录的失败结果,而不会再次发送查询请求。

这种配置可以减少对外部DNS服务器的负载,并提高解析速度。然而,需要注意的是,负缓存配置的时间要根据你的具体需求进行调整。在某些情况下,缓存时间过长可能导致不及时的更新,而缓存时间过短可能会增加对外部DNS服务器的负载。

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

P("Struggler") ?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值