diff --git a/src/v/cluster/producer_state.cc b/src/v/cluster/producer_state.cc index acc883c3dc2b3..5f3b7364e01b8 100644 --- a/src/v/cluster/producer_state.cc +++ b/src/v/cluster/producer_state.cc @@ -187,6 +187,17 @@ result requests::try_emplace( // All invariants satisfied, enqueue the request. _inflight_requests.emplace_back( ss::make_lw_shared(first, last, current, result_promise_t{})); + // if there are more than max cached requests inflight start dropping the + // oldest of them + while (_inflight_requests.size() > requests_cached_max + && _inflight_requests.front()->has_completed()) { + _inflight_requests.pop_front(); + // clear finished requests as the producer will not be interested in + // them anymore + if (!_finished_requests.empty()) { + _finished_requests.clear(); + } + } return _inflight_requests.back(); }