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

2024-01-03 09:28
文章标签 常见 缓存 较为 dns

本文主要是介绍负缓存 (在 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.confcache 30errors
}

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

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

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

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

这篇关于负缓存 (在 DNS 中较为常见)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/565229

相关文章

Linux云服务器手动配置DNS的方法步骤

《Linux云服务器手动配置DNS的方法步骤》在Linux云服务器上手动配置DNS(域名系统)是确保服务器能够正常解析域名的重要步骤,以下是详细的配置方法,包括系统文件的修改和常见问题的解决方案,需要... 目录1. 为什么需要手动配置 DNS?2. 手动配置 DNS 的方法方法 1:修改 /etc/res

MyBatis延迟加载与多级缓存全解析

《MyBatis延迟加载与多级缓存全解析》文章介绍MyBatis的延迟加载与多级缓存机制,延迟加载按需加载关联数据提升性能,一级缓存会话级默认开启,二级缓存工厂级支持跨会话共享,增删改操作会清空对应缓... 目录MyBATis延迟加载策略一对多示例一对多示例MyBatis框架的缓存一级缓存二级缓存MyBat

前端缓存策略的自解方案全解析

《前端缓存策略的自解方案全解析》缓存从来都是前端的一个痛点,很多前端搞不清楚缓存到底是何物,:本文主要介绍前端缓存的自解方案,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、为什么“清缓存”成了技术圈的梗二、先给缓存“把个脉”:浏览器到底缓存了谁?三、设计思路:把“发版”做成“自愈”四、代码

Java 缓存框架 Caffeine 应用场景解析

《Java缓存框架Caffeine应用场景解析》文章介绍Caffeine作为高性能Java本地缓存框架,基于W-TinyLFU算法,支持异步加载、灵活过期策略、内存安全机制及统计监控,重点解析其... 目录一、Caffeine 简介1. 框架概述1.1 Caffeine的核心优势二、Caffeine 基础2

Redis高性能Key-Value存储与缓存利器常见解决方案

《Redis高性能Key-Value存储与缓存利器常见解决方案》Redis是高性能内存Key-Value存储系统,支持丰富数据类型与持久化方案(RDB/AOF),本文给大家介绍Redis高性能Key-... 目录Redis:高性能Key-Value存储与缓存利器什么是Redis?为什么选择Redis?Red

React 记忆缓存的三种方法实现

《React记忆缓存的三种方法实现》本文主要介绍了React记忆缓存的三种方法实现,包含React.memo、useMemo、useCallback,用于避免不必要的组件重渲染和计算,感兴趣的可以... 目录1. React.memo2. useMemo3. useCallback使用场景与注意事项在 Re

Docker多阶段镜像构建与缓存利用性能优化实践指南

《Docker多阶段镜像构建与缓存利用性能优化实践指南》这篇文章将从原理层面深入解析Docker多阶段构建与缓存机制,结合实际项目示例,说明如何有效利用构建缓存,组织镜像层次,最大化提升构建速度并减少... 目录一、技术背景与应用场景二、核心原理深入分析三、关键 dockerfile 解读3.1 Docke

深度解析Java @Serial 注解及常见错误案例

《深度解析Java@Serial注解及常见错误案例》Java14引入@Serial注解,用于编译时校验序列化成员,替代传统方式解决运行时错误,适用于Serializable类的方法/字段,需注意签... 目录Java @Serial 注解深度解析1. 注解本质2. 核心作用(1) 主要用途(2) 适用位置3

使用Spring Cache本地缓存示例代码

《使用SpringCache本地缓存示例代码》缓存是提高应用程序性能的重要手段,通过将频繁访问的数据存储在内存中,可以减少数据库访问次数,从而加速数据读取,:本文主要介绍使用SpringCac... 目录一、Spring Cache简介核心特点:二、基础配置1. 添加依赖2. 启用缓存3. 缓存配置方案方案

Java实现本地缓存的四种方法实现与对比

《Java实现本地缓存的四种方法实现与对比》本地缓存的优点就是速度非常快,没有网络消耗,本地缓存比如caffine,guavacache这些都是比较常用的,下面我们来看看这四种缓存的具体实现吧... 目录1、HashMap2、Guava Cache3、Caffeine4、Encache本地缓存比如 caff