负缓存 (在 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

相关文章

java如何实现高并发场景下三级缓存的数据一致性

《java如何实现高并发场景下三级缓存的数据一致性》这篇文章主要为大家详细介绍了java如何实现高并发场景下三级缓存的数据一致性,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 下面代码是一个使用Java和Redisson实现的三级缓存服务,主要功能包括:1.缓存结构:本地缓存:使

Apache Ignite缓存基本操作实例详解

《ApacheIgnite缓存基本操作实例详解》文章介绍了ApacheIgnite中IgniteCache的基本操作,涵盖缓存获取、动态创建、销毁、原子及条件更新、异步执行,强调线程池注意事项,避免... 目录一、获取缓存实例(Getting an Instance of a Cache)示例代码:二、动态

MySQL深分页进行性能优化的常见方法

《MySQL深分页进行性能优化的常见方法》在Web应用中,分页查询是数据库操作中的常见需求,然而,在面对大型数据集时,深分页(deeppagination)却成为了性能优化的一个挑战,在本文中,我们将... 目录引言:深分页,真的只是“翻页慢”那么简单吗?一、背景介绍二、深分页的性能问题三、业务场景分析四、

Java 方法重载Overload常见误区及注意事项

《Java方法重载Overload常见误区及注意事项》Java方法重载允许同一类中同名方法通过参数类型、数量、顺序差异实现功能扩展,提升代码灵活性,核心条件为参数列表不同,不涉及返回类型、访问修饰符... 目录Java 方法重载(Overload)详解一、方法重载的核心条件二、构成方法重载的具体情况三、不构

SQL中如何添加数据(常见方法及示例)

《SQL中如何添加数据(常见方法及示例)》SQL全称为StructuredQueryLanguage,是一种用于管理关系数据库的标准编程语言,下面给大家介绍SQL中如何添加数据,感兴趣的朋友一起看看吧... 目录在mysql中,有多种方法可以添加数据。以下是一些常见的方法及其示例。1. 使用INSERT I

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函

关于DNS域名解析服务

《关于DNS域名解析服务》:本文主要介绍关于DNS域名解析服务,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录DNS系统的作用及类型DNS使用的协议及端口号DNS系统的分布式数据结构DNS的分布式互联网解析库域名体系结构两种查询方式DNS服务器类型统计构建DNS域

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

Python中win32包的安装及常见用途介绍

《Python中win32包的安装及常见用途介绍》在Windows环境下,PythonWin32模块通常随Python安装包一起安装,:本文主要介绍Python中win32包的安装及常见用途的相关... 目录前言主要组件安装方法常见用途1. 操作Windows注册表2. 操作Windows服务3. 窗口操作

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹