Spring WebFlux报Could not emit tick 257 due to lack of requests与OverflowException的排查方法(onBackpressureDrop、limitRate与flatMap并发数定位)

Spring WebFlux里用Flux.interval定时跑批,跑一阵日志开始刷reactor.core.Exceptions$ErrorCallbackNotImplemented: reactor.core.Exceptions$OverflowException: Could not emit tick 257 due to lack of requests (interval doesn't support small downstream requests that replenish slower than the ticks),定时任务之后就没动静了。这类报错碰到过几次,第一反应容易往内存或线程池上想,实际是背压没处理,上游按点发、下游消费不过来,Reactor直接把流打断。subscribe()没写onError时,异常包一层ErrorCallbackNotImplemented抛出来,堆栈里那句才是根因。

问题不在堆内存,也不在线程数。

Flux.interval是热数据源,按固定节拍往外发long,不看下游request(n)还剩多少。批里的doSomething()一旦比interval周期慢,任务开始互相叠加,flatMap默认的并发上限是Queues.SMALL_BUFFER_SIZE给的256,队列填满之后下游补请求的速度追不上tick,某个tick发不出去就由Exceptions.failWithOverflow抛OverflowException。实测把interval从1000毫秒调到10毫秒,报错里的tick数字跟着变小,位置还是Exceptions.java:215附近那行。当初没细究过256这个数,后来翻源码才确认flatMap的prefetch默认值和它拴在一起。

1、先定位是哪一层在拖。在链上加.log()或者doOnRequest(n -> System.out.println("req:" + n)),看request(n)每次补多少;上游是interval的话,补得慢就注定会溢出。测的时候用StepVerifier最省事,create一个Flux.interval,verifyThenAssertThat或者thenRequest只给一小批,再thenAwait等足够时间,Could not emit tick 32 due to lack of requests这种早期溢出能稳定复现,不用真等生产跑几小时。Hooks.onOperatorDebug()配合.checkpoint("batch")能把这个操作符标到堆栈里。

Flux.interval(Duration.ofMillis(1000))
    .flatMap(ignore -> doSomething())
    .subscribe();  // 跑一阵报 Could not emit tick 257 due to lack of requests

StepVerifier.create(Flux.interval(Duration.ofMillis(1)))
    .thenRequest(32)
    .thenAwait(Duration.ofSeconds(1))
    .expectError(OverflowException.class)
    .verify();

2、允许丢批就挂onBackpressureDrop,溢出的tick直接丢掉,日志里doOnNext的序号会从0、1、2一路跳到2399、2400,中间全被丢了,但流不会断。这一步要放在flatMap前面才拦得住interval发出来的信号,放后面等于没加。丢的时候配个doOnDiscard记一下计数,别静默丢完连丢了多少都不知道。

Flux.interval(Duration.ofMillis(1000))
    .onBackpressureDrop(tick -> log.info("drop tick {}", tick))
    .flatMap(ignore -> doSomething())
    .subscribe();

3、批不该重叠的话,把热数据源换成冷数据源更干净。Mono.just(1).repeat().delayElements(Duration.ofMillis(1000)).concatMap(ignore -> doSomething())是上一批干完才拉下一批,天然没有溢出这回事,代价是节拍不再严格按秒,间隔变成"任务耗时 + 1秒"。现场接过一个报表任务就是这么改的,之前用interval叠了三层flatMap,改完那行OverflowException再没出现过。onBackpressureLatest()只留最新一条也走这个思路。

4、中间态可以用onBackpressureBuffer给有界缓冲,onBackpressureBuffer(1024, d -> log.warn("overflow"), BufferOverflowStrategy.DROP_OLDEST)把容量和溢出策略都写死,别用无界缓冲,collectList().block()那种一把全攒进堆的写法迟早OOM。下游拉数据太碎的话加limitRate(256)把request拆成一批批发,或者publishOn(Schedulers.boundedElastic(), 32)把prefetch调小;阻塞式的JDBC调用挪到subscribeOn(Schedulers.boundedElastic()),别压在Netty的event loop上。用Sinks.Many手动发信号时,tryEmitNext返回FAIL_OVERFLOW就是同一种溢出,得单独处理返回值,不能当成功。

Mono.just(1).repeat()
    .delayElements(Duration.ofMillis(1000))
    .concatMap(ignore -> doSomething())
    .onBackpressureBuffer(1024, d -> log.warn("overflow"), BufferOverflowStrategy.DROP_OLDEST)
    .subscribe();

5、把flatMap并发调到Integer.MAX_VALUE那种写法要慎用,它只是把溢出往后推,任务重叠没解决,压测时间一长照样报。真正要看的是doSomething()耗时和interval周期的关系,耗时接近甚至超过周期,就得改成concatMap串行或者把批拆小。

这些背压操作符在Reactor Core 3.4.0、3.5.0、3.6.0,以及Spring Boot 3.0.0、3.1.0、3.2.0、3.3.0、3.4.0,行为一致,Spring Boot 3.0.0是2022年11月发的、基线抬到Java 17,Reactor 3.6.0是2023年11月发的,版本升级没动failWithOverflow这套逻辑。Java 9把响应式流的接口收进java.util.concurrent.Flow,是另一条线,别和Reactor的OverflowException混起来查。

把onBackpressureDrop加在interval后面、再拿limitRate把每批request压小,日志里那行OverflowException就停了。数据截至2026年11月。