DEBUG.WIKI
ZgoCloud:多地区部署,按需选节点;日本、美国、香港、德国
V.PS:让项目靠近用户,亚太、欧洲、北美 KVM VPS
晚安云;香港 CN2 ¥15/月

解决 Project Reactor 报错 Exceptions.OverflowException The receiver is unable to keep up 背压溢出修复

排查 Spring WebFlux / Project Reactor 在高吞吐数据流推送时抛出 reactor.core.Exceptions$OverflowException: The receiver is unable to keep up,分析 响应式背压机制与 onBackpressure 策略。

问题现象

向反应式流持续快速写入高频消息时,消费端处理稍慢即抛出硬中断异常并导致流被强行掐断:

reactor.core.Exceptions$OverflowException: The receiver is unable to keep up
    at reactor.core.publisher.FluxPublishOn$PublishOnSubscriber.checkTerminated(FluxPublishOn.java:310)
    at reactor.core.publisher.FluxPublishOn$PublishOnSubscriber.drain(FluxPublishOn.java:380)

触发根因

  1. 生产者(Upstream)以远超下游消费者(Downstream)处理能力的速度推送数据;
  2. 默认的内部队列缓冲容量(Queues.SMALL_BUFFER_SIZE,通常仅 256)被迅速打满;
  3. 未对流显式声明背压溢出应对策略(Backpressure Strategy),触发框架的安全硬中断保护。

解决步骤

针对高并发瞬态尖刺流开启带上限与丢弃回调的缓冲池

使用 onBackpressureBuffer 吸收微突发:

flux.onBackpressureBuffer(10000, 
    droppedItem -> log.warn("Queue full, dropping item: {}", droppedItem),
    BufferOverflowStrategy.DROP_OLDEST)
    .publishOn(Schedulers.boundedElastic())
    .subscribe(this::process);

对于可容忍数据采样的流直接应用丢弃策略

配置:flux.onBackpressureDrop(item -> metrics.incrementDropped())。

优化消费端业务处理吞吐或增加并发消费分流

避免单线程串行处理慢查询。

验证确认

重新打流压测:

数据流在背压保护机制下平稳流转,高负载下有序降级吸收,再无 OverflowException 崩溃抛出。