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

相关推荐
Oak Zhang4 小时前
sharding-jdbc自定义分片算法,表对应关系存储在mysql中,缓存到redis或者本地
redis·mysql·缓存
门牙咬脆骨5 小时前
【Redis】redis缓存击穿,缓存雪崩,缓存穿透
数据库·redis·缓存
门牙咬脆骨5 小时前
【Redis】GEO数据结构
数据库·redis·缓存
Dlwyz10 小时前
问题: redis-高并发场景下如何保证缓存数据与数据库的最终一致性
数据库·redis·缓存
吴半杯12 小时前
Redis-monitor安装与配置
数据库·redis·缓存
ö Constancy13 小时前
设计LRU缓存
c++·算法·缓存
小王码农记13 小时前
vue中路由缓存
前端·vue.js·缓存·typescript·anti-design-vue
会code的厨子13 小时前
Redis缓存高可用集群
redis·缓存
Karoku06619 小时前
【企业级分布式系统】ELK-企业级日志分析系统
运维·数据库·redis·mysql·elk·缓存
PGCCC21 小时前
【PGCCC】Postgresql 缓存替换算法
数据库·缓存·postgresql