ElasticSearch之Shard request cache settings
对于查询操作,Elasticsearch
提供了缓存特性来暂存结果。
对于相同条件的查询请求,在缓存中的数据失效前,响应后续的查询操作时可以直接从缓存中提取结果,有效降低检索操作的时延,提升检索数据时的体验。
提到缓存相关的特性,即要关注如下几点:
- 缓存的开关
- 缓存中的数据哪里来
- 缓存占用的空间
- 缓存中数据的老化机制
- 缓存中的数据规模
缓存的开关
创建索引时,默认启用缓存,但可以在创建参数中将参数index.requests.cache.enable
指定为false
,从而关闭缓存。
命令样例如下:
curl -X PUT "https://localhost:9200/testindex_003?pretty" -H 'Content-Type: application/json' -d' { "settings": { "index.requests.cache.enable": false } } ' --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"
执行结果的样例,如下:
{ "acknowledged" : true, "shards_acknowledged" : true, "index" : "testindex_003" }
检查索引testindex_003
的参数,命令样例如下:
curl -X GET "https://localhost:9200/testindex_003/_settings?pretty" --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"
执行结果的样例,如下:
{ "testindex_003" : { "settings" : { "index" : { "routing" : { "allocation" : { "include" : { "_tier_preference" : "data_content" } } }, "number_of_shards" : "1", "provided_name" : "testindex_003", "creation_date" : "1702050678193", "requests" : { "cache" : { "enable" : "false" } }, "number_of_replicas" : "1", "uuid" : "9r8VrKdURhqr1XJM8Z9egQ", "version" : { "created" : "8500003" } } } } }
在索引创建完毕之后,可以通过Update index settings API修改开关的状态。
例如刚才在创建索引testindex_003
时关闭了缓存,可以通过执行命令手工开启缓存。
命令样例如下:
curl -X PUT "https://localhost:9200/testindex_003/_settings?pretty" -H 'Content-Type: application/json' -d' { "index.requests.cache.enable": true } ' --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"
执行结果的样例,如下:
{ "acknowledged" : true }
检查索引testindex_003
的参数,命令样例如下:
curl -X GET "https://localhost:9200/testindex_003/_settings?pretty" --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"
执行结果的样例,如下:
{ "testindex_003" : { "settings" : { "index" : { "routing" : { "allocation" : { "include" : { "_tier_preference" : "data_content" } } }, "number_of_shards" : "1", "provided_name" : "testindex_003", "creation_date" : "1702050678193", "requests" : { "cache" : { "enable" : "true" } }, "number_of_replicas" : "1", "uuid" : "9r8VrKdURhqr1XJM8Z9egQ", "version" : { "created" : "8500003" } } } } }
执行检索操作时,依据业务要求指定缓存的开关,比如禁止从缓存中提取数据,命令样例如下:
curl -X GET "https://localhost:9200/testindex_003/_search?request_cache=true&pretty" -H 'Content-Type: application/json' -d' { "size": 0, "aggs": { "popular_colors": { "terms": { "field": "colors" } } } } ' --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"
缓存中的数据
假定启用了缓存特性,Elasticsearch
节点处理检索请求时,执行如下操作:
- 处理查询请求时,则使用查询条件作为关键字,从缓存中提取数据。
- 假如可以从缓存中提取到相关记录,则返回这部分记录,本次处理完毕。
- 假如没有从缓存中提取到相关记录,则执行检索操作。
- 检索操作完毕,使用查询条件作为关键字,将处理结果缓存至缓存中。
- 假如缓存空间不足,则使用
LRU
算法清理缓存中的记录,直至可用空间足够保存刚才的检索结果。 - 返回检索结果。
上述为检索操作的一般流程,实际的执行流程以Elasticsearch
的实现为准。
缓存占用的空间
缓存可占用的空间,默认值为Elasticsearch
进程Java堆空间的%1
。
在节点的配置文件$ES_HOME/config/elasticsearch.yml
中指定,配置样例如下:
indices.requests.cache.size: 2%
配置项indices.requests.cache.expire
用于指定缓存中数据的生命周期。
考虑到Elasticsearch
后台定期执行的Refresh
操作会自动清理缓存中的过期数据,因此一般不需要配置indices.requests.cache.expire
。
清理缓存
手工清理缓存,命令样例如下:
curl -X POST "https://localhost:9200/testindex_001/_cache/clear?request=true&pretty" --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"
执行结果的样例,如下:
{ "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 } }
缓存的使用情况
可以使用如下API来获取使用情况。
Index stats API
基于索引的粒度统计缓存的使用情况。
命令样例,如下:
curl -X GET "https://localhost:9200/_stats/request_cache?human&pretty" --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"
执行结果的样例,如下:
{ "_shards" : { "total" : 13, "successful" : 5, "failed" : 0 }, "_all" : { "primaries" : { "request_cache" : { "memory_size" : "0b", "memory_size_in_bytes" : 0, "evictions" : 0, "hit_count" : 0, "miss_count" : 0 } }, "total" : { "request_cache" : { "memory_size" : "0b", "memory_size_in_bytes" : 0, "evictions" : 0, "hit_count" : 0, "miss_count" : 0 } } }, "indices" : { "cloned-testindex_002" : { "uuid" : "Lrz1DOsWRY6OTt0Veawo-w", "health" : "yellow", "status" : "open", "primaries" : { "request_cache" : { "memory_size" : "0b", "memory_size_in_bytes" : 0, "evictions" : 0, "hit_count" : 0, "miss_count" : 0 } }, "total" : { "request_cache" : { "memory_size" : "0b", "memory_size_in_bytes" : 0, "evictions" : 0, "hit_count" : 0, "miss_count" : 0 } } }, "testindex_002" : { "uuid" : "k6twq9y9Qtmcs2AHK-USEQ", "health" : "yellow", "status" : "open", "primaries" : { "request_cache" : { "memory_size" : "0b", "memory_size_in_bytes" : 0, "evictions" : 0, "hit_count" : 0, "miss_count" : 0 } }, "total" : { "request_cache" : { "memory_size" : "0b", "memory_size_in_bytes" : 0, "evictions" : 0, "hit_count" : 0, "miss_count" : 0 } } }, "testindex_001" : { "uuid" : "7iGJRFfxRd2jD3qP-KDRmQ", "health" : "yellow", "status" : "open", "primaries" : { "request_cache" : { "memory_size" : "0b", "memory_size_in_bytes" : 0, "evictions" : 0, "hit_count" : 0, "miss_count" : 0 } }, "total" : { "request_cache" : { "memory_size" : "0b", "memory_size_in_bytes" : 0, "evictions" : 0, "hit_count" : 0, "miss_count" : 0 } } } } }
Nodes stats API
基于集群中节点的粒度统计缓存的使用情况。
命令样例,如下:
curl -X GET "https://localhost:9200/_nodes/stats/indices/request_cache?human&pretty" --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"
执行结果的样例,如下:
{ "_nodes" : { "total" : 1, "successful" : 1, "failed" : 0 }, "cluster_name" : "elasticsearch", "nodes" : { "aKgBu7LgS9a6iPYH8n2JPw" : { "timestamp" : 1702049373653, "name" : "jackie-ubuntu", "transport_address" : "127.0.0.1:9300", "host" : "127.0.0.1", "ip" : "127.0.0.1:9300", "roles" : [ "data", "data_cold", "data_content", "data_frozen", "data_hot", "data_warm", "ingest", "master", "ml", "remote_cluster_client", "transform" ], "attributes" : { "transform.config_version" : "10.0.0", "ml.machine_memory" : "4040318976", "ml.allocated_processors" : "4", "ml.allocated_processors_double" : "4.0", "ml.max_jvm_size" : "2021654528", "ml.config_version" : "11.0.0", "xpack.installed" : "true" }, "indices" : { "request_cache" : { "memory_size" : "0b", "memory_size_in_bytes" : 0, "evictions" : 0, "hit_count" : 0, "miss_count" : 0 } } } } }
相关资料
本文来自博客园,作者:jackieathome,转载请注明原文链接:https://www.cnblogs.com/jackieathome/p/17889364.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南