负缓存 (在 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服务器的负载。

相关推荐
冷心笑看丽美人21 分钟前
探秘 DNS 服务器:揭开域名解析的神秘面纱
linux·运维·服务器·dns
岁月变迁呀8 小时前
Redis梳理
数据库·redis·缓存
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭9 小时前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
Code apprenticeship10 小时前
怎么利用Redis实现延时队列?
数据库·redis·缓存
ZachOn1y11 小时前
计算机网络:应用层 —— 应用层概述
计算机网络·http·https·应用层·dns
fpcc12 小时前
跟我学c++中级篇——C++中的缓存利用
c++·缓存
Ewen Seong12 小时前
mysql系列5—Innodb的缓存
数据库·mysql·缓存
安全二次方security²12 小时前
SMMU软件指南SMMU编程之虚拟机结构和缓存
缓存·cache·smmu·arm安全架构·系统mmu·虚拟机结构·vms
.生产的驴15 小时前
SpringBoot 对接第三方登录 手机号登录 手机号验证 微信小程序登录 结合Redis SaToken
java·spring boot·redis·后端·缓存·微信小程序·maven
kong790692820 小时前
电商项目-网站首页高可用(二)
缓存·二级缓存·网站首页高可用