Search code examples
springspring-webfluxproject-reactor

WebFlux Flux.cache() how long cached emitted data is stored


Context: I have a finite Flux which is source of Rest API Call, and this flux is in scope of main REST API call made to my Spring Boot Application which returns Mono with response. I would like to reuse response of this Rest API for two subscriptions. I've found that Flux cache() is solving this task, but the question is:

How long the data emitted by first subscription will be stored in memory if there are no ttl duration specified? Should we clear somehow this cache to avoid memory leaks, or it will live only in scope of specific outer main HTTP Request(Subscription to Mono which will trigger inner Flux subscription) and will be cleared after termination of Flux?

Didn't find in documentation any details about this: https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#cache--


Solution

  • As @Igor Atamanov mentioned, emitted data which was cached without specified ttl will be stored in memory until last subscriber which will subscribe and reuse this flux won't complete his subscription.