DEBUG.WIKI
PanstarCloud:部署应用,接入 AI,云服务器与 AI 网关
晚安云;香港 CN2 ¥15/月
V.PS:让项目靠近用户,亚太、欧洲、北美 KVM VPS

解决 Elasticsearch 报错 429 es_rejected_execution 指南

解决高并发写入时 Elasticsearch 返回 429 es_rejected_execution_exception 的队列饱和根因,提供 bulk 批量调优与线程池配置方案。

问题现象

客户端批量写入或索引文档时,ES 返回 HTTP 429 错误,并在响应体中提示:

{
  "error": {
    "root_cause": [
      {
        "type": "es_rejected_execution_exception",
        "reason": "rejected execution of coordinating operation [coordinating_and_primary_bytes=2147483648, max=2147483648] on EsThreadPoolExecutor[name = node-1/write, queue capacity = 200]"
      }
    ]
  },
  "status": 429
}

触发根因

  1. 写入线程池(write thread pool)的工作队列已达到最大容量(默认 queue_size=200 或 1000),新到达的写入请求直接被拒绝。
  2. 客户端批量并发度过高,且单批次数据过大,造成底层 Lucene 刷新(Refresh)和合并(Merge)产生 I/O 阻塞。

解决步骤与操作命令

步骤一:监控当前线程池队列积压情况

通过 API 查看当前节点的线程池状态:

curl -s "http://localhost:9200/_cat/thread_pool/write?v&h=node_name,name,active,rejected,completed,queue"

查看 rejected 计数是否处于增长状态。

步骤二:调整客户端批量并发与指数退避重试

  1. 减小单次 Bulk 批次的大小(建议单批 5MB ~ 15MB 左右)。
  2. 在客户端代码中实现指数退避重试(Exponential Backoff):
# 遇到 429 异常时随机退避重试示例
import time, random
for attempt in range(5):
    try:
        response = es.bulk(body=bulk_data)
        break
    except Exception as e:
        if "429" in str(e):
            time.sleep((2 ** attempt) + random.uniform(0, 1))

步骤三:调优索引刷新间隔与临时写入性能

大批量数据导入期间,将刷新间隔设为 -1 或调大至 30s:

curl -X PUT "http://localhost:9200/my_index/_settings" -H 'Content-Type: application/json' -d'
{
  "index" : {
    "refresh_interval" : "30s"
  }
}
'

验证确认

再次观察 _cat/thread_pool/write:

curl -s "http://localhost:9200/_cat/thread_pool/write?v"

队列积压归零,rejected 停止增长,429 报错完全消除。