You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by lh...@apache.org on 2025/08/28 09:50:39 UTC

(pulsar) branch master updated: [improve][broker] Implement PIP-430 Pulsar Broker cache improvements (#24623)

This is an automated email from the ASF dual-hosted git repository.

lhotari pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new 490ba0cca18 [improve][broker] Implement PIP-430 Pulsar Broker cache improvements (#24623)
490ba0cca18 is described below

commit 490ba0cca18f713091e6fce3d3d6390386c1f3cf
Author: Lari Hotari <lh...@users.noreply.github.com>
AuthorDate: Thu Aug 28 12:50:33 2025 +0300

    [improve][broker] Implement PIP-430 Pulsar Broker cache improvements (#24623)
---
 .../org/apache/pulsar/tests/TestRetrySupport.java  |   8 +-
 buildtools/src/main/resources/log4j2.xml           |  54 +-
 conf/broker.conf                                   |  57 +-
 conf/standalone.conf                               |  77 +-
 distribution/server/src/assemble/LICENSE.bin.txt   |   2 +-
 managed-ledger/pom.xml                             |   4 +
 .../java/org/apache/bookkeeper/mledger/Entry.java  |  23 +
 ...acheManager.java => EntryReadCountHandler.java} |  31 +-
 .../bookkeeper/mledger/ManagedLedgerConfig.java    |   5 +
 .../bookkeeper/mledger/ManagedLedgerFactory.java   |  34 +-
 .../mledger/ManagedLedgerFactoryConfig.java        |  35 +-
 .../mledger/impl/ActiveManagedCursorContainer.java | 131 ++++
 .../impl/ActiveManagedCursorContainerImpl.java     | 836 +++++++++++++++++++++
 ...tiveManagedCursorContainerNavigableSetImpl.java | 310 ++++++++
 .../apache/bookkeeper/mledger/impl/EntryImpl.java  |  56 +-
 .../mledger/impl/EntryReadCountHandlerImpl.java    |  66 ++
 .../mledger/impl/ManagedCursorContainer.java       | 446 +++--------
 ...tainer.java => ManagedCursorContainerImpl.java} | 135 +---
 .../bookkeeper/mledger/impl/ManagedCursorImpl.java |  25 +-
 .../mledger/impl/ManagedLedgerFactoryImpl.java     |  15 +-
 .../bookkeeper/mledger/impl/ManagedLedgerImpl.java |  49 +-
 .../mledger/impl/NonDurableCursorImpl.java         |   2 +
 .../apache/bookkeeper/mledger/impl/OpAddEntry.java |  20 +-
 .../mledger/impl/ReadOnlyCursorImpl.java           |   4 +-
 .../bookkeeper/mledger/impl/cache/EntryCache.java  |   7 +-
 .../mledger/impl/cache/EntryCacheDisabled.java     |   7 +-
 .../mledger/impl/cache/EntryCacheManager.java      |  11 +-
 ...yCacheManager.java => EntryLengthFunction.java} |  30 +-
 .../mledger/impl/cache/PendingReadsManager.java    |  20 +-
 .../bookkeeper/mledger/impl/cache/RangeCache.java  |  21 +-
 .../mledger/impl/cache/RangeCacheEntryWrapper.java |  12 +
 .../mledger/impl/cache/RangeCacheRemovalQueue.java | 191 ++++-
 .../mledger/impl/cache/RangeEntryCacheImpl.java    | 151 +++-
 .../RangeEntryCacheManagerEvictionHandler.java     |   6 +
 .../impl/cache/RangeEntryCacheManagerImpl.java     |  68 +-
 .../impl/ActiveManagedCursorContainerTest.java     | 293 ++++++++
 .../mledger/impl/EntryCacheManagerTest.java        |   1 +
 .../bookkeeper/mledger/impl/EntryCacheTest.java    |  48 +-
 .../bookkeeper/mledger/impl/EntryImplTest.java     |  14 +-
 .../impl/InflightReadsLimiterIntegrationTest.java  |   6 +-
 .../mledger/impl/ManagedCursorContainerTest.java   | 567 ++------------
 .../bookkeeper/mledger/impl/ManagedCursorTest.java |   6 +-
 .../bookkeeper/mledger/impl/ManagedLedgerTest.java |   3 +-
 .../bookkeeper/mledger/impl/MockManagedCursor.java | 461 ++++++++++++
 .../mledger/impl/NonDurableCursorTest.java         |  14 +-
 .../impl/cache/PendingReadsManagerTest.java        | 112 +--
 .../impl/cache/RangeCacheRemovalQueueTest.java     | 245 ++++++
 .../mledger/impl/cache/RangeCacheTest.java         |  28 +-
 .../impl/cache/RangeEntryCacheImplTest.java        | 209 ++++++
 .../bookkeeper/test/MockedBookKeeperTestCase.java  |   1 +
 microbench/README.md                               |  15 +
 .../ActiveManagedCursorContainerBenchmark.java     | 150 ++++
 .../bookkeeper/mledger/impl/MockManagedCursor.java | 454 +++++++++++
 .../impl/cache/RangeCacheEvictionBenchmark.java    | 111 +++
 .../mledger/impl/cache/package-info.java           |  26 +-
 .../bookkeeper/mledger/impl/package-info.java      |  26 +-
 pip/pip-430.md                                     | 417 +++++-----
 pom.xml                                            |  64 ++
 .../apache/pulsar/broker/ServiceConfiguration.java |  80 +-
 .../pulsar/broker/ManagedLedgerClientFactory.java  |  11 +
 .../pulsar/broker/service/BrokerService.java       |  26 +-
 .../pulsar/broker/service/EntryAndMetadata.java    |   6 +
 ...istentStickyKeyDispatcherMultipleConsumers.java |   4 +
 .../service/persistent/PersistentSubscription.java |  10 +
 .../broker/service/persistent/PersistentTopic.java |   8 +-
 .../buffer/impl/TransactionEntryImpl.java          |   6 +
 .../mledger/impl/cache/RangeCacheTestUtil.java     |  38 +
 .../apache/pulsar/broker/MultiBrokerBaseTest.java  |  83 +-
 .../pulsar/broker/MultiBrokerTestZKBaseTest.java   |  17 +-
 .../broker/auth/MockedPulsarServiceBaseTest.java   |  43 +-
 .../AbstractBrokerEntryCacheMultiBrokerTest.java   | 296 ++++++++
 .../pulsar/broker/cache/BacklogConsumerTest.java   |   8 +
 .../cache/BrokerEntryCacheRollingRestartTest.java  | 405 ++++++++++
 .../pulsar/broker/cache/BrokerEntryCacheTest.java  | 760 +++++++++++++++++++
 .../cache/MinimumBacklogCacheStrategyTest.java     |   1 +
 .../pulsar/broker/cache/PulsarLookupProxy.java     | 109 +++
 .../apache/pulsar/broker/service/Ipv4Proxy.java    |  11 +-
 .../broker/service/PersistentTopicE2ETest.java     |   2 +-
 .../persistent/PersistentSubscriptionTest.java     |   4 +-
 .../broker/stats/ManagedLedgerMetricsTest.java     |   6 +-
 .../testcontext/NonClosableMockBookKeeper.java     |   2 +-
 .../broker/testcontext/PulsarTestContext.java      |  14 +
 .../pulsar/broker/transaction/TransactionTest.java |   3 +-
 .../client/api/InjectedClientCnxClientBuilder.java |  67 +-
 .../client/api/SimpleProducerConsumerTest.java     | 152 ++--
 .../api/SubscriptionPauseOnAckStatPersistTest.java |   3 +-
 pulsar-broker/src/test/resources/log4j2.xml        |  52 +-
 .../apache/pulsar/client/impl/ConnectionPool.java  |   8 +-
 .../bookkeeper/client/PulsarMockBookKeeper.java    |  56 ++
 .../client/PulsarMockBookKeeperReadEvent.java      |  55 ++
 .../bookkeeper/client/PulsarMockLedgerHandle.java  |  36 +-
 91 files changed, 6858 insertions(+), 1744 deletions(-)

diff --git a/buildtools/src/main/java/org/apache/pulsar/tests/TestRetrySupport.java b/buildtools/src/main/java/org/apache/pulsar/tests/TestRetrySupport.java
index fd368bd0191..e3e5c18acc9 100644
--- a/buildtools/src/main/java/org/apache/pulsar/tests/TestRetrySupport.java
+++ b/buildtools/src/main/java/org/apache/pulsar/tests/TestRetrySupport.java
@@ -67,9 +67,11 @@ public abstract class TestRetrySupport {
     public final void failureCheck(ITestResult testResult, Method method) {
         // track the setup number where the failure happened
         if (!testResult.isSuccess()) {
-            LOG.info("Detected test failure in test {}.{}, currentSetupNumber={}",
-                    method.getDeclaringClass().getSimpleName(), method.getName(),
-                    currentSetupNumber);
+            if (testResult.getStatus() != ITestResult.SKIP && testResult.getStatus() != ITestResult.CREATED) {
+                LOG.info("Detected test failure in test {}.{}, currentSetupNumber={}",
+                        method.getDeclaringClass().getSimpleName(), method.getName(),
+                        currentSetupNumber);
+            }
             failedSetupNumber = currentSetupNumber;
         }
     }
diff --git a/buildtools/src/main/resources/log4j2.xml b/buildtools/src/main/resources/log4j2.xml
index b0d01a734c5..608a18c234e 100644
--- a/buildtools/src/main/resources/log4j2.xml
+++ b/buildtools/src/main/resources/log4j2.xml
@@ -19,20 +19,40 @@
     under the License.
 
 -->
-<Configuration status="INFO">
-    <Appenders>
-        <Console name="Console" target="SYSTEM_OUT">
-            <PatternLayout pattern="%d{ISO8601_OFFSET_DATE_TIME_HHMM} %-5level [%t{12}] %c{1.} - %msg %X%n" />
-        </Console>
-    </Appenders>
-    <Loggers>
-        <Root level="warn">
-            <AppenderRef ref="Console" />
-        </Root>
-        <Logger name="org.eclipse.jetty" level="info"/>
-        <Logger name="org.apache.pulsar" level="info"/>
-        <Logger name="org.apache.bookkeeper" level="info"/>
-        <Logger name="org.apache.kafka" level="info"/>
-        <Logger name="org.testcontainers" level="info"/>
-    </Loggers>
-</Configuration>
+<Configuration xmlns="http://logging.apache.org/log4j/2.0/config"
+               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+               xsi:schemaLocation="http://logging.apache.org/log4j/2.0/config https://logging.apache.org/log4j/2.0/log4j-core.xsd"
+               status="INFO">
+  <Properties>
+    <Property name="pulsar.test.logging.appender" value="CONSOLE"/>
+    <Property name="pulsar.test.logging.file" value="target/pulsar.test.log"/>
+    <Property name="pulsar.test.logging.file.immediateFlush" value="false"/>
+  </Properties>
+  <Appenders>
+    <!-- setting follow="true" is required for using ConsoleCaptor to validate log messages -->
+    <Console name="CONSOLE" target="SYSTEM_OUT" follow="true">
+      <PatternLayout pattern="%d{ISO8601_OFFSET_DATE_TIME_HHMM} %-5level [%t{12}] %c{1.} - %msg %X%n"/>
+    </Console>
+    <RollingFile name="FILE"
+                 fileName="${sys:pulsar.test.logging.file}"
+                 filePattern="${sys:pulsar.test.logging.file}-%i.log"
+                 immediateFlush="${sys:pulsar.test.logging.file.immediateFlush}"
+                 createOnDemand="true">
+      <PatternLayout pattern="%d{ISO8601_OFFSET_DATE_TIME_HHMM} [%t] %-5level %logger{36} - %msg%n"/>
+      <Policies>
+        <SizeBasedTriggeringPolicy size="10MB"/>
+      </Policies>
+    </RollingFile>
+  </Appenders>
+  <Loggers>
+    <Root level="warn">
+      <!-- by setting -Dpulsar.test.logging.appender=FILE, it's possible to log to file only -->
+      <AppenderRef ref="${sys:pulsar.test.logging.appender}"/>
+    </Root>
+    <Logger name="org.eclipse.jetty" level="info"/>
+    <Logger name="org.apache.pulsar" level="info"/>
+    <Logger name="org.apache.bookkeeper" level="info"/>
+    <Logger name="org.apache.kafka" level="info"/>
+    <Logger name="org.testcontainers" level="info"/>
+  </Loggers>
+</Configuration>
\ No newline at end of file
diff --git a/conf/broker.conf b/conf/broker.conf
index c117596445d..f56077b5006 100644
--- a/conf/broker.conf
+++ b/conf/broker.conf
@@ -1215,6 +1215,15 @@ managedLedgerNumSchedulerThreads=
 # running  in the same broker. By default, uses 1/5th of available direct memory
 managedLedgerCacheSizeMB=
 
+# Evicting cache data by the slowest markDeletedPosition (true) or slowest read position (false).
+# This setting is ignored when cacheEvictionByExpectedReadCount is true."
+cacheEvictionByMarkDeletedPosition=false
+
+# Evicting cache data by expected read count. Expected read count is calculated by the number of
+# active cursors with a read position that is behind the position of the cached entry.
+# This setting will override the cacheEvictionByMarkDeletedPosition setting.
+cacheEvictionByExpectedReadCount=true
+
 # Whether we should make a copy of the entry payloads when inserting in cache
 managedLedgerCacheCopyEntries=false
 
@@ -1224,21 +1233,67 @@ managedLedgerCacheEvictionWatermark=0.9
 # Configure the cache eviction interval in milliseconds for the managed ledger cache
 managedLedgerCacheEvictionIntervalMs=10
 
-# All entries that have stayed in cache for more than the configured time, will be evicted
+# Controls time-to-live (TTL) for entries in the managed ledger (broker) cache.
+# The TTL can be extended in two ways:
+# 1. When cacheEvictionByExpectedReadCount is enabled: TTL is extended for entries with remaining
+# expected reads. The maximum number of extensions is controlled by
+# managedLedgerCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes.
+# 2. When cacheEvictionExtendTTLOfRecentlyAccessed is enabled: TTL is extended for entries
+# accessed since the last expiration check.
+# Default value is 1000ms.
 managedLedgerCacheEvictionTimeThresholdMillis=1000
 
+# Maximum number of times the cache can extend the TTL of an entry that has remaining expected reads.
+# Only takes effect when cacheEvictionByExpectedReadCount is enabled.
+# This helps optimize cache efficiency for scenarios like:
+# - Key_Shared subscription replays
+# - Catch-up reads for lagging consumers
+# - Consumers temporarily falling behind the tail
+# Entries with remaining expected reads will have their TTL extended up to this many times
+# before being eligible for eviction. The TTL will be extended by
+# managedLedgerCacheEvictionTimeThresholdMillis each time.
+# Default is 5.
+managedLedgerCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes=5
+
+# Controls whether recently accessed entries in the managed ledger cache should have their
+# lifetime extended before cache eviction.
+# When enabled:
+# - During eviction check, if an entry has been accessed since the last check, its expiration
+# time will be extended by managedLedgerCacheEvictionTimeThresholdMillis
+# - Makes the cache behave like a Least Recently Used (LRU) cache by keeping frequently
+# accessed entries longer
+# - Helps optimize performance for frequently accessed entries while still allowing old unused
+# entries to be evicted
+# - Minimum eviction time is 2x managedLedgerCacheEvictionTimeThresholdMillis
+# When disabled:
+# - Cache behaves more like a FIFO queue with time-based and size-based eviction
+# - Minimum eviction time is managedLedgerCacheEvictionTimeThresholdMillis
+# Default is true, to behave like a LRU cache.
+managedLedgerCacheEvictionExtendTTLOfRecentlyAccessed=true
+
+# This setting configures the duration of continuing to cache added entries while there are no
+# active cursors, when the last active cursor has left or immediately after initialization when
+# the persistent topic and the managed ledger gets loaded.
+# This setting is ignored unless cacheEvictionByExpectedReadCount is enabled.
+# The default value is 2 * managedLedgerCacheEvictionTimeThresholdMillis.
+managedLedgerContinueCachingAddedEntriesAfterLastActiveCursorLeavesMillis=
+
 # Configure the threshold (in number of entries) from where a cursor should be considered 'backlogged'
 # and thus should be set as inactive.
+# This has no effect when cacheEvictionByExpectedReadCount is enabled.
 managedLedgerCursorBackloggedThreshold=1000
 
 # Minimum cursors that must be in backlog state to cache and reuse the read entries.
 # (Default =0 to disable backlog reach cache)
+# This has no effect when cacheEvictionByExpectedReadCount is enabled.
 managedLedgerMinimumBacklogCursorsForCaching=0
 
 # Minimum backlog entries for any cursor before start caching reads.
+# This has no effect when cacheEvictionByExpectedReadCount is enabled.
 managedLedgerMinimumBacklogEntriesForCaching=1000
 
 # Maximum backlog entry difference to prevent caching entries that can't be reused.
+# This has no effect when cacheEvictionByExpectedReadCount is enabled.
 managedLedgerMaxBacklogBetweenCursorsForCaching=1000
 
 # Rate limit the amount of writes per second generated by consumer acking the messages
diff --git a/conf/standalone.conf b/conf/standalone.conf
index 17977289846..c6be0376ed9 100644
--- a/conf/standalone.conf
+++ b/conf/standalone.conf
@@ -786,6 +786,15 @@ managedLedgerNumSchedulerThreads=4
 # running  in the same broker. By default, uses 1/5th of available direct memory
 managedLedgerCacheSizeMB=
 
+# Evicting cache data by the slowest markDeletedPosition (true) or slowest read position (false).
+# This setting is ignored when cacheEvictionByExpectedReadCount is true."
+cacheEvictionByMarkDeletedPosition=false
+
+# Evicting cache data by expected read count. Expected read count is calculated by the number of
+# active cursors with a read position that is behind the position of the cached entry.
+# This setting will override the cacheEvictionByMarkDeletedPosition setting.
+cacheEvictionByExpectedReadCount=true
+
 # Whether we should make a copy of the entry payloads when inserting in cache
 managedLedgerCacheCopyEntries=false
 
@@ -795,13 +804,69 @@ managedLedgerCacheEvictionWatermark=0.9
 # Configure the cache eviction frequency for the managed ledger cache (evictions/sec)
 managedLedgerCacheEvictionFrequency=100.0
 
-# All entries that have stayed in cache for more than the configured time, will be evicted
+# Controls time-to-live (TTL) for entries in the managed ledger (broker) cache.
+# The TTL can be extended in two ways:
+# 1. When cacheEvictionByExpectedReadCount is enabled: TTL is extended for entries with remaining
+# expected reads. The maximum number of extensions is controlled by
+# managedLedgerCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes.
+# 2. When cacheEvictionExtendTTLOfRecentlyAccessed is enabled: TTL is extended for entries
+# accessed since the last expiration check.
+# Default value is 1000ms.
 managedLedgerCacheEvictionTimeThresholdMillis=1000
 
+# Maximum number of times the cache can extend the TTL of an entry that has remaining expected reads.
+# Only takes effect when cacheEvictionByExpectedReadCount is enabled.
+# This helps optimize cache efficiency for scenarios like:
+# - Key_Shared subscription replays
+# - Catch-up reads for lagging consumers
+# - Consumers temporarily falling behind the tail
+# Entries with remaining expected reads will have their TTL extended up to this many times
+# before being eligible for eviction. The TTL will be extended by
+# managedLedgerCacheEvictionTimeThresholdMillis each time.
+# Default is 5.
+managedLedgerCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes=5
+
+# Controls whether recently accessed entries in the managed ledger cache should have their
+# lifetime extended before cache eviction.
+# When enabled:
+# - During eviction check, if an entry has been accessed since the last check, its expiration
+# time will be extended by managedLedgerCacheEvictionTimeThresholdMillis
+# - Makes the cache behave like a Least Recently Used (LRU) cache by keeping frequently
+# accessed entries longer
+# - Helps optimize performance for frequently accessed entries while still allowing old unused
+# entries to be evicted
+# - Minimum eviction time is 2x managedLedgerCacheEvictionTimeThresholdMillis
+# When disabled:
+# - Cache behaves more like a FIFO queue with time-based and size-based eviction
+# - Minimum eviction time is managedLedgerCacheEvictionTimeThresholdMillis
+# Default is true, to behave like a LRU cache.
+managedLedgerCacheEvictionExtendTTLOfRecentlyAccessed=true
+
+# This setting configures the duration of continuing to cache added entries while there are no
+# active cursors, when the last active cursor has left or immediately after initialization when
+# the persistent topic and the managed ledger gets loaded.
+# This setting is ignored unless cacheEvictionByExpectedReadCount is enabled.
+# The default value is 2 * managedLedgerCacheEvictionTimeThresholdMillis.
+managedLedgerContinueCachingAddedEntriesAfterLastActiveCursorLeavesMillis=
+
 # Configure the threshold (in number of entries) from where a cursor should be considered 'backlogged'
 # and thus should be set as inactive.
+# This has no effect when cacheEvictionByExpectedReadCount is enabled.
 managedLedgerCursorBackloggedThreshold=1000
 
+# Minimum cursors that must be in backlog state to cache and reuse the read entries.
+# (Default =0 to disable backlog reach cache)
+# This has no effect when cacheEvictionByExpectedReadCount is enabled.
+managedLedgerMinimumBacklogCursorsForCaching=0
+
+# Minimum backlog entries for any cursor before start caching reads.
+# This has no effect when cacheEvictionByExpectedReadCount is enabled.
+managedLedgerMinimumBacklogEntriesForCaching=1000
+
+# Maximum backlog entry difference to prevent caching entries that can't be reused.
+# This has no effect when cacheEvictionByExpectedReadCount is enabled.
+managedLedgerMaxBacklogBetweenCursorsForCaching=1000
+
 # Rate limit the amount of writes generated by consumer acking the messages
 managedLedgerDefaultMarkDeleteRateLimit=0.1
 
@@ -901,16 +966,6 @@ managedLedgerAddEntryTimeoutSeconds=0
 # Of course, use a smaller value may degrade consumption throughput. Default is 10ms.
 managedLedgerNewEntriesCheckDelayInMillis=10
 
-# Minimum cursors that must be in backlog state to cache and reuse the read entries.
-# (Default =0 to disable backlog reach cache)
-managedLedgerMinimumBacklogCursorsForCaching=0
-
-# Minimum backlog entries for any cursor before start caching reads.
-managedLedgerMinimumBacklogEntriesForCaching=1000
-
-# Maximum backlog entry difference to prevent caching entries that can't be reused.
-managedLedgerMaxBacklogBetweenCursorsForCaching=1000
-
 # Managed ledger prometheus stats latency rollover seconds (default: 60s)
 managedLedgerPrometheusStatsLatencyRolloverSeconds=60
 
diff --git a/distribution/server/src/assemble/LICENSE.bin.txt b/distribution/server/src/assemble/LICENSE.bin.txt
index bb74cae7f40..3be63be877b 100644
--- a/distribution/server/src/assemble/LICENSE.bin.txt
+++ b/distribution/server/src/assemble/LICENSE.bin.txt
@@ -494,7 +494,7 @@ The Apache Software License, Version 2.0
     - io.jsonwebtoken-jjwt-impl-0.11.1.jar
     - io.jsonwebtoken-jjwt-jackson-0.11.1.jar
   * JCTools - Java Concurrency Tools for the JVM
-    - org.jctools-jctools-core-2.1.2.jar
+    - org.jctools-jctools-core-4.0.5.jar
   * Vertx
     - io.vertx-vertx-auth-common-4.5.10.jar
     - io.vertx-vertx-bridge-common-4.5.10.jar
diff --git a/managed-ledger/pom.xml b/managed-ledger/pom.xml
index 2b83fcb4917..ac2e9e2208c 100644
--- a/managed-ledger/pom.xml
+++ b/managed-ledger/pom.xml
@@ -113,6 +113,10 @@
       <groupId>org.roaringbitmap</groupId>
       <artifactId>RoaringBitmap</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.jctools</groupId>
+      <artifactId>jctools-core</artifactId>
+    </dependency>
     <dependency>
        <groupId>io.dropwizard.metrics</groupId>
        <artifactId>metrics-core</artifactId>
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/Entry.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/Entry.java
index c243861e9c0..6b8f8a95773 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/Entry.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/Entry.java
@@ -67,6 +67,29 @@ public interface Entry {
      */
     boolean release();
 
+    /**
+     * Returns the handler used to track the entry's expected read count for the cacheEvictionByExpectedReadCount
+     * strategy (PIP-430). May return null if unsupported by a custom Managed Ledger implementation,
+     * or not applicable.
+     *
+     * @return the read count handler, or null
+     */
+    default EntryReadCountHandler getReadCountHandler() {
+        return null;
+    }
+
+    /**
+     * Check if the entry has an associated {@link EntryReadCountHandler} and it has remaining expected reads.
+     * @return true if the entry has remaining expected reads, false otherwise
+     */
+    default boolean hasExpectedReads() {
+        EntryReadCountHandler readCountHandler = getReadCountHandler();
+        if (readCountHandler != null) {
+            return readCountHandler.hasExpectedReads();
+        }
+        return false;
+    }
+
     /**
      * Check if this entry is for the given Position.
      * @param position the position to check against
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCacheManager.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/EntryReadCountHandler.java
similarity index 61%
copy from managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCacheManager.java
copy to managed-ledger/src/main/java/org/apache/bookkeeper/mledger/EntryReadCountHandler.java
index 853cb1b8763..858698c3ba5 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCacheManager.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/EntryReadCountHandler.java
@@ -16,26 +16,13 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.bookkeeper.mledger.impl.cache;
-
-import org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl;
-
-public interface EntryCacheManager {
-    EntryCache getEntryCache(ManagedLedgerImpl ml);
-
-    void removeEntryCache(String name);
-
-    long getSize();
-
-    long getMaxSize();
-
-    void clear();
-
-    void updateCacheSizeAndThreshold(long maxSize);
-
-    void updateCacheEvictionWatermark(double cacheEvictionWatermark);
-
-    double getCacheEvictionWatermark();
-
-    void doCacheEviction(long maxTimestamp);
+package org.apache.bookkeeper.mledger;
+
+public interface EntryReadCountHandler {
+    int getExpectedReadCount();
+    void incrementExpectedReadCount();
+    void markRead();
+    default boolean hasExpectedReads() {
+        return getExpectedReadCount() >= 1;
+    }
 }
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/ManagedLedgerConfig.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/ManagedLedgerConfig.java
index aaa127973a5..bd4c1a5014e 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/ManagedLedgerConfig.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/ManagedLedgerConfig.java
@@ -84,6 +84,11 @@ public class ManagedLedgerConfig {
     @Getter
     @Setter
     private boolean cacheEvictionByMarkDeletedPosition = false;
+    @Getter
+    @Setter
+    private boolean cacheEvictionByExpectedReadCount = true;
+    @Getter
+    private long continueCachingAddedEntriesAfterLastActiveCursorLeavesMillis;
     private int minimumBacklogCursorsForCaching = 0;
     private int minimumBacklogEntriesForCaching = 1000;
     private int maxBacklogBetweenCursorsForCaching = 1000;
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/ManagedLedgerFactory.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/ManagedLedgerFactory.java
index d9c887fac46..b9adbe3b353 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/ManagedLedgerFactory.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/ManagedLedgerFactory.java
@@ -220,17 +220,43 @@ public interface ManagedLedgerFactory {
     EntryCacheManager getEntryCacheManager();
 
     /**
-     * update cache evictionTimeThreshold.
-     *
-     * @param cacheEvictionTimeThresholdNanos time threshold for eviction.
+     * update cache evictionTimeThreshold dynamically. Similar as
+     * {@link ManagedLedgerFactoryConfig#setCacheEvictionTimeThresholdMillis(long)}
+     * but the value is in nanos. This inconsistency is kept for backwards compatibility.
+     * @param cacheEvictionTimeThresholdNanos time threshold in nanos for eviction.
      */
     void updateCacheEvictionTimeThreshold(long cacheEvictionTimeThresholdNanos);
 
     /**
+     * time threshold for eviction. Similar as
+     * {@link ManagedLedgerFactoryConfig#getCacheEvictionTimeThresholdMillis()}
+     * but the value is in nanos.
      * @return time threshold for eviction.
-     * */
+     */
     long getCacheEvictionTimeThreshold();
 
+    /**
+     * Update extendTTLOfEntriesWithRemainingExpectedReadsMaxTimes. Similar as
+     * {@link ManagedLedgerFactoryConfig#setCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes(int)}.
+     * @param extendTTLOfEntriesWithRemainingExpectedReadsMaxTimes max times to extend TTL for entries with remaining
+     *                                                            expected reads.
+     */
+    default void updateCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes(
+            int extendTTLOfEntriesWithRemainingExpectedReadsMaxTimes) {
+        // Default implementation does nothing for backwards compatibility of the ManagedLedgerFactory interface.
+        // Subclasses can override this method to provide specific behavior.
+    }
+
+    /**
+     * Update cacheEvictionExtendTTLOfRecentlyAccessed. Similar as
+     * {@link ManagedLedgerFactoryConfig#setCacheEvictionExtendTTLOfRecentlyAccessed(boolean)}.
+     * @param cacheEvictionExtendTTLOfRecentlyAccessed whether to extend TTL of recently accessed entries.
+     */
+    default void updateCacheEvictionExtendTTLOfRecentlyAccessed(boolean cacheEvictionExtendTTLOfRecentlyAccessed) {
+        // Default implementation does nothing for backwards compatibility of the ManagedLedgerFactory interface.
+        // Subclasses can override this method to provide specific behavior.
+    }
+
     /**
      * @return properties of this managedLedger.
      */
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/ManagedLedgerFactoryConfig.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/ManagedLedgerFactoryConfig.java
index af538262ed4..592fd2d385e 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/ManagedLedgerFactoryConfig.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/ManagedLedgerFactoryConfig.java
@@ -47,10 +47,43 @@ public class ManagedLedgerFactoryConfig {
     private long cacheEvictionIntervalMs = 10;
 
     /**
-     * All entries that have stayed in cache for more than the configured time, will be evicted.
+     * Controls time-to-live (TTL) for entries in the managed ledger (broker) cache.
+     * The TTL can be extended in two ways:
+     * 1. When cacheEvictionByExpectedReadCount is enabled: TTL is extended for entries with remaining
+     * expected reads. The maximum number of extensions is controlled by
+     * managedLedgerCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes.
+     * 2. When cacheEvictionExtendTTLOfRecentlyAccessed is enabled: TTL is extended for entries
+     * accessed since the last expiration check.
      */
     private long cacheEvictionTimeThresholdMillis = 1000;
 
+    /**
+     * This setting configures the duration of continuing to cache added entries while there are no
+     * active cursors, when the last active cursor has left or immediately after initialization when
+     * the persistent topic and the managed ledger gets loaded.
+     * This setting is ignored unless cacheEvictionByExpectedReadCount is enabled.
+     */
+    private long continueCachingAddedEntriesAfterLastActiveCursorLeavesMillis;
+
+    /**
+     * Maximum number of times the cache can extend the TTL of an entry that has remaining expected reads.
+     * Only takes effect when cacheEvictionByExpectedReadCount is enabled.
+     * This helps optimize cache efficiency for scenarios like:
+     * - Key_Shared subscription replays
+     * - Catch-up reads for lagging consumers
+     * - Consumers temporarily falling behind the tail
+     * Entries with remaining expected reads will have their TTL extended up to this many times
+     * before being eligible for eviction. The TTL will be extended by
+     * managedLedgerCacheEvictionTimeThresholdMillis each time.
+     */
+    private int cacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes = 5;
+
+    /**
+     * Controls whether recently accessed entries in the managed ledger cache should have their
+     * lifetime extended before cache eviction.
+     */
+    private boolean cacheEvictionExtendTTLOfRecentlyAccessed = true;
+
     /**
      * Whether we should make a copy of the entry payloads when inserting in cache.
      */
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ActiveManagedCursorContainer.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ActiveManagedCursorContainer.java
new file mode 100644
index 00000000000..a526608a218
--- /dev/null
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ActiveManagedCursorContainer.java
@@ -0,0 +1,131 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.bookkeeper.mledger.impl;
+
+import org.apache.bookkeeper.mledger.ManagedCursor;
+import org.apache.bookkeeper.mledger.Position;
+import org.apache.commons.lang3.tuple.Pair;
+
+/**
+ * Contains cursors for a ManagedLedger that are actively being used.
+ * <p>
+ * The goal is to be able to use the container for cache eviction where the tracking of the slowest cursor
+ * is important for determining which cache entries can be evicted.
+ * <p>
+ * The usage depends on the cache eviction configuration:
+ *
+ * <p>When cacheEvictionByMarkDeletedPosition is enabled, the slowest cursor is the one with the oldest mark deleted
+ * position. Otherwise, it is the one with the oldest read position.
+ *
+ * <p>When cacheEvictionByExpectedReadCount is enabled, the slowest cursor is not necessarily tracked actively for
+ * every update and the position of the slowest cursor is not returned immediately. The implementation optimized
+ * for cacheEvictionByExpectedReadCount will throw {@link UnsupportedOperationException} for calls to
+ * {@link #cursorUpdated(ManagedCursor, Position)} method. The {@link #updateCursor(ManagedCursor, Position)} method
+ * is used instead to update the cursor position.
+ */
+public interface ActiveManagedCursorContainer extends Iterable<ManagedCursor> {
+    /**
+     * Adds a cursor to the container with the specified position.
+     *
+     * @param cursor    The cursor to add
+     * @param position  The position of the cursor, if null, the cursor won't be tracked in the slowest cursor
+     *                 tracking.
+     */
+    void add(ManagedCursor cursor, Position position);
+
+    /**
+     * Gets a cursor by its name.
+     *
+     * @param name the name of the cursor
+     * @return the ManagedCursor if found, otherwise null
+     */
+    ManagedCursor get(String name);
+
+    /**
+     * Removes a cursor from the container by its name.
+     *
+     * @param name the name of the cursor to remove
+     * @return true if the cursor was removed, false if it was not found
+     */
+    boolean removeCursor(String name);
+
+    /**
+     * Signal that a cursor position has been updated and that the container must re-order the cursor heap
+     * tracking the slowest cursor and return the previous position of the slowest cursor and the possibly updated
+     * position of the slowest cursor.
+     *
+     * @param cursor the cursor to update the position for
+     * @param newPosition the updated position for the cursor
+     * @return a pair of positions, representing the previous slowest cursor and the new slowest cursor (after the
+     *         update).
+     */
+    Pair<Position, Position> cursorUpdated(ManagedCursor cursor, Position newPosition);
+
+    /**
+     * Updates the cursor position without immediately returning the positions for the slowest cursor.
+     * Compared to {@link #cursorUpdated(ManagedCursor, Position)}, this method does require the implementation
+     * to immediately re-order the cursor heap tracking the slowest cursor allowing to optimize the performance
+     * to batch updates in re-ordering.
+     *
+     * @param cursor       The cursor to update
+     * @param newPosition  The new position to set for the cursor
+     */
+    void updateCursor(ManagedCursor cursor, Position newPosition);
+
+    /**
+     * Gets the position of the slowest cursor.
+     *
+     * @return the position of the slowest cursor
+     */
+    Position getSlowestCursorPosition();
+
+    /**
+     * Checks if the container is empty.
+     *
+     * @return true if the container has no cursors, false otherwise
+     */
+    boolean isEmpty();
+
+    /**
+     * Gets the number of cursors in the container.
+     *
+     * @return the number of cursors
+     */
+    int size();
+
+    /**
+     * Returns the number of cursors that are at the same position or before the given cursor.
+     * This is currently used in the cacheEvictionByExpectedReadCount cache eviction algorithm implementation
+     * to estimate how many reads are expected to be performed by cursors for a given position.
+     * @param cursor the cursor for which to count the number of cursors at the same position or before
+     * @return the number of cursors at the same position or before the given cursor, includes the cursor itself
+     */
+    default int getNumberOfCursorsAtSamePositionOrBefore(ManagedCursor cursor) {
+        throw new UnsupportedOperationException("This method is not supported by this implementation");
+    }
+
+    /**
+     * Returns true if added entries should be cached. By default, added entries are cached if the container is not
+     * empty. The implementation can override this method to change the default behavior.
+     * @return true if added entries should be cached, false otherwise
+     */
+    default boolean shouldCacheAddedEntry() {
+        return !isEmpty();
+    }
+}
\ No newline at end of file
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ActiveManagedCursorContainerImpl.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ActiveManagedCursorContainerImpl.java
new file mode 100644
index 00000000000..649f3060e9c
--- /dev/null
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ActiveManagedCursorContainerImpl.java
@@ -0,0 +1,836 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.bookkeeper.mledger.impl;
+
+import static java.util.Objects.requireNonNull;
+import com.google.common.annotations.VisibleForTesting;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.PriorityQueue;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.locks.StampedLock;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.bookkeeper.mledger.ManagedCursor;
+import org.apache.bookkeeper.mledger.Position;
+import org.apache.commons.lang3.mutable.MutableInt;
+import org.apache.commons.lang3.tuple.Pair;
+
+/**
+ * Implementation of {@link ActiveManagedCursorContainer} that tracks active cursors for cache eviction purposes.
+ * This implementation is optimized for the use with cacheEvictionByExpectedReadCount. It doesn't implement
+ * the {@link #cursorUpdated(ManagedCursor, Position)} method, as it is not needed for this use case. Cursors
+ * are updated using the {@link #updateCursor(ManagedCursor, Position)} method instead. This allows lazy updates
+ * to track the ordering of cursors and their positions without the need to immediately reorder the list of cursors
+ * on every cursor update. The cacheEvictionByExpectedReadCount use case will only need to know the number of cursors
+ * and to be able to know how many cursors are at the same position or before a given position when a backlogged read
+ * is performed. When cursors are all performing tailing reads, the ordering of cursors is not important and can be
+ * updated lazily when needed.
+ */
+@Slf4j
+public class ActiveManagedCursorContainerImpl implements ActiveManagedCursorContainer {
+    private static class Node {
+        final ManagedCursor cursor;
+        Position position;
+        Position pendingPosition;
+        boolean pendingRemove = false;
+        MutableInt numberOfCursorsAtSamePositionOrBefore;
+        Node prev;
+        Node next;
+
+        Node(ManagedCursor cursor, Position pendingPosition) {
+            this.cursor = cursor;
+            this.pendingPosition = pendingPosition;
+        }
+    }
+    // number of nodes in the double-linked list
+    int trackedNodeCount = 0;
+    private final long continueCachingAddedEntriesAfterLastActiveCursorLeavesMillis;
+    private volatile long cursorRemovedTimestampMillis;
+    private volatile int cursorCount;
+
+    public ActiveManagedCursorContainerImpl() {
+        this(-1L);
+    }
+
+    public ActiveManagedCursorContainerImpl(long continueCachingAddedEntriesAfterLastActiveCursorLeavesMillis) {
+        this.continueCachingAddedEntriesAfterLastActiveCursorLeavesMillis =
+                continueCachingAddedEntriesAfterLastActiveCursorLeavesMillis;
+        this.cursorRemovedTimestampMillis = System.currentTimeMillis();
+    }
+
+    // Head of the double-linked list (sorted by position)
+    private Node head = null;
+    private Node tail = null;
+
+    // Maps a cursor to the node
+    private final ConcurrentMap<String, Node> cursors = new ConcurrentHashMap<>();
+    private final Map<String, Node> pendingRemovedCursors = new HashMap<>();
+    private final PriorityQueue<Node> pendingPositionUpdates = new PriorityQueue<>(new Comparator<Node>() {
+        @Override
+        public int compare(Node o1, Node o2) {
+            if (o1.position == null) {
+                return 1; // o1 is null, should be after o2
+            }
+            if (o2.position == null) {
+                return -1; // o2 is null, should be after o1
+            }
+            return o2.position.compareTo(o1.position);
+        }
+    });
+
+    private final StampedLock rwLock = new StampedLock();
+
+    @Override
+    public void add(ManagedCursor cursor, Position position) {
+        long stamp = rwLock.writeLock();
+        try {
+            Node node = cursors.get(cursor.getName());
+            if (node != null) {
+                if (position == null) {
+                    if (node.position != null) {
+                        pendingRemovedCursors.put(cursor.getName(), node);
+                        node.pendingRemove = true;
+                    }
+                    node.pendingPosition = null;
+                } else {
+                    if (node.pendingPosition == null) {
+                        pendingPositionUpdates.add(node);
+                    }
+                    node.pendingPosition = position;
+                }
+            } else {
+                if (position != null) {
+                    node = pendingRemovedCursors.remove(cursor.getName());
+                    if (node != null) {
+                        node.pendingRemove = false;
+                    }
+                }
+                if (node == null) {
+                    node = new Node(cursor, position);
+                }
+                if (position != null) {
+                    node.pendingPosition = position;
+                    pendingPositionUpdates.add(node);
+                }
+                if (cursors.put(cursor.getName(), node) == null) {
+                    cursorCount++;
+                }
+            }
+        } finally {
+            rwLock.unlockWrite(stamp);
+        }
+    }
+
+    /**
+     * Insert a node into the sorted double-linked list and update counters.
+     * Nodes with the same position are kept in their insertion order.
+     */
+    private void insertNodeIntoList(Node node) {
+        trackedNodeCount++;
+
+        if (head == null) {
+            // First node in the list
+            head = tail = node;
+            node.numberOfCursorsAtSamePositionOrBefore = new MutableInt(1);
+            return;
+        }
+
+        // Find the correct position using insertion sort
+        Node current = head;
+        int countBefore = 0;
+
+        Node firstNodeWithSamePosition = null;
+
+        // Count all nodes with position less than or equal to the new node's position
+        // We insert after all nodes with the same position to maintain insertion order
+        while (current != null) {
+            int comparison = current.position.compareTo(node.position);
+            if (comparison <= 0) {
+                if (comparison == 0 && firstNodeWithSamePosition == null) {
+                    firstNodeWithSamePosition = current;
+                }
+                if (comparison < 0) {
+                    countBefore++;
+                }
+                current = current.next;
+            } else {
+                break;
+            }
+        }
+
+        // Set the counter for the new node
+        node.numberOfCursorsAtSamePositionOrBefore =
+                firstNodeWithSamePosition != null ? firstNodeWithSamePosition.numberOfCursorsAtSamePositionOrBefore :
+                        new MutableInt(countBefore + 1);
+
+        // Insert the node at the correct position
+        if (current == null) {
+            // Insert at the end
+            node.prev = tail;
+            tail.next = node;
+            tail = node;
+        } else if (current == head) {
+            // Insert at the beginning
+            node.next = head;
+            head.prev = node;
+            head = node;
+        } else {
+            // Insert in the middle
+            node.prev = current.prev;
+            node.next = current;
+            current.prev.next = node;
+            current.prev = node;
+        }
+
+        Node counterUpdateStartNode;
+        if (firstNodeWithSamePosition != null) {
+            // If we found a node with the same position, we start updating counters from it
+            counterUpdateStartNode = firstNodeWithSamePosition;
+        } else {
+            // Otherwise, we start updating from the next node
+            counterUpdateStartNode = node.next;
+        }
+        // Update counters for nodes
+        updateCounters(counterUpdateStartNode, true);
+    }
+
+    /**
+     * Update counters after inserting or removing a node.
+     */
+    private void updateCounters(Node startNode, boolean increment) {
+        if (startNode == null) {
+            return;
+        }
+        Node current = startNode;
+        Position lastUpdatedPosition = null;
+        while (current != null) {
+            if (lastUpdatedPosition == null || current.position.compareTo(lastUpdatedPosition) != 0) {
+                lastUpdatedPosition = current.position;
+                MutableInt numberOfCursorsAtSamePositionOrBefore = current.numberOfCursorsAtSamePositionOrBefore;
+                if (increment) {
+                    numberOfCursorsAtSamePositionOrBefore.increment();
+                } else {
+                    numberOfCursorsAtSamePositionOrBefore.decrement();
+                }
+            }
+            current = current.next;
+        }
+    }
+
+    @Override
+    public ManagedCursor get(String name) {
+        long stamp = rwLock.readLock();
+        try {
+            Node node = cursors.get(name);
+            return node != null ? node.cursor : null;
+        } finally {
+            rwLock.unlockRead(stamp);
+        }
+    }
+
+    @Override
+    public boolean removeCursor(String name) {
+        long stamp = rwLock.writeLock();
+        try {
+            Node node = cursors.remove(name);
+            if (node != null) {
+                if (node.position != null) {
+                    pendingRemovedCursors.put(name, node);
+                    node.pendingRemove = true;
+                }
+                node.pendingPosition = null;
+                cursorRemovedTimestampMillis = System.currentTimeMillis();
+                cursorCount--;
+                return true;
+            } else {
+                return false;
+            }
+        } finally {
+            rwLock.unlockWrite(stamp);
+        }
+    }
+
+    /**
+     * Remove a node from the double-linked list and update counters.
+     */
+    private void removeNodeFromList(Node node) {
+        Node firstNodeWithSamePositionBeforeNode = findFirstNodeWithSamePositionBeforeNode(node);
+
+        // Remove the node from the list
+        if (node.prev != null) {
+            node.prev.next = node.next;
+        } else {
+            // Node was the head
+            head = node.next;
+        }
+
+        if (node.next != null) {
+            node.next.prev = node.prev;
+        } else {
+            // Node was the tail
+            tail = node.prev;
+        }
+
+        // Update counters
+        updateCounters(
+                firstNodeWithSamePositionBeforeNode != null ? firstNodeWithSamePositionBeforeNode : node.next, false);
+
+        // Clear the node's links
+        node.prev = null;
+        node.next = null;
+        trackedNodeCount--;
+    }
+
+    private static Node findFirstNodeWithSamePositionBeforeNode(Node node) {
+        Position samePosition = node.position;
+        // Find the first node with the same position by scanning backwards
+        Node firstNodeWithSamePositionBeforeNode = null;
+        Node current = node.prev;
+        while (current != null && current.position.compareTo(samePosition) == 0) {
+            firstNodeWithSamePositionBeforeNode = current;
+            current = current.prev;
+        }
+        return firstNodeWithSamePositionBeforeNode;
+    }
+
+    @Override
+    public void updateCursor(ManagedCursor cursor, Position newPosition) {
+        requireNonNull(cursor);
+
+        long stamp = rwLock.writeLock();
+        try {
+            Node node = cursors.get(cursor.getName());
+            if (node == null) {
+                return;
+            }
+            if (newPosition == null) {
+                pendingRemovedCursors.put(cursor.getName(), node);
+                node.pendingRemove = true;
+                node.pendingPosition = null;
+            } else {
+                if (node.pendingRemove) {
+                    node.pendingRemove = false;
+                    pendingRemovedCursors.remove(cursor.getName());
+                }
+                // position changed, mark the node as pending
+                if (node.pendingPosition == null) {
+                    pendingPositionUpdates.add(node);
+                }
+                node.pendingPosition = newPosition;
+            }
+        } finally {
+            rwLock.unlockWrite(stamp);
+        }
+    }
+
+    private void processPendingPositions() {
+        if (pendingRemovedCursors.isEmpty() && pendingPositionUpdates.isEmpty()) {
+            // No pending changes, nothing to do
+            return;
+        }
+        if (pendingPositionUpdates.size() >= trackedNodeCount / 2) {
+            rebuildEntireList();
+            pendingPositionUpdates.clear();
+        } else {
+            performIncrementalUpdate();
+        }
+    }
+
+    private void performIncrementalUpdate() {
+        if (!pendingRemovedCursors.isEmpty()) {
+            for (Node node : pendingRemovedCursors.values()) {
+                removeNodeFromList(node);
+                node.pendingRemove = false;
+            }
+            pendingRemovedCursors.clear();
+        }
+        while (true) {
+            Node node = pendingPositionUpdates.poll();
+            if (node == null) {
+                break; // No more pending updates
+            }
+            if (node.pendingPosition != null) {
+                if (node.position == null) {
+                    node.position = node.pendingPosition;
+                    insertNodeIntoList(node);
+                } else {
+                    moveNodeToNewPosition(node, node.position, node.pendingPosition);
+                }
+                node.pendingPosition = null;
+            }
+        }
+    }
+
+    // Rebuild the entire list from scratch for better performance with batch updates
+    // This is more efficient than individual moves when many cursors need updating
+    private void rebuildEntireList() {
+        // Clear the existing list
+        head = null;
+        tail = null;
+        trackedNodeCount = 0;
+
+        // Collect all nodes that should be in the list and update their positions
+        List<Node> activeNodes = new ArrayList<>();
+        for (Node node : cursors.values()) {
+            if (node.pendingPosition != null) {
+                node.position = node.pendingPosition;
+                node.pendingPosition = null;
+            }
+            if (node.position != null && !node.pendingRemove) {
+                activeNodes.add(node);
+                trackedNodeCount++;
+            }
+            node.pendingRemove = false;
+        }
+
+        pendingRemovedCursors.clear();
+
+        // Sort nodes by position (maintaining insertion order for equal positions)
+        activeNodes.sort((n1, n2) -> {
+            int cmp = n1.position.compareTo(n2.position);
+            if (cmp != 0) {
+                return cmp;
+            }
+            // For equal positions, maintain the original order if possible
+            return 0;
+        });
+
+        // Rebuild the linked list and counters
+        Node prev = null;
+        MutableInt currentCounter = null;
+        Position lastPosition = null;
+        int runningCount = 0;
+
+        for (Node node : activeNodes) {
+            // Link the node
+            node.prev = prev;
+            node.next = null;
+            if (prev != null) {
+                prev.next = node;
+            } else {
+                head = node;
+            }
+
+            // Update counters
+            if (lastPosition == null || !node.position.equals(lastPosition)) {
+                runningCount++;
+                currentCounter = new MutableInt(runningCount);
+                lastPosition = node.position;
+            } else {
+                currentCounter.increment();
+                runningCount++;
+            }
+            node.numberOfCursorsAtSamePositionOrBefore = currentCounter;
+
+            prev = node;
+        }
+        tail = prev;
+    }
+
+    @Override
+    public Pair<Position, Position> cursorUpdated(ManagedCursor cursor, Position newPosition) {
+        throw new UnsupportedOperationException("cursorUpdated method is not supported by this implementation");
+    }
+
+    /**
+     * Move {@code node} from {@code oldPosition} to {@code newPosition} while
+     * keeping the list ordered and maintaining the shared counters.
+     *
+     * The implementation updates counters only for the position-groups
+     * crossed by the move, hence it is still O(distance) and faster than a
+     * full remove + insert that would touch every node twice.
+     */
+    private void moveNodeToNewPosition(Node node, Position oldPosition, Position newPosition) {
+        if (oldPosition.compareTo(newPosition) == 0) {
+            return; // No change in position, nothing to do
+        }
+        boolean movingForward = oldPosition.compareTo(newPosition) < 0;
+
+        // Check if the node is already in the correct position
+        if (isAlreadyInCorrectPosition(node, newPosition)) {
+            // Node is already in the correct position, no need to move the node
+
+            // update the position
+            node.position = newPosition;
+            if (node == tail) {
+                // Node is at the tail, no need to update counters
+                return;
+            }
+
+            // update the counters
+            if (movingForward) {
+                // first decrement the counter for the old position
+                node.numberOfCursorsAtSamePositionOrBefore.decrement();
+            }
+            if (node.prev != null && node.prev.position.compareTo(newPosition) == 0) {
+                node.numberOfCursorsAtSamePositionOrBefore = node.prev.numberOfCursorsAtSamePositionOrBefore;
+                // We joined the previous position group, so we need to increment the counter
+                node.numberOfCursorsAtSamePositionOrBefore.increment();
+            } else if (node.next != null && node.next.position.compareTo(newPosition) == 0) {
+                node.numberOfCursorsAtSamePositionOrBefore = node.next.numberOfCursorsAtSamePositionOrBefore;
+            } else {
+                // If neither prev nor next has the same position, we need to recalculate the counter
+                int base = node.prev != null ? node.prev.numberOfCursorsAtSamePositionOrBefore.intValue() : 0;
+                node.numberOfCursorsAtSamePositionOrBefore = new MutableInt(base + 1);
+            }
+            return;
+        }
+
+        // ------------------------------------------------------------------
+        // 1. Unlink the node from the list (but keep its prev/next for later
+        //    traversal where needed).
+        // ------------------------------------------------------------------
+        if (node.prev != null) {
+            node.prev.next = node.next;
+        } else {
+            head = node.next;
+        }
+        if (node.next != null) {
+            node.next.prev = node.prev;
+        } else {
+            tail = node.prev;
+        }
+
+        Position lastProcessedPosition = null;
+
+        if (movingForward) {
+            // decrement the counter for the old position group
+            node.numberOfCursorsAtSamePositionOrBefore.decrement();
+            lastProcessedPosition = oldPosition;
+            // ----------------------------------------------------------------
+            // 2.a Decrement counters for every position-group strictly between
+            //     (oldPosition, newPosition)
+            // ----------------------------------------------------------------
+            Node current = node.next;
+            while (current != null && current.position.compareTo(newPosition) < 0) {
+                if (current.position.compareTo(lastProcessedPosition) != 0) {
+                    current.numberOfCursorsAtSamePositionOrBefore.decrement();
+                    lastProcessedPosition = current.position;
+                }
+                current = current.next;
+            }
+
+            // current is the first node with position >= newPosition (or null)
+            insertNodeAtNewPositionForward(node, current, newPosition);
+        } else {
+            // no need to change the counter for the old position group
+            lastProcessedPosition = oldPosition;
+            // moving backward
+            // ----------------------------------------------------------------
+            // 2.b Increment counters for every position-group >= oldPosition
+            //     and > newPosition (we already decremented the oldPosition
+            //     group right above).
+            // ----------------------------------------------------------------
+            Node current = node.prev;
+            while (current != null && current.position.compareTo(newPosition) > 0) {
+                if (current.position.compareTo(lastProcessedPosition) != 0) {
+                    current.numberOfCursorsAtSamePositionOrBefore.increment();
+                    lastProcessedPosition = current.position;
+                }
+                current = current.prev;
+            }
+
+            // current is the **last** node with position <= newPosition (or null)
+            insertNodeAtNewPositionBackward(node, current, newPosition);
+        }
+
+        // finally update the node's stored position
+        node.position = newPosition;
+    }
+
+    /**
+     * Helper invoked when the cursor is moved forward
+     * (newPosition > oldPosition).
+     *
+     * @param node    the node being moved (already detached)
+     * @param current the first node whose position is >= newPosition, or null
+     */
+    private void insertNodeAtNewPositionForward(Node node, Node current, Position newPosition) {
+        if (current == null) {
+            // Insert at tail
+            node.prev = tail;
+            node.next = null;
+            if (tail != null) {
+                tail.next = node;
+            } else {
+                head = node;   // list was empty
+            }
+            tail = node;
+            node.numberOfCursorsAtSamePositionOrBefore =
+                    new MutableInt(node.prev.numberOfCursorsAtSamePositionOrBefore.intValue() + 1);
+            return;
+        }
+
+        if (current.position.compareTo(newPosition) == 0) {
+            // Join an existing position-group (keep insertion order before
+            // the first element having that position)
+            node.prev = current.prev;
+            node.next = current;
+            if (current.prev != null) {
+                current.prev.next = node;
+            } else {
+                head = node;
+            }
+            current.prev = node;
+            node.numberOfCursorsAtSamePositionOrBefore = current.numberOfCursorsAtSamePositionOrBefore;
+            return;
+        }
+
+        // Create a new position-group *before* 'current'
+        node.prev = current.prev;
+        node.next = current;
+        if (current.prev != null) {
+            current.prev.next = node;
+        } else {
+            head = node;
+        }
+        current.prev = node;
+        node.numberOfCursorsAtSamePositionOrBefore =
+                new MutableInt(node.prev != null ? node.prev.numberOfCursorsAtSamePositionOrBefore.intValue() + 1 : 1);
+    }
+
+    /**
+     * Helper invoked when the cursor is moved backward
+     * (newPosition < oldPosition).
+     *
+     * @param node    the node being moved (already detached)
+     * @param current the last node whose position is <= newPosition, or null
+     */
+    private void insertNodeAtNewPositionBackward(Node node, Node current, Position newPosition) {
+        if (current == null) {
+            // Insert at head
+            node.prev = null;
+            node.next = head;
+            if (head != null) {
+                head.prev = node;
+            }
+            head = node;
+            if (tail == null) {
+                tail = node;
+            }
+            node.numberOfCursorsAtSamePositionOrBefore = new MutableInt(1);
+            return;
+        }
+
+        if (current.position.compareTo(newPosition) == 0) {
+            // Append into an existing position-group (keep insertion order)
+            node.prev = current;
+            node.next = current.next;
+            if (current.next != null) {
+                current.next.prev = node;
+            } else {
+                tail = node;
+            }
+            current.next = node;
+            node.numberOfCursorsAtSamePositionOrBefore = current.numberOfCursorsAtSamePositionOrBefore;
+            node.numberOfCursorsAtSamePositionOrBefore.increment();
+            return;
+        }
+
+        // Insert *after* 'current', creating a brand-new group
+        node.prev = current;
+        node.next = current.next;
+        if (current.next != null) {
+            current.next.prev = node;
+        } else {
+            tail = node;
+        }
+        current.next = node;
+
+        node.numberOfCursorsAtSamePositionOrBefore =
+                new MutableInt(current.numberOfCursorsAtSamePositionOrBefore.intValue() + 1);
+    }
+
+    /**
+     * Check if a node is already in the correct position.
+     */
+    private boolean isAlreadyInCorrectPosition(Node node, Position newPosition) {
+        if (node.prev != null && node.prev.position.compareTo(newPosition) > 0) {
+            // Previous node has a greater position, so this node is not in the correct place
+            return false;
+        }
+        if (node.next != null && node.next.position.compareTo(newPosition) < 0) {
+            // Next node has a smaller position, so this node is not in the correct place
+            return false;
+        }
+        return true;
+    }
+
+    private Position internalSlowestReaderPosition() {
+        processPendingPositions();
+        return head != null ? head.position : null;
+    }
+
+    @Override
+    public Position getSlowestCursorPosition() {
+        long stamp = rwLock.readLock();
+        try {
+            return internalSlowestReaderPosition();
+        } finally {
+            rwLock.unlockRead(stamp);
+        }
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return size() < 1;
+    }
+
+    @Override
+    public String toString() {
+        long stamp = rwLock.readLock();
+        try {
+            StringBuilder sb = new StringBuilder();
+            sb.append('[');
+
+            boolean first = true;
+            for (Node node : cursors.values()) {
+                if (!first) {
+                    sb.append(", ");
+                }
+
+                first = false;
+                sb.append(node.cursor);
+            }
+
+            sb.append(']');
+            return sb.toString();
+        } finally {
+            rwLock.unlockRead(stamp);
+        }
+    }
+
+    @Override
+    public Iterator<ManagedCursor> iterator() {
+        final Iterator<Map.Entry<String, Node>> it = cursors.entrySet().iterator();
+        return new Iterator<ManagedCursor>() {
+            @Override
+            public boolean hasNext() {
+                return it.hasNext();
+            }
+
+            @Override
+            public ManagedCursor next() {
+                return it.next().getValue().cursor;
+            }
+
+            @Override
+            public void remove() {
+                throw new IllegalArgumentException("Cannot remove ManagedCursor from container");
+            }
+        };
+    }
+
+    @Override
+    public int getNumberOfCursorsAtSamePositionOrBefore(ManagedCursor cursor) {
+        long stamp = rwLock.writeLock();
+        try {
+            processPendingPositions();
+            Node node = cursors.get(cursor.getName());
+            if (node == null || node.position == null) {
+                return 0;
+            }
+            return node.numberOfCursorsAtSamePositionOrBefore.intValue();
+        } finally {
+            rwLock.unlockWrite(stamp);
+        }
+    }
+
+    @Override
+    public int size() {
+        return cursorCount;
+    }
+
+    @Override
+    public boolean shouldCacheAddedEntry() {
+        if (!isEmpty()) {
+            return true;
+        }
+        // keep on adding entries to cache for the configured amount of time since the last cursor was removed
+        // this will avoid cache misses when a cursor gets disconnected and immediately reconnects
+        if (continueCachingAddedEntriesAfterLastActiveCursorLeavesMillis > 0) {
+            long sinceLastCursorLeftMillis = System.currentTimeMillis() - cursorRemovedTimestampMillis;
+            if (sinceLastCursorLeftMillis < continueCachingAddedEntriesAfterLastActiveCursorLeavesMillis) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Check the ordering and number of cursors count for each cursor.
+     * This method is used for testing purposes to ensure that the internal state is consistent.
+     */
+    @VisibleForTesting
+    void checkOrderingAndNumberOfCursorsState() {
+        long stamp = rwLock.readLock();
+        try {
+            processPendingPositions();
+            Node current = head;
+            int currentCount = 0;
+            Position lastPosition = null;
+            List<String> lastPositionCursorNames = new ArrayList<>();
+            while (current != null) {
+                if (current.prev != null && current.position.compareTo(current.prev.position) < 0) {
+                    throw new IllegalStateException("Cursors are not ordered: " + current.cursor.getName()
+                            + " with position " + current.position + " is after cursor " + current.prev.cursor.getName()
+                            + " with position " + current.prev.position);
+                }
+                currentCount++;
+                if (lastPosition == null) {
+                    lastPosition = current.position;
+                    lastPositionCursorNames.add(current.cursor.getName());
+                } else if (current.position.compareTo(lastPosition) > 0 || current.next == null) {
+                    int expectedCount;
+                    if (current.position.compareTo(lastPosition) <= 0) {
+                        lastPositionCursorNames.add(current.cursor.getName());
+                        lastPosition = current.position;
+                        expectedCount = currentCount;
+                    } else {
+                        expectedCount = currentCount - 1;
+                    }
+                    if (current.prev.numberOfCursorsAtSamePositionOrBefore.intValue() != expectedCount) {
+                        throw new IllegalStateException(
+                                "Number of cursors at same position is not correct for position "
+                                        + current.prev.position + ": expected " + expectedCount
+                                        + ", but got " + current.prev.numberOfCursorsAtSamePositionOrBefore
+                                        + " for cursors "
+                                        + lastPositionCursorNames + " current.next: " + current.next);
+                    }
+                    if (current.next != null) {
+                        lastPositionCursorNames.clear();
+                        lastPositionCursorNames.add(current.cursor.getName());
+                        lastPosition = current.position;
+                    }
+                } else {
+                    lastPositionCursorNames.add(current.cursor.getName());
+                }
+                current = current.next;
+
+            }
+        } finally {
+            rwLock.unlockRead(stamp);
+        }
+    }
+}
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ActiveManagedCursorContainerNavigableSetImpl.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ActiveManagedCursorContainerNavigableSetImpl.java
new file mode 100644
index 00000000000..79654b0aa59
--- /dev/null
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ActiveManagedCursorContainerNavigableSetImpl.java
@@ -0,0 +1,310 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.bookkeeper.mledger.impl;
+
+import static java.util.Objects.requireNonNull;
+import com.google.common.annotations.VisibleForTesting;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.NavigableSet;
+import java.util.TreeSet;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.locks.StampedLock;
+import org.apache.bookkeeper.mledger.ManagedCursor;
+import org.apache.bookkeeper.mledger.Position;
+import org.apache.commons.lang3.tuple.Pair;
+
+/**
+ * Implementation of {@link ActiveManagedCursorContainer} that uses a {@link NavigableSet} to keep track of the
+ * cursors ordered by their positions. This implementation is currently used in JMH benchmarks to compare
+ * performance of different cursor containers.
+ */
+public class ActiveManagedCursorContainerNavigableSetImpl implements ActiveManagedCursorContainer {
+    // Counter to keep track of the order in which cursors are added.
+    // This is used to maintain a stable order of cursors that have the same position.
+    private final AtomicInteger addOrderCounter = new AtomicInteger(0);
+
+    private static class Item implements Comparable<Item> {
+        final ManagedCursor cursor;
+        final int addOrder;
+        Position position;
+
+        Item(ManagedCursor cursor, Position position, int addOrder) {
+            this.cursor = cursor;
+            this.addOrder = addOrder;
+            this.position = position;
+        }
+
+        @Override
+        public int compareTo(Item o) {
+            int positionComparison = this.position.compareTo(o.position);
+            if (positionComparison != 0) {
+                return positionComparison;
+            }
+            // If positions are equal, compare by add order to maintain a stable order
+            // for cursors that have the same position.
+            return Integer.compare(this.addOrder, o.addOrder);
+        }
+    }
+
+    public ActiveManagedCursorContainerNavigableSetImpl() {}
+
+    // Used to keep track of slowest cursor and the cursors before a given position.
+    private final NavigableSet<Item> sortedByPosition = new TreeSet<>();
+
+    // Maps a cursor to its position in the heap
+    private final ConcurrentMap<String, Item> cursors = new ConcurrentHashMap<>();
+
+    private final StampedLock rwLock = new StampedLock();
+
+    @Override
+    public void add(ManagedCursor cursor, Position position) {
+        long stamp = rwLock.writeLock();
+        try {
+            Item item = new Item(cursor, position, addOrderCounter.getAndIncrement());
+            cursors.put(cursor.getName(), item);
+            if (position != null) {
+                sortedByPosition.add(item);
+            }
+        } finally {
+            rwLock.unlockWrite(stamp);
+        }
+    }
+
+    @Override
+    public ManagedCursor get(String name) {
+        long stamp = rwLock.readLock();
+        try {
+            Item item = cursors.get(name);
+            return item != null ? item.cursor : null;
+        } finally {
+            rwLock.unlockRead(stamp);
+        }
+    }
+
+    @Override
+    public boolean removeCursor(String name) {
+        long stamp = rwLock.writeLock();
+        try {
+            Item item = cursors.remove(name);
+            if (item != null) {
+                if (item.position != null) {
+                    sortedByPosition.remove(item);
+                }
+                return true;
+            } else {
+                return false;
+            }
+        } finally {
+            rwLock.unlockWrite(stamp);
+        }
+    }
+
+    @Override
+    public Pair<Position, Position> cursorUpdated(ManagedCursor cursor, Position newPosition) {
+        requireNonNull(cursor);
+
+        long stamp = rwLock.writeLock();
+        try {
+            Item item = cursors.get(cursor.getName());
+            if (item == null) {
+                return null;
+            }
+            Position previousSlowestConsumer = internalSlowestReaderPosition();
+            // it is necessary to remove the item from the sorted set
+            // before updating the position, since otherwise sorting would not work correctly
+            if (item.position != null) {
+                sortedByPosition.remove(item);
+            }
+            item.position = newPosition;
+            // if the new position is not null, we add it back to the sorted set
+            // to maintain the order of cursors by position
+            if (newPosition != null) {
+                sortedByPosition.add(item);
+            }
+            Position newSlowestConsumer = internalSlowestReaderPosition();
+            return Pair.of(previousSlowestConsumer, newSlowestConsumer);
+        } finally {
+            rwLock.unlockWrite(stamp);
+        }
+    }
+
+    @Override
+    public void updateCursor(ManagedCursor cursor, Position newPosition) {
+        cursorUpdated(cursor, newPosition);
+    }
+
+    private Position internalSlowestReaderPosition() {
+        return !sortedByPosition.isEmpty() ? sortedByPosition.first().position : null;
+    }
+
+    @Override
+    public Position getSlowestCursorPosition() {
+        long stamp = rwLock.readLock();
+        try {
+            return internalSlowestReaderPosition();
+        } finally {
+            rwLock.unlockRead(stamp);
+        }
+    }
+
+    @Override
+    public boolean isEmpty() {
+        long stamp = rwLock.tryOptimisticRead();
+        boolean isEmpty = cursors.isEmpty();
+        if (!rwLock.validate(stamp)) {
+            // Fallback to read lock
+            stamp = rwLock.readLock();
+            try {
+                isEmpty = cursors.isEmpty();
+            } finally {
+                rwLock.unlockRead(stamp);
+            }
+        }
+
+        return isEmpty;
+    }
+
+    @Override
+    public String toString() {
+        long stamp = rwLock.readLock();
+        try {
+            StringBuilder sb = new StringBuilder();
+            sb.append('[');
+
+            boolean first = true;
+            for (Item item : cursors.values()) {
+                if (!first) {
+                    sb.append(", ");
+                }
+
+                first = false;
+                sb.append(item.cursor);
+            }
+
+            sb.append(']');
+            return sb.toString();
+        } finally {
+            rwLock.unlockRead(stamp);
+        }
+    }
+
+    @Override
+    public Iterator<ManagedCursor> iterator() {
+        final Iterator<Map.Entry<String, Item>> it = cursors.entrySet().iterator();
+        return new Iterator<ManagedCursor>() {
+            @Override
+            public boolean hasNext() {
+                return it.hasNext();
+            }
+
+            @Override
+            public ManagedCursor next() {
+                return it.next().getValue().cursor;
+            }
+
+            @Override
+            public void remove() {
+                throw new IllegalArgumentException("Cannot remove ManagedCursor from container");
+            }
+        };
+    }
+
+    @Override
+    public int getNumberOfCursorsAtSamePositionOrBefore(ManagedCursor cursor) {
+        long stamp = rwLock.readLock();
+        try {
+            Item item = cursors.get(cursor.getName());
+            if (item == null || item.position == null) {
+                return 0;
+            } else {
+                int count = 0;
+                for (Item o : sortedByPosition) {
+                    if (o.position.compareTo(item.position) > 0) {
+                        break;
+                    }
+                    count++;
+                }
+                return count;
+            }
+        } finally {
+            rwLock.unlockRead(stamp);
+        }
+    }
+
+    @Override
+    public int size() {
+        long stamp = rwLock.tryOptimisticRead();
+        int size = cursors.size();
+        if (!rwLock.validate(stamp)) {
+            // Fallback to read lock
+            stamp = rwLock.readLock();
+            try {
+                size = cursors.size();
+            } finally {
+                rwLock.unlockRead(stamp);
+            }
+        }
+        return size;
+    }
+
+    /**
+     * Check the ordering and number of cursors count for each cursor.
+     * This method is used for testing purposes to ensure that the internal state is consistent.
+     */
+    @VisibleForTesting
+    void checkOrderingAndNumberOfCursorsState() {
+        int currentCount = 0;
+        Position lastPosition = null;
+        List<String> lastPositionCursorNames = new ArrayList<>();
+        Item previousItem = null;
+        for (Item o : sortedByPosition) {
+            currentCount++;
+            if (lastPosition != null && o.position.compareTo(lastPosition) > 0) {
+                int expectedCount = currentCount - 1;
+                int numberOfCursorsAtSamePositionOrBefore =
+                        getNumberOfCursorsAtSamePositionOrBefore(previousItem.cursor);
+                if (numberOfCursorsAtSamePositionOrBefore != expectedCount) {
+                    throw new IllegalStateException("Number of cursors at same position is not correct for position "
+                            + previousItem.position + ": expected " + expectedCount
+                            + ", but got " + numberOfCursorsAtSamePositionOrBefore + " for cursors "
+                            + lastPositionCursorNames);
+                }
+                lastPositionCursorNames.clear();
+            }
+            lastPosition = o.position;
+            lastPositionCursorNames.add(o.cursor.getName());
+            previousItem = o;
+        }
+        if (lastPosition != null) {
+            int expectedCount = currentCount;
+            int numberOfCursorsAtSamePositionOrBefore = getNumberOfCursorsAtSamePositionOrBefore(previousItem.cursor);
+            if (numberOfCursorsAtSamePositionOrBefore != expectedCount) {
+                throw new IllegalStateException("Number of cursors at same position is not correct for position "
+                        + previousItem.position + ": expected " + expectedCount
+                        + ", but got " + numberOfCursorsAtSamePositionOrBefore + " for cursors "
+                        + lastPositionCursorNames);
+            }
+        }
+    }
+}
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/EntryImpl.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/EntryImpl.java
index c2b8b906e91..b85a1bc45fe 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/EntryImpl.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/EntryImpl.java
@@ -27,6 +27,7 @@ import io.netty.util.ReferenceCounted;
 import org.apache.bookkeeper.client.api.LedgerEntry;
 import org.apache.bookkeeper.client.impl.LedgerEntryImpl;
 import org.apache.bookkeeper.mledger.Entry;
+import org.apache.bookkeeper.mledger.EntryReadCountHandler;
 import org.apache.bookkeeper.mledger.Position;
 import org.apache.bookkeeper.mledger.PositionFactory;
 import org.apache.bookkeeper.mledger.ReferenceCountedEntry;
@@ -48,20 +49,24 @@ public final class EntryImpl extends AbstractCASReferenceCounted
     private long entryId;
     private Position position;
     ByteBuf data;
+    private EntryReadCountHandler readCountHandler;
+    private boolean decreaseReadCountOnRelease = true;
 
     private Runnable onDeallocate;
 
-    public static EntryImpl create(LedgerEntry ledgerEntry) {
+    public static EntryImpl create(LedgerEntry ledgerEntry, int expectedReadCount) {
         EntryImpl entry = RECYCLER.get();
         entry.ledgerId = ledgerEntry.getLedgerId();
         entry.entryId = ledgerEntry.getEntryId();
         entry.data = ledgerEntry.getEntryBuffer();
         entry.data.retain();
+        entry.readCountHandler = EntryReadCountHandlerImpl.maybeCreate(expectedReadCount);
         entry.setRefCnt(1);
         return entry;
     }
 
-    public static EntryImpl create(LedgerEntry ledgerEntry, ManagedLedgerInterceptor interceptor) {
+    public static EntryImpl create(LedgerEntry ledgerEntry, ManagedLedgerInterceptor interceptor,
+                                   int expectedReadCount) {
         ManagedLedgerInterceptor.PayloadProcessorHandle processorHandle = null;
         if (interceptor != null) {
             ByteBuf duplicateBuffer = ledgerEntry.getEntryBuffer().retainedDuplicate();
@@ -74,7 +79,7 @@ public final class EntryImpl extends AbstractCASReferenceCounted
                 duplicateBuffer.release();
             }
         }
-        EntryImpl returnEntry = create(ledgerEntry);
+        EntryImpl returnEntry = create(ledgerEntry, expectedReadCount);
         if (processorHandle != null) {
             processorHandle.release();
             ledgerEntry.close();
@@ -84,41 +89,66 @@ public final class EntryImpl extends AbstractCASReferenceCounted
 
     @VisibleForTesting
     public static EntryImpl create(long ledgerId, long entryId, byte[] data) {
+        return create(ledgerId, entryId, data, 0);
+    }
+
+    @VisibleForTesting
+    public static EntryImpl create(long ledgerId, long entryId, byte[] data, int expectedReadCount) {
         EntryImpl entry = RECYCLER.get();
         entry.ledgerId = ledgerId;
         entry.entryId = entryId;
         entry.data = Unpooled.wrappedBuffer(data);
+        entry.readCountHandler = EntryReadCountHandlerImpl.maybeCreate(expectedReadCount);
         entry.setRefCnt(1);
         return entry;
     }
 
     public static EntryImpl create(long ledgerId, long entryId, ByteBuf data) {
+        return create(ledgerId, entryId, data, 0);
+    }
+
+    public static EntryImpl create(long ledgerId, long entryId, ByteBuf data, int expectedReadCount) {
         EntryImpl entry = RECYCLER.get();
         entry.ledgerId = ledgerId;
         entry.entryId = entryId;
         entry.data = data;
         entry.data.retain();
+        entry.readCountHandler = EntryReadCountHandlerImpl.maybeCreate(expectedReadCount);
         entry.setRefCnt(1);
         return entry;
     }
 
-    public static EntryImpl create(Position position, ByteBuf data) {
+    public static EntryImpl create(Position position, ByteBuf data, int expectedReadCount) {
         EntryImpl entry = RECYCLER.get();
         entry.position = PositionFactory.create(position);
         entry.ledgerId = position.getLedgerId();
         entry.entryId = position.getEntryId();
         entry.data = data;
         entry.data.retain();
+        entry.readCountHandler = EntryReadCountHandlerImpl.maybeCreate(expectedReadCount);
+        entry.setRefCnt(1);
+        return entry;
+    }
+
+    public static EntryImpl createWithRetainedDuplicate(Position position, ByteBuf data, int expectedReadCount) {
+        EntryImpl entry = RECYCLER.get();
+        entry.position = PositionFactory.create(position);
+        entry.ledgerId = position.getLedgerId();
+        entry.entryId = position.getEntryId();
+        entry.data = data.retainedDuplicate();
+        entry.readCountHandler = EntryReadCountHandlerImpl.maybeCreate(expectedReadCount);
         entry.setRefCnt(1);
         return entry;
     }
 
-    public static EntryImpl createWithRetainedDuplicate(Position position, ByteBuf data) {
+    public static EntryImpl createWithRetainedDuplicate(Position position, ByteBuf data,
+                                                        EntryReadCountHandler entryReadCountHandler) {
         EntryImpl entry = RECYCLER.get();
         entry.position = PositionFactory.create(position);
         entry.ledgerId = position.getLedgerId();
         entry.entryId = position.getEntryId();
         entry.data = data.retainedDuplicate();
+        entry.readCountHandler = entryReadCountHandler;
         entry.setRefCnt(1);
         return entry;
     }
@@ -130,6 +160,7 @@ public final class EntryImpl extends AbstractCASReferenceCounted
         entry.ledgerId = other.ledgerId;
         entry.entryId = other.entryId;
         entry.data = other.data.retainedDuplicate();
+        entry.readCountHandler = other.readCountHandler;
         entry.setRefCnt(1);
         return entry;
     }
@@ -140,6 +171,7 @@ public final class EntryImpl extends AbstractCASReferenceCounted
         entry.ledgerId = other.getLedgerId();
         entry.entryId = other.getEntryId();
         entry.data = other.getDataBuffer().retainedDuplicate();
+        entry.readCountHandler = other.getReadCountHandler();
         entry.setRefCnt(1);
         return entry;
     }
@@ -227,6 +259,9 @@ public final class EntryImpl extends AbstractCASReferenceCounted
 
     @Override
     protected void deallocate() {
+        if (decreaseReadCountOnRelease && readCountHandler != null) {
+            readCountHandler.markRead();
+        }
         // This method is called whenever the ref-count of the EntryImpl reaches 0, so that now we can recycle it
         if (onDeallocate != null) {
             try {
@@ -240,6 +275,8 @@ public final class EntryImpl extends AbstractCASReferenceCounted
         ledgerId = -1;
         entryId = -1;
         position = null;
+        readCountHandler = null;
+        decreaseReadCountOnRelease = true;
         recyclerHandle.recycle(this);
     }
 
@@ -248,6 +285,15 @@ public final class EntryImpl extends AbstractCASReferenceCounted
         return key != null && key.compareTo(ledgerId, entryId) == 0;
     }
 
+    @Override
+    public EntryReadCountHandler getReadCountHandler() {
+        return readCountHandler;
+    }
+
+    public void setDecreaseReadCountOnRelease(boolean enabled) {
+        decreaseReadCountOnRelease = enabled;
+    }
+
     @Override
     public String toString() {
         return getClass().getName() + "@" + System.identityHashCode(this)
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/EntryReadCountHandlerImpl.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/EntryReadCountHandlerImpl.java
new file mode 100644
index 00000000000..c7b60b03321
--- /dev/null
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/EntryReadCountHandlerImpl.java
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.bookkeeper.mledger.impl;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
+import lombok.ToString;
+import org.apache.bookkeeper.mledger.EntryReadCountHandler;
+
+@ToString
+public class EntryReadCountHandlerImpl implements EntryReadCountHandler {
+    private static final AtomicIntegerFieldUpdater<EntryReadCountHandlerImpl> EXPECTED_READ_COUNT_UPDATER =
+            AtomicIntegerFieldUpdater.newUpdater(EntryReadCountHandlerImpl.class, "expectedReadCount");
+
+    private volatile int expectedReadCount;
+
+    private EntryReadCountHandlerImpl(int expectedReadCount) {
+        this.expectedReadCount = expectedReadCount;
+    }
+
+    public int getExpectedReadCount() {
+        return expectedReadCount;
+    }
+
+    @Override
+    public void incrementExpectedReadCount() {
+        EXPECTED_READ_COUNT_UPDATER.incrementAndGet(this);
+    }
+
+    @Override
+    public void markRead() {
+        EXPECTED_READ_COUNT_UPDATER.decrementAndGet(this);
+    }
+
+    /**
+     * Creates an instance of EntryReadCountHandlerImpl if the expected read count is greater than 0.
+     * If the expected read count is 0 or less, it returns null.
+     *
+     * @param expectedReadCount the expected read count for the entry
+     * @return an instance of EntryReadCountHandlerImpl or null
+     */
+    public static EntryReadCountHandlerImpl maybeCreate(int expectedReadCount) {
+        return expectedReadCount > 0 ? new EntryReadCountHandlerImpl(expectedReadCount) : null;
+    }
+
+    @VisibleForTesting
+    public void setExpectedReadCount(int expectedReadCount) {
+        this.expectedReadCount = expectedReadCount;
+    }
+}
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorContainer.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorContainer.java
index ba901ece51c..cc740f5d37c 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorContainer.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorContainer.java
@@ -18,13 +18,6 @@
  */
 package org.apache.bookkeeper.mledger.impl;
 
-import static java.util.Objects.requireNonNull;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.ConcurrentSkipListMap;
-import java.util.concurrent.locks.StampedLock;
 import lombok.Value;
 import lombok.experimental.UtilityClass;
 import org.apache.bookkeeper.mledger.ManagedCursor;
@@ -34,25 +27,94 @@ import org.apache.commons.lang3.tuple.Pair;
 /**
  * Contains cursors for a ManagedLedger.
  * <p>
- * The goal is to always know the slowest consumer and hence decide which is the oldest ledger we need to keep.
- * <p>
- * This data structure maintains a heap and a map of cursors. The map is used to relate a cursor name with
- * an entry index in the heap. The heap data structure sorts cursors in a binary tree which is represented
- * in a single array. More details about heap implementations:
- * <a href="https://en.wikipedia.org/wiki/Heap_(data_structure)#Implementation">here</a>
- * <p>
- * The heap is updated and kept sorted when a cursor is updated.
- *
+ * The goal is to be able to find out the slowest cursor and hence decide which is the oldest ledger we need to keep.
  */
-public class ManagedCursorContainer implements Iterable<ManagedCursor> {
+public interface ManagedCursorContainer extends Iterable<ManagedCursor> {
+    /**
+     * Adds a cursor to the container with the specified position.
+     *
+     * @param cursor    The cursor to add
+     * @param position  The position of the cursor, if null, the cursor won't be tracked in the slowest cursor
+     *                 tracking.
+     */
+    void add(ManagedCursor cursor, Position position);
+
+    /**
+     * Gets a cursor by its name.
+     *
+     * @param name the name of the cursor
+     * @return the ManagedCursor if found, otherwise null
+     */
+    ManagedCursor get(String name);
+
+    /**
+     * Removes a cursor from the container by its name.
+     *
+     * @param name the name of the cursor to remove
+     * @return true if the cursor was removed, false if it was not found
+     */
+    boolean removeCursor(String name);
+
+    /**
+     * Signal that a cursor position has been updated and that the container must re-order the cursor heap
+     * tracking the slowest cursor and return the previous position of the slowest cursor and the possibly updated
+     * position of the slowest cursor.
+     *
+     * @param cursor the cursor to update the position for
+     * @param newPosition the updated position for the cursor
+     * @return a pair of positions, representing the previous slowest cursor and the new slowest cursor (after the
+     *         update).
+     */
+    Pair<Position, Position> cursorUpdated(ManagedCursor cursor, Position newPosition);
 
     /**
-     * This field is incremented everytime the cursor information is updated.
+     * Gets the position of the slowest cursor.
+     *
+     * @return the position of the slowest cursor
+     */
+    Position getSlowestCursorPosition();
+
+    /**
+     * Gets the slowest cursor.
+     *
+     * @return the slowest ManagedCursor
+     */
+    ManagedCursor getSlowestCursor();
+
+    /**
+     * Gets the cursor's {@link CursorInfo} with the oldest position.
+     *
+     * @return the CursorInfo containing the cursor and its position
+     */
+    CursorInfo getCursorWithOldestPosition();
+
+    /**
+     * Checks if the container is empty.
+     *
+     * @return true if the container has no cursors, false otherwise
+     */
+    boolean isEmpty();
+
+    /**
+     * Gets the number of cursors in the container.
+     *
+     * @return the number of cursors
+     */
+    int size();
+
+    /**
+     * Checks if the container has any durable cursors.
+     *
+     * @return true if there are durable cursors, false otherwise
      */
-    private long version;
+    boolean hasDurableCursors();
 
+    /**
+     * Information about a cursor and its position and the version of the cursor info.
+     * Any update to any cursor in the container will increment the version of the container.
+     */
     @Value
-    public static class CursorInfo {
+    class CursorInfo {
         ManagedCursor cursor;
         Position position;
 
@@ -65,23 +127,11 @@ public class ManagedCursorContainer implements Iterable<ManagedCursor> {
         long version;
     }
 
-    private static class Item {
-        final ManagedCursor cursor;
-        Position position;
-        int idx;
-
-        Item(ManagedCursor cursor, Position position, int idx) {
-            this.cursor = cursor;
-            this.position = position;
-            this.idx = idx;
-        }
-    }
-
     /**
      * Utility class to manage a data version, which rolls over to 0 when reaching Long.MAX_VALUE.
      */
     @UtilityClass
-    public class DataVersion {
+    class DataVersion {
 
         /**
          * Compares two data versions, which either rolls overs to 0 when reaching Long.MAX_VALUE.
@@ -93,11 +143,12 @@ public class ManagedCursorContainer implements Iterable<ManagedCursor> {
          * to know which one is a later version in case the furthest rolls over to beyond 0. We
          * assume the shortest distance between them dictates that.
          * <p>
+         *
          * @param v1 First version to compare
          * @param v2 Second version to compare
          * @return the value {@code 0} if {@code v1 == v2};
-         *         a value less than {@code 0} if {@code v1 < v2}; and
-         *         a value greater than {@code 0} if {@code v1 > v2}
+         * a value less than {@code 0} if {@code v1 < v2}; and
+         * a value greater than {@code 0} if {@code v1 > v2}
          */
         public static int compareVersions(long v1, long v2) {
             if (v1 == v2) {
@@ -114,7 +165,7 @@ public class ManagedCursorContainer implements Iterable<ManagedCursor> {
                     return 1;
                 }
 
-            // 0-------v2--------v1--------MAX_LONG
+                // 0-------v2--------v1--------MAX_LONG
             } else {
                 long distance = v1 - v2;
                 long wrapAroundDistance = (Long.MAX_VALUE - v1) + v2;
@@ -134,327 +185,4 @@ public class ManagedCursorContainer implements Iterable<ManagedCursor> {
             }
         }
     }
-
-    public ManagedCursorContainer() {}
-
-    // Used to keep track of slowest cursor.
-    private final ArrayList<Item> heap = new ArrayList<>();
-
-    // Maps a cursor to its position in the heap
-    private final ConcurrentMap<String, Item> cursors = new ConcurrentSkipListMap<>();
-
-    private final StampedLock rwLock = new StampedLock();
-
-    private int durableCursorCount;
-
-
-    /**
-     * Add a cursor to the container. The cursor will be optionally tracked for the slowest reader when
-     * a position is passed as the second argument. It is expected that the position is updated with
-     * {@link #cursorUpdated(ManagedCursor, Position)} method when the position changes.
-     *
-     * @param cursor cursor to add
-     * @param position position of the cursor to use for ordering, pass null if the cursor's position shouldn't be
-     *                 tracked for the slowest reader.
-     */
-    public void add(ManagedCursor cursor, Position position) {
-        long stamp = rwLock.writeLock();
-        try {
-            Item item = new Item(cursor, position, position != null ? heap.size() : -1);
-            cursors.put(cursor.getName(), item);
-            if (position != null) {
-                heap.add(item);
-                if (heap.size() > 1) {
-                    siftUp(item);
-                }
-            }
-            if (cursor.isDurable()) {
-                durableCursorCount++;
-            }
-            version = DataVersion.getNextVersion(version);
-        } finally {
-            rwLock.unlockWrite(stamp);
-        }
-    }
-
-    public ManagedCursor get(String name) {
-        long stamp = rwLock.readLock();
-        try {
-            Item item = cursors.get(name);
-            return item != null ? item.cursor : null;
-        } finally {
-            rwLock.unlockRead(stamp);
-        }
-    }
-
-    public boolean removeCursor(String name) {
-        long stamp = rwLock.writeLock();
-        try {
-            Item item = cursors.remove(name);
-            if (item != null) {
-                if (item.idx >= 0) {
-                    if (heap.size() == 1) {
-                        heap.clear();
-                    } else {
-                        // Move the item to the right end of the heap to be removed
-                        Item lastItem = heap.get(heap.size() - 1);
-                        swap(item, lastItem);
-                        heap.remove(item.idx);
-                        // Update the heap
-                        siftDown(lastItem);
-                    }
-                }
-                if (item.cursor.isDurable()) {
-                    durableCursorCount--;
-                }
-                version = DataVersion.getNextVersion(version);
-                return true;
-            } else {
-                return false;
-            }
-        } finally {
-            rwLock.unlockWrite(stamp);
-        }
-    }
-
-    /**
-     * Signal that a cursor position has been updated and that the container must re-order the cursor heap
-     * tracking the slowest reader.
-     * Only those cursors are tracked and can be updated which were added to the container with the
-     * {@link #add(ManagedCursor, Position)} method that specified the initial position in the position
-     * parameter.
-     *
-     * @param cursor the cursor to update the position for
-     * @param newPosition the updated position for the cursor
-     * @return a pair of positions, representing the previous slowest reader and the new slowest reader (after the
-     *         update).
-     */
-    public Pair<Position, Position> cursorUpdated(ManagedCursor cursor, Position newPosition) {
-        requireNonNull(cursor);
-
-        long stamp = rwLock.writeLock();
-        try {
-            Item item = cursors.get(cursor.getName());
-            if (item == null || item.idx == -1) {
-                return null;
-            }
-
-            Position previousSlowestConsumer = heap.get(0).position;
-            item.position = newPosition;
-            version = DataVersion.getNextVersion(version);
-
-            if (heap.size() == 1) {
-                return Pair.of(previousSlowestConsumer, item.position);
-            }
-
-            // When the cursor moves forward, we need to push it toward the
-            // bottom of the tree and push it up if a reset was done
-            if (item.idx == 0 || getParent(item).position.compareTo(item.position) <= 0) {
-                siftDown(item);
-            } else {
-                siftUp(item);
-            }
-            Position newSlowestConsumer = heap.get(0).position;
-            return Pair.of(previousSlowestConsumer, newSlowestConsumer);
-        } finally {
-            rwLock.unlockWrite(stamp);
-        }
-    }
-
-    /**
-     * Get the slowest reader position for the cursors that are ordered.
-     *
-     * @return the slowest reader position
-     */
-    public Position getSlowestReaderPosition() {
-        long stamp = rwLock.readLock();
-        try {
-            return heap.isEmpty() ? null : heap.get(0).position;
-        } finally {
-            rwLock.unlockRead(stamp);
-        }
-    }
-
-    public ManagedCursor getSlowestReader() {
-        long stamp = rwLock.readLock();
-        try {
-            return heap.isEmpty() ? null : heap.get(0).cursor;
-        } finally {
-            rwLock.unlockRead(stamp);
-        }
-    }
-
-    /**
-     * @return Returns the CursorInfo for the cursor with the oldest position,
-     *         or null if there aren't any tracked cursors
-     */
-    public CursorInfo getCursorWithOldestPosition() {
-        long stamp = rwLock.readLock();
-        try {
-            if (heap.isEmpty()) {
-                return null;
-            } else {
-                Item item = heap.get(0);
-                return new CursorInfo(item.cursor, item.position, version);
-            }
-        } finally {
-            rwLock.unlockRead(stamp);
-        }
-    }
-
-    /**
-     *  Check whether there are any cursors.
-     * @return true is there are no cursors and false if there are
-     */
-    public boolean isEmpty() {
-        long stamp = rwLock.tryOptimisticRead();
-        boolean isEmpty = cursors.isEmpty();
-        if (!rwLock.validate(stamp)) {
-            // Fallback to read lock
-            stamp = rwLock.readLock();
-            try {
-                isEmpty = cursors.isEmpty();
-            } finally {
-                rwLock.unlockRead(stamp);
-            }
-        }
-
-        return isEmpty;
-    }
-
-    /**
-     * Check whether that are any durable cursors.
-     * @return true if there are durable cursors and false if there are not
-     */
-    public boolean hasDurableCursors() {
-        long stamp = rwLock.tryOptimisticRead();
-        int count = durableCursorCount;
-        if (!rwLock.validate(stamp)) {
-            // Fallback to read lock
-            stamp = rwLock.readLock();
-            try {
-                count = durableCursorCount;
-            } finally {
-                rwLock.unlockRead(stamp);
-            }
-        }
-
-        return count > 0;
-    }
-
-    @Override
-    public String toString() {
-        long stamp = rwLock.readLock();
-        try {
-            StringBuilder sb = new StringBuilder();
-            sb.append('[');
-
-            boolean first = true;
-            for (Item item : cursors.values()) {
-                if (!first) {
-                    sb.append(", ");
-                }
-
-                first = false;
-                sb.append(item.cursor);
-            }
-
-            sb.append(']');
-            return sb.toString();
-        } finally {
-            rwLock.unlockRead(stamp);
-        }
-    }
-
-    @Override
-    public Iterator<ManagedCursor> iterator() {
-        final Iterator<Map.Entry<String, Item>> it = cursors.entrySet().iterator();
-        return new Iterator<ManagedCursor>() {
-            @Override
-            public boolean hasNext() {
-                return it.hasNext();
-            }
-
-            @Override
-            public ManagedCursor next() {
-                return it.next().getValue().cursor;
-            }
-
-            @Override
-            public void remove() {
-                throw new IllegalArgumentException("Cannot remove ManagedCursor from container");
-            }
-        };
-    }
-
-    // //////////////////////
-
-    /**
-     * Push the item up towards the root of the tree (the lowest reading position).
-     */
-    private void siftUp(Item item) {
-        Item parent = getParent(item);
-        while (item.idx > 0 && parent.position.compareTo(item.position) > 0) {
-            swap(item, parent);
-            parent = getParent(item);
-        }
-    }
-
-    /**
-     * Push the item down towards the bottom of the tree (the highest reading position).
-     */
-    private void siftDown(final Item item) {
-        while (true) {
-            Item j = null;
-            Item right = getRight(item);
-            if (right != null && right.position.compareTo(item.position) < 0) {
-                Item left = getLeft(item);
-                if (left != null && left.position.compareTo(right.position) < 0) {
-                    j = left;
-                } else {
-                    j = right;
-                }
-            } else {
-                Item left = getLeft(item);
-                if (left != null && left.position.compareTo(item.position) < 0) {
-                    j = left;
-                }
-            }
-
-            if (j != null) {
-                swap(item, j);
-            } else {
-                break;
-            }
-        }
-    }
-
-    /**
-     * Swap two items in the heap.
-     */
-    private void swap(Item item1, Item item2) {
-        int idx1 = item1.idx;
-        int idx2 = item2.idx;
-
-        heap.set(idx2, item1);
-        heap.set(idx1, item2);
-
-        // Update the indexes too
-        item1.idx = idx2;
-        item2.idx = idx1;
-    }
-
-    private Item getParent(Item item) {
-        return heap.get((item.idx - 1) / 2);
-    }
-
-    private Item getLeft(Item item) {
-        int i = item.idx * 2 + 1;
-        return i < heap.size() ? heap.get(i) : null;
-    }
-
-    private Item getRight(Item item) {
-        int i = item.idx * 2 + 2;
-        return i < heap.size() ? heap.get(i) : null;
-    }
 }
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorContainer.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorContainerImpl.java
similarity index 75%
copy from managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorContainer.java
copy to managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorContainerImpl.java
index ba901ece51c..b632ee7f21c 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorContainer.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorContainerImpl.java
@@ -25,16 +25,13 @@ import java.util.Map;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.ConcurrentSkipListMap;
 import java.util.concurrent.locks.StampedLock;
-import lombok.Value;
-import lombok.experimental.UtilityClass;
 import org.apache.bookkeeper.mledger.ManagedCursor;
 import org.apache.bookkeeper.mledger.Position;
 import org.apache.commons.lang3.tuple.Pair;
 
 /**
- * Contains cursors for a ManagedLedger.
- * <p>
- * The goal is to always know the slowest consumer and hence decide which is the oldest ledger we need to keep.
+ * Implementation of {@link ManagedCursorContainer} that contains cursors for a ManagedLedger.
+ * This also implements {@link ActiveManagedCursorContainer} to contain active cursors.
  * <p>
  * This data structure maintains a heap and a map of cursors. The map is used to relate a cursor name with
  * an entry index in the heap. The heap data structure sorts cursors in a binary tree which is represented
@@ -42,29 +39,14 @@ import org.apache.commons.lang3.tuple.Pair;
  * <a href="https://en.wikipedia.org/wiki/Heap_(data_structure)#Implementation">here</a>
  * <p>
  * The heap is updated and kept sorted when a cursor is updated.
- *
  */
-public class ManagedCursorContainer implements Iterable<ManagedCursor> {
+public class ManagedCursorContainerImpl implements ManagedCursorContainer, ActiveManagedCursorContainer {
 
     /**
      * This field is incremented everytime the cursor information is updated.
      */
     private long version;
 
-    @Value
-    public static class CursorInfo {
-        ManagedCursor cursor;
-        Position position;
-
-        /**
-         * Cursor info's version.
-         * <p>
-         * Use {@link  DataVersion#compareVersions(long, long)} to compare between two versions,
-         * since it rolls over to 0 once reaching Long.MAX_VALUE
-         */
-        long version;
-    }
-
     private static class Item {
         final ManagedCursor cursor;
         Position position;
@@ -77,65 +59,7 @@ public class ManagedCursorContainer implements Iterable<ManagedCursor> {
         }
     }
 
-    /**
-     * Utility class to manage a data version, which rolls over to 0 when reaching Long.MAX_VALUE.
-     */
-    @UtilityClass
-    public class DataVersion {
-
-        /**
-         * Compares two data versions, which either rolls overs to 0 when reaching Long.MAX_VALUE.
-         * <p>
-         * Use {@link DataVersion#getNextVersion(long)} to increment the versions. The assumptions
-         * are that metric versions are compared with close time proximity one to another, hence,
-         * they are expected not close to each other in terms of distance, hence we don't
-         * expect the distance ever to exceed Long.MAX_VALUE / 2, otherwise we wouldn't be able
-         * to know which one is a later version in case the furthest rolls over to beyond 0. We
-         * assume the shortest distance between them dictates that.
-         * <p>
-         * @param v1 First version to compare
-         * @param v2 Second version to compare
-         * @return the value {@code 0} if {@code v1 == v2};
-         *         a value less than {@code 0} if {@code v1 < v2}; and
-         *         a value greater than {@code 0} if {@code v1 > v2}
-         */
-        public static int compareVersions(long v1, long v2) {
-            if (v1 == v2) {
-                return 0;
-            }
-
-            // 0-------v1--------v2--------MAX_LONG
-            if (v2 > v1) {
-                long distance = v2 - v1;
-                long wrapAroundDistance = (Long.MAX_VALUE - v2) + v1;
-                if (distance < wrapAroundDistance) {
-                    return -1;
-                } else {
-                    return 1;
-                }
-
-            // 0-------v2--------v1--------MAX_LONG
-            } else {
-                long distance = v1 - v2;
-                long wrapAroundDistance = (Long.MAX_VALUE - v1) + v2;
-                if (distance < wrapAroundDistance) {
-                    return 1; // v1 is bigger
-                } else {
-                    return -1; // v2 is bigger
-                }
-            }
-        }
-
-        public static long getNextVersion(long existingVersion) {
-            if (existingVersion == Long.MAX_VALUE) {
-                return 0;
-            } else {
-                return existingVersion + 1;
-            }
-        }
-    }
-
-    public ManagedCursorContainer() {}
+    public ManagedCursorContainerImpl() {}
 
     // Used to keep track of slowest cursor.
     private final ArrayList<Item> heap = new ArrayList<>();
@@ -157,6 +81,7 @@ public class ManagedCursorContainer implements Iterable<ManagedCursor> {
      * @param position position of the cursor to use for ordering, pass null if the cursor's position shouldn't be
      *                 tracked for the slowest reader.
      */
+    @Override
     public void add(ManagedCursor cursor, Position position) {
         long stamp = rwLock.writeLock();
         try {
@@ -177,6 +102,7 @@ public class ManagedCursorContainer implements Iterable<ManagedCursor> {
         }
     }
 
+    @Override
     public ManagedCursor get(String name) {
         long stamp = rwLock.readLock();
         try {
@@ -187,6 +113,7 @@ public class ManagedCursorContainer implements Iterable<ManagedCursor> {
         }
     }
 
+    @Override
     public boolean removeCursor(String name) {
         long stamp = rwLock.writeLock();
         try {
@@ -217,18 +144,8 @@ public class ManagedCursorContainer implements Iterable<ManagedCursor> {
         }
     }
 
-    /**
-     * Signal that a cursor position has been updated and that the container must re-order the cursor heap
-     * tracking the slowest reader.
-     * Only those cursors are tracked and can be updated which were added to the container with the
-     * {@link #add(ManagedCursor, Position)} method that specified the initial position in the position
-     * parameter.
-     *
-     * @param cursor the cursor to update the position for
-     * @param newPosition the updated position for the cursor
-     * @return a pair of positions, representing the previous slowest reader and the new slowest reader (after the
-     *         update).
-     */
+
+    @Override
     public Pair<Position, Position> cursorUpdated(ManagedCursor cursor, Position newPosition) {
         requireNonNull(cursor);
 
@@ -261,12 +178,18 @@ public class ManagedCursorContainer implements Iterable<ManagedCursor> {
         }
     }
 
+    @Override
+    public void updateCursor(ManagedCursor cursor, Position newPosition) {
+        cursorUpdated(cursor, newPosition);
+    }
+
     /**
      * Get the slowest reader position for the cursors that are ordered.
      *
      * @return the slowest reader position
      */
-    public Position getSlowestReaderPosition() {
+    @Override
+    public Position getSlowestCursorPosition() {
         long stamp = rwLock.readLock();
         try {
             return heap.isEmpty() ? null : heap.get(0).position;
@@ -275,7 +198,8 @@ public class ManagedCursorContainer implements Iterable<ManagedCursor> {
         }
     }
 
-    public ManagedCursor getSlowestReader() {
+    @Override
+    public ManagedCursor getSlowestCursor() {
         long stamp = rwLock.readLock();
         try {
             return heap.isEmpty() ? null : heap.get(0).cursor;
@@ -288,6 +212,7 @@ public class ManagedCursorContainer implements Iterable<ManagedCursor> {
      * @return Returns the CursorInfo for the cursor with the oldest position,
      *         or null if there aren't any tracked cursors
      */
+    @Override
     public CursorInfo getCursorWithOldestPosition() {
         long stamp = rwLock.readLock();
         try {
@@ -303,9 +228,10 @@ public class ManagedCursorContainer implements Iterable<ManagedCursor> {
     }
 
     /**
-     *  Check whether there are any cursors.
+     * Check whether there are any cursors.
      * @return true is there are no cursors and false if there are
      */
+    @Override
     public boolean isEmpty() {
         long stamp = rwLock.tryOptimisticRead();
         boolean isEmpty = cursors.isEmpty();
@@ -322,10 +248,27 @@ public class ManagedCursorContainer implements Iterable<ManagedCursor> {
         return isEmpty;
     }
 
+    @Override
+    public int size() {
+        long stamp = rwLock.tryOptimisticRead();
+        int size = cursors.size();
+        if (!rwLock.validate(stamp)) {
+            // Fallback to read lock
+            stamp = rwLock.readLock();
+            try {
+                size = cursors.size();
+            } finally {
+                rwLock.unlockRead(stamp);
+            }
+        }
+        return size;
+    }
+
     /**
-     * Check whether that are any durable cursors.
+     * Check whether there are any durable cursors.
      * @return true if there are durable cursors and false if there are not
      */
+    @Override
     public boolean hasDurableCursors() {
         long stamp = rwLock.tryOptimisticRead();
         int count = durableCursorCount;
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorImpl.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorImpl.java
index 2a0c5d06e4b..f74e834e8be 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorImpl.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorImpl.java
@@ -103,6 +103,7 @@ import org.apache.bookkeeper.mledger.proto.MLDataFormats.MessageRange;
 import org.apache.bookkeeper.mledger.proto.MLDataFormats.PositionInfo;
 import org.apache.bookkeeper.mledger.proto.MLDataFormats.PositionInfo.Builder;
 import org.apache.bookkeeper.mledger.proto.MLDataFormats.StringProperty;
+import org.apache.commons.lang3.mutable.MutableBoolean;
 import org.apache.commons.lang3.mutable.MutableInt;
 import org.apache.commons.lang3.tuple.Pair;
 import org.apache.pulsar.common.policies.data.ManagedLedgerInternalStats;
@@ -2074,7 +2075,8 @@ public class ManagedCursorImpl implements ManagedCursor {
         markDeletePosition = newMarkDeletePosition;
         individualDeletedMessages.removeAtMost(markDeletePosition.getLedgerId(), markDeletePosition.getEntryId());
 
-        READ_POSITION_UPDATER.updateAndGet(this, currentReadPosition -> {
+        MutableBoolean readPositionUpdated = new MutableBoolean(false);
+        Position updatedReadPosition = READ_POSITION_UPDATER.updateAndGet(this, currentReadPosition -> {
             if (currentReadPosition.compareTo(markDeletePosition) <= 0) {
                 // If the position that is mark-deleted is past the read position, it
                 // means that the client has skipped some entries. We need to move
@@ -2084,12 +2086,15 @@ public class ManagedCursorImpl implements ManagedCursor {
                     log.debug("[{}] Moved read position from: {} to: {}, and new mark-delete position {}",
                             ledger.getName(), currentReadPosition, newReadPosition, markDeletePosition);
                 }
-                ledger.onCursorReadPositionUpdated(ManagedCursorImpl.this, newReadPosition);
+                readPositionUpdated.setTrue();
                 return newReadPosition;
             } else {
                 return currentReadPosition;
             }
         });
+        if (readPositionUpdated.booleanValue()) {
+            ledger.onCursorReadPositionUpdated(this, updatedReadPosition);
+        }
 
         return newMarkDeletePosition;
     }
@@ -4086,4 +4091,20 @@ public class ManagedCursorImpl implements ManagedCursor {
             registeredToWaitingCursors = true;
         }
     }
+
+    /**
+     * When cache eviction by expected read count is enabled, this method returns the number of cursors
+     * that are at the same position or before this cursor.
+     *
+     * @return the number of cursors at the same position or before
+     */
+    public int getNumberOfCursorsAtSamePositionOrBefore() {
+        if (ledger.getConfig().isCacheEvictionByExpectedReadCount()) {
+            return ledger.getNumberOfCursorsAtSamePositionOrBefore(this);
+        } else if (isCacheReadEntry()) {
+            return 1;
+        } else {
+            return 0;
+        }
+    }
 }
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerFactoryImpl.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerFactoryImpl.java
index e31db8bac46..8a4f0fa3a3c 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerFactoryImpl.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerFactoryImpl.java
@@ -319,8 +319,7 @@ public class ManagedLedgerFactoryImpl implements ManagedLedgerFactory {
 
     @VisibleForTesting
     public synchronized void doCacheEviction() {
-        long maxTimestamp = System.nanoTime() - cacheEvictionTimeThresholdNanos;
-        entryCacheManager.doCacheEviction(maxTimestamp);
+        entryCacheManager.doCacheEviction();
     }
 
     /**
@@ -1152,6 +1151,18 @@ public class ManagedLedgerFactoryImpl implements ManagedLedgerFactory {
         return cacheEvictionTimeThresholdNanos;
     }
 
+    @Override
+    public void updateCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes(
+            int extendTTLOfEntriesWithRemainingExpectedReadsMaxTimes) {
+        entryCacheManager.updateCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes(
+                extendTTLOfEntriesWithRemainingExpectedReadsMaxTimes);
+    }
+
+    @Override
+    public void updateCacheEvictionExtendTTLOfRecentlyAccessed(boolean cacheEvictionExtendTTLOfRecentlyAccessed) {
+        entryCacheManager.updateCacheEvictionExtendTTLOfRecentlyAccessed(cacheEvictionExtendTTLOfRecentlyAccessed);
+    }
+
     @Override
     public ManagedLedgerFactoryMXBean getCacheStats() {
         return this.mbean;
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java
index 895984ab470..0be740e1bda 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java
@@ -65,6 +65,7 @@ import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLongFieldUpdater;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
+import java.util.function.IntSupplier;
 import java.util.function.Predicate;
 import java.util.function.Supplier;
 import java.util.stream.Collectors;
@@ -178,11 +179,11 @@ public class ManagedLedgerImpl implements ManagedLedger, CreateCallback {
     protected volatile Stat ledgersStat;
 
     // contains all cursors, where durable cursors are ordered by mark delete position
-    private final ManagedCursorContainer cursors = new ManagedCursorContainer();
+    private final ManagedCursorContainer cursors = new ManagedCursorContainerImpl();
     // contains active cursors eligible for caching,
     // ordered by read position (when cacheEvictionByMarkDeletedPosition=false) or by mark delete position
     // (when cacheEvictionByMarkDeletedPosition=true)
-    private final ManagedCursorContainer activeCursors = new ManagedCursorContainer();
+    private final ActiveManagedCursorContainer activeCursors;
 
 
     // Ever-increasing counter of entries added
@@ -359,6 +360,12 @@ public class ManagedLedgerImpl implements ManagedLedger, CreateCallback {
     public ManagedLedgerImpl(ManagedLedgerFactoryImpl factory, BookKeeper bookKeeper, MetaStore store,
             ManagedLedgerConfig config, OrderedScheduler scheduledExecutor,
             final String name, final Supplier<CompletableFuture<Boolean>> mlOwnershipChecker) {
+        if (config.isCacheEvictionByExpectedReadCount()) {
+            activeCursors = new ActiveManagedCursorContainerImpl(
+                    config.getContinueCachingAddedEntriesAfterLastActiveCursorLeavesMillis());
+        } else {
+            activeCursors = new ManagedCursorContainerImpl();
+        }
         this.factory = factory;
         this.bookKeeper = bookKeeper;
         this.config = config;
@@ -506,7 +513,7 @@ public class ManagedLedgerImpl implements ManagedLedger, CreateCallback {
                                                 entries.getEntry(lh.getLastAddConfirmed());
                                         if (ledgerEntry != null) {
                                             promise.complete(
-                                                    Optional.of(EntryImpl.create(ledgerEntry)));
+                                                    Optional.of(EntryImpl.create(ledgerEntry, 0)));
                                         } else {
                                             promise.complete(Optional.empty());
                                         }
@@ -1202,7 +1209,7 @@ public class ManagedLedgerImpl implements ManagedLedger, CreateCallback {
     }
 
     @Override
-    public ManagedCursorContainer getActiveCursors() {
+    public ActiveManagedCursorContainer getActiveCursors() {
         return activeCursors;
     }
 
@@ -1224,7 +1231,7 @@ public class ManagedLedgerImpl implements ManagedLedger, CreateCallback {
     @Override
     public long getNumberOfActiveEntries() {
         long totalEntries = getNumberOfEntries();
-        Position pos = cursors.getSlowestReaderPosition();
+        Position pos = cursors.getSlowestCursorPosition();
         if (pos == null) {
             // If there are no consumers, there are no active entries
             return 0;
@@ -2372,7 +2379,7 @@ public class ManagedLedgerImpl implements ManagedLedger, CreateCallback {
 
     protected void asyncReadEntry(ReadHandle ledger, long firstEntry, long lastEntry, OpReadEntry opReadEntry,
             Object ctx) {
-        boolean shouldCacheEntry = opReadEntry.cursor.isCacheReadEntry();
+        IntSupplier expectedReadCount = opReadEntry.cursor::getNumberOfCursorsAtSamePositionOrBefore;
         if (config.getReadEntryTimeoutSeconds() > 0) {
             // set readOpCount to uniquely validate if ReadEntryCallbackWrapper is already recycled
             long readOpCount = READ_OP_COUNT_UPDATER.incrementAndGet(this);
@@ -2380,9 +2387,9 @@ public class ManagedLedgerImpl implements ManagedLedger, CreateCallback {
             ReadEntryCallbackWrapper readCallback = ReadEntryCallbackWrapper.create(name, ledger.getId(), firstEntry,
                     opReadEntry, readOpCount, createdTime, ctx);
             lastReadCallback = readCallback;
-            entryCache.asyncReadEntry(ledger, firstEntry, lastEntry, shouldCacheEntry, readCallback, readOpCount);
+            entryCache.asyncReadEntry(ledger, firstEntry, lastEntry, expectedReadCount, readCallback, readOpCount);
         } else {
-            entryCache.asyncReadEntry(ledger, firstEntry, lastEntry, shouldCacheEntry, opReadEntry, ctx);
+            entryCache.asyncReadEntry(ledger, firstEntry, lastEntry, expectedReadCount, opReadEntry, ctx);
         }
     }
 
@@ -2544,10 +2551,10 @@ public class ManagedLedgerImpl implements ManagedLedger, CreateCallback {
     // slowest reader position is earliest mark delete position when cacheEvictionByMarkDeletedPosition=true
     // it is the earliest read position when cacheEvictionByMarkDeletedPosition=false
     private void invalidateEntriesUpToSlowestReaderPosition() {
-        if (entryCache.getSize() <= 0) {
+        if (entryCache.getSize() <= 0 || config.isCacheEvictionByExpectedReadCount()) {
             return;
         }
-        Position slowestReaderPosition = activeCursors.getSlowestReaderPosition();
+        Position slowestReaderPosition = activeCursors.getSlowestCursorPosition();
         Position evictionPos = slowestReaderPosition != null ? slowestReaderPosition : PositionFactory.LATEST;
         Position currentEvictionPosition = cacheEvictionPosition.getAndUpdate(currentValue -> {
             if (currentValue == null || currentValue == PositionFactory.LATEST
@@ -2603,10 +2610,13 @@ public class ManagedLedgerImpl implements ManagedLedger, CreateCallback {
     }
 
     private void updateActiveCursor(ManagedCursorImpl cursor, Position newPosition) {
-        Pair<Position, Position> slowestPositions = activeCursors.cursorUpdated(cursor, newPosition);
-        if (slowestPositions != null
-                && !slowestPositions.getLeft().equals(slowestPositions.getRight())) {
-            invalidateEntriesUpToSlowestReaderPosition();
+        if (config.isCacheEvictionByExpectedReadCount()) {
+            activeCursors.updateCursor(cursor, newPosition);
+        } else {
+            Pair<Position, Position> slowestPositions = activeCursors.cursorUpdated(cursor, newPosition);
+            if (slowestPositions != null && !slowestPositions.getLeft().equals(slowestPositions.getRight())) {
+                invalidateEntriesUpToSlowestReaderPosition();
+            }
         }
     }
 
@@ -2936,7 +2946,7 @@ public class ManagedLedgerImpl implements ManagedLedger, CreateCallback {
                 // include lastLedger in the trimming.
                 slowestReaderLedgerId = currentLedger.getId() + 1;
             } else {
-                Position slowestReaderPosition = cursors.getSlowestReaderPosition();
+                Position slowestReaderPosition = cursors.getSlowestCursorPosition();
                 if (slowestReaderPosition != null) {
                     // The slowest reader position is the mark delete position.
                     // If the slowest reader position point the last entry in the ledger x,
@@ -4055,7 +4065,7 @@ public class ManagedLedgerImpl implements ManagedLedger, CreateCallback {
 
     @Override
     public ManagedCursor getSlowestConsumer() {
-        return cursors.getSlowestReader();
+        return cursors.getSlowestCursor();
     }
 
     Position getMarkDeletePositionOfSlowestConsumer() {
@@ -4124,6 +4134,11 @@ public class ManagedLedgerImpl implements ManagedLedger, CreateCallback {
         }
     }
 
+    public int getNumberOfCursorsAtSamePositionOrBefore(ManagedCursor cursor) {
+        return activeCursors.getNumberOfCursorsAtSamePositionOrBefore(cursor);
+    }
+
+
     public void removeWaitingCursor(ManagedCursor cursor) {
         ((ManagedCursorImpl) cursor).removeWaitingCursorRequested(() -> {
             // remove only if the cursor has been registered
@@ -4890,7 +4905,7 @@ public class ManagedLedgerImpl implements ManagedLedger, CreateCallback {
 
 
     public void checkCursorsToCacheEntries() {
-        if (minBacklogCursorsForCaching < 1) {
+        if (minBacklogCursorsForCaching < 1 || config.isCacheEvictionByExpectedReadCount()) {
             return;
         }
         Iterator<ManagedCursor> it = cursors.iterator();
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/NonDurableCursorImpl.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/NonDurableCursorImpl.java
index c2398aaf6ec..d1fc54a9d6d 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/NonDurableCursorImpl.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/NonDurableCursorImpl.java
@@ -71,6 +71,7 @@ public class NonDurableCursorImpl extends ManagedCursorImpl {
     private void recoverCursor(Position mdPosition) {
         Pair<Position, Long> lastEntryAndCounter = ledger.getLastPositionAndCounter();
         this.readPosition = isReadCompacted() ? mdPosition.getNext() : ledger.getNextValidPosition(mdPosition);
+        ledger.onCursorReadPositionUpdated(this, readPosition);
         markDeletePosition = ledger.getPreviousPosition(this.readPosition);
 
         // Initialize the counter such that the difference between the messages written on the ML and the
@@ -136,6 +137,7 @@ public class NonDurableCursorImpl extends ManagedCursorImpl {
             super.rewind();
         } else {
             readPosition = markDeletePosition.getNext();
+            ledger.onCursorReadPositionUpdated(this, readPosition);
         }
     }
 
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/OpAddEntry.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/OpAddEntry.java
index 2a361fcebf2..49bc3f309d4 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/OpAddEntry.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/OpAddEntry.java
@@ -259,13 +259,21 @@ public class OpAddEntry implements AddCallback, CloseCallback, Runnable, Managed
 
         long ledgerId = ledger != null ? ledger.getId() : ((Position) ctx).getLedgerId();
         // Don't insert to the entry cache for the ShadowManagedLedger
-        if (!(ml instanceof ShadowManagedLedgerImpl) && ml.hasActiveCursors()) {
+        if (!(ml instanceof ShadowManagedLedgerImpl)) {
             // Avoid caching entries if no cursor has been created
-            EntryImpl entry = EntryImpl.create(ledgerId, entryId, data);
-            // EntryCache.insert: duplicates entry by allocating new entry and data. so, recycle entry after calling
-            // insert
-            ml.entryCache.insert(entry);
-            entry.release();
+            if (ml.getActiveCursors().shouldCacheAddedEntry()) {
+                int expectedReadCount = 0;
+                // only use expectedReadCount if cache eviction is enabled by expected read count
+                if (ml.getConfig().isCacheEvictionByExpectedReadCount()) {
+                    expectedReadCount = ml.getActiveCursors().size();
+                }
+                EntryImpl entry = EntryImpl.create(ledgerId, entryId, data, expectedReadCount);
+                entry.setDecreaseReadCountOnRelease(false);
+                // EntryCache.insert: duplicates entry by allocating new entry and data. so, recycle entry after calling
+                // insert
+                ml.entryCache.insert(entry);
+                entry.release();
+            }
         }
 
         Position lastEntry = PositionFactory.create(ledgerId, entryId);
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ReadOnlyCursorImpl.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ReadOnlyCursorImpl.java
index d79aa5e6145..8c6bbca3384 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ReadOnlyCursorImpl.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ReadOnlyCursorImpl.java
@@ -40,6 +40,7 @@ public class ReadOnlyCursorImpl extends ManagedCursorImpl implements ReadOnlyCur
         } else {
             readPosition = startPosition;
         }
+        ledger.onCursorReadPositionUpdated(this, readPosition);
 
         if (ledger.getLastPosition().compareTo(readPosition) <= 0) {
             messagesConsumedCounter = 0;
@@ -53,8 +54,9 @@ public class ReadOnlyCursorImpl extends ManagedCursorImpl implements ReadOnlyCur
     @Override
     public void skipEntries(int numEntriesToSkip) {
         log.info("[{}] Skipping {} entries on read-only cursor {}", ledger.getName(), numEntriesToSkip, getName());
-        READ_POSITION_UPDATER.getAndUpdate(this, lastRead ->
+        Position updatedReadPosition = READ_POSITION_UPDATER.updateAndGet(this, lastRead ->
                 ledger.getPositionAfterN(lastRead, numEntriesToSkip, PositionBound.startIncluded).getNext());
+        ledger.onCursorReadPositionUpdated(this, updatedReadPosition);
     }
 
     @Override
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCache.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCache.java
index 57228939834..b2ebf743056 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCache.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCache.java
@@ -18,6 +18,7 @@
  */
 package org.apache.bookkeeper.mledger.impl.cache;
 
+import java.util.function.IntSupplier;
 import org.apache.bookkeeper.client.api.ReadHandle;
 import org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback;
 import org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback;
@@ -78,14 +79,14 @@ public interface EntryCache {
      *            the first entry to read (inclusive)
      * @param lastEntry
      *            the last entry to read (inclusive)
-     * @param shouldCacheEntry
-     *            whether the read entry should be cached
+     * @param expectedReadCount resolves the expected read count for the given entry. When the expected read count is
+     *                         >0, the entry can be cached and reused later.
      * @param callback
      *            the callback object that will be notified when read is done
      * @param ctx
      *            the context object
      */
-    void asyncReadEntry(ReadHandle lh, long firstEntry, long lastEntry, boolean shouldCacheEntry,
+    void asyncReadEntry(ReadHandle lh, long firstEntry, long lastEntry, IntSupplier expectedReadCount,
             ReadEntriesCallback callback, Object ctx);
 
     /**
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCacheDisabled.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCacheDisabled.java
index afb8091dcb6..9cd63d99f4c 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCacheDisabled.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCacheDisabled.java
@@ -22,6 +22,7 @@ import static org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.createManaged
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.function.IntSupplier;
 import org.apache.bookkeeper.client.api.LedgerEntry;
 import org.apache.bookkeeper.client.api.ReadHandle;
 import org.apache.bookkeeper.mledger.AsyncCallbacks;
@@ -67,7 +68,7 @@ public class EntryCacheDisabled implements EntryCache {
     }
 
     @Override
-    public void asyncReadEntry(ReadHandle lh, long firstEntry, long lastEntry, boolean shouldCacheEntry,
+    public void asyncReadEntry(ReadHandle lh, long firstEntry, long lastEntry, IntSupplier expectedReadCount,
                                final AsyncCallbacks.ReadEntriesCallback callback, Object ctx) {
         ReadEntryUtils.readAsync(ml, lh, firstEntry, lastEntry).thenAcceptAsync(
                 ledgerEntries -> {
@@ -76,7 +77,7 @@ public class EntryCacheDisabled implements EntryCache {
                     try {
                         for (LedgerEntry e : ledgerEntries) {
                             // Insert the entries at the end of the list (they will be unsorted for now)
-                            EntryImpl entry = EntryImpl.create(e, interceptor);
+                            EntryImpl entry = EntryImpl.create(e, interceptor, 0);
                             entries.add(entry);
                             totalSize += entry.getLength();
                         }
@@ -109,7 +110,7 @@ public class EntryCacheDisabled implements EntryCache {
                         Iterator<LedgerEntry> iterator = ledgerEntries.iterator();
                         if (iterator.hasNext()) {
                             LedgerEntry ledgerEntry = iterator.next();
-                            EntryImpl returnEntry = EntryImpl.create(ledgerEntry, interceptor);
+                            EntryImpl returnEntry = EntryImpl.create(ledgerEntry, interceptor, 0);
 
                             ml.getMbean().recordReadEntriesOpsCacheMisses(1, returnEntry.getLength());
                             ml.getFactory().getMbean().recordCacheMiss(1, returnEntry.getLength());
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCacheManager.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCacheManager.java
index 853cb1b8763..816ccd4a3e4 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCacheManager.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCacheManager.java
@@ -18,10 +18,10 @@
  */
 package org.apache.bookkeeper.mledger.impl.cache;
 
-import org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl;
+import org.apache.bookkeeper.mledger.ManagedLedger;
 
 public interface EntryCacheManager {
-    EntryCache getEntryCache(ManagedLedgerImpl ml);
+    EntryCache getEntryCache(ManagedLedger ml);
 
     void removeEntryCache(String name);
 
@@ -37,5 +37,10 @@ public interface EntryCacheManager {
 
     double getCacheEvictionWatermark();
 
-    void doCacheEviction(long maxTimestamp);
+    void doCacheEviction();
+
+    void updateCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes(
+            int extendTTLOfEntriesWithRemainingExpectedReadsMaxTimes);
+
+    void updateCacheEvictionExtendTTLOfRecentlyAccessed(boolean cacheEvictionExtendTTLOfRecentlyAccessed);
 }
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCacheManager.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryLengthFunction.java
similarity index 59%
copy from managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCacheManager.java
copy to managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryLengthFunction.java
index 853cb1b8763..0145346871f 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCacheManager.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryLengthFunction.java
@@ -18,24 +18,18 @@
  */
 package org.apache.bookkeeper.mledger.impl.cache;
 
+import org.apache.bookkeeper.mledger.Entry;
 import org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl;
 
-public interface EntryCacheManager {
-    EntryCache getEntryCache(ManagedLedgerImpl ml);
-
-    void removeEntryCache(String name);
-
-    long getSize();
-
-    long getMaxSize();
-
-    void clear();
-
-    void updateCacheSizeAndThreshold(long maxSize);
-
-    void updateCacheEvictionWatermark(double cacheEvictionWatermark);
-
-    double getCacheEvictionWatermark();
-
-    void doCacheEviction(long maxTimestamp);
+/**
+ * Function that computes the length of an entry.
+ * The reason for adding this interface is to allow testing caching scenarios with large entry sizes
+ * without having to test with actual real entries that match the larger size.
+ * This simplifies the testing of the cache when it's necessary to simulate a scenario where the cache size
+ * becomes a limiting factor in cache efficiency.
+ */
+@FunctionalInterface
+public interface EntryLengthFunction {
+    EntryLengthFunction DEFAULT = (ml, entry) -> entry.getLength();
+    int getEntryLength(ManagedLedgerImpl ml, Entry entry);
 }
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/PendingReadsManager.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/PendingReadsManager.java
index c3bd97e5a34..5f904f3cf84 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/PendingReadsManager.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/PendingReadsManager.java
@@ -28,6 +28,7 @@ import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.IntSupplier;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.bookkeeper.client.api.ReadHandle;
 import org.apache.bookkeeper.mledger.AsyncCallbacks;
@@ -275,7 +276,7 @@ public class PendingReadsManager {
                 if (first.startEntry == key.startEntry
                         && first.endEntry == key.endEntry) {
                     // perfect match, no copy, this is the most common case
-                    first.callback.readEntriesComplete((List) entriesToReturn, first.ctx);
+                    first.callback.readEntriesComplete(entriesToReturn, first.ctx);
                 } else {
                     first.callback.readEntriesComplete(
                             keepEntries(entriesToReturn, first.startEntry, first.endEntry), first.ctx);
@@ -286,6 +287,8 @@ public class PendingReadsManager {
                             copyEntries(entriesToReturn, callback.startEntry, callback.endEntry), callback.ctx);
                 }
                 for (Entry entry : entriesToReturn) {
+                    // don't decrease the read count when these entries are released
+                    ((EntryImpl) entry).setDecreaseReadCountOnRelease(false);
                     entry.release();
                 }
             }
@@ -326,7 +329,7 @@ public class PendingReadsManager {
     }
 
 
-    void readEntries(ReadHandle lh, long firstEntry, long lastEntry, boolean shouldCacheEntry,
+    void readEntries(ReadHandle lh, long firstEntry, long lastEntry, IntSupplier expectedReadCount,
                      final AsyncCallbacks.ReadEntriesCallback callback, Object ctx) {
         final PendingReadKey key = new PendingReadKey(firstEntry, lastEntry);
 
@@ -348,9 +351,9 @@ public class PendingReadsManager {
                     continue;
                 }
                 CompletableFuture<List<Entry>> readFromLeftFuture =
-                        recursiveReadMissingEntriesAsync(lh, shouldCacheEntry, findBestCandidateOutcome.missingOnLeft);
+                        recursiveReadMissingEntriesAsync(lh, expectedReadCount, findBestCandidateOutcome.missingOnLeft);
                 CompletableFuture<List<Entry>> readFromRightFuture =
-                        recursiveReadMissingEntriesAsync(lh, shouldCacheEntry,
+                        recursiveReadMissingEntriesAsync(lh, expectedReadCount,
                                 findBestCandidateOutcome.missingOnRight);
                 readFromLeftFuture
                         .thenCombine(readFromMidFuture, (left, mid) -> {
@@ -378,20 +381,21 @@ public class PendingReadsManager {
 
             if (createdByThisThread.get()) {
                 CompletableFuture<List<Entry>> readResult = rangeEntryCache.readFromStorage(lh, firstEntry,
-                        lastEntry, shouldCacheEntry);
+                        lastEntry, expectedReadCount);
                 pendingRead.attach(readResult);
             }
         }
     }
 
-    private CompletableFuture<List<Entry>> recursiveReadMissingEntriesAsync(ReadHandle lh, boolean shouldCacheEntry,
+    private CompletableFuture<List<Entry>> recursiveReadMissingEntriesAsync(ReadHandle lh,
+                                                                            IntSupplier expectedReadCount,
                                                                             PendingReadKey missingKey) {
         CompletableFuture<List<Entry>> future;
         if (missingKey != null) {
             future = new CompletableFuture<>();
             ReadEntriesCallback callback = new ReadEntriesCallback(future);
-            rangeEntryCache.asyncReadEntry0(lh, missingKey.startEntry, missingKey.endEntry,
-                    shouldCacheEntry, callback, null, false);
+            rangeEntryCache.asyncReadEntry0(lh, missingKey.startEntry, missingKey.endEntry, expectedReadCount, callback,
+                    null, false);
         } else {
             future = CompletableFuture.completedFuture(Collections.emptyList());
         }
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeCache.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeCache.java
index d3d72ffffcf..a5592f7098e 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeCache.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeCache.java
@@ -61,20 +61,20 @@ class RangeCache {
      * Insert.
      *
      * @param key
-     * @param value ref counted value with at least 1 ref to pass on the cache
+     * @param value       ref counted value with at least 1 ref to pass on the cache
+     * @param entryLength size of the entry in bytes
      * @return whether the entry was inserted in the cache
      */
-    public boolean put(Position key, ReferenceCountedEntry value) {
+    public boolean put(Position key, ReferenceCountedEntry value, int entryLength) {
         // retain value so that it's not released before we put it in the cache and calculate the weight
         value.retain();
         try {
             if (!value.matchesPosition(key)) {
                 throw new IllegalArgumentException("Value '" + value + "' does not match key '" + key + "'");
             }
-            long entrySize = value.getLength();
-            boolean added = RangeCacheEntryWrapper.withNewInstance(this, key, value, entrySize, newWrapper -> {
+            boolean added = RangeCacheEntryWrapper.withNewInstance(this, key, value, entryLength, newWrapper -> {
                 if (entries.putIfAbsent(key, newWrapper) == null && removalQueue.addEntry(newWrapper)) {
-                    this.size.addAndGet(entrySize);
+                    this.size.addAndGet(entryLength);
                     return true;
                 } else {
                     // recycle the new wrapper as it was not used
@@ -88,6 +88,17 @@ class RangeCache {
         }
     }
 
+    /**
+     * Insert to cache with entry length determined directly from the value.
+     * This method is used in tests.
+     * @param key
+     * @param value
+     * @return
+     */
+    public boolean put(Position key, ReferenceCountedEntry value) {
+        return put(key, value, value.getLength());
+    }
+
     public boolean exists(Position key) {
         return key != null ? entries.containsKey(key) : true;
     }
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeCacheEntryWrapper.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeCacheEntryWrapper.java
index 2852cd28f97..db5ceaefd38 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeCacheEntryWrapper.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeCacheEntryWrapper.java
@@ -44,6 +44,8 @@ class RangeCacheEntryWrapper {
     RangeCache rangeCache;
     long size;
     long timestampNanos;
+    int requeueCount;
+    volatile boolean accessed;
 
     private RangeCacheEntryWrapper(Recycler.Handle<RangeCacheEntryWrapper> recyclerHandle) {
         this.recyclerHandle = recyclerHandle;
@@ -118,6 +120,7 @@ class RangeCacheEntryWrapper {
         if (localKey != key && (requireSameKeyInstance || localKey == null || !localKey.equals(key))) {
             return null;
         }
+        accessed = true;
         return localValue;
     }
 
@@ -138,6 +141,7 @@ class RangeCacheEntryWrapper {
         long removedSize = size;
         size = 0;
         timestampNanos = 0;
+        requeueCount = 0;
         return removedSize;
     }
 
@@ -150,12 +154,20 @@ class RangeCacheEntryWrapper {
         }
     }
 
+    void markRequeued() {
+        timestampNanos = System.nanoTime();
+        accessed = false;
+        requeueCount++;
+    }
+
     void recycle() {
         rangeCache = null;
         key = null;
         value = null;
         size = 0;
         timestampNanos = 0;
+        requeueCount = 0;
+        accessed = false;
         recyclerHandle.recycle(this);
     }
 }
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeCacheRemovalQueue.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeCacheRemovalQueue.java
index 31c7e87b21e..41d98c441cd 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeCacheRemovalQueue.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeCacheRemovalQueue.java
@@ -19,35 +19,89 @@
 package org.apache.bookkeeper.mledger.impl.cache;
 
 import static com.google.common.base.Preconditions.checkArgument;
+import com.google.common.annotations.VisibleForTesting;
+import java.util.function.Consumer;
+import org.apache.commons.lang3.mutable.MutableInt;
+import org.apache.commons.lang3.mutable.MutableLong;
 import org.apache.commons.lang3.tuple.Pair;
 import org.jctools.queues.MpscUnboundedArrayQueue;
 
 /**
  * A central queue to hold entries that are scheduled for removal from all range cache instances.
- * Removal of entries is done in a single thread to avoid contention.
- * This queue is used to evict entries based on timestamp or based on size to free up space in the cache.
+ * This class provides thread-safe eviction of cached entries based on age and memory pressure.
+ *
+ * This solution is influenced by S3FIFO algorithm (explanation at
+ * https://blog.jasony.me/system/cache/2023/08/01/s3fifo and https://s3fifo.com/),
+ * although it is not an implementation of S3FIFO. It accomplishes a similar goal as S3FIFO, but for the broker cache.
+ * The goal of S3FIFO is to quickly remove entries that are unlikely to be accessed in the future. Similarly,
+ * the PIP-430 broker cache removal queue aims to quickly and efficiently remove entries that would be unlikely to be
+ * accessed in the future. The reason why S3FIFO is not implemented in broker cache is that in broker caching the
+ * "expected read count" concept is used to determine whether an entry is likely to be accessed in the future. This
+ * information is not available in generic cache implementations and in the broker cache this information can be used to
+ * keep the entries in the cache longer that are likely to be accessed in the future.
+ *
+ * The S3FIFO algorithm explanation blog post states:
+ * "While the eviction algorithm so far have been centered around LRU,
+ * I believe modern eviction algorithms should be designed with FIFO queues."
+ * This is one of the reasons why the PIP-430 broker cache removal queue is also based on a FIFO queue.
+ *
+ * Key features:
+ * - Single threaded removal to avoid contention via MPSC (Multiple Producer Single Consumer) queue
+ * - Entries are approximately ordered by timestamp for efficient eviction
+ * - Two main eviction modes:
+ * 1. Time-based: Removes entries older than specified timestamp
+ * 2. Memory-based: Frees up memory by removing oldest/least accessed entries first
  */
 class RangeCacheRemovalQueue {
     // The removal queue is unbounded, but we allocate memory in chunks to avoid frequent memory allocations.
     private static final int REMOVAL_QUEUE_CHUNK_SIZE = 128 * 1024;
     private final MpscUnboundedArrayQueue<RangeCacheEntryWrapper> removalQueue = new MpscUnboundedArrayQueue<>(
             REMOVAL_QUEUE_CHUNK_SIZE);
+    private int maxRequeueCountWhenHasExpectedReads;
+    private boolean extendTTLOfRecentlyAccessed;
 
-    public Pair<Integer, Long> evictLEntriesBeforeTimestamp(long timestampNanos) {
-        return evictEntries(
-                (e, c) -> e.timestampNanos < timestampNanos ? EvictionResult.REMOVE : EvictionResult.STOP);
+    RangeCacheRemovalQueue(int maxRequeueCountWhenHasExpectedReads, boolean extendTTLOfRecentlyAccessed) {
+        this.maxRequeueCountWhenHasExpectedReads = maxRequeueCountWhenHasExpectedReads;
+        this.extendTTLOfRecentlyAccessed = extendTTLOfRecentlyAccessed;
     }
 
-    public Pair<Integer, Long> evictLeastAccessedEntries(long sizeToFree) {
-        checkArgument(sizeToFree > 0);
-        return evictEntries(
-                (e, c) -> {
-                    // stop eviction if we have already removed enough entries
-                    if (c.removedSize >= sizeToFree) {
-                        return EvictionResult.STOP;
-                    }
+    public synchronized Pair<Integer, Long> evictLEntriesBeforeTimestamp(long timestampNanos) {
+        return evictEntries((e, c) -> {
+            boolean expired = e.timestampNanos < timestampNanos;
+            if (expired) {
+                if (shouldRequeue(e)) {
+                    return EvictionResult.REQUEUE;
+                } else  {
                     return EvictionResult.REMOVE;
-                });
+                }
+            } else {
+                // if the entry is not expired, we stop eviction
+                return EvictionResult.STOP;
+            }
+        });
+    }
+
+    public synchronized Pair<Integer, Long> evictLeastAccessedEntries(long sizeToFree) {
+        checkArgument(sizeToFree > 0);
+        return evictEntries((e, c) -> {
+            // stop eviction if we have already removed enough entries
+            if (c.removedSize >= sizeToFree) {
+                return EvictionResult.STOP;
+            }
+            if (shouldRequeue(e)) {
+                return EvictionResult.REQUEUE;
+            }
+            // remove by default since there's a need to free up more memory
+            return EvictionResult.REMOVE;
+        });
+    }
+
+    private boolean shouldRequeue(RangeCacheEntryWrapper e) {
+        return  // If the entry is recently accessed and the feature is enabled, we requeue it.
+                (extendTTLOfRecentlyAccessed && e.accessed)
+                        // If the entry has expected reads, we requeue it if it has not exceeded the maximum requeue
+                        // count.
+                        || (e.value.hasExpectedReads() && e.requeueCount < maxRequeueCountWhenHasExpectedReads);
     }
 
     public boolean addEntry(RangeCacheEntryWrapper newWrapper) {
@@ -55,21 +109,29 @@ class RangeCacheRemovalQueue {
     }
 
     enum EvictionResult {
-        REMOVE, MISSING, STOP;
+        REMOVE, REQUEUE, STOP, MISSING;
+
+        boolean shouldStop() {
+            return this == STOP;
+        }
+
+        boolean shouldRemovePeekedEntry() {
+            return shouldRequeue() || shouldDrop();
+        }
 
-        boolean isContinueEviction() {
-            return this != STOP;
+        boolean shouldRequeue() {
+            return this == REQUEUE;
         }
 
-        boolean shouldRemoveFromQueue() {
+        boolean shouldDrop() {
             return this == REMOVE || this == MISSING;
         }
-    }
 
+    }
     interface EvictionPredicate {
+
         EvictionResult test(RangeCacheEntryWrapper entry, RangeCacheRemovalCounters counters);
     }
-
     /**
      * Evict entries from the removal queue based on the provided eviction predicate.
      * This method is synchronized to prevent multiple threads from removing entries simultaneously.
@@ -84,21 +146,27 @@ class RangeCacheRemovalQueue {
         return handleRemovalResult(counters);
     }
 
-    private void handleQueue(EvictionPredicate evictionPredicate,
-                                                   RangeCacheRemovalCounters counters) {
+    private void handleQueue(EvictionPredicate evictionPredicate, RangeCacheRemovalCounters counters) {
         while (!Thread.currentThread().isInterrupted()) {
             RangeCacheEntryWrapper entry = removalQueue.peek();
             if (entry == null) {
+                // no more entries to process
                 break;
             }
             EvictionResult evictionResult = handleEviction(evictionPredicate, entry, counters);
-            if (evictionResult.shouldRemoveFromQueue()) {
+            if (evictionResult.shouldRemovePeekedEntry()) {
                 // remove the peeked entry from the queue
                 removalQueue.poll();
+            }
+            if (evictionResult.shouldRequeue()) {
+                // add the entry back to the end of the queue to be processed later
+                removalQueue.add(entry);
+            } else if (evictionResult.shouldDrop()) {
                 // recycle the entry after it has been removed from the queue
                 entry.recycle();
             }
-            if (!evictionResult.isContinueEviction()) {
+            if (evictionResult.shouldStop()) {
+                // stop eviction if the predicate returned STOP
                 break;
             }
         }
@@ -107,10 +175,11 @@ class RangeCacheRemovalQueue {
     private EvictionResult handleEviction(EvictionPredicate evictionPredicate, RangeCacheEntryWrapper entry,
                                           RangeCacheRemovalCounters counters) {
         EvictionResult evictionResult = entry.withWriteLock(e -> {
-            EvictionResult result =
-                    evaluateEvictionPredicate(evictionPredicate, counters, e);
+            EvictionResult result = evaluateEvictionPredicate(evictionPredicate, counters, e);
             if (result == EvictionResult.REMOVE) {
                 e.rangeCache.removeEntry(e.key, e.value, e, counters, true);
+            } else if (result == EvictionResult.REQUEUE) {
+                e.markRequeued();
             }
             return result;
         });
@@ -118,7 +187,8 @@ class RangeCacheRemovalQueue {
     }
 
     private static EvictionResult evaluateEvictionPredicate(EvictionPredicate evictionPredicate,
-                                                    RangeCacheRemovalCounters counters, RangeCacheEntryWrapper entry) {
+                                                            RangeCacheRemovalCounters counters,
+                                                            RangeCacheEntryWrapper entry) {
         if (entry.key == null) {
             // entry has been removed by another thread
             return EvictionResult.MISSING;
@@ -131,4 +201,71 @@ class RangeCacheRemovalQueue {
         counters.recycle();
         return result;
     }
+
+    /**
+     * Returns the actual size of the removal queue, excluding entries that don't have expected reads.
+     * This method is used for testing purposes to verify actual size of retained cached entries.
+     * This has a performance impact, so it should not be used in production code.
+     * @return a pair containing the number of entries and their total size in bytes
+     */
+    @VisibleForTesting
+    public synchronized Pair<Integer, Long> getNonEvictableSize() {
+        final MutableInt entries = new MutableInt(0);
+        final MutableLong bytesSize = new MutableLong(0L);
+        forEachEntry((entryWrapper) -> {
+            // only count entries that haven't met the expected reads condition
+            // or don't have a read count handler
+            if (entryWrapper.value.getReadCountHandler() == null || entryWrapper.value.hasExpectedReads()) {
+                entries.increment();
+                bytesSize.add(entryWrapper.size);
+            }
+        });
+        return Pair.of(entries.getValue(), bytesSize.getValue());
+    }
+
+    /**
+     * Iterates over all entries in the removal queue.
+     * This is only for testing purposes and should not be used in production code
+     * @param consumer the consumer to apply to each entry
+     */
+    @VisibleForTesting
+    public synchronized void forEachEntry(Consumer<RangeCacheEntryWrapper> consumer) {
+        RangeCacheEntryWrapper firstEntry = null;
+        while (!Thread.currentThread().isInterrupted()) {
+            RangeCacheEntryWrapper entry = removalQueue.peek();
+            if (entry == null || entry == firstEntry) {
+                // no more entries to process
+                break;
+            }
+            boolean exists = entry.withWriteLock(wrapper -> {
+                if (wrapper.key != null && wrapper.value != null) {
+                    consumer.accept(wrapper);
+                    return true;
+                }
+                return false;
+            });
+            // remove the entry from the queue
+            removalQueue.poll();
+            if (exists) {
+                // add the entry to the end of the queue to retain the order
+                removalQueue.add(entry);
+                if (firstEntry == null) {
+                    // remember the first entry to avoid infinite loop
+                    firstEntry = entry;
+                }
+            }
+        }
+    }
+
+    public synchronized void setMaxRequeueCountWhenHasExpectedReads(int maxRequeueCountWhenHasExpectedReads) {
+        this.maxRequeueCountWhenHasExpectedReads = maxRequeueCountWhenHasExpectedReads;
+    }
+
+    public synchronized void setExtendTTLOfRecentlyAccessed(boolean extendTTLOfRecentlyAccessed) {
+        this.extendTTLOfRecentlyAccessed = extendTTLOfRecentlyAccessed;
+    }
+
+    public boolean isEmpty() {
+        return removalQueue.isEmpty();
+    }
 }
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeEntryCacheImpl.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeEntryCacheImpl.java
index 03aba0e425e..44056bc44aa 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeEntryCacheImpl.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeEntryCacheImpl.java
@@ -32,6 +32,7 @@ import java.util.Optional;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.LongAdder;
+import java.util.function.IntSupplier;
 import org.apache.bookkeeper.client.api.BKException;
 import org.apache.bookkeeper.client.api.LedgerEntry;
 import org.apache.bookkeeper.client.api.ReadHandle;
@@ -47,6 +48,7 @@ import org.apache.bookkeeper.mledger.impl.EntryImpl;
 import org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl;
 import org.apache.bookkeeper.mledger.intercept.ManagedLedgerInterceptor;
 import org.apache.commons.lang3.tuple.Pair;
+import org.apache.pulsar.common.util.FutureUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -85,13 +87,20 @@ public class RangeEntryCacheImpl implements EntryCache {
 
     private final LongAdder totalAddedEntriesSize = new LongAdder();
     private final LongAdder totalAddedEntriesCount = new LongAdder();
+    private final EntryLengthFunction entryLengthFunction;
 
     public RangeEntryCacheImpl(RangeEntryCacheManagerImpl manager, ManagedLedgerImpl ml, boolean copyEntries,
-                               RangeCacheRemovalQueue rangeCacheRemovalQueue) {
+                               RangeCacheRemovalQueue rangeCacheRemovalQueue, EntryLengthFunction entryLengthFunction) {
+        this(manager, ml, copyEntries, rangeCacheRemovalQueue, entryLengthFunction, null);
+    }
 
+    RangeEntryCacheImpl(RangeEntryCacheManagerImpl manager, ManagedLedgerImpl ml, boolean copyEntries,
+                        RangeCacheRemovalQueue rangeCacheRemovalQueue, EntryLengthFunction entryLengthFunction,
+                        PendingReadsManager pendingReadsManager) {
         this.manager = manager;
         this.ml = ml;
-        this.pendingReadsManager = new PendingReadsManager(this);
+        this.pendingReadsManager = pendingReadsManager == null ? new PendingReadsManager(this) : pendingReadsManager;
+        this.entryLengthFunction = entryLengthFunction;
         this.interceptor = ml.getManagedLedgerInterceptor();
         this.entries = new RangeCache(rangeCacheRemovalQueue);
         this.copyEntries = copyEntries;
@@ -123,7 +132,7 @@ public class RangeEntryCacheImpl implements EntryCache {
 
     @Override
     public boolean insert(Entry entry) {
-        int entryLength = entry.getLength();
+        int entryLength = entryLengthFunction.getEntryLength(ml, entry);
 
         if (log.isDebugEnabled()) {
             log.debug("[{}] Adding entry to cache: {} - size: {}", ml.getName(), entry.getPosition(),
@@ -147,9 +156,10 @@ public class RangeEntryCacheImpl implements EntryCache {
             cachedData = entry.getDataBuffer().retain();
         }
 
-        ReferenceCountedEntry cacheEntry = EntryImpl.createWithRetainedDuplicate(position, cachedData);
+        ReferenceCountedEntry cacheEntry =
+                EntryImpl.createWithRetainedDuplicate(position, cachedData, entry.getReadCountHandler());
         cachedData.release();
-        if (entries.put(position, cacheEntry)) {
+        if (entries.put(position, cacheEntry, entryLength)) {
             totalAddedEntriesSize.add(entryLength);
             totalAddedEntriesCount.increment();
             manager.entryAdded(entryLength);
@@ -229,7 +239,7 @@ public class RangeEntryCacheImpl implements EntryCache {
             final Object ctx) {
         try {
             asyncReadEntriesByPosition(lh, position, position, 1,
-                    DEFAULT_CACHE_INDIVIDUAL_READ_ENTRY,
+                    () -> DEFAULT_CACHE_INDIVIDUAL_READ_ENTRY ? 1 : 0,
                     new ReadEntriesCallback() {
                 @Override
                 public void readEntriesComplete(List<Entry> entries, Object ctx) {
@@ -256,10 +266,10 @@ public class RangeEntryCacheImpl implements EntryCache {
     }
 
     @Override
-    public void asyncReadEntry(ReadHandle lh, long firstEntry, long lastEntry, boolean shouldCacheEntry,
+    public void asyncReadEntry(ReadHandle lh, long firstEntry, long lastEntry, IntSupplier expectedReadCount,
             final ReadEntriesCallback callback, Object ctx) {
         try {
-            asyncReadEntry0(lh, firstEntry, lastEntry, shouldCacheEntry, callback, ctx, true);
+            asyncReadEntry0(lh, firstEntry, lastEntry, expectedReadCount, callback, ctx, true);
         } catch (Throwable t) {
             log.warn("failed to read entries for {}--{}-{}", lh.getId(), firstEntry, lastEntry, t);
             // invalidate all entries related to ledger from the cache (it might happen if entry gets corrupt
@@ -271,18 +281,18 @@ public class RangeEntryCacheImpl implements EntryCache {
     }
 
     @SuppressWarnings({ "unchecked", "rawtypes" })
-    void asyncReadEntry0(ReadHandle lh, long firstEntry, long lastEntry, boolean shouldCacheEntry,
+    void asyncReadEntry0(ReadHandle lh, long firstEntry, long lastEntry, IntSupplier expectedReadCount,
             final ReadEntriesCallback callback, Object ctx, boolean acquirePermits) {
         final long ledgerId = lh.getId();
         final int numberOfEntries = (int) (lastEntry - firstEntry) + 1;
         final Position firstPosition = PositionFactory.create(ledgerId, firstEntry);
         final Position lastPosition = PositionFactory.create(ledgerId, lastEntry);
-        asyncReadEntriesByPosition(lh, firstPosition, lastPosition, numberOfEntries, shouldCacheEntry, callback, ctx,
+        asyncReadEntriesByPosition(lh, firstPosition, lastPosition, numberOfEntries, expectedReadCount, callback, ctx,
                 acquirePermits);
     }
 
     void asyncReadEntriesByPosition(ReadHandle lh, Position firstPosition, Position lastPosition, int numberOfEntries,
-                                    boolean shouldCacheEntry, final ReadEntriesCallback originalCallback,
+                                    IntSupplier expectedReadCount, final ReadEntriesCallback originalCallback,
                                     Object ctx, boolean acquirePermits) {
         checkArgument(firstPosition.getLedgerId() == lastPosition.getLedgerId(),
                 "Invalid range. Entries %s and %s should be in the same ledger.",
@@ -298,7 +308,7 @@ public class RangeEntryCacheImpl implements EntryCache {
 
         InflightReadsLimiter pendingReadsLimiter = getPendingReadsLimiter();
         if (!acquirePermits || pendingReadsLimiter.isDisabled()) {
-            doAsyncReadEntriesByPosition(lh, firstPosition, lastPosition, numberOfEntries, shouldCacheEntry,
+            doAsyncReadEntriesByPosition(lh, firstPosition, lastPosition, numberOfEntries, expectedReadCount,
                     originalCallback, ctx);
         } else {
             long estimatedEntrySize = getEstimatedEntrySize(lh);
@@ -314,19 +324,19 @@ public class RangeEntryCacheImpl implements EntryCache {
                         // or timeout
                         ml.getExecutor().execute(() -> {
                             doAsyncReadEntriesWithAcquiredPermits(lh, firstPosition, lastPosition, numberOfEntries,
-                                    shouldCacheEntry, originalCallback, ctx, handle, estimatedReadSize);
+                                    expectedReadCount, originalCallback, ctx, handle, estimatedReadSize);
                         });
                     });
             // permits were immediately available and acquired
             if (optionalHandle.isPresent()) {
                 doAsyncReadEntriesWithAcquiredPermits(lh, firstPosition, lastPosition, numberOfEntries,
-                        shouldCacheEntry, originalCallback, ctx, optionalHandle.get(), estimatedReadSize);
+                        expectedReadCount, originalCallback, ctx, optionalHandle.get(), estimatedReadSize);
             }
         }
     }
 
     void doAsyncReadEntriesWithAcquiredPermits(ReadHandle lh, Position firstPosition, Position lastPosition,
-                                               int numberOfEntries, boolean shouldCacheEntry,
+                                               int numberOfEntries, IntSupplier expectedReadCount,
                                                final ReadEntriesCallback originalCallback, Object ctx,
                                                InflightReadsLimiter.Handle handle, long estimatedReadSize) {
         if (!handle.success()) {
@@ -366,12 +376,12 @@ public class RangeEntryCacheImpl implements EntryCache {
                 originalCallback.readEntriesFailed(exception, ctx2);
             }
         };
-        doAsyncReadEntriesByPosition(lh, firstPosition, lastPosition, numberOfEntries, shouldCacheEntry,
+        doAsyncReadEntriesByPosition(lh, firstPosition, lastPosition, numberOfEntries, expectedReadCount,
                 wrappedCallback, ctx);
     }
 
     void doAsyncReadEntriesByPosition(ReadHandle lh, Position firstPosition, Position lastPosition, int numberOfEntries,
-                                      boolean shouldCacheEntry, final ReadEntriesCallback callback,
+                                      IntSupplier expectedReadCount, final ReadEntriesCallback callback,
                                       Object ctx) {
         Collection<ReferenceCountedEntry> cachedEntries;
         if (firstPosition.compareTo(lastPosition) == 0) {
@@ -385,37 +395,109 @@ public class RangeEntryCacheImpl implements EntryCache {
             cachedEntries = entries.getRange(firstPosition, lastPosition);
         }
 
-        if (cachedEntries.size() == numberOfEntries) {
+        if (cachedEntries.size() > 0) {
             long totalCachedSize = 0;
             final List<Entry> entriesToReturn = new ArrayList<>(numberOfEntries);
+            for (int i = 0; i < numberOfEntries; i++) {
+                entriesToReturn.add(null); // Initialize with nulls
+            }
 
-            // All entries found in cache
             for (Entry entry : cachedEntries) {
-                entriesToReturn.add(EntryImpl.create(entry));
+                int index = (int) (entry.getPosition().getEntryId() - firstPosition.getEntryId());
+                entriesToReturn.set(index, EntryImpl.create(entry));
                 totalCachedSize += entry.getLength();
                 entry.release();
             }
 
-            manager.mlFactoryMBean.recordCacheHits(entriesToReturn.size(), totalCachedSize);
+            manager.getMlFactoryMBean().recordCacheHits(cachedEntries.size(), totalCachedSize);
             if (log.isDebugEnabled()) {
                 log.debug("[{}] Cache hit for {} entries in range {} to {}", ml.getName(), numberOfEntries,
                         firstPosition, lastPosition);
             }
 
-            callback.readEntriesComplete(entriesToReturn, ctx);
-
-        } else {
-            // TODO: consider reusing the partially cached entries and only reading the missing ones
-            if (!cachedEntries.isEmpty()) {
-                cachedEntries.forEach(entry -> entry.release());
+            if (cachedEntries.size() == numberOfEntries) {
+                callback.readEntriesComplete(entriesToReturn, ctx);
+            } else {
+                // read missing ranges
+                long firstEntryInRange = -1;
+                List<CompletableFuture<List<Entry>>> futures = new ArrayList<>();
+                for (int i = 0; i < numberOfEntries; i++) {
+                    if (entriesToReturn.get(i) == null) {
+                        if (firstEntryInRange == -1) {
+                            firstEntryInRange = firstPosition.getEntryId() + i;
+                        }
+                    } else {
+                        if (firstEntryInRange != -1) {
+                            futures.add(
+                                    readMissingEntriesAsync(lh, firstEntryInRange, firstPosition.getEntryId() + i - 1,
+                                            expectedReadCount, ctx));
+                            firstEntryInRange = -1;
+                        }
+                    }
+                }
+                if (firstEntryInRange != -1) {
+                    futures.add(
+                            readMissingEntriesAsync(lh, firstEntryInRange, lastPosition.getEntryId(),
+                                    expectedReadCount, ctx));
+                }
+                FutureUtil.waitForAll(futures).whenComplete((__, t) -> {
+                    if (t != null) {
+                        // release cached entries placed in entriesToReturn
+                        for (Entry entry : entriesToReturn) {
+                            if (entry != null) {
+                                entry.release();
+                            }
+                        }
+                        // release entries for futures which were completed successfully
+                        for (CompletableFuture<List<Entry>> future : futures) {
+                            if (!future.isCompletedExceptionally()) {
+                                List<Entry> readEntries = future.getNow(null);
+                                if (readEntries != null && !readEntries.isEmpty()) {
+                                    for (Entry entry : readEntries) {
+                                        entry.release();
+                                    }
+                                }
+                            }
+                        }
+                        log.warn("Failed to read missing entries from bookkeeper, retrying by reading all", t);
+                        // Read all the entries from bookkeeper
+                        pendingReadsManager.readEntries(lh, firstPosition.getEntryId(), lastPosition.getEntryId(),
+                                expectedReadCount, callback, ctx);
+                        return;
+                    }
+                    for (CompletableFuture<List<Entry>> future : futures) {
+                        List<Entry> readEntries = future.getNow(null);
+                        if (readEntries != null && !readEntries.isEmpty()) {
+                            for (Entry entry : readEntries) {
+                                int index = (int) (entry.getPosition().getEntryId() - firstPosition.getEntryId());
+                                if (index >= 0 && index < entriesToReturn.size()) {
+                                    entriesToReturn.set(index, entry);
+                                } else {
+                                    log.warn("Received entry {} outside of expected range {} to {}",
+                                            entry.getPosition(), firstPosition, lastPosition);
+                                }
+                            }
+                        }
+                    }
+                    callback.readEntriesComplete(entriesToReturn, ctx);
+                });
             }
-
+        } else {
             // Read all the entries from bookkeeper
             pendingReadsManager.readEntries(lh, firstPosition.getEntryId(), lastPosition.getEntryId(),
-                    shouldCacheEntry, callback, ctx);
+                    expectedReadCount, callback, ctx);
         }
     }
 
+    private CompletableFuture<List<Entry>> readMissingEntriesAsync(ReadHandle lh,
+                                                                   long firstEntry, long lastEntry,
+                                                                   IntSupplier expectedReadCount, Object ctx) {
+        CompletableFuture<List<Entry>> future = new CompletableFuture<>();
+        PendingReadsManager.ReadEntriesCallback callback = new PendingReadsManager.ReadEntriesCallback(future);
+        pendingReadsManager.readEntries(lh, firstEntry, lastEntry, expectedReadCount, callback, ctx);
+        return future;
+    }
+
     @VisibleForTesting
     public long getEstimatedEntrySize(ReadHandle lh) {
         if (lh.getLength() == 0 || lh.getLastAddConfirmed() < 0) {
@@ -436,11 +518,11 @@ public class RangeEntryCacheImpl implements EntryCache {
      * @param lh the handle
      * @param firstEntry the first entry
      * @param lastEntry the last entry
-     * @param shouldCacheEntry if we should put the entry into the cache
+     * @param expectedReadCount if we should put the entry into the cache
      * @return a handle to the operation
      */
     CompletableFuture<List<Entry>> readFromStorage(ReadHandle lh, long firstEntry, long lastEntry,
-                                                   boolean shouldCacheEntry) {
+                                                   IntSupplier expectedReadCount) {
         final int entriesToRead = (int) (lastEntry - firstEntry) + 1;
         CompletableFuture<List<Entry>> readResult = ReadEntryUtils.readAsync(ml, lh, firstEntry, lastEntry)
                 .thenApply(
@@ -451,18 +533,19 @@ public class RangeEntryCacheImpl implements EntryCache {
                             try {
                                 // We got the entries, we need to transform them to a List<> type
                                 long totalSize = 0;
+                                int expectedReadCountVal = expectedReadCount.getAsInt();
                                 final List<Entry> entriesToReturn = new ArrayList<>(entriesToRead);
                                 for (LedgerEntry e : ledgerEntries) {
-                                    EntryImpl entry = EntryImpl.create(e, interceptor);
+                                    EntryImpl entry = EntryImpl.create(e, interceptor, expectedReadCountVal);
                                     entriesToReturn.add(entry);
                                     totalSize += entry.getLength();
-                                    if (shouldCacheEntry) {
+                                    if (expectedReadCountVal > 0) {
                                         insert(entry);
                                     }
                                 }
 
                                 ml.getMbean().recordReadEntriesOpsCacheMisses(entriesToReturn.size(), totalSize);
-                                manager.mlFactoryMBean.recordCacheMiss(entriesToReturn.size(), totalSize);
+                                manager.getMlFactoryMBean().recordCacheMiss(entriesToReturn.size(), totalSize);
                                 ml.getMbean().addReadEntriesSample(entriesToReturn.size(), totalSize);
 
                                 return entriesToReturn;
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeEntryCacheManagerEvictionHandler.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeEntryCacheManagerEvictionHandler.java
index ff408bbca76..229c2dd8843 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeEntryCacheManagerEvictionHandler.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeEntryCacheManagerEvictionHandler.java
@@ -20,6 +20,7 @@ package org.apache.bookkeeper.mledger.impl.cache;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static org.apache.bookkeeper.mledger.impl.cache.RangeEntryCacheManagerImpl.MB;
+import com.google.common.annotations.VisibleForTesting;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.tuple.Pair;
 
@@ -64,4 +65,9 @@ class RangeEntryCacheManagerEvictionHandler {
         manager.entriesRemoved(evictedSize, evictedEntries);
         return evicted;
     }
+
+    @VisibleForTesting
+    public Pair<Integer, Long> getNonEvictableSize() {
+        return rangeCacheRemovalQueue.getNonEvictableSize();
+    }
 }
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeEntryCacheManagerImpl.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeEntryCacheManagerImpl.java
index 1e81d3166dc..6d048e11389 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeEntryCacheManagerImpl.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeEntryCacheManagerImpl.java
@@ -18,6 +18,7 @@
  */
 package org.apache.bookkeeper.mledger.impl.cache;
 
+import com.google.common.annotations.VisibleForTesting;
 import io.opentelemetry.api.OpenTelemetry;
 import java.util.Optional;
 import java.util.concurrent.CompletableFuture;
@@ -26,11 +27,16 @@ import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.Consumer;
+import lombok.Getter;
+import lombok.Setter;
 import org.apache.bookkeeper.common.util.OrderedScheduler;
+import org.apache.bookkeeper.mledger.ManagedLedger;
 import org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig;
 import org.apache.bookkeeper.mledger.impl.ManagedLedgerFactoryImpl;
 import org.apache.bookkeeper.mledger.impl.ManagedLedgerFactoryMBeanImpl;
 import org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl;
+import org.apache.commons.lang3.tuple.Pair;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -48,12 +54,15 @@ public class RangeEntryCacheManagerImpl implements EntryCacheManager {
     private final AtomicReference<CompletableFuture<Void>> evictionInProgress = new AtomicReference<>(null);
 
     private final ManagedLedgerFactoryImpl mlFactory;
+    @Getter
     protected final ManagedLedgerFactoryMBeanImpl mlFactoryMBean;
     private final InflightReadsLimiter inflightReadsLimiter;
 
     protected static final double MB = 1024 * 1024;
     private static final double evictionTriggerThresholdPercent = 0.98;
 
+    @Setter
+    private volatile EntryLengthFunction entryLengthFunction = EntryLengthFunction.DEFAULT;
 
     public RangeEntryCacheManagerImpl(ManagedLedgerFactoryImpl factory, OrderedScheduler scheduledExecutor,
                                       OpenTelemetry openTelemetry) {
@@ -67,20 +76,23 @@ public class RangeEntryCacheManagerImpl implements EntryCacheManager {
         this.cacheEvictionWatermark = config.getCacheEvictionWatermark();
         this.mlFactory = factory;
         this.mlFactoryMBean = factory.getMbean();
-        this.rangeCacheRemovalQueue = new RangeCacheRemovalQueue();
+        this.rangeCacheRemovalQueue = new RangeCacheRemovalQueue(
+                config.getCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes(),
+                config.isCacheEvictionExtendTTLOfRecentlyAccessed());
         this.evictionHandler = new RangeEntryCacheManagerEvictionHandler(this, rangeCacheRemovalQueue);
 
         log.info("Initialized managed-ledger entry cache of {} Mb", maxSize / MB);
     }
 
-    public EntryCache getEntryCache(ManagedLedgerImpl ml) {
+    public EntryCache getEntryCache(ManagedLedger ml) {
         if (maxSize == 0) {
             // Cache is disabled
-            return new EntryCacheDisabled(ml);
+            return new EntryCacheDisabled((ManagedLedgerImpl) ml);
         }
 
         EntryCache newEntryCache =
-                new RangeEntryCacheImpl(this, ml, mlFactory.getConfig().isCopyEntriesInCache(), rangeCacheRemovalQueue);
+                new RangeEntryCacheImpl(this, (ManagedLedgerImpl) ml, mlFactory.getConfig().isCopyEntriesInCache(),
+                        rangeCacheRemovalQueue, entryLengthFunction);
         EntryCache currentEntryCache = caches.putIfAbsent(ml.getName(), newEntryCache);
         if (currentEntryCache != null) {
             return currentEntryCache;
@@ -163,13 +175,18 @@ public class RangeEntryCacheManagerImpl implements EntryCacheManager {
             if (sizeToEvict > 0) {
                 try {
                     long startTime = System.nanoTime();
-                    log.info("Triggering cache eviction. total size: {} Mb -- Need to discard: {} Mb", currentSize / MB,
-                            sizeToEvict / MB);
+                    if (log.isDebugEnabled()) {
+                        log.debug("Triggering cache eviction. total size: {} Mb -- Need to discard: {} Mb",
+                                currentSize / MB, sizeToEvict / MB);
+                    }
                     evictionHandler.evictEntries(sizeToEvict);
-                    long endTime = System.nanoTime();
-                    double durationMs = TimeUnit.NANOSECONDS.toMillis(endTime - startTime);
-                    log.info("Eviction completed. Removed {} Mb in {} ms", (currentSize - this.currentSize.get()) / MB,
-                            durationMs);
+                    if (log.isDebugEnabled()) {
+                        long endTime = System.nanoTime();
+                        double durationMs = TimeUnit.NANOSECONDS.toMillis(endTime - startTime);
+                        log.debug("Eviction completed. Removed {} Mb in {} ms",
+                                (currentSize - this.currentSize.get()) / MB,
+                                durationMs);
+                    }
                 } finally {
                     mlFactoryMBean.recordCacheEviction();
                 }
@@ -193,6 +210,11 @@ public class RangeEntryCacheManagerImpl implements EntryCacheManager {
         return currentSize.get();
     }
 
+    @VisibleForTesting
+    public Pair<Integer, Long> getNonEvictableSize() {
+        return evictionHandler.getNonEvictableSize();
+    }
+
     @Override
     public long getMaxSize() {
         return maxSize;
@@ -204,11 +226,12 @@ public class RangeEntryCacheManagerImpl implements EntryCacheManager {
     }
 
     @Override
-    public void doCacheEviction(long maxTimestamp) {
+    public void doCacheEviction() {
         // this method is expected to be called from the cache eviction executor
         CompletableFuture<Void> evictionCompletionFuture = new CompletableFuture<>();
         evictionInProgress.set(evictionCompletionFuture);
         try {
+            long maxTimestamp = System.nanoTime() - mlFactory.getCacheEvictionTimeThreshold();
             evictionHandler.invalidateEntriesBeforeTimestampNanos(maxTimestamp);
             doEvictToWatermarkWhenOverThreshold();
         } finally {
@@ -226,5 +249,28 @@ public class RangeEntryCacheManagerImpl implements EntryCacheManager {
         return inflightReadsLimiter;
     }
 
+
+    @Override
+    public void updateCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes(
+            int extendTTLOfEntriesWithRemainingExpectedReadsMaxTimes) {
+        rangeCacheRemovalQueue.setMaxRequeueCountWhenHasExpectedReads(
+                extendTTLOfEntriesWithRemainingExpectedReadsMaxTimes);
+    }
+
+    @Override
+    public void updateCacheEvictionExtendTTLOfRecentlyAccessed(boolean cacheEvictionExtendTTLOfRecentlyAccessed) {
+        rangeCacheRemovalQueue.setExtendTTLOfRecentlyAccessed(cacheEvictionExtendTTLOfRecentlyAccessed);
+    }
+
+    @VisibleForTesting
+    RangeCacheRemovalQueue getRangeCacheRemovalQueue() {
+        return rangeCacheRemovalQueue;
+    }
+
+    @VisibleForTesting
+    void forEachEntry(Consumer<RangeCacheEntryWrapper> consumer) {
+        rangeCacheRemovalQueue.forEachEntry(consumer);
+    }
+
     private static final Logger log = LoggerFactory.getLogger(RangeEntryCacheManagerImpl.class);
 }
diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ActiveManagedCursorContainerTest.java b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ActiveManagedCursorContainerTest.java
new file mode 100644
index 00000000000..c6095810119
--- /dev/null
+++ b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ActiveManagedCursorContainerTest.java
@@ -0,0 +1,293 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.bookkeeper.mledger.impl;
+
+import static org.apache.bookkeeper.mledger.impl.MockManagedCursor.addCursor;
+import static org.apache.bookkeeper.mledger.impl.MockManagedCursor.createCursor;
+import static org.assertj.core.api.Assertions.assertThat;
+import java.lang.reflect.Method;
+import java.util.Collections;
+import java.util.List;
+import java.util.Random;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import org.apache.bookkeeper.mledger.ManagedCursor;
+import org.apache.bookkeeper.mledger.Position;
+import org.apache.bookkeeper.mledger.PositionFactory;
+import org.testng.ITest;
+import org.testng.ITestResult;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Factory;
+import org.testng.annotations.Test;
+
+public class ActiveManagedCursorContainerTest implements ITest {
+    String testName;
+
+    @BeforeMethod(alwaysRun = true)
+    public void beforeMethod(Method method, ITestResult result){
+        testName = method.getName();
+    }
+
+    @Override
+    public String getTestName() {
+        return testName + " - " + containerClass.getSimpleName();
+    }
+
+    private final Supplier<ActiveManagedCursorContainer> containerSupplier;
+    private final Class<? extends ActiveManagedCursorContainer> containerClass;
+
+    @Factory
+    public static Object[] createTestInstances() {
+        return new Object[]{new ActiveManagedCursorContainerTest(ActiveManagedCursorContainerImpl.class,
+                ActiveManagedCursorContainerImpl::new),
+                new ActiveManagedCursorContainerTest(ActiveManagedCursorContainerNavigableSetImpl.class,
+                        ActiveManagedCursorContainerNavigableSetImpl::new)};
+    }
+
+    ActiveManagedCursorContainerTest(Class<? extends ActiveManagedCursorContainer> containerClass,
+                                     Supplier<ActiveManagedCursorContainer> containerSupplier) {
+        this.containerClass = containerClass;
+        this.containerSupplier = containerSupplier;
+    }
+
+    @Test
+    public void testAddingInReverseOrder() {
+        int numberOfCursors = 3;
+        ActiveManagedCursorContainer container = containerSupplier.get();
+        List<MockManagedCursor> cursors = IntStream.rangeClosed(1, numberOfCursors)
+                .mapToObj(idx -> createCursor(container, "cursor" + idx, PositionFactory.create(0, idx)))
+                .collect(Collectors.toList());
+        Collections.reverse(cursors);
+        cursors.forEach(cursor -> container.add(cursor, cursor.getReadPosition()));
+        for (int i = 1; i <= numberOfCursors; i++) {
+            ManagedCursor cursor = container.get("cursor" + i);
+            int numberOfCursorsBefore = container.getNumberOfCursorsAtSamePositionOrBefore(cursor);
+            assertThat(numberOfCursorsBefore).describedAs("cursor:%s", cursor).isEqualTo(i);
+        }
+    }
+
+    @Test
+    public void testAddingMultipleWithSamePosition() {
+        int numberOfCursors = 1000;
+        ActiveManagedCursorContainer container = containerSupplier.get();
+        List<MockManagedCursor> cursors = IntStream.rangeClosed(1, numberOfCursors)
+                .mapToObj(idx -> createCursor(container, "cursor" + idx, PositionFactory.create(0, idx / 2)))
+                .collect(Collectors.toList());
+        Collections.shuffle(cursors);
+        cursors.forEach(cursor -> container.add(cursor, cursor.getReadPosition()));
+        for (int i = 1; i <= numberOfCursors; i++) {
+            ManagedCursor cursor = container.get("cursor" + i);
+            assertThat(container.getNumberOfCursorsAtSamePositionOrBefore(cursor))
+                    .describedAs("cursor:%s", cursor)
+                    .isEqualTo(i > 1 && i < numberOfCursors ? i + ((i + 1) % 2) : i);
+        }
+    }
+
+    @Test
+    public void testCountNumberOfCursorsAtSamePositionOrBefore() {
+        ActiveManagedCursorContainer container = containerSupplier.get();
+        List<MockManagedCursor> cursors = IntStream.rangeClosed(1, 1000)
+                .mapToObj(idx -> createCursor(container, "cursor" + idx, PositionFactory.create(0, idx)))
+                .collect(Collectors.toList());
+        // randomize adding order
+        Collections.shuffle(cursors);
+        cursors.forEach(cursor -> container.add(cursor, cursor.getReadPosition()));
+        for (int i = 1; i <= 1000; i++) {
+            ManagedCursor cursor = container.get("cursor" + i);
+            int numberOfCursorsBefore = container.getNumberOfCursorsAtSamePositionOrBefore(cursor);
+            assertThat(numberOfCursorsBefore).describedAs("cursor:%s", cursor).isEqualTo(i);
+        }
+
+        // test updating mark delete position
+        // cursor500 will be updated to position 0:1001
+        ManagedCursor cursor500 = container.get("cursor500");
+        cursor500.seek(PositionFactory.create(0, 1001));
+        assertThat(container.getNumberOfCursorsAtSamePositionOrBefore(cursor500)).describedAs(
+                "cursor500 should have 1000 at same position or before it").isEqualTo(1000);
+        for (int i = 1; i <= 499; i++) {
+            ManagedCursor cursor = container.get("cursor" + i);
+            int numberOfCursorsBefore = container.getNumberOfCursorsAtSamePositionOrBefore(cursor);
+            assertThat(numberOfCursorsBefore).describedAs("cursor:%s", cursor).isEqualTo(i);
+        }
+        for (int i = 501; i <= 1000; i++) {
+            ManagedCursor cursor = container.get("cursor" + i);
+            int numberOfCursorsBefore = container.getNumberOfCursorsAtSamePositionOrBefore(cursor);
+            assertThat(numberOfCursorsBefore).describedAs("cursor:%s", cursor).isEqualTo(i - 1);
+        }
+    }
+
+    @Test
+    public void testCountNumberOfCursorsAtSamePositionOrBefore_SamePosition() {
+        ActiveManagedCursorContainer container = containerSupplier.get();
+        addCursor(container, "cursor1", PositionFactory.create(0, 1));
+        addCursor(container, "cursor2", PositionFactory.create(0, 2));
+        for (int i = 3; i <= 998; i++) {
+            addCursor(container, "cursor" + i, PositionFactory.create(0, 3));
+        }
+        addCursor(container, "cursor999", PositionFactory.create(0, 4));
+        addCursor(container, "cursor1000", PositionFactory.create(0, 5));
+        ManagedCursor cursor = container.get("cursor4");
+        assertThat(container.getNumberOfCursorsAtSamePositionOrBefore(cursor)).isEqualTo(998);
+    }
+
+    private void checkState(ActiveManagedCursorContainer container) {
+        if (container instanceof ActiveManagedCursorContainerImpl c) {
+            c.checkOrderingAndNumberOfCursorsState();
+        } else if (container instanceof ActiveManagedCursorContainerNavigableSetImpl c) {
+            // checking state for navigable set implementation is very slow, disable it by default
+            //c.checkOrderingAndNumberOfCursorsState();
+        }
+    }
+
+    @Test
+    public void testRandomSeekingForward() {
+        ActiveManagedCursorContainer container = containerSupplier.get();
+        List<MockManagedCursor> cursors = IntStream.rangeClosed(1, 1000)
+                .mapToObj(idx -> createCursor(container, "cursor" + idx, PositionFactory.create(0, idx)))
+                .collect(Collectors.toList());
+        // randomize adding order
+        Collections.shuffle(cursors);
+        cursors.forEach(cursor -> container.add(cursor, cursor.getReadPosition()));
+        checkState(container);
+
+        // seek randomly forward in random cursors
+        Random random = new Random(1);
+        for (int j = 0; j < 10000; j++) {
+            int i = random.nextInt(1, 1000);
+            ManagedCursor cursor = container.get("cursor" + i);
+            Position currentPosition = cursor.getReadPosition();
+            Position newPosition = currentPosition.getPositionAfterEntries(random.nextInt(1, 100));
+            cursor.seek(newPosition);
+            checkState(container);
+        }
+
+        // seek to original positions
+        for (int i = 1; i <= 1000; i++) {
+            ManagedCursor cursor = container.get("cursor" + i);
+            cursor.seek(PositionFactory.create(0, i));
+            checkState(container);
+        }
+
+        // check that the number of cursors at same position or before are still correct
+        for (int i = 1; i <= 1000; i++) {
+            ManagedCursor cursor = container.get("cursor" + i);
+            int numberOfCursorsBefore = container.getNumberOfCursorsAtSamePositionOrBefore(cursor);
+            assertThat(numberOfCursorsBefore).describedAs("cursor:%s", cursor).isEqualTo(i);
+        }
+    }
+
+    @Test
+    public void testRandomSeeking() {
+        ActiveManagedCursorContainer container = containerSupplier.get();
+        List<MockManagedCursor> cursors = IntStream.rangeClosed(1, 1000)
+                .mapToObj(idx -> createCursor(container, "cursor" + idx, PositionFactory.create(1, idx)))
+                .collect(Collectors.toList());
+        // randomize adding order
+        Collections.shuffle(cursors);
+        cursors.forEach(cursor -> container.add(cursor, cursor.getReadPosition()));
+        checkState(container);
+
+        // seek randomly forward in random cursors
+        Random random = new Random(1);
+        for (int j = 0; j < 10000; j++) {
+            int i = random.nextInt(1, 1000);
+            ManagedCursor cursor = container.get("cursor" + i);
+            Position currentPosition = cursor.getReadPosition();
+            Position newPosition = PositionFactory.create(currentPosition.getLedgerId(),
+                    Math.max(currentPosition.getEntryId() + random.nextInt(1, 100) - 25, 1));
+            cursor.seek(newPosition);
+            checkState(container);
+        }
+
+        for (int j = 0; j < 10000; j++) {
+            int i = random.nextInt(1, 1000);
+            ManagedCursor cursor = container.get("cursor" + i);
+            Position originalPosition = cursor.getReadPosition();
+            // seek to first entry
+            cursor.seek(PositionFactory.create(0, 1));
+            checkState(container);
+            // seek to last entry
+            cursor.seek(PositionFactory.create(2, Long.MAX_VALUE));
+            checkState(container);
+            cursor.seek(originalPosition);
+            checkState(container);
+        }
+
+        // random seeking with a lot of same positions
+        for (int j = 0; j < 100; j++) {
+            for (int i = 1; i <= 1000; i++) {
+                ManagedCursor cursor = container.get("cursor" + i);
+                int entryId = random.nextInt(1, 10);
+                cursor.seek(PositionFactory.create(0, entryId));
+                checkState(container);
+            }
+        }
+
+        // seek to original positions
+        for (int i = 1; i <= 1000; i++) {
+            ManagedCursor cursor = container.get("cursor" + i);
+            cursor.seek(PositionFactory.create(0, i));
+            checkState(container);
+        }
+
+        // check that the number of cursors at same position or before are still correct
+        for (int i = 1; i <= 1000; i++) {
+            ManagedCursor cursor = container.get("cursor" + i);
+            int numberOfCursorsBefore = container.getNumberOfCursorsAtSamePositionOrBefore(cursor);
+            assertThat(numberOfCursorsBefore).describedAs("cursor:%s", cursor).isEqualTo(i);
+        }
+    }
+
+    @Test
+    public void testRandomSeekingWithLazyUpdates() {
+        ActiveManagedCursorContainer container = containerSupplier.get();
+        int numberOfCursors = 10000;
+        List<MockManagedCursor> cursors = IntStream.rangeClosed(1, numberOfCursors)
+                .mapToObj(idx -> createCursor(container, "cursor" + idx, PositionFactory.create(1, idx)))
+                .collect(Collectors.toList());
+        // randomize adding order
+        Collections.shuffle(cursors);
+        cursors.forEach(cursor -> container.add(cursor, cursor.getReadPosition()));
+
+        // seek randomly forward in random cursors
+        Random random = new Random(1);
+        for (int j = 0; j < numberOfCursors * 10; j++) {
+            int i = (j % numberOfCursors) + 1;
+            ManagedCursor cursor = container.get("cursor" + i);
+            Position currentPosition = cursor.getReadPosition();
+            Position newPosition = PositionFactory.create(currentPosition.getLedgerId(),
+                    Math.max(currentPosition.getEntryId() + random.nextInt(1, 100) - 25, 1));
+            cursor.seek(newPosition);
+        }
+
+        // seek to original positions
+        for (int i = 1; i <= numberOfCursors; i++) {
+            ManagedCursor cursor = container.get("cursor" + i);
+            cursor.seek(PositionFactory.create(0, i));
+        }
+
+        // check that the number of cursors at same position or before are still correct
+        for (int i = 1; i <= numberOfCursors; i++) {
+            ManagedCursor cursor = container.get("cursor" + i);
+            int numberOfCursorsBefore = container.getNumberOfCursorsAtSamePositionOrBefore(cursor);
+            assertThat(numberOfCursorsBefore).describedAs("cursor:%s", cursor).isEqualTo(i);
+        }
+    }
+}
\ No newline at end of file
diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/EntryCacheManagerTest.java b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/EntryCacheManagerTest.java
index 38b87f8ff01..49743d9386c 100644
--- a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/EntryCacheManagerTest.java
+++ b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/EntryCacheManagerTest.java
@@ -292,6 +292,7 @@ public class EntryCacheManagerTest extends MockedBookKeeperTestCase {
         config.setCacheEvictionIntervalMs(1000);
 
         ManagedLedgerConfig managedLedgerConfig = new ManagedLedgerConfig();
+        managedLedgerConfig.setCacheEvictionByExpectedReadCount(false);
         managedLedgerConfig.setCacheEvictionByMarkDeletedPosition(false);
 
         @Cleanup("shutdown")
diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/EntryCacheTest.java b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/EntryCacheTest.java
index a7ce874d7c3..0fe889a2da5 100644
--- a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/EntryCacheTest.java
+++ b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/EntryCacheTest.java
@@ -34,6 +34,7 @@ import java.util.Optional;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import java.util.function.Consumer;
+import java.util.function.IntSupplier;
 import lombok.Cleanup;
 import org.apache.bookkeeper.client.BKException.BKNoSuchLedgerExistsException;
 import org.apache.bookkeeper.client.api.LedgerEntries;
@@ -81,7 +82,7 @@ public class EntryCacheTest extends MockedBookKeeperTestCase {
         }
 
         when(ml.getLastConfirmedEntry()).thenReturn(PositionFactory.create(0, 9));
-        final var entries = readEntry(entryCache, lh, 0, 9, false, null);
+        final var entries = readEntry(entryCache, lh, 0, 9, () -> 0, null);
         assertEquals(entries.size(), 10);
         entries.forEach(Entry::release);
 
@@ -105,7 +106,7 @@ public class EntryCacheTest extends MockedBookKeeperTestCase {
         }
 
         when(ml.getLastConfirmedEntry()).thenReturn(PositionFactory.create(0, 9));
-        final var entries = readEntry(entryCache, lh, 0, 9, false, null);
+        final var entries = readEntry(entryCache, lh, 0, 9, () -> 0, null);
         assertEquals(entries.size(), 10);
     }
 
@@ -124,7 +125,7 @@ public class EntryCacheTest extends MockedBookKeeperTestCase {
         }
 
         when(ml.getLastConfirmedEntry()).thenReturn(PositionFactory.create(0, 9));
-        final var entries = readEntry(entryCache, lh, 0, 9, false, null);
+        final var entries = readEntry(entryCache, lh, 0, 9, () -> 0, null);
         assertEquals(entries.size(), 10);
     }
 
@@ -144,7 +145,7 @@ public class EntryCacheTest extends MockedBookKeeperTestCase {
         entryCache.insert(EntryImpl.create(0, 9, data));
 
         when(ml.getLastConfirmedEntry()).thenReturn(PositionFactory.create(0, 9));
-        final var entries = readEntry(entryCache, lh, 0, 9, false, null);
+        final var entries = readEntry(entryCache, lh, 0, 9, () -> 0, null);
         assertEquals(entries.size(), 10);
     }
 
@@ -164,7 +165,7 @@ public class EntryCacheTest extends MockedBookKeeperTestCase {
         entryCache.insert(EntryImpl.create(0, 8, data));
 
         when(ml.getLastConfirmedEntry()).thenReturn(PositionFactory.create(0, 9));
-        final var entries = readEntry(entryCache, lh, 0, 9, false, null);
+        final var entries = readEntry(entryCache, lh, 0, 9, () -> 0, null);
         assertEquals(entries.size(), 10);
     }
 
@@ -177,25 +178,25 @@ public class EntryCacheTest extends MockedBookKeeperTestCase {
         @Cleanup(value = "clear")
         EntryCache entryCache = cacheManager.getEntryCache(ml);
 
-        readEntry(entryCache, lh, 0, 1, true, e -> {
+        readEntry(entryCache, lh, 0, 1, () -> 1, e -> {
             assertTrue(e instanceof ManagedLedgerException);
             assertTrue(e.getMessage().contains("LastConfirmedEntry is null when reading ledger 0"));
         });
 
         when(ml.getLastConfirmedEntry()).thenReturn(PositionFactory.create(-1, -1));
-        readEntry(entryCache, lh, 0, 1, true, e -> {
+        readEntry(entryCache, lh, 0, 1, () -> 1, e -> {
             assertTrue(e instanceof ManagedLedgerException);
             assertTrue(e.getMessage().contains("LastConfirmedEntry is -1:-1 when reading ledger 0"));
         });
 
         when(ml.getLastConfirmedEntry()).thenReturn(PositionFactory.create(0, 0));
-        readEntry(entryCache, lh, 0, 1, true, e -> {
+        readEntry(entryCache, lh, 0, 1, () -> 1, e -> {
             assertTrue(e instanceof ManagedLedgerException);
             assertTrue(e.getMessage().contains("LastConfirmedEntry is 0:0 when reading entry 1"));
         });
 
         when(ml.getLastConfirmedEntry()).thenReturn(PositionFactory.create(0, 1));
-        List<Entry> cacheMissEntries = readEntry(entryCache, lh, 0, 1, true, null);
+        List<Entry> cacheMissEntries = readEntry(entryCache, lh, 0, 1, () -> 1, null);
         // Ensure first entry is 0 and
         assertEquals(cacheMissEntries.size(), 2);
         assertEquals(cacheMissEntries.get(0).getEntryId(), 0);
@@ -204,7 +205,7 @@ public class EntryCacheTest extends MockedBookKeeperTestCase {
         // Move the reader index to simulate consumption
         cacheMissEntries.get(0).getDataBuffer().readerIndex(10);
 
-        List<Entry> cacheHitEntries = readEntry(entryCache, lh, 0, 1, true, null);
+        List<Entry> cacheHitEntries = readEntry(entryCache, lh, 0, 1, () -> 1, null);
         assertEquals(cacheHitEntries.get(0).getEntryId(), 0);
         assertEquals(cacheHitEntries.get(0).getDataBuffer().readerIndex(), 0);
     }
@@ -228,7 +229,7 @@ public class EntryCacheTest extends MockedBookKeeperTestCase {
         entryCache.insert(EntryImpl.create(0, 2, data));
 
         when(ml.getLastConfirmedEntry()).thenReturn(PositionFactory.create(0, 9));
-        readEntry(entryCache, lh, 0, 9, false, e ->
+        readEntry(entryCache, lh, 0, 9, () -> 0, e ->
                 assertTrue(e instanceof ManagedLedgerException.LedgerNotExistException));
     }
 
@@ -252,20 +253,21 @@ public class EntryCacheTest extends MockedBookKeeperTestCase {
     }
 
     private List<Entry> readEntry(EntryCache entryCache, ReadHandle lh, long firstEntry, long lastEntry,
-                                  boolean shouldCacheEntry, Consumer<Throwable> assertion)
+                                  IntSupplier expectedReadCount, Consumer<Throwable> assertion)
             throws InterruptedException {
         final var future = new CompletableFuture<List<Entry>>();
-        entryCache.asyncReadEntry(lh, firstEntry, lastEntry, shouldCacheEntry, new ReadEntriesCallback() {
-            @Override
-            public void readEntriesComplete(List<Entry> entries, Object ctx) {
-                future.complete(entries);
-            }
-
-            @Override
-            public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
-                future.completeExceptionally(exception);
-            }
-        }, null);
+        entryCache.asyncReadEntry(lh, firstEntry, lastEntry, expectedReadCount,
+                new ReadEntriesCallback() {
+                    @Override
+                    public void readEntriesComplete(List<Entry> entries, Object ctx) {
+                        future.complete(entries);
+                    }
+
+                    @Override
+                    public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
+                        future.completeExceptionally(exception);
+                    }
+                }, null);
         try {
             final var entries = future.get();
             assertNull(assertion);
diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/EntryImplTest.java b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/EntryImplTest.java
index 618a6f88ecc..eac61f5d88e 100644
--- a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/EntryImplTest.java
+++ b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/EntryImplTest.java
@@ -43,7 +43,7 @@ public class EntryImplTest {
         ByteBuf data = Unpooled.wrappedBuffer(testData);
 
         // When
-        EntryImpl entry = EntryImpl.create(ledgerId, entryId, data);
+        EntryImpl entry = EntryImpl.create(ledgerId, entryId, data, 1);
 
         try {
             // Then
@@ -65,7 +65,7 @@ public class EntryImplTest {
         ByteBuf data = Unpooled.wrappedBuffer(testData);
 
         // When
-        EntryImpl entry = EntryImpl.create(position, data);
+        EntryImpl entry = EntryImpl.create(position, data, 1);
 
         try {
             // Then
@@ -88,7 +88,7 @@ public class EntryImplTest {
         ByteBuf data = Unpooled.wrappedBuffer(testData);
 
         // When
-        EntryImpl entry = EntryImpl.createWithRetainedDuplicate(position, data);
+        EntryImpl entry = EntryImpl.createWithRetainedDuplicate(position, data, 1);
 
         try {
             // Then
@@ -109,7 +109,7 @@ public class EntryImplTest {
         long entryId = 222L;
         byte[] testData = "original-entry-data".getBytes();
         ByteBuf originalData = Unpooled.wrappedBuffer(testData);
-        EntryImpl originalEntry = EntryImpl.create(ledgerId, entryId, originalData);
+        EntryImpl originalEntry = EntryImpl.create(ledgerId, entryId, originalData, 1);
 
         try {
             // When
@@ -170,7 +170,7 @@ public class EntryImplTest {
         ByteBuf data = Unpooled.EMPTY_BUFFER;
 
         // When
-        EntryImpl entry = EntryImpl.create(ledgerId, entryId, data);
+        EntryImpl entry = EntryImpl.create(ledgerId, entryId, data, 1);
 
         try {
             // Then
@@ -215,7 +215,7 @@ public class EntryImplTest {
     public void testCreateWithPositionThatIsntImmutable() {
         // Given
         Position position = new AckSetPositionImpl(1L, 2L, new long[0]);
-        EntryImpl entry = EntryImpl.create(position, Unpooled.EMPTY_BUFFER);
+        EntryImpl entry = EntryImpl.create(position, Unpooled.EMPTY_BUFFER, 1);
 
         // Expect that the position is different since it's not immutable
         assertNotSame(entry.getPosition(), position);
@@ -228,7 +228,7 @@ public class EntryImplTest {
     public void testCreateWithPositionThatIsImmutable() {
         // Given
         Position position = PositionFactory.create(1L, 2L);
-        EntryImpl entry = EntryImpl.create(position, Unpooled.EMPTY_BUFFER);
+        EntryImpl entry = EntryImpl.create(position, Unpooled.EMPTY_BUFFER, 1);
 
         // Expect that the position is same since it's immutable
         assertSame(entry.getPosition(), position);
diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/InflightReadsLimiterIntegrationTest.java b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/InflightReadsLimiterIntegrationTest.java
index 17601946355..01a8808a2b9 100644
--- a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/InflightReadsLimiterIntegrationTest.java
+++ b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/InflightReadsLimiterIntegrationTest.java
@@ -139,7 +139,7 @@ public class InflightReadsLimiterIntegrationTest extends MockedBookKeeperTestCas
         // Initialize "entryCache.estimatedEntrySize" to the correct value.
         Object ctx = new Object();
         SimpleReadEntriesCallback cb0 = new SimpleReadEntriesCallback();
-        entryCache.asyncReadEntry(spyCurrentLedger, 125, 125, true, cb0, ctx);
+        entryCache.asyncReadEntry(spyCurrentLedger, 125, 125, () -> 1, cb0, ctx);
         cb0.entries.join();
         int sizePerEntry = Long.valueOf(entryCache.getEstimatedEntrySize(ml.currentLedger)).intValue();
         Awaitility.await().untilAsserted(() -> {
@@ -153,7 +153,7 @@ public class InflightReadsLimiterIntegrationTest extends MockedBookKeeperTestCas
         SimpleReadEntriesCallback cb1 = new SimpleReadEntriesCallback();
         SimpleReadEntriesCallback cb2 = new SimpleReadEntriesCallback();
         threadFactory.newThread(() -> {
-            entryCache.asyncReadEntry(spyCurrentLedger, start1, end1, true, cb1, ctx);
+            entryCache.asyncReadEntry(spyCurrentLedger, start1, end1, () -> 1, cb1, ctx);
         }).start();
         threadFactory.newThread(() -> {
             try {
@@ -161,7 +161,7 @@ public class InflightReadsLimiterIntegrationTest extends MockedBookKeeperTestCas
             } catch (InterruptedException e) {
                 throw new RuntimeException(e);
             }
-            entryCache.asyncReadEntry(spyCurrentLedger, start2, end2, true, cb2, ctx);
+            entryCache.asyncReadEntry(spyCurrentLedger, start2, end2, () -> 1, cb2, ctx);
         }).start();
 
         long bytesAcquired1 = calculateBytesSizeBeforeFirstReading(readCount1 + readCount2, sizePerEntry);
diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorContainerTest.java b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorContainerTest.java
index 55abd2d3e9d..90685537a41 100644
--- a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorContainerTest.java
+++ b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorContainerTest.java
@@ -18,6 +18,7 @@
  */
 package org.apache.bookkeeper.mledger.impl;
 
+import static org.apache.bookkeeper.mledger.impl.MockManagedCursor.createCursor;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.spy;
@@ -25,451 +26,21 @@ import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertNull;
 import static org.testng.Assert.assertTrue;
-import static org.testng.Assert.fail;
 import com.google.common.collect.Lists;
-import com.google.common.collect.Range;
-import com.google.common.collect.Sets;
-import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.CompletableFuture;
-import java.util.function.Predicate;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
-import org.apache.bookkeeper.mledger.AsyncCallbacks;
-import org.apache.bookkeeper.mledger.AsyncCallbacks.ClearBacklogCallback;
-import org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCallback;
-import org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback;
-import org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback;
-import org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback;
-import org.apache.bookkeeper.mledger.AsyncCallbacks.SkipEntriesCallback;
-import org.apache.bookkeeper.mledger.Entry;
 import org.apache.bookkeeper.mledger.ManagedCursor;
-import org.apache.bookkeeper.mledger.ManagedCursorMXBean;
-import org.apache.bookkeeper.mledger.ManagedLedger;
-import org.apache.bookkeeper.mledger.ManagedLedgerException;
 import org.apache.bookkeeper.mledger.Position;
 import org.apache.bookkeeper.mledger.PositionFactory;
-import org.apache.pulsar.common.policies.data.ManagedLedgerInternalStats;
 import org.testng.annotations.Test;
 
 public class ManagedCursorContainerTest {
-
-    private static class MockManagedCursor implements ManagedCursor {
-
-        ManagedCursorContainer container;
-        Position markDeletePosition;
-        Position readPosition;
-        private final boolean updateMarkDeletePosition;
-        private final boolean durable;
-        String name;
-
-        public MockManagedCursor(ManagedCursorContainer container, String name, Position markDeletePosition) {
-            this(container, name, markDeletePosition, null, true, true);
-        }
-
-        public MockManagedCursor(ManagedCursorContainer container, String name, Position markDeletePosition,
-                                 Position readPosition, boolean updateMarkDeletePosition, boolean durable) {
-            this.container = container;
-            this.name = name;
-            this.markDeletePosition = markDeletePosition;
-            this.readPosition = readPosition;
-            this.updateMarkDeletePosition = updateMarkDeletePosition;
-            this.durable = durable;
-        }
-
-        @Override
-        public Map<String, Long> getProperties() {
-            return Collections.emptyMap();
-        }
-
-        @Override
-        public Map<String, String> getCursorProperties() {
-            return Collections.emptyMap();
-        }
-
-        @Override
-        public CompletableFuture<Void> putCursorProperty(String key, String value) {
-            return CompletableFuture.completedFuture(null);
-        }
-
-        @Override
-        public CompletableFuture<Void> setCursorProperties(Map<String, String> cursorProperties) {
-            return CompletableFuture.completedFuture(null);
-        }
-
-        @Override
-        public CompletableFuture<Void> removeCursorProperty(String key) {
-            return CompletableFuture.completedFuture(null);
-        }
-
-        @Override
-        public boolean putProperty(String key, Long value) {
-            return false;
-        }
-
-        @Override
-        public boolean removeProperty(String key) {
-            return false;
-        }
-
-        @Override
-        public boolean isDurable() {
-            return durable;
-        }
-
-        @Override
-        public List<Entry> readEntries(int numberOfEntriesToRead) {
-            return new ArrayList();
-        }
-
-        @Override
-        public void asyncReadEntries(int numberOfEntriesToRead, ReadEntriesCallback callback, Object ctx,
-                                     Position maxPosition) {
-            callback.readEntriesComplete(null, ctx);
-        }
-
-        @Override
-        public void asyncReadEntries(int numberOfEntriesToRead, long maxSizeBytes, ReadEntriesCallback callback,
-                                     Object ctx, Position maxPosition) {
-            callback.readEntriesComplete(null, ctx);
-        }
-
-        @Override
-        public boolean hasMoreEntries() {
-            return true;
-        }
-
-        @Override
-        public long getNumberOfEntries() {
-            return 0;
-        }
-
-        @Override
-        public long getNumberOfEntriesInBacklog(boolean isPrecise) {
-            return 0;
-        }
-
-        @Override
-        public void markDelete(Position position) {
-            markDelete(position, Collections.emptyMap());
-        }
-
-        @Override
-        public void markDelete(Position position, Map<String, Long> properties) {
-            this.markDeletePosition = position;
-            if (updateMarkDeletePosition) {
-                container.cursorUpdated(this, position);
-            }
-        }
-
-        @Override
-        public void asyncMarkDelete(Position position, MarkDeleteCallback callback, Object ctx) {
-            fail();
-        }
-
-        @Override
-        public void asyncMarkDelete(Position position, Map<String, Long> properties, MarkDeleteCallback callback,
-                Object ctx) {
-            fail();
-        }
-
-        @Override
-        public Position getMarkDeletedPosition() {
-            return markDeletePosition;
-        }
-
-        @Override
-        public Position getPersistentMarkDeletedPosition() {
-            return markDeletePosition;
-        }
-
-        @Override
-        public String getName() {
-            return name;
-        }
-
-        @Override
-        public long getLastActive() {
-            return System.currentTimeMillis();
-        }
-
-        @Override
-        public void updateLastActive() {
-            // no-op
-        }
-
-        public String toString() {
-            return String.format("%s=%s/%s", name, markDeletePosition, readPosition);
-        }
-
-        @Override
-        public Position getReadPosition() {
-            return readPosition;
-        }
-
-        @Override
-        public void rewind() {
-        }
-
-        @Override
-        public void seek(Position newReadPosition, boolean force) {
-            this.readPosition = newReadPosition;
-            if (!updateMarkDeletePosition) {
-                container.cursorUpdated(this, newReadPosition);
-            }
-        }
-
-        @Override
-        public void close() {
-        }
-
-        @Override
-        public void asyncClose(AsyncCallbacks.CloseCallback callback, Object ctx) {
-        }
-
-        @Override
-        public void delete(Position position) {
-        }
-
-        @Override
-        public void asyncDelete(Position position, DeleteCallback callback, Object ctx) {
-        }
-
-        @Override
-        public void delete(Iterable<Position> positions) {
-        }
-
-        @Override
-        public void asyncDelete(Iterable<Position> position, DeleteCallback callback, Object ctx) {
-        }
-
-        @Override
-        public void clearBacklog() {
-        }
-
-        @Override
-        public void asyncClearBacklog(ClearBacklogCallback callback, Object ctx) {
-        }
-
-        @Override
-        public void skipEntries(int numEntriesToSkip, IndividualDeletedEntries deletedEntries) {
-        }
-
-        @Override
-        public void asyncSkipEntries(int numEntriesToSkip, IndividualDeletedEntries deletedEntries,
-                final SkipEntriesCallback callback, Object ctx) {
-        }
-
-        @Override
-        public Position findNewestMatching(Predicate<Entry> condition) {
-            return null;
-        }
-
-        @Override
-        public Position findNewestMatching(FindPositionConstraint constraint, Predicate<Entry> condition) {
-            return null;
-        }
-
-        @Override
-        public void asyncFindNewestMatching(FindPositionConstraint constraint, Predicate<Entry> condition,
-                AsyncCallbacks.FindEntryCallback callback, Object ctx) {
-        }
-
-        @Override
-        public void asyncFindNewestMatching(FindPositionConstraint constraint, Predicate<Entry> condition,
-                AsyncCallbacks.FindEntryCallback callback, Object ctx, boolean isFindFromLedger) {
-        }
-
-        @Override
-        public void asyncResetCursor(final Position position, boolean forceReset,
-                AsyncCallbacks.ResetCursorCallback callback) {
-
-        }
-
-        @Override
-        public void resetCursor(final Position position) {
-
-        }
-
-        @Override
-        public Position getFirstPosition() {
-            return null;
-        }
-
-        @Override
-        public void setAlwaysInactive() {
-        }
-
-        @Override
-        public List<Entry> replayEntries(Set<? extends Position> positions) {
-            return null;
-        }
-
-        @Override
-        public Set<? extends Position> asyncReplayEntries(Set<? extends Position> positions,
-                                                        ReadEntriesCallback callback, Object ctx) {
-            return Sets.newConcurrentHashSet();
-        }
-
-        @Override
-        public Set<? extends Position> asyncReplayEntries(Set<? extends Position> positions,
-                                                        ReadEntriesCallback callback, Object ctx, boolean sortEntries) {
-            return Sets.newConcurrentHashSet();
-        }
-
-        @Override
-        public List<Entry> readEntriesOrWait(int numberOfEntriesToRead) {
-            return null;
-        }
-
-        @Override
-        public void asyncReadEntriesOrWait(int numberOfEntriesToRead, ReadEntriesCallback callback, Object ctx,
-                                           Position maxPosition) {
-        }
-
-        @Override
-        public void asyncReadEntriesOrWait(int maxEntries, long maxSizeBytes, ReadEntriesCallback callback,
-                                           Object ctx, Position maxPosition) {
-
-        }
-
-        @Override
-        public boolean cancelPendingReadRequest() {
-            return true;
-        }
-
-        @Override
-        public Entry getNthEntry(int num, IndividualDeletedEntries deletedEntries) {
-            return null;
-        }
-
-        @Override
-        public void asyncGetNthEntry(int num, IndividualDeletedEntries deletedEntries, ReadEntryCallback callback,
-                Object ctx) {
-        }
-
-        @Override
-        public void setActive() {
-        }
-
-        @Override
-        public void setInactive() {
-        }
-
-        @Override
-        public boolean isActive() {
-            return true;
-        }
-
-        @Override
-        public long getNumberOfEntriesSinceFirstNotAckedMessage() {
-            return 0;
-        }
-
-        @Override
-        public int getTotalNonContiguousDeletedMessagesRange() {
-            return 0;
-        }
-
-        @Override
-        public int getNonContiguousDeletedMessagesRangeSerializedSize() {
-            return 0;
-        }
-
-        @Override
-        public long getEstimatedSizeSinceMarkDeletePosition() {
-            return 0L;
-        }
-
-        @Override
-        public void setThrottleMarkDelete(double throttleMarkDelete) {
-        }
-
-        @Override
-        public double getThrottleMarkDelete() {
-            return -1;
-        }
-
-        @Override
-        public ManagedLedger getManagedLedger() {
-            return null;
-        }
-
-        @Override
-        public Range<Position> getLastIndividualDeletedRange() {
-            return null;
-        }
-
-        @Override
-        public void trimDeletedEntries(List<Entry> entries) {
-
-        }
-
-        @Override
-        public long[] getDeletedBatchIndexesAsLongArray(Position position) {
-            return new long[0];
-        }
-
-        @Override
-        public ManagedCursorMXBean getStats() {
-            return null;
-        }
-
-        @Override
-        public List<Entry> readEntriesOrWait(int maxEntries, long maxSizeBytes) {
-            return null;
-        }
-
-        @Override
-        public boolean checkAndUpdateReadPositionChanged() {
-            return false;
-        }
-
-        @Override
-        public boolean isClosed() {
-            return false;
-        }
-
-        @Override
-        public ManagedLedgerInternalStats.CursorStats getCursorStats() {
-            return null;
-        }
-
-        @Override
-        public boolean isMessageDeleted(Position position) {
-            return false;
-        }
-
-        @Override
-        public ManagedCursor duplicateNonDurableCursor(String nonDurableCursorName) throws ManagedLedgerException {
-            return null;
-        }
-
-        @Override
-        public long[] getBatchPositionAckSet(Position position) {
-            return new long[0];
-        }
-
-        @Override
-        public int applyMaxSizeCap(int maxEntries, long maxSizeBytes) {
-            return 0;
-        }
-
-        @Override
-        public void updateReadStats(int readEntriesCount, long readEntriesSize) {
-
-        }
-
-
-    }
-
     @Test
     public void testSlowestReadPositionForActiveCursors() {
-        ManagedCursorContainer container = new ManagedCursorContainer();
-        assertNull(container.getSlowestReaderPosition());
+        ManagedCursorContainerImpl container = new ManagedCursorContainerImpl();
+        assertNull(container.getSlowestCursorPosition());
 
         // Add no durable cursor
         Position position = PositionFactory.create(5, 5);
@@ -477,7 +48,7 @@ public class ManagedCursorContainerTest {
         doReturn(false).when(cursor1).isDurable();
         doReturn(position).when(cursor1).getReadPosition();
         container.add(cursor1, position);
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(5, 5));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(5, 5));
 
         // Add no durable cursor
         position = PositionFactory.create(1, 1);
@@ -485,45 +56,45 @@ public class ManagedCursorContainerTest {
         doReturn(false).when(cursor2).isDurable();
         doReturn(position).when(cursor2).getReadPosition();
         container.add(cursor2, position);
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(1, 1));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(1, 1));
 
         // Move forward cursor, cursor1 = 5:5, cursor2 = 5:6, slowest is 5:5
         position = PositionFactory.create(5, 6);
         container.cursorUpdated(cursor2, position);
         doReturn(position).when(cursor2).getReadPosition();
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(5, 5));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(5, 5));
 
         // Move forward cursor, cursor1 = 5:8, cursor2 = 5:6, slowest is 5:6
         position = PositionFactory.create(5, 8);
         doReturn(position).when(cursor1).getReadPosition();
         container.cursorUpdated(cursor1, position);
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(5, 6));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(5, 6));
 
         // Remove cursor, only cursor1 left, cursor1 = 5:8
         container.removeCursor(cursor2.getName());
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(5, 8));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(5, 8));
     }
 
     @Test
     public void simple() throws Exception {
-        ManagedCursorContainer container = new ManagedCursorContainer();
-        assertNull(container.getSlowestReaderPosition());
+        ManagedCursorContainerImpl container = new ManagedCursorContainerImpl();
+        assertNull(container.getSlowestCursorPosition());
 
         ManagedCursor cursor1 = new MockManagedCursor(container, "test1", PositionFactory.create(5, 5));
         container.add(cursor1, cursor1.getMarkDeletedPosition());
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(5, 5));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(5, 5));
         assertEqualsCursorAndPosition(container.getCursorWithOldestPosition(),
                 cursor1, PositionFactory.create(5, 5));
 
         ManagedCursor cursor2 = new MockManagedCursor(container, "test2", PositionFactory.create(2, 2));
         container.add(cursor2, cursor2.getMarkDeletedPosition());
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(2, 2));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(2, 2));
         assertEqualsCursorAndPosition(container.getCursorWithOldestPosition(),
                 cursor2, PositionFactory.create(2, 2));
 
         ManagedCursor cursor3 = new MockManagedCursor(container, "test3", PositionFactory.create(2, 0));
         container.add(cursor3, cursor3.getMarkDeletedPosition());
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(2, 0));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(2, 0));
         assertEqualsCursorAndPosition(container.getCursorWithOldestPosition(),
                 cursor3, PositionFactory.create(2, 0));
 
@@ -531,64 +102,64 @@ public class ManagedCursorContainerTest {
 
         ManagedCursor cursor4 = new MockManagedCursor(container, "test4", PositionFactory.create(4, 0));
         container.add(cursor4, cursor4.getMarkDeletedPosition());
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(2, 0));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(2, 0));
 
         ManagedCursor cursor5 = new MockManagedCursor(container, "test5", PositionFactory.create(3, 5));
         container.add(cursor5, cursor5.getMarkDeletedPosition());
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(2, 0));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(2, 0));
 
         cursor3.markDelete(PositionFactory.create(3, 0));
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(2, 2));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(2, 2));
         assertEqualsCursorAndPosition(container.getCursorWithOldestPosition(),
                 cursor2, PositionFactory.create(2, 2));
 
         cursor2.markDelete(PositionFactory.create(10, 5));
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(3, 0));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(3, 0));
 
         container.removeCursor(cursor3.getName());
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(3, 5));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(3, 5));
 
         container.removeCursor(cursor2.getName());
         container.removeCursor(cursor5.getName());
         container.removeCursor(cursor1.getName());
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(4, 0));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(4, 0));
         assertEqualsCursorAndPosition(container.getCursorWithOldestPosition(),
                 cursor4, PositionFactory.create(4, 0));
 
         assertTrue(container.hasDurableCursors());
 
         container.removeCursor(cursor4.getName());
-        assertNull(container.getSlowestReaderPosition());
+        assertNull(container.getSlowestCursorPosition());
 
         assertFalse(container.hasDurableCursors());
 
         ManagedCursor cursor6 = new MockManagedCursor(container, "test6", PositionFactory.create(6, 5));
         container.add(cursor6, cursor6.getMarkDeletedPosition());
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(6, 5));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(6, 5));
 
         assertEquals(container.toString(), "[test6=6:5/null]");
     }
 
     @Test
     public void updatingCursorOutsideContainer() {
-        ManagedCursorContainer container = new ManagedCursorContainer();
+        ManagedCursorContainerImpl container = new ManagedCursorContainerImpl();
 
         ManagedCursor cursor1 = new MockManagedCursor(container, "test1", PositionFactory.create(5, 5));
         container.add(cursor1, cursor1.getMarkDeletedPosition());
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(5, 5));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(5, 5));
 
         MockManagedCursor cursor2 = new MockManagedCursor(container, "test2", PositionFactory.create(2, 2));
         container.add(cursor2, cursor2.getMarkDeletedPosition());
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(2, 2));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(2, 2));
 
         cursor2.markDeletePosition = PositionFactory.create(8, 8);
 
         // Until we don't update the container, the ordering will not change
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(2, 2));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(2, 2));
 
         container.cursorUpdated(cursor2, cursor2.markDeletePosition);
 
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(5, 5));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(5, 5));
         assertEqualsCursorAndPosition(container.getCursorWithOldestPosition(),
                 cursor1, PositionFactory.create(5, 5));
     }
@@ -602,21 +173,21 @@ public class ManagedCursorContainerTest {
 
     @Test
     public void removingCursor() {
-        ManagedCursorContainer container = new ManagedCursorContainer();
+        ManagedCursorContainerImpl container = new ManagedCursorContainerImpl();
 
         ManagedCursor cursor1 = new MockManagedCursor(container, "test1", PositionFactory.create(5, 5));
         container.add(cursor1, cursor1.getMarkDeletedPosition());
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(5, 5));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(5, 5));
         assertEquals(container.get("test1"), cursor1);
 
         MockManagedCursor cursor2 = new MockManagedCursor(container, "test2", PositionFactory.create(2, 2));
         container.add(cursor2, cursor2.getMarkDeletedPosition());
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(2, 2));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(2, 2));
         assertEquals(container.get("test2"), cursor2);
 
         MockManagedCursor cursor3 = new MockManagedCursor(container, "test3", PositionFactory.create(1, 1));
         container.add(cursor3, cursor3.getMarkDeletedPosition());
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(1, 1));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(1, 1));
         assertEquals(container.get("test3"), cursor3);
 
         assertEquals(container, Lists.newArrayList(cursor1, cursor2, cursor3));
@@ -628,18 +199,18 @@ public class ManagedCursorContainerTest {
 
         assertNull(container.get("test2"));
 
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(1, 1));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(1, 1));
 
         container.removeCursor("test3");
 
         assertEquals(container, Lists.newArrayList(cursor1));
 
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(5, 5));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(5, 5));
     }
 
     @Test
     public void ordering() throws Exception {
-        ManagedCursorContainer container = new ManagedCursorContainer();
+        ManagedCursorContainerImpl container = new ManagedCursorContainerImpl();
 
         ManagedCursor cursor1 = new MockManagedCursor(container, "test1", PositionFactory.create(5, 5));
         ManagedCursor cursor2 = new MockManagedCursor(container, "test2", PositionFactory.create(5, 1));
@@ -653,19 +224,19 @@ public class ManagedCursorContainerTest {
         container.add(cursor4, cursor4.getMarkDeletedPosition());
         container.add(cursor5, cursor5.getMarkDeletedPosition());
 
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(5, 1));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(5, 1));
         container.removeCursor("test2");
 
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(5, 5));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(5, 5));
         container.removeCursor("test1");
 
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(6, 4));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(6, 4));
         container.removeCursor("test4");
 
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(7, 0));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(7, 0));
         container.removeCursor("test5");
 
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(7, 1));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(7, 1));
         container.removeCursor("test3");
 
         assertFalse(container.hasDurableCursors());
@@ -673,7 +244,7 @@ public class ManagedCursorContainerTest {
 
     @Test
     public void orderingWithUpdates() {
-        ManagedCursorContainer container = new ManagedCursorContainer();
+        ManagedCursorContainerImpl container = new ManagedCursorContainerImpl();
 
         MockManagedCursor c1 = new MockManagedCursor(container, "test1", PositionFactory.create(5, 5));
         MockManagedCursor c2 = new MockManagedCursor(container, "test2", PositionFactory.create(5, 1));
@@ -687,50 +258,50 @@ public class ManagedCursorContainerTest {
         container.add(c4, c4.getMarkDeletedPosition());
         container.add(c5, c5.getMarkDeletedPosition());
 
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(5, 1));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(5, 1));
 
         c1.markDeletePosition = PositionFactory.create(5, 8);
         container.cursorUpdated(c1, c1.markDeletePosition);
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(5, 1));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(5, 1));
 
         c2.markDeletePosition = PositionFactory.create(5, 6);
         container.cursorUpdated(c2, c2.markDeletePosition);
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(5, 6));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(5, 6));
 
         c1.markDeletePosition = PositionFactory.create(6, 8);
         container.cursorUpdated(c1, c1.markDeletePosition);
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(5, 6));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(5, 6));
 
         c3.markDeletePosition = PositionFactory.create(8, 5);
         container.cursorUpdated(c3, c3.markDeletePosition);
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(5, 6));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(5, 6));
 
         c1.markDeletePosition = PositionFactory.create(8, 4);
         container.cursorUpdated(c1, c1.markDeletePosition);
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(5, 6));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(5, 6));
 
         c2.markDeletePosition = PositionFactory.create(8, 4);
         container.cursorUpdated(c2, c2.markDeletePosition);
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(6, 4));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(6, 4));
 
         c4.markDeletePosition = PositionFactory.create(7, 1);
         container.cursorUpdated(c4, c4.markDeletePosition);
 
         // ////
 
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(7, 0));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(7, 0));
         container.removeCursor("test5");
 
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(7, 1));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(7, 1));
         container.removeCursor("test4");
 
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(8, 4));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(8, 4));
         container.removeCursor("test1");
 
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(8, 4));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(8, 4));
         container.removeCursor("test2");
 
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(8, 5));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(8, 5));
         container.removeCursor("test3");
 
         assertFalse(container.hasDurableCursors());
@@ -738,7 +309,7 @@ public class ManagedCursorContainerTest {
 
     @Test
     public void orderingWithUpdatesAndReset() {
-        ManagedCursorContainer container = new ManagedCursorContainer();
+        ManagedCursorContainerImpl container = new ManagedCursorContainerImpl();
 
         MockManagedCursor c1 = new MockManagedCursor(container, "test1", PositionFactory.create(5, 5));
         MockManagedCursor c2 = new MockManagedCursor(container, "test2", PositionFactory.create(5, 1));
@@ -752,50 +323,50 @@ public class ManagedCursorContainerTest {
         container.add(c4, c4.getMarkDeletedPosition());
         container.add(c5, c5.getMarkDeletedPosition());
 
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(5, 1));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(5, 1));
 
         c1.markDeletePosition = PositionFactory.create(5, 8);
         container.cursorUpdated(c1, c1.markDeletePosition);
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(5, 1));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(5, 1));
 
         c1.markDeletePosition = PositionFactory.create(5, 6);
         container.cursorUpdated(c1, c1.markDeletePosition);
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(5, 1));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(5, 1));
 
         c2.markDeletePosition = PositionFactory.create(6, 8);
         container.cursorUpdated(c2, c2.markDeletePosition);
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(5, 6));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(5, 6));
 
         c3.markDeletePosition = PositionFactory.create(8, 5);
         container.cursorUpdated(c3, c3.markDeletePosition);
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(5, 6));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(5, 6));
 
         c1.markDeletePosition = PositionFactory.create(8, 4);
         container.cursorUpdated(c1, c1.markDeletePosition);
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(6, 4));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(6, 4));
 
         c2.markDeletePosition = PositionFactory.create(4, 4);
         container.cursorUpdated(c2, c2.markDeletePosition);
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(4, 4));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(4, 4));
 
         c4.markDeletePosition = PositionFactory.create(7, 1);
         container.cursorUpdated(c4, c4.markDeletePosition);
 
         // ////
 
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(4, 4));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(4, 4));
         container.removeCursor("test2");
 
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(7, 0));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(7, 0));
         container.removeCursor("test5");
 
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(7, 1));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(7, 1));
         container.removeCursor("test1");
 
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(7, 1));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(7, 1));
         container.removeCursor("test4");
 
-        assertEquals(container.getSlowestReaderPosition(), PositionFactory.create(8, 5));
+        assertEquals(container.getSlowestCursorPosition(), PositionFactory.create(8, 5));
         container.removeCursor("test3");
 
         assertFalse(container.hasDurableCursors());
@@ -827,7 +398,7 @@ public class ManagedCursorContainerTest {
 
     @Test
     public void testVersions() {
-        ManagedCursorContainer container = new ManagedCursorContainer();
+        ManagedCursorContainerImpl container = new ManagedCursorContainerImpl();
 
         MockManagedCursor c1 = new MockManagedCursor(container, "test1", PositionFactory.create(5, 5));
         MockManagedCursor c2 = new MockManagedCursor(container, "test2", PositionFactory.create(5, 1));
@@ -857,18 +428,14 @@ public class ManagedCursorContainerTest {
     public void testSlowestReader() {
         // test 100 times
         for (int i = 0; i < 100; i++) {
-            ManagedCursorContainer container = new ManagedCursorContainer();
+            ManagedCursorContainerImpl container = new ManagedCursorContainerImpl();
             List<ManagedCursor> cursors = IntStream.rangeClosed(1, 100)
                     .mapToObj(idx -> createCursor(container, "cursor" + idx, PositionFactory.create(0, idx)))
                     .collect(Collectors.toList());
             // randomize adding order
             Collections.shuffle(cursors);
             cursors.forEach(cursor -> container.add(cursor, cursor.getReadPosition()));
-            assertEquals(container.getSlowestReader().getName(), "cursor1");
+            assertEquals(container.getSlowestCursor().getName(), "cursor1");
         }
     }
-
-    private static ManagedCursor createCursor(ManagedCursorContainer container, String name, Position position) {
-        return new MockManagedCursor(container, name, position, position, false, true);
-    }
 }
diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java
index b77cd8dbf62..23b739987f4 100644
--- a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java
+++ b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java
@@ -277,7 +277,7 @@ public class ManagedCursorTest extends MockedBookKeeperTestCase {
         cursor.markDelete(lastEntry);
 
         // Assert persist mark deleted position to ZK was successful.
-        Position slowestReadPosition = ml.getCursors().getSlowestReaderPosition();
+        Position slowestReadPosition = ml.getCursors().getSlowestCursorPosition();
         assertTrue(slowestReadPosition.getLedgerId() >= lastEntry.getLedgerId());
         assertTrue(slowestReadPosition.getEntryId() >= lastEntry.getEntryId());
         assertEquals(cursor.getStats().getPersistLedgerSucceed(), 0);
@@ -375,7 +375,7 @@ public class ManagedCursorTest extends MockedBookKeeperTestCase {
         assertTrue(persistZookeeperSucceed2 > persistZookeeperSucceed1);
 
         // Assert persist mark deleted position to ZK was successful.
-        Position slowestReadPosition = ml.getCursors().getSlowestReaderPosition();
+        Position slowestReadPosition = ml.getCursors().getSlowestCursorPosition();
         assertTrue(slowestReadPosition.getLedgerId() >= lastEntry.getLedgerId());
         assertTrue(slowestReadPosition.getEntryId() >= lastEntry.getEntryId());
         assertEquals(cursor.getPersistentMarkDeletedPosition(), lastEntry);
@@ -2936,7 +2936,7 @@ public class ManagedCursorTest extends MockedBookKeeperTestCase {
     }
 
     private static EntryImpl createEntryAndReleaseBuffer(Position p1, ByteBuf buffer) {
-        EntryImpl entry = EntryImpl.create(p1, buffer);
+        EntryImpl entry = EntryImpl.create(p1, buffer, 0);
         buffer.release();
         return entry;
     }
diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerTest.java b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerTest.java
index b8c268a5ba4..a26dba1aedd 100644
--- a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerTest.java
+++ b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerTest.java
@@ -538,7 +538,7 @@ public class ManagedLedgerTest extends MockedBookKeeperTestCase {
         Thread.sleep(1000 + 2 * factory.getConfig().getCacheEvictionIntervalMs());
 
         ManagedCursorContainer activeCursors = (ManagedCursorContainer) ledger.getActiveCursors();
-        assertEquals(activeCursors.getSlowestReaderPosition(), lastMarkDeletePos);
+        assertEquals(activeCursors.getSlowestCursorPosition(), lastMarkDeletePos);
 
         // THEN it is expected that the cache evicts entries to the earliest read position
         int evictedEntriesCount =
@@ -1852,6 +1852,7 @@ public class ManagedLedgerTest extends MockedBookKeeperTestCase {
         ManagedLedgerConfig config = new ManagedLedgerConfig();
         initManagedLedgerConfig(config);
         config.setCacheEvictionByMarkDeletedPosition(true);
+        config.setCacheEvictionByExpectedReadCount(false);
         ManagedLedgerImpl ledger =
                 (ManagedLedgerImpl) factory.open("my_test_ledger_for_invalidateEntriesFromCacheByMarkDeletePosition",
                         config);
diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/MockManagedCursor.java b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/MockManagedCursor.java
new file mode 100644
index 00000000000..6dfe49b099a
--- /dev/null
+++ b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/MockManagedCursor.java
@@ -0,0 +1,461 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.bookkeeper.mledger.impl;
+
+import static org.testng.Assert.fail;
+import com.google.common.collect.Range;
+import com.google.common.collect.Sets;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Predicate;
+import org.apache.bookkeeper.mledger.AsyncCallbacks;
+import org.apache.bookkeeper.mledger.Entry;
+import org.apache.bookkeeper.mledger.ManagedCursor;
+import org.apache.bookkeeper.mledger.ManagedCursorMXBean;
+import org.apache.bookkeeper.mledger.ManagedLedger;
+import org.apache.bookkeeper.mledger.ManagedLedgerException;
+import org.apache.bookkeeper.mledger.Position;
+import org.apache.pulsar.common.policies.data.ManagedLedgerInternalStats;
+
+class MockManagedCursor implements ManagedCursor {
+
+    ActiveManagedCursorContainer container;
+    Position markDeletePosition;
+    Position readPosition;
+    private final boolean updateMarkDeletePosition;
+    private final boolean durable;
+    String name;
+
+    MockManagedCursor(ActiveManagedCursorContainer container, String name, Position markDeletePosition) {
+        this(container, name, markDeletePosition, null, true, true);
+    }
+
+    MockManagedCursor(ActiveManagedCursorContainer container, String name, Position markDeletePosition,
+                      Position readPosition, boolean updateMarkDeletePosition, boolean durable) {
+        this.container = container;
+        this.name = name;
+        this.markDeletePosition = markDeletePosition;
+        this.readPosition = readPosition;
+        this.updateMarkDeletePosition = updateMarkDeletePosition;
+        this.durable = durable;
+    }
+
+    static MockManagedCursor createCursor(ActiveManagedCursorContainer container, String name,
+                                          Position position) {
+        return new MockManagedCursor(container, name, position, position, false, true);
+    }
+
+    static void addCursor(ActiveManagedCursorContainer container, String name, Position position) {
+        container.add(createCursor(container, name, position), position);
+    }
+
+    @Override
+    public Map<String, Long> getProperties() {
+        return Collections.emptyMap();
+    }
+
+    @Override
+    public Map<String, String> getCursorProperties() {
+        return Collections.emptyMap();
+    }
+
+    @Override
+    public CompletableFuture<Void> putCursorProperty(String key, String value) {
+        return CompletableFuture.completedFuture(null);
+    }
+
+    @Override
+    public CompletableFuture<Void> setCursorProperties(Map<String, String> cursorProperties) {
+        return CompletableFuture.completedFuture(null);
+    }
+
+    @Override
+    public CompletableFuture<Void> removeCursorProperty(String key) {
+        return CompletableFuture.completedFuture(null);
+    }
+
+    @Override
+    public boolean putProperty(String key, Long value) {
+        return false;
+    }
+
+    @Override
+    public boolean removeProperty(String key) {
+        return false;
+    }
+
+    @Override
+    public boolean isDurable() {
+        return durable;
+    }
+
+    @Override
+    public List<Entry> readEntries(int numberOfEntriesToRead) {
+        return new ArrayList<Entry>();
+    }
+
+    @Override
+    public void asyncReadEntries(int numberOfEntriesToRead, AsyncCallbacks.ReadEntriesCallback callback, Object ctx,
+                                 Position maxPosition) {
+        callback.readEntriesComplete(null, ctx);
+    }
+
+    @Override
+    public void asyncReadEntries(int numberOfEntriesToRead, long maxSizeBytes,
+                                 AsyncCallbacks.ReadEntriesCallback callback,
+                                 Object ctx, Position maxPosition) {
+        callback.readEntriesComplete(null, ctx);
+    }
+
+    @Override
+    public boolean hasMoreEntries() {
+        return true;
+    }
+
+    @Override
+    public long getNumberOfEntries() {
+        return 0;
+    }
+
+    @Override
+    public long getNumberOfEntriesInBacklog(boolean isPrecise) {
+        return 0;
+    }
+
+    @Override
+    public void markDelete(Position position) {
+        markDelete(position, Collections.emptyMap());
+    }
+
+    @Override
+    public void markDelete(Position position, Map<String, Long> properties) {
+        this.markDeletePosition = position;
+        if (updateMarkDeletePosition) {
+            container.cursorUpdated(this, position);
+        }
+    }
+
+    @Override
+    public void asyncMarkDelete(Position position, AsyncCallbacks.MarkDeleteCallback callback, Object ctx) {
+        fail();
+    }
+
+    @Override
+    public void asyncMarkDelete(Position position, Map<String, Long> properties,
+                                AsyncCallbacks.MarkDeleteCallback callback, Object ctx) {
+        fail();
+    }
+
+    @Override
+    public Position getMarkDeletedPosition() {
+        return markDeletePosition;
+    }
+
+    @Override
+    public Position getPersistentMarkDeletedPosition() {
+        return markDeletePosition;
+    }
+
+    @Override
+    public String getName() {
+        return name;
+    }
+
+    @Override
+    public long getLastActive() {
+        return System.currentTimeMillis();
+    }
+
+    @Override
+    public void updateLastActive() {
+        // no-op
+    }
+
+    @Override
+    public String toString() {
+        return String.format("%s=%s/%s", name, markDeletePosition, readPosition);
+    }
+
+    @Override
+    public Position getReadPosition() {
+        return readPosition;
+    }
+
+    @Override
+    public void rewind() {
+    }
+
+    @Override
+    public void seek(Position newReadPosition, boolean force) {
+        this.readPosition = newReadPosition;
+        if (!updateMarkDeletePosition) {
+            container.updateCursor(this, newReadPosition);
+        }
+    }
+
+    @Override
+    public void close() {
+    }
+
+    @Override
+    public void asyncClose(AsyncCallbacks.CloseCallback callback, Object ctx) {
+    }
+
+    @Override
+    public void delete(Position position) {
+    }
+
+    @Override
+    public void asyncDelete(Position position, AsyncCallbacks.DeleteCallback callback, Object ctx) {
+    }
+
+    @Override
+    public void delete(Iterable<Position> positions) {
+    }
+
+    @Override
+    public void asyncDelete(Iterable<Position> position, AsyncCallbacks.DeleteCallback callback, Object ctx) {
+    }
+
+    @Override
+    public void clearBacklog() {
+    }
+
+    @Override
+    public void asyncClearBacklog(AsyncCallbacks.ClearBacklogCallback callback, Object ctx) {
+    }
+
+    @Override
+    public void skipEntries(int numEntriesToSkip, IndividualDeletedEntries deletedEntries) {
+    }
+
+    @Override
+    public void asyncSkipEntries(int numEntriesToSkip, IndividualDeletedEntries deletedEntries,
+                                 final AsyncCallbacks.SkipEntriesCallback callback, Object ctx) {
+    }
+
+    @Override
+    public Position findNewestMatching(Predicate<Entry> condition) {
+        return null;
+    }
+
+    @Override
+    public Position findNewestMatching(FindPositionConstraint constraint, Predicate<Entry> condition) {
+        return null;
+    }
+
+    @Override
+    public void asyncFindNewestMatching(FindPositionConstraint constraint, Predicate<Entry> condition,
+                                        AsyncCallbacks.FindEntryCallback callback, Object ctx) {
+    }
+
+    @Override
+    public void asyncFindNewestMatching(FindPositionConstraint constraint, Predicate<Entry> condition,
+                                        AsyncCallbacks.FindEntryCallback callback, Object ctx,
+                                        boolean isFindFromLedger) {
+    }
+
+    @Override
+    public void asyncResetCursor(final Position position, boolean forceReset,
+                                 AsyncCallbacks.ResetCursorCallback callback) {
+
+    }
+
+    @Override
+    public void resetCursor(final Position position) {
+
+    }
+
+    @Override
+    public Position getFirstPosition() {
+        return null;
+    }
+
+    @Override
+    public void setAlwaysInactive() {
+    }
+
+    @Override
+    public List<Entry> replayEntries(Set<? extends Position> positions) {
+        return null;
+    }
+
+    @Override
+    public Set<? extends Position> asyncReplayEntries(Set<? extends Position> positions,
+                                                      AsyncCallbacks.ReadEntriesCallback callback, Object ctx) {
+        return Sets.newConcurrentHashSet();
+    }
+
+    @Override
+    public Set<? extends Position> asyncReplayEntries(Set<? extends Position> positions,
+                                                      AsyncCallbacks.ReadEntriesCallback callback, Object ctx,
+                                                      boolean sortEntries) {
+        return Sets.newConcurrentHashSet();
+    }
+
+    @Override
+    public List<Entry> readEntriesOrWait(int numberOfEntriesToRead) {
+        return null;
+    }
+
+    @Override
+    public void asyncReadEntriesOrWait(int numberOfEntriesToRead, AsyncCallbacks.ReadEntriesCallback callback,
+                                       Object ctx, Position maxPosition) {
+    }
+
+    @Override
+    public void asyncReadEntriesOrWait(int maxEntries, long maxSizeBytes,
+                                       AsyncCallbacks.ReadEntriesCallback callback, Object ctx,
+                                       Position maxPosition) {
+
+    }
+
+    @Override
+    public boolean cancelPendingReadRequest() {
+        return true;
+    }
+
+    @Override
+    public Entry getNthEntry(int num, IndividualDeletedEntries deletedEntries) {
+        return null;
+    }
+
+    @Override
+    public void asyncGetNthEntry(int num, IndividualDeletedEntries deletedEntries,
+                                 AsyncCallbacks.ReadEntryCallback callback, Object ctx) {
+    }
+
+    @Override
+    public void setActive() {
+    }
+
+    @Override
+    public void setInactive() {
+    }
+
+    @Override
+    public boolean isActive() {
+        return true;
+    }
+
+    @Override
+    public long getNumberOfEntriesSinceFirstNotAckedMessage() {
+        return 0;
+    }
+
+    @Override
+    public int getTotalNonContiguousDeletedMessagesRange() {
+        return 0;
+    }
+
+    @Override
+    public int getNonContiguousDeletedMessagesRangeSerializedSize() {
+        return 0;
+    }
+
+    @Override
+    public long getEstimatedSizeSinceMarkDeletePosition() {
+        return 0L;
+    }
+
+    @Override
+    public void setThrottleMarkDelete(double throttleMarkDelete) {
+    }
+
+    @Override
+    public double getThrottleMarkDelete() {
+        return -1;
+    }
+
+    @Override
+    public ManagedLedger getManagedLedger() {
+        return null;
+    }
+
+    @Override
+    public Range<Position> getLastIndividualDeletedRange() {
+        return null;
+    }
+
+    @Override
+    public void trimDeletedEntries(List<Entry> entries) {
+
+    }
+
+    @Override
+    public long[] getDeletedBatchIndexesAsLongArray(Position position) {
+        return new long[0];
+    }
+
+    @Override
+    public ManagedCursorMXBean getStats() {
+        return null;
+    }
+
+    @Override
+    public List<Entry> readEntriesOrWait(int maxEntries, long maxSizeBytes) {
+        return null;
+    }
+
+    @Override
+    public boolean checkAndUpdateReadPositionChanged() {
+        return false;
+    }
+
+    @Override
+    public boolean isClosed() {
+        return false;
+    }
+
+    @Override
+    public ManagedLedgerInternalStats.CursorStats getCursorStats() {
+        return null;
+    }
+
+    @Override
+    public boolean isMessageDeleted(Position position) {
+        return false;
+    }
+
+    @Override
+    public ManagedCursor duplicateNonDurableCursor(String nonDurableCursorName) throws ManagedLedgerException {
+        return null;
+    }
+
+    @Override
+    public long[] getBatchPositionAckSet(Position position) {
+        return new long[0];
+    }
+
+    @Override
+    public int applyMaxSizeCap(int maxEntries, long maxSizeBytes) {
+        return 0;
+    }
+
+    @Override
+    public void updateReadStats(int readEntriesCount, long readEntriesSize) {
+
+    }
+
+
+}
diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/NonDurableCursorTest.java b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/NonDurableCursorTest.java
index 0bb1b0fc972..a7fe289f56c 100644
--- a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/NonDurableCursorTest.java
+++ b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/NonDurableCursorTest.java
@@ -666,31 +666,31 @@ public class NonDurableCursorTest extends MockedBookKeeperTestCase {
 
         ManagedCursor cursor1 = ledger.openCursor(c1);
         cursor1.seek(p3);
-        assertEquals(p3, ledger.getCursors().getSlowestReaderPosition());
+        assertEquals(p3, ledger.getCursors().getSlowestCursorPosition());
 
         ManagedCursor nonCursor1 = ledger.newNonDurableCursor(p2, nc1);
         // The slowest reader should still be the durable cursor since non-durable readers are not taken into account
-        assertEquals(p3, ledger.getCursors().getSlowestReaderPosition());
+        assertEquals(p3, ledger.getCursors().getSlowestCursorPosition());
 
         Position earliestPos = PositionFactory.create(-1, -2);
 
         ManagedCursor nonCursorEarliest = ledger.newNonDurableCursor(earliestPos, ncEarliest);
 
         // The slowest reader should still be the durable cursor since non-durable readers are not taken into account
-        assertEquals(p3, ledger.getCursors().getSlowestReaderPosition());
+        assertEquals(p3, ledger.getCursors().getSlowestCursorPosition());
 
         // move non-durable cursor should NOT update the slowest reader position
         nonCursorEarliest.markDelete(p1);
-        assertEquals(p3, ledger.getCursors().getSlowestReaderPosition());
+        assertEquals(p3, ledger.getCursors().getSlowestCursorPosition());
 
         nonCursorEarliest.markDelete(p2);
-        assertEquals(p3, ledger.getCursors().getSlowestReaderPosition());
+        assertEquals(p3, ledger.getCursors().getSlowestCursorPosition());
 
         nonCursorEarliest.markDelete(p3);
-        assertEquals(p3, ledger.getCursors().getSlowestReaderPosition());
+        assertEquals(p3, ledger.getCursors().getSlowestCursorPosition());
 
         nonCursor1.markDelete(p3);
-        assertEquals(p3, ledger.getCursors().getSlowestReaderPosition());
+        assertEquals(p3, ledger.getCursors().getSlowestCursorPosition());
 
         ledger.close();
     }
diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/cache/PendingReadsManagerTest.java b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/cache/PendingReadsManagerTest.java
index 5656aa7cac1..9c6c79eb95b 100644
--- a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/cache/PendingReadsManagerTest.java
+++ b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/cache/PendingReadsManagerTest.java
@@ -21,13 +21,14 @@ package org.apache.bookkeeper.mledger.impl.cache;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyBoolean;
 import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.ArgumentMatchers.argThat;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertSame;
 import static org.testng.Assert.assertTrue;
-import static org.testng.AssertJUnit.assertSame;
 import io.opentelemetry.api.OpenTelemetry;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
@@ -43,6 +44,7 @@ import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.function.BiConsumer;
 import java.util.function.BiFunction;
+import java.util.function.IntSupplier;
 import java.util.stream.Collectors;
 import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
@@ -112,14 +114,14 @@ public class PendingReadsManagerTest  {
                 ReadHandle rh = invocationOnMock.getArgument(0);
                 long startEntry = invocationOnMock.getArgument(1);
                 long endEntry = invocationOnMock.getArgument(2);
-                boolean shouldCacheEntry = invocationOnMock.getArgument(3);
+                IntSupplier expectedReadCount = invocationOnMock.getArgument(3);
                 AsyncCallbacks.ReadEntriesCallback callback = invocationOnMock.getArgument(4);
                 Object ctx = invocationOnMock.getArgument(5);
-                pendingReadsManager.readEntries(lh, startEntry, endEntry, shouldCacheEntry, callback, ctx);
+                pendingReadsManager.readEntries(lh, startEntry, endEntry, expectedReadCount, callback, ctx);
                 return null;
             }
         }).when(rangeEntryCache).asyncReadEntry0(any(), anyLong(), anyLong(),
-                anyBoolean(), any(), any(), anyBoolean());
+                any(), any(), any(), anyBoolean());
 
         lh = mock(ReadHandle.class);
         ml = mock(ManagedLedgerImpl.class);
@@ -176,17 +178,17 @@ public class PendingReadsManagerTest  {
     private static class PreparedReadFromStorage extends CompletableFuture<List<EntryImpl>> {
         final long firstEntry;
         final long endEntry;
-        final boolean shouldCacheEntry;
+        final IntSupplier expectedReadCount;
 
-        public PreparedReadFromStorage(long firstEntry, long endEntry, boolean shouldCacheEntry) {
+        public PreparedReadFromStorage(long firstEntry, long endEntry, IntSupplier expectedReadCount) {
             this.firstEntry = firstEntry;
             this.endEntry = endEntry;
-            this.shouldCacheEntry = shouldCacheEntry;
+            this.expectedReadCount = expectedReadCount;
         }
 
         @Override
         public String toString() {
-            return "PreparedReadFromStorage(" + firstEntry + "," + endEntry + "," + shouldCacheEntry + ")";
+            return "PreparedReadFromStorage(" + firstEntry + "," + endEntry + "," + expectedReadCount + ")";
         }
 
         public void storageReadCompleted() {
@@ -195,13 +197,16 @@ public class PendingReadsManagerTest  {
     }
 
     private PreparedReadFromStorage prepareReadFromStorage(ReadHandle lh, RangeEntryCacheImpl rangeEntryCache,
-                                                     long firstEntry, long endEntry, boolean shouldCacheEntry) {
-        PreparedReadFromStorage read = new PreparedReadFromStorage(firstEntry, endEntry, shouldCacheEntry);
-        log.info("prepareReadFromStorage from {} to {} shouldCacheEntry {}", firstEntry, endEntry, shouldCacheEntry);
-        when(rangeEntryCache.readFromStorage(eq(lh), eq(firstEntry), eq(endEntry), eq(shouldCacheEntry))).thenAnswer(
+                                                           long firstEntry, long endEntry,
+                                                           IntSupplier expectedReadCount) {
+        PreparedReadFromStorage read = new PreparedReadFromStorage(firstEntry, endEntry, expectedReadCount);
+        log.info("prepareReadFromStorage from {} to {} expectedReadCount {}", firstEntry, endEntry, expectedReadCount);
+        when(rangeEntryCache.readFromStorage(eq(lh), eq(firstEntry), eq(endEntry),
+                argThat(expectedReadCountArg -> expectedReadCountArg.getAsInt()
+                        == expectedReadCount.getAsInt()))).thenAnswer(
                 (invocationOnMock -> {
-                    log.info("readFromStorage from {} to {} shouldCacheEntry {}", firstEntry, endEntry,
-                            shouldCacheEntry);
+                    log.info("readFromStorage from {} to {} expectedReadCount {}", firstEntry, endEntry,
+                            expectedReadCount);
                     entryRangeReadCount.computeIfAbsent(Pair.of(firstEntry, endEntry), __ -> new AtomicInteger(0))
                             .getAndIncrement();
                     return read;
@@ -215,13 +220,13 @@ public class PendingReadsManagerTest  {
 
         long firstEntry = 100;
         long endEntry = 199;
-        boolean shouldCacheEntry = false;
+        IntSupplier expectedReadCount = () -> 0;
 
         PreparedReadFromStorage read1 =
-                prepareReadFromStorage(lh, rangeEntryCache, firstEntry, endEntry, shouldCacheEntry);
+                prepareReadFromStorage(lh, rangeEntryCache, firstEntry, endEntry, expectedReadCount);
 
         CapturingReadEntriesCallback callback = new CapturingReadEntriesCallback();
-        pendingReadsManager.readEntries(lh, firstEntry, endEntry, shouldCacheEntry, callback, CTX);
+        pendingReadsManager.readEntries(lh, firstEntry, endEntry, expectedReadCount, callback, CTX);
 
         // complete the read
         read1.storageReadCompleted();
@@ -240,17 +245,17 @@ public class PendingReadsManagerTest  {
 
         long firstEntry = 100;
         long endEntry = 199;
-        boolean shouldCacheEntry = false;
+        IntSupplier expectedReadCount = () -> 0;
 
         PreparedReadFromStorage read1 =
-                prepareReadFromStorage(lh, rangeEntryCache, firstEntry, endEntry, shouldCacheEntry);
+                prepareReadFromStorage(lh, rangeEntryCache, firstEntry, endEntry, expectedReadCount);
 
         PendingReadsManager pendingReadsManager = new PendingReadsManager(rangeEntryCache);
         CapturingReadEntriesCallback callback = new CapturingReadEntriesCallback();
-        pendingReadsManager.readEntries(lh, firstEntry, endEntry, shouldCacheEntry, callback, CTX);
+        pendingReadsManager.readEntries(lh, firstEntry, endEntry, expectedReadCount, callback, CTX);
 
         CapturingReadEntriesCallback callback2 = new CapturingReadEntriesCallback();
-        pendingReadsManager.readEntries(lh, firstEntry, endEntry, shouldCacheEntry, callback2, CTX2);
+        pendingReadsManager.readEntries(lh, firstEntry, endEntry, expectedReadCount, callback2, CTX2);
 
         // complete the read from BK
         // only one read completes 2 callbacks
@@ -282,19 +287,19 @@ public class PendingReadsManagerTest  {
         long firstEntrySecondRead = firstEntry + 10;
         long endEntrySecondRead = endEntry - 10;
 
-        boolean shouldCacheEntry = false;
+        IntSupplier expectedReadCount = () -> 0;
 
         PreparedReadFromStorage read1 =
-                prepareReadFromStorage(lh, rangeEntryCache, firstEntry, endEntry, shouldCacheEntry);
+                prepareReadFromStorage(lh, rangeEntryCache, firstEntry, endEntry, expectedReadCount);
 
         PendingReadsManager pendingReadsManager = new PendingReadsManager(rangeEntryCache);
         CapturingReadEntriesCallback callback = new CapturingReadEntriesCallback();
-        pendingReadsManager.readEntries(lh, firstEntry, endEntry, shouldCacheEntry, callback, CTX);
+        pendingReadsManager.readEntries(lh, firstEntry, endEntry, expectedReadCount, callback, CTX);
 
 
         CapturingReadEntriesCallback callback2 = new CapturingReadEntriesCallback();
-        pendingReadsManager.readEntries(lh, firstEntrySecondRead, endEntrySecondRead,
-                shouldCacheEntry, callback2, CTX2);
+        pendingReadsManager.readEntries(lh, firstEntrySecondRead, endEntrySecondRead, expectedReadCount, callback2,
+                CTX2);
 
         // complete the read from BK
         // only one read completes 2 callbacks
@@ -329,21 +334,21 @@ public class PendingReadsManagerTest  {
         long firstEntrySecondRead = firstEntry - 10;
         long endEntrySecondRead = endEntry;
 
-        boolean shouldCacheEntry = false;
+        IntSupplier expectedReadCount = () -> 0;
 
         PreparedReadFromStorage read1 =
-                prepareReadFromStorage(lh, rangeEntryCache, firstEntry, endEntry, shouldCacheEntry);
+                prepareReadFromStorage(lh, rangeEntryCache, firstEntry, endEntry, expectedReadCount);
 
         PreparedReadFromStorage readForLeft =
-                prepareReadFromStorage(lh, rangeEntryCache, firstEntrySecondRead, firstEntry - 1, shouldCacheEntry);
+                prepareReadFromStorage(lh, rangeEntryCache, firstEntrySecondRead, firstEntry - 1, expectedReadCount);
 
         PendingReadsManager pendingReadsManager = new PendingReadsManager(rangeEntryCache);
         CapturingReadEntriesCallback callback = new CapturingReadEntriesCallback();
-        pendingReadsManager.readEntries(lh, firstEntry, endEntry, shouldCacheEntry, callback, CTX);
+        pendingReadsManager.readEntries(lh, firstEntry, endEntry, expectedReadCount, callback, CTX);
 
         CapturingReadEntriesCallback callback2 = new CapturingReadEntriesCallback();
-        pendingReadsManager.readEntries(lh, firstEntrySecondRead,
-                endEntrySecondRead, shouldCacheEntry, callback2, CTX2);
+        pendingReadsManager.readEntries(lh, firstEntrySecondRead, endEntrySecondRead, expectedReadCount, callback2,
+                CTX2);
 
         // complete the read from BK
         read1.storageReadCompleted();
@@ -370,21 +375,21 @@ public class PendingReadsManagerTest  {
         long firstEntrySecondRead = firstEntry;
         long endEntrySecondRead = endEntry + 10;
 
-        boolean shouldCacheEntry = false;
+        IntSupplier expectedReadCount = () -> 0;
 
         PreparedReadFromStorage read1 =
-                prepareReadFromStorage(lh, rangeEntryCache, firstEntry, endEntry, shouldCacheEntry);
+                prepareReadFromStorage(lh, rangeEntryCache, firstEntry, endEntry, expectedReadCount);
 
         PreparedReadFromStorage readForRight =
-                prepareReadFromStorage(lh, rangeEntryCache, endEntry + 1, endEntrySecondRead, shouldCacheEntry);
+                prepareReadFromStorage(lh, rangeEntryCache, endEntry + 1, endEntrySecondRead, expectedReadCount);
 
         PendingReadsManager pendingReadsManager = new PendingReadsManager(rangeEntryCache);
         CapturingReadEntriesCallback callback = new CapturingReadEntriesCallback();
-        pendingReadsManager.readEntries(lh, firstEntry, endEntry, shouldCacheEntry, callback, CTX);
+        pendingReadsManager.readEntries(lh, firstEntry, endEntry, expectedReadCount, callback, CTX);
 
         CapturingReadEntriesCallback callback2 = new CapturingReadEntriesCallback();
-        pendingReadsManager.readEntries(lh, firstEntrySecondRead,
-                endEntrySecondRead, shouldCacheEntry, callback2, CTX2);
+        pendingReadsManager.readEntries(lh, firstEntrySecondRead, endEntrySecondRead, expectedReadCount, callback2,
+                CTX2);
 
         // complete the read from BK
         read1.storageReadCompleted();
@@ -411,24 +416,24 @@ public class PendingReadsManagerTest  {
         long firstEntrySecondRead = firstEntry - 10;
         long endEntrySecondRead = endEntry + 10;
 
-        boolean shouldCacheEntry = false;
+        IntSupplier expectedReadCount = () -> 0;
 
         PreparedReadFromStorage read1 =
-                prepareReadFromStorage(lh, rangeEntryCache, firstEntry, endEntry, shouldCacheEntry);
+                prepareReadFromStorage(lh, rangeEntryCache, firstEntry, endEntry, expectedReadCount);
 
         PreparedReadFromStorage readForLeft =
-                prepareReadFromStorage(lh, rangeEntryCache, firstEntrySecondRead, firstEntry - 1, shouldCacheEntry);
+                prepareReadFromStorage(lh, rangeEntryCache, firstEntrySecondRead, firstEntry - 1, expectedReadCount);
 
         PreparedReadFromStorage readForRight =
-                prepareReadFromStorage(lh, rangeEntryCache, endEntry + 1, endEntrySecondRead, shouldCacheEntry);
+                prepareReadFromStorage(lh, rangeEntryCache, endEntry + 1, endEntrySecondRead, expectedReadCount);
 
         PendingReadsManager pendingReadsManager = new PendingReadsManager(rangeEntryCache);
         CapturingReadEntriesCallback callback = new CapturingReadEntriesCallback();
-        pendingReadsManager.readEntries(lh, firstEntry, endEntry, shouldCacheEntry, callback, CTX);
+        pendingReadsManager.readEntries(lh, firstEntry, endEntry, expectedReadCount, callback, CTX);
 
         CapturingReadEntriesCallback callback2 = new CapturingReadEntriesCallback();
-        pendingReadsManager.readEntries(lh, firstEntrySecondRead,
-                endEntrySecondRead, shouldCacheEntry, callback2, CTX2);
+        pendingReadsManager.readEntries(lh, firstEntrySecondRead, endEntrySecondRead, expectedReadCount, callback2,
+                CTX2);
 
         // complete the read from BK
         read1.storageReadCompleted();
@@ -456,21 +461,22 @@ public class PendingReadsManagerTest  {
         long firstEntrySecondRead = 1000;
         long endEntrySecondRead = 1099;
 
-        boolean shouldCacheEntry = false;
+        IntSupplier expectedReadCount = () -> 0;
 
         PreparedReadFromStorage read1 =
-                prepareReadFromStorage(lh, rangeEntryCache, firstEntry, endEntry, shouldCacheEntry);
+                prepareReadFromStorage(lh, rangeEntryCache, firstEntry, endEntry, expectedReadCount);
 
         PreparedReadFromStorage read2 =
-                prepareReadFromStorage(lh, rangeEntryCache, firstEntrySecondRead, endEntrySecondRead, shouldCacheEntry);
+                prepareReadFromStorage(lh, rangeEntryCache, firstEntrySecondRead, endEntrySecondRead,
+                        expectedReadCount);
 
         PendingReadsManager pendingReadsManager = new PendingReadsManager(rangeEntryCache);
         CapturingReadEntriesCallback callback = new CapturingReadEntriesCallback();
-        pendingReadsManager.readEntries(lh, firstEntry, endEntry, shouldCacheEntry, callback, CTX);
+        pendingReadsManager.readEntries(lh, firstEntry, endEntry, expectedReadCount, callback, CTX);
 
         CapturingReadEntriesCallback callback2 = new CapturingReadEntriesCallback();
-        pendingReadsManager.readEntries(lh, firstEntrySecondRead,
-                endEntrySecondRead, shouldCacheEntry, callback2, CTX2);
+        pendingReadsManager.readEntries(lh, firstEntrySecondRead, endEntrySecondRead, expectedReadCount, callback2,
+                CTX2);
 
         read1.storageReadCompleted();
         callback.get();
@@ -491,11 +497,11 @@ public class PendingReadsManagerTest  {
         final var readFutures = new ArrayList<CapturingReadEntriesCallback>();
         final BiConsumer<Long, Long> readEntries = (firstEntry, lastEntry) -> {
             final var callback = new CapturingReadEntriesCallback();
-            pendingReadsManager.readEntries(lh, firstEntry, lastEntry, false, callback, CTX);
+            pendingReadsManager.readEntries(lh, firstEntry, lastEntry, () -> 0, callback, CTX);
             readFutures.add(callback);
         };
         final BiFunction<Long, Long, PreparedReadFromStorage> mockReadFromStorage = (firstEntry, lastEntry) ->
-                prepareReadFromStorage(lh, rangeEntryCache, firstEntry, lastEntry, false);
+                prepareReadFromStorage(lh, rangeEntryCache, firstEntry, lastEntry, () -> 0);
 
         final var read0 = mockReadFromStorage.apply(10L, 70L);
         readEntries.accept(10L, 70L);
diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/cache/RangeCacheRemovalQueueTest.java b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/cache/RangeCacheRemovalQueueTest.java
new file mode 100644
index 00000000000..35b7d548e3c
--- /dev/null
+++ b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/cache/RangeCacheRemovalQueueTest.java
@@ -0,0 +1,245 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.bookkeeper.mledger.impl.cache;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.reset;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
+import io.netty.buffer.Unpooled;
+import org.apache.bookkeeper.mledger.Position;
+import org.apache.bookkeeper.mledger.PositionFactory;
+import org.apache.bookkeeper.mledger.impl.EntryImpl;
+import org.apache.commons.lang3.tuple.Pair;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+public class RangeCacheRemovalQueueTest {
+    private RangeCache rangeCache;
+
+    @BeforeMethod
+    public void setup() {
+        rangeCache = mock(RangeCache.class);
+        // mock removal so that it updates counters correctly
+        doAnswer(invocation -> {
+            RangeCacheEntryWrapper entryWrapper = invocation.getArgument(2);
+            RangeCacheRemovalCounters counters = invocation.getArgument(3);
+            counters.entryRemoved(entryWrapper.size);
+            return true;
+        }).when(rangeCache).removeEntry(any(), any(), any(), any(), anyBoolean());
+    }
+
+    @AfterMethod
+    public void tearDown() {
+        // Clean up any resources if needed
+        reset(rangeCache);
+        rangeCache = null;
+    }
+
+    private RangeCacheEntryWrapper addWrappedEntry(RangeCacheRemovalQueue q,
+                                                   long ledgerId,
+                                                   long entryId,
+                                                   int expectedReads,
+                                                   long size) {
+        Position pos = PositionFactory.create(ledgerId, entryId);
+        // Use real EntryImpl (ReferenceCountedEntry) with expectedReadCount
+        EntryImpl entry = EntryImpl.create(ledgerId, entryId, Unpooled.EMPTY_BUFFER, expectedReads);
+
+        // Create and add a real RangeCacheEntryWrapper (no mocks)
+        return RangeCacheEntryWrapper.withNewInstance(
+                rangeCache,
+                pos,
+                entry,
+                size,
+                wrapper -> {
+                    boolean offered = q.addEntry(wrapper);
+                    assertTrue(offered, "Entry should be added to the removal queue");
+                    return wrapper;
+                }
+        );
+    }
+
+    @Test
+    public void testAddEntryAndIsEmpty() {
+        RangeCacheRemovalQueue q = new RangeCacheRemovalQueue(0, false);
+        assertTrue(q.isEmpty());
+        RangeCacheEntryWrapper w = addWrappedEntry(q, 1L, 1L, 0, 10);
+        assertNotNull(w);
+        assertFalse(q.isEmpty());
+    }
+
+    @Test
+    public void testTimeBasedEvictionRespectsExpectedReadsRequeueThreshold() throws Exception {
+        // Allow one requeue for entries that still have expected reads
+        RangeCacheRemovalQueue q = new RangeCacheRemovalQueue(
+                /*maxRequeueCountWhenHasExpectedReads*/ 1,
+                /*extendTTLOfRecentlyAccessed*/ false);
+
+        RangeCacheEntryWrapper w = addWrappedEntry(q, 2L, 1L, 1 /* has expected reads */, 10);
+        assertNotNull(w);
+
+        // Make it "expired" by setting timestamp far in the past
+        long now = System.nanoTime();
+        w.timestampNanos = now - 10_000_000L; // 10ms earlier
+        long evictionThreshold = now - 1L;
+
+        // First eviction - should requeue due to expected reads
+        Pair<Integer, Long> result1 = q.evictLEntriesBeforeTimestamp(evictionThreshold);
+        assertNotNull(result1);
+        assertFalse(q.isEmpty(), "Entry should be requeued, not dropped");
+        assertEquals(w.requeueCount, 1, "Entry should have been requeued once");
+
+        // Make sure the entry is still retained; now it should be dropped after reaching threshold
+        // Mark it expired again (in case markRequeued bumped timestamp)
+        w.timestampNanos = System.nanoTime() - 10_000_000L;
+
+        Pair<Integer, Long> result2 = q.evictLEntriesBeforeTimestamp(System.nanoTime() - 1L);
+        assertNotNull(result2);
+        assertTrue(q.isEmpty(), "Entry should be removed after exceeding requeue threshold");
+    }
+
+    @Test
+    public void testExtendTTLOfRecentlyAccessedRequeuesOnTimeBasedEviction() {
+        // Enable extend TTL for recently accessed entries
+        RangeCacheRemovalQueue q = new RangeCacheRemovalQueue(
+                /*maxRequeueCountWhenHasExpectedReads*/ 0,
+                /*extendTTLOfRecentlyAccessed*/ true);
+
+        RangeCacheEntryWrapper w = addWrappedEntry(q, 3L, 1L, 0 /* no expected reads */, 10);
+        assertNotNull(w);
+
+        // Simulate access
+        w.accessed = true;
+
+        // Make the entry expired
+        long now = System.nanoTime();
+        w.timestampNanos = now - 10_000_000L;
+        long evictionThreshold = now - 1L;
+
+        // First eviction should requeue because 'extendTTLOfRecentlyAccessed' is enabled and the entry was accessed
+        Pair<Integer, Long> result1 = q.evictLEntriesBeforeTimestamp(evictionThreshold);
+        assertNotNull(result1);
+        assertFalse(q.isEmpty(), "Recently accessed entry should be requeued, not dropped");
+        assertEquals(w.requeueCount, 1, "Entry should have been requeued once due to recent access");
+        assertFalse(w.accessed, "Access flag should be cleared on requeue");
+
+        // Expire again and evict - since it was not accessed after the requeue, it should be removed now
+        w.timestampNanos = System.nanoTime() - 10_000_000L;
+        Pair<Integer, Long> result2 = q.evictLEntriesBeforeTimestamp(System.nanoTime() - 1L);
+        assertNotNull(result2);
+        assertTrue(q.isEmpty(), "Entry should be removed if it is expired and not recently accessed again");
+    }
+
+    @Test
+    public void testSizeBasedEvictionEvictsAsLongAsNeeded() {
+        RangeCacheRemovalQueue q = new RangeCacheRemovalQueue(
+                /*maxRequeueCountWhenHasExpectedReads*/ 1,
+                /*extendTTLOfRecentlyAccessed*/ true);
+
+        // Three entries, total = 40 + 50 + 30 = 120
+        RangeCacheEntryWrapper e1 = addWrappedEntry(q, 11L, 1L, 0, 40); // recently accessed; first pass should requeue
+        e1.accessed = true;
+        RangeCacheEntryWrapper e2 = addWrappedEntry(q, 11L, 2L, 3, 50); // has expected reads; last resort
+        RangeCacheEntryWrapper e3 = addWrappedEntry(q, 11L, 3L, 0, 30); // easiest to evict
+
+        // Request to free everything in one go
+        Pair<Integer, Long> r1 = q.evictLeastAccessedEntries(120);
+        assertNotNull(r1);
+        assertThat(r1.getRight().longValue()).isEqualTo(120L);
+        assertThat(e1.key).isNull();
+        assertThat(e2.key).isNull();
+        assertThat(e3.key).isNull();
+    }
+
+    @Test
+    public void testSizeBasedEvictionEvictsEasiestFirstAsLongAsNeeded() {
+        RangeCacheRemovalQueue q = new RangeCacheRemovalQueue(
+                /*maxRequeueCountWhenHasExpectedReads*/ 1,
+                /*extendTTLOfRecentlyAccessed*/ true);
+
+        // Three entries, total = 40 + 50 + 30 = 120
+        RangeCacheEntryWrapper e1 = addWrappedEntry(q, 11L, 1L, 0, 40); // recently accessed; first pass should requeue
+        e1.accessed = true;
+        RangeCacheEntryWrapper e2 = addWrappedEntry(q, 11L, 2L, 3, 50); // has expected reads; last resort
+        RangeCacheEntryWrapper e3 = addWrappedEntry(q, 11L, 3L, 0, 30); // easiest to evict
+
+        // Request to free everything in one go
+        Pair<Integer, Long> r1 = q.evictLeastAccessedEntries(20);
+        assertNotNull(r1);
+        assertThat(r1.getRight().longValue()).isEqualTo(30L);
+        assertThat(e1.key).isNotNull();
+        assertThat(e2.key).isNotNull();
+        assertThat(e3.key).isNull();
+    }
+
+    @Test
+    public void testSizeBasedEvictionEvictsExpectedReadsLast() {
+        RangeCacheRemovalQueue q = new RangeCacheRemovalQueue(
+                /*maxRequeueCountWhenHasExpectedReads*/ 5,
+                /*extendTTLOfRecentlyAccessed*/ true);
+
+        // Three entries, total = 40 + 50 + 30 = 120
+        RangeCacheEntryWrapper e1 = addWrappedEntry(q, 11L, 1L, 0, 40); // recently accessed; first pass should requeue
+        e1.accessed = true;
+        RangeCacheEntryWrapper e2 = addWrappedEntry(q, 11L, 2L, 3, 50); // has expected reads; last resort
+        RangeCacheEntryWrapper e3 = addWrappedEntry(q, 11L, 3L, 0, 30); // easiest to evict
+
+        // Request to free everything in one go
+        Pair<Integer, Long> r1 = q.evictLeastAccessedEntries(35);
+        assertNotNull(r1);
+        assertThat(r1.getRight().longValue()).isEqualTo(70L);
+        assertThat(e1.key).isNull();
+        assertThat(e2.key).isNotNull();
+        assertThat(e3.key).isNull();
+    }
+
+    @Test
+    public void testGetNonEvictableSizeCountsEntriesWithExpectedReads() {
+        RangeCacheRemovalQueue q = new RangeCacheRemovalQueue(
+                /*maxRequeueCountWhenHasExpectedReads*/ 0,
+                /*extendTTLOfRecentlyAccessed*/ false);
+
+        // Entry that has expected reads should be included
+        RangeCacheEntryWrapper w1 = addWrappedEntry(q, 1L, 1L, 2 /* expectedReads > 0 */, 100);
+        // Entry that has no expected reads should be excluded
+        RangeCacheEntryWrapper w2 = addWrappedEntry(q, 1L, 2L, 1, 50);
+
+        // Make sure wrappers were created
+        assertNotNull(w1);
+        assertNotNull(w2);
+
+        Pair<Integer, Long> nonEvictable = q.getNonEvictableSize();
+        assertEquals(nonEvictable.getLeft().intValue(), 2);
+        assertEquals(nonEvictable.getRight().longValue(), 150L);
+
+        // mark the second entry as read to simulate expected reads
+        w2.value.getReadCountHandler().markRead();
+
+        nonEvictable = q.getNonEvictableSize();
+        assertEquals(nonEvictable.getLeft().intValue(), 1, "Only entries with expected reads should be counted");
+        assertEquals(nonEvictable.getRight().longValue(), 100L, "Size should match the entry having expected reads");
+    }
+}
\ No newline at end of file
diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/cache/RangeCacheTest.java b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/cache/RangeCacheTest.java
index 1e9e0862e2b..ea3a7401bcf 100644
--- a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/cache/RangeCacheTest.java
+++ b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/cache/RangeCacheTest.java
@@ -49,7 +49,7 @@ public class RangeCacheTest {
 
     @Test
     public void simple() {
-        RangeCacheRemovalQueue removalQueue = new RangeCacheRemovalQueue();
+        RangeCacheRemovalQueue removalQueue = createRemovalQueue();
         RangeCache cache = new RangeCache(removalQueue);
 
         putToCache(cache, 0, "0");
@@ -96,6 +96,10 @@ public class RangeCacheTest {
         assertEquals(cache.getNumberOfEntries(), 0);
     }
 
+    private static RangeCacheRemovalQueue createRemovalQueue() {
+        return new RangeCacheRemovalQueue(5, false);
+    }
+
     private void putToCache(RangeCache cache, int i, String str) {
         Position position = createPosition(i);
         ReferenceCountedEntry cachedEntry = createCachedEntry(position, str);
@@ -107,7 +111,7 @@ public class RangeCacheTest {
     }
 
     private static ReferenceCountedEntry createCachedEntry(Position position, String str) {
-        return EntryImpl.create(position, Unpooled.wrappedBuffer(str.getBytes()));
+        return EntryImpl.create(position, Unpooled.wrappedBuffer(str.getBytes()), 0);
     }
 
     private static Position createPosition(int i) {
@@ -122,7 +126,7 @@ public class RangeCacheTest {
 
     @Test(dataProvider = "retainBeforeEviction")
     public void customTimeExtraction(boolean retain) {
-        RangeCacheRemovalQueue removalQueue = new RangeCacheRemovalQueue();
+        RangeCacheRemovalQueue removalQueue = createRemovalQueue();
         RangeCache cache = new RangeCache(removalQueue);
 
         putToCache(cache, 1, "1");
@@ -162,7 +166,7 @@ public class RangeCacheTest {
 
     @Test
     public void doubleInsert() {
-        RangeCacheRemovalQueue removalQueue = new RangeCacheRemovalQueue();
+        RangeCacheRemovalQueue removalQueue = createRemovalQueue();
         RangeCache cache = new RangeCache(removalQueue);
 
         ReferenceCountedEntry s0 = createCachedEntry(0, "zero");
@@ -194,7 +198,7 @@ public class RangeCacheTest {
 
     @Test
     public void getRange() {
-        RangeCacheRemovalQueue removalQueue = new RangeCacheRemovalQueue();
+        RangeCacheRemovalQueue removalQueue = createRemovalQueue();
         RangeCache cache = new RangeCache(removalQueue);
 
         putToCache(cache, 0, "0");
@@ -228,7 +232,7 @@ public class RangeCacheTest {
 
     @Test
     public void eviction() {
-        RangeCacheRemovalQueue removalQueue = new RangeCacheRemovalQueue();
+        RangeCacheRemovalQueue removalQueue = createRemovalQueue();
         RangeCache cache = new RangeCache(removalQueue);
 
         putToCache(cache, 0, "zero");
@@ -271,7 +275,7 @@ public class RangeCacheTest {
 
     @Test
     public void evictions() {
-        RangeCacheRemovalQueue removalQueue = new RangeCacheRemovalQueue();
+        RangeCacheRemovalQueue removalQueue = createRemovalQueue();
         RangeCache cache = new RangeCache(removalQueue);
 
         int expectedSize = 0;
@@ -317,7 +321,7 @@ public class RangeCacheTest {
 
     @Test
     public void testPutWhileClearIsCalledConcurrently() {
-        RangeCacheRemovalQueue removalQueue = new RangeCacheRemovalQueue();
+        RangeCacheRemovalQueue removalQueue = createRemovalQueue();
         RangeCache cache = new RangeCache(removalQueue);
         int numberOfThreads = 8;
         @Cleanup("shutdownNow")
@@ -338,7 +342,7 @@ public class RangeCacheTest {
 
     @Test
     public void testPutSameObj() {
-        RangeCacheRemovalQueue removalQueue = new RangeCacheRemovalQueue();
+        RangeCacheRemovalQueue removalQueue = createRemovalQueue();
         RangeCache cache = new RangeCache(removalQueue);
         ReferenceCountedEntry s0 = createCachedEntry(0, "zero");
         assertEquals(s0.refCnt(), 1);
@@ -348,7 +352,7 @@ public class RangeCacheTest {
 
     @Test
     public void testRemoveEntryWithInvalidRefCount() {
-        RangeCacheRemovalQueue removalQueue = new RangeCacheRemovalQueue();
+        RangeCacheRemovalQueue removalQueue = createRemovalQueue();
         RangeCache cache = new RangeCache(removalQueue);
         ReferenceCountedEntry value = createCachedEntry(1, "1");
         cache.put(value.getPosition(), value);
@@ -360,7 +364,7 @@ public class RangeCacheTest {
 
     @Test
     public void testInvalidMatchingKey() {
-        RangeCacheRemovalQueue removalQueue = new RangeCacheRemovalQueue();
+        RangeCacheRemovalQueue removalQueue = createRemovalQueue();
         RangeCache cache = new RangeCache(removalQueue);
         ReferenceCountedEntry value = createCachedEntry(1, "1");
         cache.put(value.getPosition(), value);
@@ -383,7 +387,7 @@ public class RangeCacheTest {
 
     @Test
     public void testGetKeyWithDifferentInstance() {
-        RangeCacheRemovalQueue removalQueue = new RangeCacheRemovalQueue();
+        RangeCacheRemovalQueue removalQueue = createRemovalQueue();
         RangeCache cache = new RangeCache(removalQueue);
         Position key = createPosition(129);
         ReferenceCountedEntry value = createCachedEntry(key, "129");
diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/cache/RangeEntryCacheImplTest.java b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/cache/RangeEntryCacheImplTest.java
new file mode 100644
index 00000000000..2922a3d2679
--- /dev/null
+++ b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/cache/RangeEntryCacheImplTest.java
@@ -0,0 +1,209 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.bookkeeper.mledger.impl.cache;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import io.netty.buffer.Unpooled;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.IntSupplier;
+import org.apache.bookkeeper.client.api.ReadHandle;
+import org.apache.bookkeeper.mledger.AsyncCallbacks;
+import org.apache.bookkeeper.mledger.Entry;
+import org.apache.bookkeeper.mledger.ManagedLedgerException;
+import org.apache.bookkeeper.mledger.impl.EntryImpl;
+import org.apache.bookkeeper.mledger.impl.ManagedLedgerFactoryMBeanImpl;
+import org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl;
+import org.apache.bookkeeper.mledger.impl.ManagedLedgerMBeanImpl;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+public class RangeEntryCacheImplTest {
+    private RangeEntryCacheImpl rangeEntryCache;
+    private PendingReadsManager pendingReadsManager;
+    private ReadHandle lh;
+    private IntSupplier expectedReadCount;
+
+    @BeforeMethod
+    public void setup() {
+        RangeEntryCacheManagerImpl mockEntryCacheManager = mock(RangeEntryCacheManagerImpl.class);
+        ManagedLedgerFactoryMBeanImpl mlFactoryMBean = mock(ManagedLedgerFactoryMBeanImpl.class);
+        when(mockEntryCacheManager.getMlFactoryMBean()).thenReturn(mlFactoryMBean);
+        ManagedLedgerImpl mockManagedLedger = mock(ManagedLedgerImpl.class);
+        ManagedLedgerMBeanImpl mockManagedLedgerMBean = mock(ManagedLedgerMBeanImpl.class);
+        when(mockManagedLedger.getMbean()).thenReturn(mockManagedLedgerMBean);
+        when(mockManagedLedger.getName()).thenReturn("testManagedLedger");
+        RangeCacheRemovalQueue mockRangeCacheRemovalQueue = mock(RangeCacheRemovalQueue.class);
+        when(mockRangeCacheRemovalQueue.addEntry(any())).thenReturn(true);
+        InflightReadsLimiter inflightReadsLimiter = mock(InflightReadsLimiter.class);
+        when(mockEntryCacheManager.getInflightReadsLimiter()).thenReturn(inflightReadsLimiter);
+        doAnswer(invocation -> {
+            long permits = invocation.getArgument(0);
+            InflightReadsLimiter.Handle
+                    handle = new InflightReadsLimiter.Handle(permits, System.currentTimeMillis(), true);
+            return Optional.of(handle);
+        }).when(inflightReadsLimiter).acquire(anyLong(), any());
+        pendingReadsManager = mock(PendingReadsManager.class);
+        doAnswer(invocation -> {
+            long firstEntry = invocation.getArgument(1);
+            long lastEntry = invocation.getArgument(2);
+            AsyncCallbacks.ReadEntriesCallback callback = invocation.getArgument(4);
+            Object ctx = invocation.getArgument(5);
+            List<Entry> entries = new ArrayList<>((int) (lastEntry - firstEntry + 1));
+            for (long entryId = firstEntry; entryId <= lastEntry; entryId++) {
+                entries.add(EntryImpl.create(1, entryId, Unpooled.EMPTY_BUFFER));
+            }
+            callback.readEntriesComplete(entries, ctx);
+            return null;
+        }).when(pendingReadsManager).readEntries(any(), anyLong(), anyLong(), any(), any(), any());
+        rangeEntryCache =
+                new RangeEntryCacheImpl(mockEntryCacheManager, mockManagedLedger, false, mockRangeCacheRemovalQueue,
+                        EntryLengthFunction.DEFAULT, pendingReadsManager);
+        lh = mock(ReadHandle.class);
+        when(lh.getId()).thenReturn(1L);
+        expectedReadCount = () -> 1;
+    }
+
+    @Test
+    public void testPartialCachingWithMiddleEntryInCache() {
+        Entry entry = EntryImpl.create(1, 50, Unpooled.EMPTY_BUFFER);
+        rangeEntryCache.insert(entry);
+        performReadAndValidateResult();
+        verify(pendingReadsManager, times(1)).readEntries(any(), eq(0L), eq(49L), any(), any(), any());
+        verify(pendingReadsManager, times(1)).readEntries(any(), eq(51L), eq(99L), any(), any(), any());
+    }
+
+    @Test
+    public void testPartialCachingWithFirstEntryInCache() {
+        Entry entry = EntryImpl.create(1, 0, Unpooled.EMPTY_BUFFER);
+        rangeEntryCache.insert(entry);
+        performReadAndValidateResult();
+        verify(pendingReadsManager, times(1)).readEntries(any(), eq(1L), eq(99L), any(), any(), any());
+    }
+
+    @Test
+    public void testPartialCachingWithLastEntryInCache() {
+        Entry entry = EntryImpl.create(1, 99, Unpooled.EMPTY_BUFFER);
+        rangeEntryCache.insert(entry);
+        performReadAndValidateResult();
+        verify(pendingReadsManager, times(1)).readEntries(any(), eq(0L), eq(98L), any(), any(), any());
+    }
+
+    @Test
+    public void testPartialCachingWithMiddleRangeInCache() {
+        Entry entry = EntryImpl.create(1, 50, Unpooled.EMPTY_BUFFER);
+        rangeEntryCache.insert(entry);
+        entry = EntryImpl.create(1, 51, Unpooled.EMPTY_BUFFER);
+        rangeEntryCache.insert(entry);
+        performReadAndValidateResult();
+        verify(pendingReadsManager, times(1)).readEntries(any(), eq(0L), eq(49L), any(), any(), any());
+        verify(pendingReadsManager, times(1)).readEntries(any(), eq(52L), eq(99L), any(), any(), any());
+    }
+
+    @Test
+    public void testPartialCachingWithFirstRangeInCache() {
+        Entry entry = EntryImpl.create(1, 0, Unpooled.EMPTY_BUFFER);
+        rangeEntryCache.insert(entry);
+        entry = EntryImpl.create(1, 1, Unpooled.EMPTY_BUFFER);
+        rangeEntryCache.insert(entry);
+        performReadAndValidateResult();
+        verify(pendingReadsManager, times(1)).readEntries(any(), eq(2L), eq(99L), any(), any(), any());
+    }
+
+    @Test
+    public void testPartialCachingWithLastRangeInCache() {
+        Entry entry = EntryImpl.create(1, 98, Unpooled.EMPTY_BUFFER);
+        rangeEntryCache.insert(entry);
+        EntryImpl.create(1, 99, Unpooled.EMPTY_BUFFER);
+        rangeEntryCache.insert(entry);
+        performReadAndValidateResult();
+        verify(pendingReadsManager, times(1)).readEntries(any(), eq(0L), eq(97L), any(), any(), any());
+    }
+
+    @Test
+    public void testPartialCachingWithMultipleEntriesInCache() {
+        Entry entry = EntryImpl.create(1, 5, Unpooled.EMPTY_BUFFER);
+        rangeEntryCache.insert(entry);
+        entry = EntryImpl.create(1, 15, Unpooled.EMPTY_BUFFER);
+        rangeEntryCache.insert(entry);
+        entry = EntryImpl.create(1, 75, Unpooled.EMPTY_BUFFER);
+        rangeEntryCache.insert(entry);
+        entry = EntryImpl.create(1, 76, Unpooled.EMPTY_BUFFER);
+        rangeEntryCache.insert(entry);
+        entry = EntryImpl.create(1, 78, Unpooled.EMPTY_BUFFER);
+        rangeEntryCache.insert(entry);
+        performReadAndValidateResult();
+        verify(pendingReadsManager, times(1)).readEntries(any(), eq(0L), eq(4L), any(), any(), any());
+        verify(pendingReadsManager, times(1)).readEntries(any(), eq(6L), eq(14L), any(), any(), any());
+        verify(pendingReadsManager, times(1)).readEntries(any(), eq(16L), eq(74L), any(), any(), any());
+        verify(pendingReadsManager, times(1)).readEntries(any(), eq(77L), eq(77L), any(), any(), any());
+        verify(pendingReadsManager, times(1)).readEntries(any(), eq(79L), eq(99L), any(), any(), any());
+    }
+
+    @Test
+    public void testPartialCachingWithMultipleEntriesInCacheWhilePartialReadFails() {
+        Entry entry = EntryImpl.create(1, 50, Unpooled.EMPTY_BUFFER);
+        rangeEntryCache.insert(entry);
+        doAnswer(invocation -> {
+            AsyncCallbacks.ReadEntriesCallback callback = invocation.getArgument(4);
+            Object ctx = invocation.getArgument(5);
+            System.out.println("Injecting test failure for readEntries");
+            callback.readEntriesFailed(new ManagedLedgerException("Injected test failure"), ctx);
+            return null;
+        }).when(pendingReadsManager).readEntries(any(), eq(51L), eq(99L), any(), any(), any());
+        performReadAndValidateResult();
+        verify(pendingReadsManager, times(1)).readEntries(any(), eq(0L), eq(49L), any(), any(), any());
+        verify(pendingReadsManager, times(1)).readEntries(any(), eq(51L), eq(99L), any(), any(), any());
+        verify(pendingReadsManager, times(1)).readEntries(any(), eq(0L), eq(99L), any(), any(), any());
+    }
+
+    private void performReadAndValidateResult() {
+        CompletableFuture<List<Entry>> future = new CompletableFuture<>();
+        rangeEntryCache.asyncReadEntry(lh, 0, 99, expectedReadCount, new AsyncCallbacks.ReadEntriesCallback() {
+            @Override
+            public void readEntriesComplete(List<Entry> entries, Object ctx) {
+                future.complete(entries);
+            }
+
+            @Override
+            public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
+                future.completeExceptionally(exception);
+            }
+        }, null);
+        assertThat(future).isCompleted().satisfies(f -> {
+            List<Entry> entries = f.getNow(null);
+            assertThat(entries).hasSize(100);
+            for (int i = 0; i < 100; i++) {
+                Entry e = entries.get(i);
+                assertThat(e.getLedgerId()).isEqualTo(1L);
+                assertThat(e.getEntryId()).isEqualTo(i);
+            }
+        });
+    }
+}
\ No newline at end of file
diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/test/MockedBookKeeperTestCase.java b/managed-ledger/src/test/java/org/apache/bookkeeper/test/MockedBookKeeperTestCase.java
index 86201547956..f4619955ea1 100644
--- a/managed-ledger/src/test/java/org/apache/bookkeeper/test/MockedBookKeeperTestCase.java
+++ b/managed-ledger/src/test/java/org/apache/bookkeeper/test/MockedBookKeeperTestCase.java
@@ -94,6 +94,7 @@ public abstract class MockedBookKeeperTestCase {
     }
 
     protected ManagedLedgerConfig initManagedLedgerConfig(ManagedLedgerConfig config) {
+        config.setCacheEvictionByExpectedReadCount(false);
         return config;
     }
 
diff --git a/microbench/README.md b/microbench/README.md
index 5c69c3bba81..047de7c425d 100644
--- a/microbench/README.md
+++ b/microbench/README.md
@@ -61,6 +61,14 @@ Running specific benchmarks:
 java -jar microbench/target/microbenchmarks.jar ".*BenchmarkName.*"
 ```
 
+Running specific benchmarks with machine-readable output and saving the output to a file:
+
+```shell
+java -jar microbench/target/microbenchmarks.jar -rf json -rff jmh-result-$(date +%s).json ".*BenchmarkName.*" | tee jmh-result-$(date +%s).txt
+```
+
+The `jmh-result-*.json` file can be used to visualize the results using [JMH Visualizer](https://jmh.morethan.io/).
+
 Checking what benchmarks match the pattern:
 
 ```shell
@@ -76,3 +84,10 @@ LIBASYNCPROFILER_PATH=$HOME/async-profiler/lib/libasyncProfiler.dylib
 java -jar microbench/target/microbenchmarks.jar -prof async:libPath=$LIBASYNCPROFILER_PATH\;output=flamegraph\;dir=profile-results ".*BenchmarkName.*"
 ```
 
+When profiling on Mac OS, you might need to add `\;event=itimer` to the `-prof` argument since it's the only [async profiler CPU sampling engine that supports Mac OS](https://github.com/async-profiler/async-profiler/blob/master/docs/CpuSamplingEngines.md#summary). The default value for `event` is `cpu`.
+
+It's possible to add options to the async-profiler that aren't supported by the JMH async-profiler plugin. This can be done by adding `rawCommand` option to the `-prof` argument. This example shows how to add `all` (new in Async Profiler 4.1), `jfrsync` (record JFR events such as garbage collection) and `cstack=vmx` options.
+
+```shell
+java -jar microbench/target/microbenchmarks.jar -prof async:libPath=$LIBASYNCPROFILER_PATH\;output=jfr\;dir=profile-results\;rawCommand=all,jfrsync,cstack=vmx ".*BenchmarkName.*"
+```
\ No newline at end of file
diff --git a/microbench/src/main/java/org/apache/bookkeeper/mledger/impl/ActiveManagedCursorContainerBenchmark.java b/microbench/src/main/java/org/apache/bookkeeper/mledger/impl/ActiveManagedCursorContainerBenchmark.java
new file mode 100644
index 00000000000..75aea0166fa
--- /dev/null
+++ b/microbench/src/main/java/org/apache/bookkeeper/mledger/impl/ActiveManagedCursorContainerBenchmark.java
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.bookkeeper.mledger.impl;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Supplier;
+import java.util.stream.IntStream;
+import org.apache.bookkeeper.mledger.ManagedCursor;
+import org.apache.bookkeeper.mledger.PositionFactory;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.TearDown;
+import org.openjdk.jmh.annotations.Threads;
+import org.openjdk.jmh.annotations.Warmup;
+
+@BenchmarkMode(Mode.Throughput)
+@OutputTimeUnit(TimeUnit.SECONDS)
+@State(Scope.Benchmark)
+@Warmup(time = 10, timeUnit = TimeUnit.SECONDS, iterations = 1)
+@Measurement(time = 10, timeUnit = TimeUnit.SECONDS, iterations = 1)
+@Fork(1)
+public class ActiveManagedCursorContainerBenchmark {
+    @Param({"10", "100", "500"})
+    private int numberOfCursors;
+
+    public enum ActiveManagedCursorContainerImplType {
+        DEFAULT(ActiveManagedCursorContainerImpl::new, true),
+        NAVIGABLE_SET(ActiveManagedCursorContainerNavigableSetImpl::new, true),
+        MANAGED_CURSOR_CONTAINER(ManagedCursorContainerImpl::new, false);
+
+        private final Supplier<ActiveManagedCursorContainer> factory;
+        private final boolean supportsGetNumberOfCursorsAtSamePositionOrBefore;
+
+        ActiveManagedCursorContainerImplType(Supplier<ActiveManagedCursorContainer> factory,
+                                             boolean supportsGetNumberOfCursorsAtSamePositionOrBefore) {
+            this.factory = factory;
+            this.supportsGetNumberOfCursorsAtSamePositionOrBefore = supportsGetNumberOfCursorsAtSamePositionOrBefore;
+        }
+
+        public ActiveManagedCursorContainer create() {
+            return factory.get();
+        }
+    }
+    @Param
+    private ActiveManagedCursorContainerImplType activeManagedCursorContainerImplType;
+
+    @Param({"0", "1", "10", "100"})
+    private int getNumberOfCursorsAtSamePositionOrBeforeRatio;
+
+    private ActiveManagedCursorContainer container;
+    private final List<MockManagedCursor> cursors = new ArrayList<>();
+
+    @State(Scope.Thread)
+    public static class ThreadState {
+        // Use a fixed seed for reproducibility in benchmarks
+        Random random = new Random(1);
+        long counter = 0;
+
+        public long nextCounter() {
+            return counter++;
+        }
+    }
+
+    @Setup(Level.Trial)
+    public void setup() throws Exception {
+        if (!activeManagedCursorContainerImplType.supportsGetNumberOfCursorsAtSamePositionOrBefore
+                && getNumberOfCursorsAtSamePositionOrBeforeRatio > 0) {
+            throw new UnsupportedOperationException("Skipping getNumberOfCursorsAtSamePositionOrBeforeRatio > 0 for "
+                    + activeManagedCursorContainerImplType.name()
+                    + " as it does not support this method");
+        }
+        container = activeManagedCursorContainerImplType.create();
+        IntStream.rangeClosed(1, numberOfCursors).mapToObj(
+                        idx -> MockManagedCursor.createCursor(container, "cursor" + idx, PositionFactory.create(0,
+                                idx)))
+                .forEach(cursors::add);
+        cursors.forEach(cursor -> container.add(cursor, cursor.getReadPosition()));
+        checkState(container);
+    }
+
+    @TearDown(Level.Trial)
+    public void tearDown() throws Exception {
+        // check that the state of the container is correct
+        checkState(container);
+    }
+
+    private void checkState(ActiveManagedCursorContainer container) {
+        if (container instanceof ActiveManagedCursorContainerImpl c) {
+            c.checkOrderingAndNumberOfCursorsState();
+        } else if (container instanceof ActiveManagedCursorContainerNavigableSetImpl c) {
+            c.checkOrderingAndNumberOfCursorsState();
+        }
+    }
+
+    @Threads(1)
+    @Benchmark
+    public int randomSeekingForward01(ThreadState threadState) {
+        return doRandomSeekingForward(threadState);
+    }
+
+    @Threads(10)
+    @Benchmark
+    public int randomSeekingForward10(ThreadState threadState) {
+        return doRandomSeekingForward(threadState);
+    }
+
+    private int doRandomSeekingForward(ThreadState threadState) {
+        Random random = threadState.random;
+        long counter = threadState.nextCounter();
+        // pick a random cursor
+        ManagedCursor cursor = cursors.get(random.nextInt(0, cursors.size()));
+        // seek forward by a random number of entries
+        cursor.seek(cursor.getReadPosition().getPositionAfterEntries(random.nextInt(1, 100)));
+        // return the number of cursors at the same position or before so that this is also part of the benchmark
+        return getNumberOfCursorsAtSamePositionOrBeforeRatio > 0
+                && counter % getNumberOfCursorsAtSamePositionOrBeforeRatio == 0
+                ? container.getNumberOfCursorsAtSamePositionOrBefore(cursor)
+                // if benchmarking this method is not enabled or the implementation does not support it,
+                // we return the entry id of the cursor's read position
+                : (int) cursor.getReadPosition().getEntryId();
+    }
+}
\ No newline at end of file
diff --git a/microbench/src/main/java/org/apache/bookkeeper/mledger/impl/MockManagedCursor.java b/microbench/src/main/java/org/apache/bookkeeper/mledger/impl/MockManagedCursor.java
new file mode 100644
index 00000000000..af602a8a9fe
--- /dev/null
+++ b/microbench/src/main/java/org/apache/bookkeeper/mledger/impl/MockManagedCursor.java
@@ -0,0 +1,454 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.bookkeeper.mledger.impl;
+
+import com.google.common.collect.Range;
+import com.google.common.collect.Sets;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Predicate;
+import org.apache.bookkeeper.mledger.AsyncCallbacks;
+import org.apache.bookkeeper.mledger.Entry;
+import org.apache.bookkeeper.mledger.ManagedCursor;
+import org.apache.bookkeeper.mledger.ManagedCursorMXBean;
+import org.apache.bookkeeper.mledger.ManagedLedger;
+import org.apache.bookkeeper.mledger.ManagedLedgerException;
+import org.apache.bookkeeper.mledger.Position;
+import org.apache.pulsar.common.policies.data.ManagedLedgerInternalStats;
+
+class MockManagedCursor implements ManagedCursor {
+    ActiveManagedCursorContainer container;
+    Position markDeletePosition;
+    Position readPosition;
+    private final boolean updateMarkDeletePosition;
+    private final boolean durable;
+    String name;
+
+    MockManagedCursor(ActiveManagedCursorContainer container, String name, Position markDeletePosition) {
+        this(container, name, markDeletePosition, null, true, true);
+    }
+
+    MockManagedCursor(ActiveManagedCursorContainer container, String name, Position markDeletePosition,
+                      Position readPosition, boolean updateMarkDeletePosition, boolean durable) {
+        this.container = container;
+        this.name = name;
+        this.markDeletePosition = markDeletePosition;
+        this.readPosition = readPosition;
+        this.updateMarkDeletePosition = updateMarkDeletePosition;
+        this.durable = durable;
+    }
+
+    static MockManagedCursor createCursor(ActiveManagedCursorContainer container, String name, Position position) {
+        return new MockManagedCursor(container, name, position, position, false, true);
+    }
+
+    static void addCursor(ActiveManagedCursorContainer container, String name, Position position) {
+        container.add(createCursor(container, name, position), position);
+    }
+
+    @Override
+    public Map<String, Long> getProperties() {
+        return Collections.emptyMap();
+    }
+
+    @Override
+    public Map<String, String> getCursorProperties() {
+        return Collections.emptyMap();
+    }
+
+    @Override
+    public CompletableFuture<Void> putCursorProperty(String key, String value) {
+        return CompletableFuture.completedFuture(null);
+    }
+
+    @Override
+    public CompletableFuture<Void> setCursorProperties(Map<String, String> cursorProperties) {
+        return CompletableFuture.completedFuture(null);
+    }
+
+    @Override
+    public CompletableFuture<Void> removeCursorProperty(String key) {
+        return CompletableFuture.completedFuture(null);
+    }
+
+    @Override
+    public boolean putProperty(String key, Long value) {
+        return false;
+    }
+
+    @Override
+    public boolean removeProperty(String key) {
+        return false;
+    }
+
+    @Override
+    public boolean isDurable() {
+        return durable;
+    }
+
+    @Override
+    public List<Entry> readEntries(int numberOfEntriesToRead) {
+        return new ArrayList<Entry>();
+    }
+
+    @Override
+    public void asyncReadEntries(int numberOfEntriesToRead, AsyncCallbacks.ReadEntriesCallback callback, Object ctx,
+                                 Position maxPosition) {
+        callback.readEntriesComplete(null, ctx);
+    }
+
+    @Override
+    public void asyncReadEntries(int numberOfEntriesToRead, long maxSizeBytes,
+                                 AsyncCallbacks.ReadEntriesCallback callback, Object ctx, Position maxPosition) {
+        callback.readEntriesComplete(null, ctx);
+    }
+
+    @Override
+    public boolean hasMoreEntries() {
+        return true;
+    }
+
+    @Override
+    public long getNumberOfEntries() {
+        return 0;
+    }
+
+    @Override
+    public long getNumberOfEntriesInBacklog(boolean isPrecise) {
+        return 0;
+    }
+
+    @Override
+    public void markDelete(Position position) {
+        markDelete(position, Collections.emptyMap());
+    }
+
+    @Override
+    public void markDelete(Position position, Map<String, Long> properties) {
+        this.markDeletePosition = position;
+        if (updateMarkDeletePosition) {
+            container.updateCursor(this, position);
+        }
+    }
+
+    @Override
+    public void asyncMarkDelete(Position position, AsyncCallbacks.MarkDeleteCallback callback, Object ctx) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public void asyncMarkDelete(Position position, Map<String, Long> properties,
+                                AsyncCallbacks.MarkDeleteCallback callback, Object ctx) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public Position getMarkDeletedPosition() {
+        return markDeletePosition;
+    }
+
+    @Override
+    public Position getPersistentMarkDeletedPosition() {
+        return markDeletePosition;
+    }
+
+    @Override
+    public String getName() {
+        return name;
+    }
+
+    @Override
+    public long getLastActive() {
+        return System.currentTimeMillis();
+    }
+
+    @Override
+    public void updateLastActive() {
+        // no-op
+    }
+
+    @Override
+    public String toString() {
+        return String.format("%s=%s/%s", name, markDeletePosition, readPosition);
+    }
+
+    @Override
+    public Position getReadPosition() {
+        return readPosition;
+    }
+
+    @Override
+    public void rewind() {
+    }
+
+    @Override
+    public void seek(Position newReadPosition, boolean force) {
+        this.readPosition = newReadPosition;
+        if (!updateMarkDeletePosition) {
+            container.updateCursor(this, newReadPosition);
+        }
+    }
+
+    @Override
+    public void close() {
+    }
+
+    @Override
+    public void asyncClose(AsyncCallbacks.CloseCallback callback, Object ctx) {
+    }
+
+    @Override
+    public void delete(Position position) {
+    }
+
+    @Override
+    public void asyncDelete(Position position, AsyncCallbacks.DeleteCallback callback, Object ctx) {
+    }
+
+    @Override
+    public void delete(Iterable<Position> positions) {
+    }
+
+    @Override
+    public void asyncDelete(Iterable<Position> position, AsyncCallbacks.DeleteCallback callback, Object ctx) {
+    }
+
+    @Override
+    public void clearBacklog() {
+    }
+
+    @Override
+    public void asyncClearBacklog(AsyncCallbacks.ClearBacklogCallback callback, Object ctx) {
+    }
+
+    @Override
+    public void skipEntries(int numEntriesToSkip, IndividualDeletedEntries deletedEntries) {
+    }
+
+    @Override
+    public void asyncSkipEntries(int numEntriesToSkip, IndividualDeletedEntries deletedEntries,
+                                 final AsyncCallbacks.SkipEntriesCallback callback, Object ctx) {
+    }
+
+    @Override
+    public Position findNewestMatching(Predicate<Entry> condition) {
+        return null;
+    }
+
+    @Override
+    public Position findNewestMatching(FindPositionConstraint constraint, Predicate<Entry> condition) {
+        return null;
+    }
+
+    @Override
+    public void asyncFindNewestMatching(FindPositionConstraint constraint, Predicate<Entry> condition,
+                                        AsyncCallbacks.FindEntryCallback callback, Object ctx) {
+    }
+
+    @Override
+    public void asyncFindNewestMatching(FindPositionConstraint constraint, Predicate<Entry> condition,
+                                        AsyncCallbacks.FindEntryCallback callback, Object ctx,
+                                        boolean isFindFromLedger) {
+    }
+
+    @Override
+    public void asyncResetCursor(final Position position, boolean forceReset,
+                                 AsyncCallbacks.ResetCursorCallback callback) {
+
+    }
+
+    @Override
+    public void resetCursor(final Position position) {
+
+    }
+
+    @Override
+    public Position getFirstPosition() {
+        return null;
+    }
+
+    @Override
+    public void setAlwaysInactive() {
+    }
+
+    @Override
+    public List<Entry> replayEntries(Set<? extends Position> positions) {
+        return null;
+    }
+
+    @Override
+    public Set<? extends Position> asyncReplayEntries(Set<? extends Position> positions,
+                                                      AsyncCallbacks.ReadEntriesCallback callback, Object ctx) {
+        return Sets.newConcurrentHashSet();
+    }
+
+    @Override
+    public Set<? extends Position> asyncReplayEntries(Set<? extends Position> positions,
+                                                      AsyncCallbacks.ReadEntriesCallback callback, Object ctx,
+                                                      boolean sortEntries) {
+        return Sets.newConcurrentHashSet();
+    }
+
+    @Override
+    public List<Entry> readEntriesOrWait(int numberOfEntriesToRead) {
+        return null;
+    }
+
+    @Override
+    public void asyncReadEntriesOrWait(int numberOfEntriesToRead, AsyncCallbacks.ReadEntriesCallback callback,
+                                       Object ctx, Position maxPosition) {
+    }
+
+    @Override
+    public void asyncReadEntriesOrWait(int maxEntries, long maxSizeBytes, AsyncCallbacks.ReadEntriesCallback callback,
+                                       Object ctx, Position maxPosition) {
+
+    }
+
+    @Override
+    public boolean cancelPendingReadRequest() {
+        return true;
+    }
+
+    @Override
+    public Entry getNthEntry(int num, IndividualDeletedEntries deletedEntries) {
+        return null;
+    }
+
+    @Override
+    public void asyncGetNthEntry(int num, IndividualDeletedEntries deletedEntries,
+                                 AsyncCallbacks.ReadEntryCallback callback, Object ctx) {
+    }
+
+    @Override
+    public void setActive() {
+    }
+
+    @Override
+    public void setInactive() {
+    }
+
+    @Override
+    public boolean isActive() {
+        return true;
+    }
+
+    @Override
+    public long getNumberOfEntriesSinceFirstNotAckedMessage() {
+        return 0;
+    }
+
+    @Override
+    public int getTotalNonContiguousDeletedMessagesRange() {
+        return 0;
+    }
+
+    @Override
+    public int getNonContiguousDeletedMessagesRangeSerializedSize() {
+        return 0;
+    }
+
+    @Override
+    public long getEstimatedSizeSinceMarkDeletePosition() {
+        return 0L;
+    }
+
+    @Override
+    public void setThrottleMarkDelete(double throttleMarkDelete) {
+    }
+
+    @Override
+    public double getThrottleMarkDelete() {
+        return -1;
+    }
+
+    @Override
+    public ManagedLedger getManagedLedger() {
+        return null;
+    }
+
+    @Override
+    public Range<Position> getLastIndividualDeletedRange() {
+        return null;
+    }
+
+    @Override
+    public void trimDeletedEntries(List<Entry> entries) {
+
+    }
+
+    @Override
+    public long[] getDeletedBatchIndexesAsLongArray(Position position) {
+        return new long[0];
+    }
+
+    @Override
+    public ManagedCursorMXBean getStats() {
+        return null;
+    }
+
+    @Override
+    public List<Entry> readEntriesOrWait(int maxEntries, long maxSizeBytes) {
+        return null;
+    }
+
+    @Override
+    public boolean checkAndUpdateReadPositionChanged() {
+        return false;
+    }
+
+    @Override
+    public boolean isClosed() {
+        return false;
+    }
+
+    @Override
+    public ManagedLedgerInternalStats.CursorStats getCursorStats() {
+        return null;
+    }
+
+    @Override
+    public boolean isMessageDeleted(Position position) {
+        return false;
+    }
+
+    @Override
+    public ManagedCursor duplicateNonDurableCursor(String nonDurableCursorName) throws ManagedLedgerException {
+        return null;
+    }
+
+    @Override
+    public long[] getBatchPositionAckSet(Position position) {
+        return new long[0];
+    }
+
+    @Override
+    public int applyMaxSizeCap(int maxEntries, long maxSizeBytes) {
+        return 0;
+    }
+
+    @Override
+    public void updateReadStats(int readEntriesCount, long readEntriesSize) {
+
+    }
+}
diff --git a/microbench/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeCacheEvictionBenchmark.java b/microbench/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeCacheEvictionBenchmark.java
new file mode 100644
index 00000000000..3bc5e44b129
--- /dev/null
+++ b/microbench/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeCacheEvictionBenchmark.java
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.bookkeeper.mledger.impl.cache;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+import java.util.concurrent.TimeUnit;
+import org.apache.bookkeeper.mledger.impl.EntryImpl;
+import org.apache.bookkeeper.mledger.impl.EntryReadCountHandlerImpl;
+import org.apache.commons.lang3.tuple.Pair;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.TearDown;
+import org.openjdk.jmh.annotations.Threads;
+import org.openjdk.jmh.annotations.Warmup;
+
+@BenchmarkMode(Mode.SampleTime)
+@OutputTimeUnit(TimeUnit.MILLISECONDS)
+@Threads(1)
+@State(Scope.Thread)
+@Warmup(time = 10, timeUnit = TimeUnit.SECONDS, iterations = 1)
+@Measurement(time = 10, timeUnit = TimeUnit.SECONDS, iterations = 1)
+@Fork(1)
+public class RangeCacheEvictionBenchmark {
+    private final int numberOfEntries = 1_000_000;
+    private final double propabitilyOfExpectedReadsRemaining = 0.10d;
+    private final double propabitilyOfAccessedEntries = 0.10d;
+    @Param({"true", "false"})
+    private boolean useRequeuing;
+    private List<EntryImpl> entries;
+    private Random random = new Random(1);
+    private RangeCacheRemovalQueue removalQueue;
+    private RangeCache rangeCache;
+    private long timestampNanosStart;
+
+    @Setup(Level.Trial)
+    public void setup() throws Exception {
+        entries = new ArrayList<>(numberOfEntries);
+        for (int i = 0; i < numberOfEntries; i++) {
+            EntryImpl entry = EntryImpl.create(0, i, new byte[0], useRequeuing ? 1 : 0);
+            entries.add(entry);
+        }
+    }
+
+    @Setup(Level.Invocation)
+    public void setupIteration() {
+        removalQueue = new RangeCacheRemovalQueue(5, true);
+        rangeCache = new RangeCache(removalQueue);
+        timestampNanosStart = System.nanoTime();
+        for (int i = 0; i < numberOfEntries; i++) {
+            EntryImpl entry = entries.get(i);
+            entry.retain();
+            rangeCache.put(entry.getPosition(), entry);
+            if (useRequeuing) {
+                ((EntryReadCountHandlerImpl) entry.getReadCountHandler()).setExpectedReadCount(1);
+                if (random.nextDouble() > propabitilyOfExpectedReadsRemaining) {
+                    entry.getReadCountHandler().markRead();
+                }
+                if (random.nextDouble() < propabitilyOfAccessedEntries) {
+                    rangeCache.get(entry.getPosition());
+                }
+            }
+        }
+    }
+
+    @TearDown(Level.Invocation)
+    public void tearDownIteration() {
+        Pair<Integer, Long> counters = rangeCache.clear();
+        if (counters.getLeft() > 0 || counters.getRight() > 0) {
+            throw new IllegalStateException("Cache wasn't empty after iteration: "
+                    + "entries=" + counters.getLeft() + ", bytes=" + counters.getRight());
+        }
+    }
+
+    @Benchmark
+    public boolean evictAllEntries() {
+        long evictBefore = timestampNanosStart;
+        while (!removalQueue.isEmpty()) {
+            timestampNanosStart = System.nanoTime();
+            removalQueue.evictLEntriesBeforeTimestamp(evictBefore);
+            evictBefore = timestampNanosStart;
+        }
+        return removalQueue.isEmpty();
+    }
+}
\ No newline at end of file
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCacheManager.java b/microbench/src/main/java/org/apache/bookkeeper/mledger/impl/cache/package-info.java
similarity index 60%
copy from managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCacheManager.java
copy to microbench/src/main/java/org/apache/bookkeeper/mledger/impl/cache/package-info.java
index 853cb1b8763..b3b812b6317 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCacheManager.java
+++ b/microbench/src/main/java/org/apache/bookkeeper/mledger/impl/cache/package-info.java
@@ -16,26 +16,8 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.bookkeeper.mledger.impl.cache;
 
-import org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl;
-
-public interface EntryCacheManager {
-    EntryCache getEntryCache(ManagedLedgerImpl ml);
-
-    void removeEntryCache(String name);
-
-    long getSize();
-
-    long getMaxSize();
-
-    void clear();
-
-    void updateCacheSizeAndThreshold(long maxSize);
-
-    void updateCacheEvictionWatermark(double cacheEvictionWatermark);
-
-    double getCacheEvictionWatermark();
-
-    void doCacheEviction(long maxTimestamp);
-}
+/**
+ * Microbenchmarks for Managed Ledger's cache implementation package.
+ */
+package org.apache.bookkeeper.mledger.impl.cache;
\ No newline at end of file
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCacheManager.java b/microbench/src/main/java/org/apache/bookkeeper/mledger/impl/package-info.java
similarity index 60%
copy from managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCacheManager.java
copy to microbench/src/main/java/org/apache/bookkeeper/mledger/impl/package-info.java
index 853cb1b8763..fe87bbee66c 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCacheManager.java
+++ b/microbench/src/main/java/org/apache/bookkeeper/mledger/impl/package-info.java
@@ -16,26 +16,8 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.bookkeeper.mledger.impl.cache;
 
-import org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl;
-
-public interface EntryCacheManager {
-    EntryCache getEntryCache(ManagedLedgerImpl ml);
-
-    void removeEntryCache(String name);
-
-    long getSize();
-
-    long getMaxSize();
-
-    void clear();
-
-    void updateCacheSizeAndThreshold(long maxSize);
-
-    void updateCacheEvictionWatermark(double cacheEvictionWatermark);
-
-    double getCacheEvictionWatermark();
-
-    void doCacheEviction(long maxTimestamp);
-}
+/**
+ * Microbenchmarks for Managed Ledger's implementation package.
+ */
+package org.apache.bookkeeper.mledger.impl;
\ No newline at end of file
diff --git a/pip/pip-430.md b/pip/pip-430.md
index 74d8065bfd8..9972c176463 100644
--- a/pip/pip-430.md
+++ b/pip/pip-430.md
@@ -1,13 +1,13 @@
-# PIP-430: Pulsar Broker cache improvements: refactoring eviction and adding a new cache strategy based on expected read count
+# PIP-430: Pulsar Broker Cache Improvements: Refactoring Eviction and Adding a New Cache Strategy Based on Expected Read Count
 
-# Background knowledge
+## Background Knowledge
 
 Apache Pulsar brokers maintain an in-memory entry cache to reduce latency and load on BookKeeper and tiered storage (S3) by serving frequently accessed message data directly from memory.
 
 Key concepts:
-- **`ManagedLedgerImpl`**: Manages the storage for a single topic partition. Each `ManagedLedgerImpl` instance has its own `EntryCache` (typically `RangeEntryCacheImpl`) instance.
+- **`ManagedLedgerImpl`**: Manages storage for a single topic partition. Each `ManagedLedgerImpl` instance has its own `EntryCache` instance.
 - **`EntryCache`**: Stores message entries (payloads) in memory. The default implementation is `RangeEntryCacheImpl`, which uses a `RangeCache` internally.
-- **`RangeCache`**: A specialized cache storing entries mapped by their `Position` (ledgerId, entryId). It supports range-based operations for retrieval and expiration.
+- **`RangeCache`**: A specialized cache that stores entries mapped by their `Position` (ledgerId, entryId). It supports range-based operations for retrieval and expiration.
 - **`EntryCacheManager` (`RangeEntryCacheManagerImpl`)**: A global component that limits the total size of all entry caches in a broker. When the total size exceeds a threshold, it triggers eviction.
 - **Cache Eviction Policies**: Mechanisms to remove entries from the cache to make space or remove old data.
     - **Timestamp-based eviction**: Removes entries older than a configured threshold (e.g., `managedLedgerCacheEvictionTimeThresholdMillis`). This is currently handled periodically by `ManagedLedgerFactoryImpl` iterating over all managed ledgers.
@@ -19,266 +19,198 @@ The broker cache serves various read patterns:
 - **Catch-up reads (backlogged cursors)**: Consumers reading older messages to catch up.
 - **Key_Shared subscription reads**: Messages with the same key are routed to the same consumer. If a consumer is slow, messages might be replayed while the cursor reads more messages for available consumers.
 
-# Motivation
+## Motivation
 
 The current Pulsar broker entry cache implementation and its eviction mechanisms face several challenges that impact performance, efficiency, and predictability:
 
-1.  **Inefficient and Flawed Size-Based Eviction**:
-    The `EntryCacheDefaultEvictionPolicy` (the current default for size-based eviction) does not guarantee the removal of the oldest entries globally. It sorts individual `EntryCache` instances by their size, selects a percentage of the largest caches, and then asks each of them to evict a proportional amount of data. This can lead to newer entries being evicted from large, active caches while older, less relevant entries remain in smaller or less active caches, resulting in suboptimal c [...]
+1. **Inefficient and Flawed Size-Based Eviction**:
+   The `EntryCacheDefaultEvictionPolicy` (the current default for size-based eviction) does not guarantee the removal of the oldest entries globally. It sorts individual `EntryCache` instances by their size, selects a percentage of the largest caches, and then asks each of them to evict a proportional amount of data. This can lead to newer entries being evicted from large, active caches while older, less relevant entries remain in smaller or less active caches, resulting in suboptimal ca [...]
 
-2.  **Inefficient and Incorrect Timestamp-Based Eviction**:
-    The existing timestamp-based eviction mechanism, triggered by `ManagedLedgerFactoryImpl`, has significant performance and correctness issues:
-    *   **Performance**: It iterates through *all* `ManagedLedgerImpl` instances and their respective `EntryCache` instances periodically (default every 10ms). In brokers with a large number of topics, this frequent and exhaustive iteration leads to high CPU utilization and memory pressure.
-    *   **Correctness**: The per-cache eviction (e.g., `RangeCache.evictLEntriesBeforeTimestamp`) often assumes entries within a single `RangeCache` are primarily ordered by timestamp due to typical append-only workloads. This assumption breaks down with mixed read patterns like catch-up reads or when entries are inserted out of their natural position order (Key_shared subscription replay queue scenario), potentially leading to incorrect eviction decisions or inefficient scanning.
+2. **Inefficient and Incorrect Timestamp-Based Eviction**:
+   The existing timestamp-based eviction mechanism, triggered by `ManagedLedgerFactoryImpl`, has significant performance and correctness issues:
+   - **Performance**: It iterates through *all* `ManagedLedgerImpl` instances and their respective `EntryCache` instances periodically (default every 10ms). In brokers with a large number of topics, this frequent and exhaustive iteration leads to high CPU utilization and memory pressure.
+   - **Correctness**: The per-cache eviction (e.g., `RangeCache.evictLEntriesBeforeTimestamp`) often assumes entries within a single `RangeCache` are primarily ordered by timestamp due to typical append-only workloads. This assumption breaks down with mixed read patterns like catch-up reads or when entries are inserted out of their natural position order (Key_shared subscription replay queue scenario), potentially leading to incorrect eviction decisions or inefficient scanning.
 
-3.  **Limited Cache Scope and Effectiveness for Diverse Read Patterns**:
-    The original `RangeCache` was primarily designed with tailing reads in mind. While support for caching for backlogged cursors and replay queue reads was added later, the eviction algorithms were not holistically updated to effectively manage mixed read patterns (tailing, catch-up, replays in Key_Shared). This can lead to:
-    *   Unnecessary BookKeeper and tiered storage (S3) reads during catch-up scenarios, even if data was recently read for another consumer.
-    *   Poor cache hit rates for Key_Shared subscriptions with slow consumers, as entries might be evicted before a replayed message (due to consumer unacknowledgment or redelivery request) is read again.
+3. **Limited Cache Scope and Effectiveness for Diverse Read Patterns**:
+   The original `RangeCache` was primarily designed with tailing reads in mind. While support for caching backlogged cursors and replay queue reads was added later, the eviction algorithms were not holistically updated to effectively manage mixed read patterns (tailing, catch-up, replays in Key_Shared). This can lead to:
+   - Unnecessary BookKeeper and tiered storage (S3) reads during catch-up scenarios, even if data was recently read for another consumer.
+   - Poor cache hit rates for Key_Shared subscriptions with slow consumers, as entries might be evicted before a replayed message (due to consumer unacknowledgment or redelivery request) is read again.
 
-4.  **Foundation for Advanced Caching Strategies Needed**:
-    The current cache architecture makes it difficult to implement more intelligent caching strategies that could further optimize for common Pulsar use cases, such as efficiently handling fan-out to multiple shared consumers or retaining entries expected to be read by several cursors.
+4. **Foundation for Advanced Caching Strategies Needed**:
+   The current cache architecture makes it difficult to implement more intelligent caching strategies that could further optimize for common Pulsar use cases, such as efficiently handling fan-out to multiple shared consumers or retaining entries expected to be read by several cursors.
 
 Addressing these issues is crucial for improving broker performance, reducing operational costs (lower BookKeeper load), and providing a more robust caching layer that can adapt to diverse workloads.
 
-# Goals
+## Goals
 
-The refactoring aims to make the cache eviction more robust, performant, and predictable. The "expected read count" strategy is an attempt to make the cache more aware of Pulsar's specific consumption patterns.
+The refactoring aims to make cache eviction more robust, performant, and predictable. The "expected read count" strategy is an attempt to make the cache more aware of Pulsar's specific consumption patterns.
 
-## In Scope
+### In Scope
 
--   **Refactor Cache Eviction Mechanism**:
-    -   Replace the existing per-cache iteration for timestamp eviction and the `EntryCacheDefaultEvictionPolicy` for size-based eviction with a centralized, insertion-order aware mechanism.
-    -   Implement a `RangeCacheRemovalQueue` that tracks all cached entries globally in approximate insertion order.
-    -   Ensure timestamp-based eviction reliably removes entries older than the threshold by processing this queue.
-    -   Ensure size-based eviction globally removes the oldest entries first from this queue until the target size is met.
--   **Introduce "Expected Read Count" Cache Strategy**:
-    -   Implement a new caching strategy where entries track an "expected read count," representing how many active cursors are anticipated to read that entry.
-    -   Prioritize retaining entries with a positive expected read count during size-based eviction.
-    -   Provide a new configuration option (`cacheEvictionByExpectedReadCount`) to enable this strategy.
--   **Improve Performance and Efficiency**:
-    -   Reduce CPU overhead associated with cache eviction, particularly timestamp-based eviction.
-    -   Improve overall cache hit rates by making better eviction decisions.
--   **Enhance Correctness**: Ensure eviction policies work correctly across various read patterns.
--   **Provide a Foundation for Future Cache Optimizations**: The refactored design should make it easier to implement further caching improvements, such as more sophisticated strategies for catch-up reads or Key_Shared subscriptions.
--   **Simplify RangeCache Implementation**: Remove unnecessary generic type parameters from the RangeCache implementation. Since RangeCache is used exclusively for a single purpose within the Pulsar codebase (caching managed ledger entries), the generic key and value parameters add unnecessary complexity without providing any practical benefit. This simplification will improve code readability and reduce cognitive overhead.
+- **Refactor Cache Eviction Mechanism**:
+  - Replace the existing per-cache iteration for timestamp eviction and the `EntryCacheDefaultEvictionPolicy` for size-based eviction with a centralized, insertion-order aware mechanism.
+  - Implement a `RangeCacheRemovalQueue` that tracks all cached entries globally in approximate insertion order.
+  - Ensure timestamp-based eviction reliably removes entries older than the threshold by processing this queue.
+  - Ensure size-based eviction globally removes the oldest entries first from this queue until the target size is met.
+- **Introduce "Expected Read Count" Cache Strategy**:
+  - Implement a new caching strategy where entries track an "expected read count," representing how many active cursors are anticipated to read that entry.
+  - Prioritize retaining entries with a positive expected read count during size-based eviction.
+  - Provide a new configuration option (`cacheEvictionByExpectedReadCount`) to enable this strategy.
+- **Introduce LRU Cache Behavior**:
+  - The entry's TTL will be extended by `managedLedgerCacheEvictionTimeThresholdMillis` during eviction check if it has been accessed since the last check.
+  - This behavior is controlled by the `managedLedgerCacheEvictionExtendTTLOfRecentlyAccessed` configuration option.
+  - This will help keep entries in the cache longer if they are accessed frequently. This will improve cache hit rates for topics with large fan-out consumers in scenarios where the consumers aren't connected at the time that the entry is read and aren't covered by the "expected read count" cache strategy. This is useful when all consumers are reconnecting after a broker restart, simultaneous network disconnection, or namespace bundle load balancing event.
+- **Improve Performance and Efficiency**:
+  - Reduce CPU overhead associated with cache eviction, particularly timestamp-based eviction.
+  - Improve overall cache hit rates by making better eviction decisions.
+- **Enhance Correctness**: Ensure eviction policies work correctly across various read patterns.
+- **Provide a Foundation for Future Cache Optimizations**: The refactored design should make it easier to implement further caching improvements, such as more sophisticated strategies for catch-up reads or Key_Shared subscriptions.
+- **Simplify RangeCache Implementation**: Remove unnecessary generic type parameters from the RangeCache implementation. Since RangeCache is used exclusively for a single purpose within the Pulsar codebase (caching managed ledger entries), the generic key and value parameters add unnecessary complexity without providing any practical benefit. This simplification will improve code readability and reduce cognitive overhead.
 
-# High Level Design
+## High Level Design
 
-The proposed solution involves two main components: a refactored eviction mechanism using a centralized removal queue and a new cache strategy based on expected read count.
+The proposed solution involves two main components: a refactored eviction mechanism using a centralized removal queue and a new cache strategy based on expected read count and additionally LRU cache behavior.
 
-## 1. Centralized Cache Eviction with `RangeCacheRemovalQueue`
+### 1. Centralized Cache Eviction with `RangeCacheRemovalQueue`
 
 A new component, `RangeCacheRemovalQueue`, will be introduced at the `EntryCacheManager` level.
--   **Entry Tracking**: When an entry is inserted into any `RangeEntryCacheImpl` (the per-ledger cache), a lightweight wrapper for this entry (`RangeCacheEntryWrapper`) is also added to this global `RangeCacheRemovalQueue`. This queue maintains entries in their global insertion order (FIFO). This wrapper is already necessary in `RangeEntryCacheImpl` to prevent consistency issues. The current internal wrapper class is refactored to a top-level class so that it can be used with the removal queue.
--   **Timestamp-Based Eviction**: A single, periodic task (e.g., managed by `ManagedLedgerFactoryImpl`'s `cacheEvictionExecutor`) will process the `RangeCacheRemovalQueue`. It will iterate from the head of the queue, removing entries whose `timestampNanos` are older than the `cacheEvictionTimeThresholdNanos`. Since the queue is insertion-ordered, this process can often stop early once it encounters an entry that is not expired.
--   **Size-Based Eviction**: When the `EntryCacheManager` detects that the total cache size exceeds `evictionTriggerThresholdPercent * maxSize`, it will trigger an eviction cycle. This cycle will also process the `RangeCacheRemovalQueue` from the head, removing the oldest entries (regardless of which specific ledger they belong to) until the cache size is brought down to `cacheEvictionWatermark * maxSize`.
--   **Stashing Mechanism**: During size-based eviction, if an entry is encountered that should not be evicted immediately (e.g., due to a positive "expected read count" as per the new strategy, and it hasn't met timestamp expiration), it will be temporarily "stashed" (moved to a secondary list within `RangeCacheRemovalQueue`). Stashed entries are reconsidered in subsequent eviction passes or if they eventually expire by timestamp. This prevents premature eviction of entries that are like [...]
+- **Entry Tracking**: When an entry is inserted into any `RangeEntryCacheImpl` (the per-ledger cache), a lightweight wrapper for this entry (`RangeCacheEntryWrapper`) is also added to this global `RangeCacheRemovalQueue`. This queue maintains entries in their global insertion order (FIFO). This wrapper is already necessary in `RangeEntryCacheImpl` to prevent consistency issues. The current internal wrapper class is refactored to a top-level class so that it can be used with the removal queue.
+- **Timestamp-Based Eviction**: A single, periodic task (e.g., managed by `ManagedLedgerFactoryImpl`'s `cacheEvictionExecutor`) will process the `RangeCacheRemovalQueue`. It will iterate from the head of the queue, removing entries whose `timestampNanos` are older than the `cacheEvictionTimeThresholdNanos`. Since the queue is insertion-ordered, this process can often stop early once it encounters an entry that is not expired.
+- **Size-Based Eviction**: When the `EntryCacheManager` detects that the total cache size exceeds `evictionTriggerThresholdPercent * maxSize`, it will trigger an eviction cycle. This cycle will also process the `RangeCacheRemovalQueue` from the head, removing the oldest entries (regardless of which specific ledger they belong to) until the cache size is brought down to `cacheEvictionWatermark * maxSize`.
+- **Requeuing Mechanism**: During time-based or size-based eviction, if an entry is encountered that should not be evicted immediately (e.g., due to a positive "expected read count" as per the new strategy), it will be temporarily "requeued" (moved to the end of the `removalQueue`). Requeued entries are reconsidered in subsequent eviction passes or if they eventually expire by `managedLedgerCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes` setting. This prevents prematur [...]
 
 This centralized approach replaces the distributed, per-cache iteration for timestamp eviction and the less precise `EntryCacheDefaultEvictionPolicy` for size eviction, leading to more globally optimal and efficient eviction decisions.
 
-## 2. "Expected Read Count" Cache Strategy
+### S3FIFO Cache Eviction Algorithm Influence
 
-This new strategy aims to improve cache hit rates by retaining entries that are likely to be read by multiple active consumers/cursors.
--   **`EntryReadCountHandler`**: Each cached entry (`CachedEntryImpl`) will be associated with an `EntryReadCountHandlerImpl`. This handler maintains an `expectedReadCount` (an atomic integer).
--   **Initialization**:
-    -   When a new entry is added to the ledger (`OpAddEntry`), its `expectedReadCount` is initialized to the number of *active* cursors.
-    -   When entries are read from BookKeeper or tiered storage and inserted into the cache (`RangeEntryCacheImpl.readFromStorage`), the `expectedReadCount` is initialized based on the current state of active cursors currently positioned before or at the entry being added. This information is sourced from `ManagedCursorContainer.getNumberOfCursorsAtSamePositionOrBefore(ManagedCursor)`.
--   **Dynamic Updates**:
-    -   When an entry is actually delivered to a consumer from the cache and subsequently released by the delivery mechanism, its `expectedReadCount` is decremented (via `EntryReadCountHandler.markRead()`).
-    -   The `expectedReadCount` on a cached entry is incremented when the entry is added to the replay queue. This will de-prioritize the removal of the entry from the cache when size based eviction is performed so that when the Key_Shared consumer is available to read the entry, it would more likely be available in the cache.
--   **Eviction Consideration**:
-    -   The `RangeCacheRemovalQueue`'s size-based eviction logic will consult `CachedEntry.canEvict()`. This method returns `true` if `expectedReadCount <= 0`.
-    -   If `canEvict()` is false and the entry hasn't met timestamp expiration, it's stashed instead of being immediately evicted during a size-based eviction pass.
-    -   If an entry is forcefully removed from the cache (e.g., ledger deletion, or direct invalidation call that bypasses normal eviction), `EntryReadCountHandler.markEvicted()` is called to set a special state, preventing further increments and ensuring it's recognized as fully processed.
--   **Configuration**: A new broker configuration setting, `cacheEvictionByExpectedReadCount` (boolean, default: `true`), will enable this strategy. If set to `false`, the cache will fall back to simpler eviction based on timestamp and potentially `cacheEvictionByMarkDeletedPosition` (if enabled, though the latter's effectiveness is reduced without the new read count logic).
+This FIFO queue-based solution is influenced by the [S3FIFO cache eviction algorithm](https://s3fifo.com/) ([algorithm explanation](https://blog.jasony.me/system/cache/2023/08/01/s3fifo)), although it is not an implementation of S3FIFO. It accomplishes a similar goal as S3FIFO, but for the managed ledger broker cache. The goal of S3FIFO is to quickly remove entries that are unlikely to be accessed in the future. Similarly, the PIP-430 broker cache removal queue aims to quickly and effici [...]
 
-This strategy allows the cache to be more intelligent about retaining entries that have higher utility, especially in fan-out scenarios or with slightly lagging consumers in Key_Shared subscriptions. The replay queue is also used in Shared subscriptions when more entries are read than can be consumed by the available consumers. This strategy will also avoid cache misses in those scenarios.
+The S3FIFO algorithm explanation blog post states: "While the eviction algorithms so far have been centered around LRU, I believe modern eviction algorithms should be designed with FIFO queues." This is one of the reasons why the PIP-430 broker cache removal queue is also based on a FIFO queue.
 
-When using the expected read count caching strategy, there would be separate configuration values for specifying the time-to-live values for entries that have already reached their "expected read count" and for entries that still have a positive "expected read count". The current `managedLedgerCacheEvictionTimeThresholdMillis` setting would be used for entries that have reached their "expected read count". For entries that still have a positive "expected read count", a separate setting ` [...]
-
-# Detailed Design
-
-## Design & Implementation Details
-
-### 1. `RangeCacheRemovalQueue`
--   **Structure**:
-    -   `removalQueue`: `org.jctools.queues.MpscUnboundedArrayQueue<RangeCacheEntryWrapper>` to hold entries in insertion order.
-    -   `stash`: `RangeCacheRemovalQueueStash` (internal class), a list-based mechanism to temporarily hold entries that are not immediately evictable during size-based eviction (e.g., due to positive expected read count).
-        -   `RangeCacheRemovalQueueStash.add(RangeCacheEntryWrapper)`: Adds an entry to the stash.
-        -   `RangeCacheRemovalQueueStash.evictEntries(...)`: Iterates stashed entries, applying eviction predicate.
-        -   `RangeCacheRemovalQueueStash.maybeTrim()`: Compacts the stash list if many entries are removed to reclaim space.
--   **Eviction Logic (`evictEntries` method with `EvictionPredicate`)**:
-    -   Synchronized method to ensure single-threaded eviction.
-    -   First processes the `stash`, then the `removalQueue`.
-    -   `EvictionPredicate` determines the `EvictionResult` for each entry:
-        -   `REMOVE`: Entry is evicted.
-        -   `STASH`: Entry is moved from `removalQueue` to `stash` (only during size-based eviction if entry is not evictable by read count but not yet timestamp-expired).
-        -   `STOP`: Stops processing further entries in the current pass (e.g., for timestamp eviction when a non-expired entry is found, or for size eviction when enough space is freed).
-        -   `MISSING`: Entry was already removed from cache by other means.
-    -   `evictLEntriesBeforeTimestamp(long timestampNanos)`: Uses a predicate: `e.timestampNanos < timestampNanos ? REMOVE : STOP`. (Note: The `STASH_AND_STOP` mentioned in a discussion snippet was refined; for timestamp eviction, it's just `STOP` if not expired, as stashing by itself is mainly for size-based eviction policy related to `expectedReadCount`).
-    -   `evictLeastAccessedEntries(long sizeToFree, long expirationTimestampNanos)`: Uses a predicate:
-        ```java
-        (e, c) -> {
-            if (c.removedSize >= sizeToFree) return EvictionResult.STOP;
-            boolean expired = e.timestampNanos < expirationTimestampNanos;
-            if (!(e.value.canEvict() || expired)) return EvictionResult.STASH; // Not evictable by read count and not expired
-            return EvictionResult.REMOVE;
-        }
-        ```
+### 2. "Expected Read Count" Cache Strategy
 
-### 2. `CachedEntry`
+This new strategy aims to improve cache hit rates by retaining entries that are likely to be read by multiple active consumers/cursors.
+- **`EntryReadCountHandler`**: Each cached entry (`EntryImpl`) will be associated with an `EntryReadCountHandlerImpl`. This handler maintains an `expectedReadCount` (an atomic integer).
+- **Initialization**:
+  - When a new entry is added to the ledger (`OpAddEntry`), its `expectedReadCount` is initialized to the number of *active* cursors.
+  - When entries are read from BookKeeper or tiered storage and inserted into the cache (`RangeEntryCacheImpl.readFromStorage`), the `expectedReadCount` is initialized based on the current state of active cursors currently positioned before or at the entry being added. This information is sourced from `ActiveManagedCursorContainer.getNumberOfCursorsAtSamePositionOrBefore(ManagedCursor)`.
+- **Dynamic Updates**:
+  - When an entry is actually delivered to a consumer from the cache and subsequently released by the delivery mechanism, its `expectedReadCount` is decremented (via `EntryReadCountHandler.markRead()`).
+  - The `expectedReadCount` on a cached entry is incremented when the entry is added to the replay queue. This will de-prioritize the removal of the entry from the cache when size-based eviction is performed so that when the Key_Shared consumer is available to read the entry, it would more likely be available in the cache.
+- **Eviction Consideration**:
+  - The `RangeCacheRemovalQueue`'s size-based eviction logic will consult `EntryImpl.hasExpectedReads()`. This method returns `true` if `expectedReadCount > 0` and it has been defined.
+  - If an entry is forcefully removed from the cache (e.g., ledger deletion, or direct invalidation call that bypasses normal eviction), the entry is simply removed.
+- **Configuration**:
+  - `cacheEvictionByExpectedReadCount` (boolean, default: `true`): Enables the new eviction strategy based on expected read count. When true, entries with `expectedReadCount > 0` are less likely to be evicted by size-based eviction unless they also meet timestamp expiration.
+  - `managedLedgerCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes` (Integer, default: `5`): Maximum number of times the cache can extend the TTL of an entry that has remaining expected reads. Only takes effect when `cacheEvictionByExpectedReadCount` is enabled.
+  - `managedLedgerCacheEvictionExtendTTLOfRecentlyAccessed` (boolean, default: `true`): Controls whether recently accessed entries in the managed ledger cache should have their lifetime extended before cache eviction.
 
-Then value entries in the range cache are `CachedEntry` instances. The key is a `Position`.
+This strategy allows the cache to be more intelligent about retaining entries that have higher utility, especially in fan-out scenarios or with slightly lagging consumers in Key_Shared subscriptions. The replay queue is also used in Shared subscriptions when more entries are read than can be consumed by the available consumers. This strategy will also avoid cache misses in those scenarios.
 
-```java
-/**
- * Interface for cached entries in the {@link RangeCache}.
- */
-public interface CachedEntry extends Entry, ReferenceCounted {
-    boolean matchesKey(Position key);
-    boolean canEvict();
-    boolean increaseReadCount(int expectedReadCount);
-}
-```
+## Detailed Design
+
+### Design & Implementation Details
 
-These are the relevant details related to the "expected read count" implementation:
--   **`CachedEntry` interface**:
-    -   `boolean canEvict()`: Returns `true` if `expectedReadCount <= 0` (or if no read count handler is present).
-    -   `boolean increaseReadCount(int expectedReadCount)`: Increments the internal expected read count.
--   **`CachedEntryImpl` class**: Implements `CachedEntry`. Holds a reference to `EntryReadCountHandlerImpl`.
+#### 1. `RangeCacheRemovalQueue`
+- **Structure**:
+  - `removalQueue`: `org.jctools.queues.MpscUnboundedArrayQueue<RangeCacheEntryWrapper>` to hold entries in insertion order in a FIFO queue.
+  - `EvictionPredicate` determines the `EvictionResult` for each entry:
+    - `REMOVE`: Entry is evicted.
+    - `REQUEUE`: Entry is moved to the tail of the `removalQueue` (which extends the TTL by `managedLedgerCacheEvictionTimeThresholdMillis`).
+    - `STOP`: Stops processing further entries in the current pass (e.g., for timestamp eviction when a non-expired entry is found, or for size eviction when enough space is freed).
+    - `MISSING`: Entry was already removed from cache by other means.
+  - `evictLEntriesBeforeTimestamp(long timestampNanos)`: Uses a predicate to remove entries whose `timestampNanos` is older than the provided timestamp. It requeues entries if `extendTTLOfRecentlyAccessed` is enabled and the entry has been accessed since the last eviction check or if the entry has remaining expected reads and the requeue count is less than the configured maximum.
+  - `evictLeastAccessedEntries(long sizeToFree, long expirationTimestampNanos)`: Uses a predicate to remove the least accessed entries until the specified size is freed. It considers the `extendTTLOfRecentlyAccessed` and expected read count when determining eviction eligibility.
 
-### 3. `EntryReadCountHandler`
+#### 2. `EntryImpl` Modifications and `EntryReadCountHandler`
 
-A `CachedEntry` implementatation holds a reference to a `EntryReadCountHandler` which handles the state related to read count.
-`EntryReadCountHandler` is used as an abstraction and integration between `RangeCacheRemovalQueue`, `CachedEntry` and the Managed Ledger layer. The `EntryImpl` in Managed Ledger layer has also been modified to hold an `EntryReadCountHandler` instance so that the information can be passed on. The implementation of `EntryReadCountHandler` holds the state in a single `volatile int` field.
+The values stored inside a `RangeCache` are `EntryImpl` instances (they implement `ReferenceCountedEntry`). `EntryImpl` has been modified to hold a reference to an `EntryReadCountHandler` instance which handles the state related to read count. `EntryReadCountHandler` is used as an abstraction and integration between `RangeCacheRemovalQueue`, `EntryImpl` and the Managed Ledger layer. The implementation of `EntryReadCountHandler` holds the state in a single `volatile int` field.
 
 ```java
 public interface EntryReadCountHandler {
     int getExpectedReadCount();
-    boolean incrementExpectedReadCount();
-    boolean incrementExpectedReadCount(int increment);
+
+    void incrementExpectedReadCount();
+
     void markRead();
-    void markEvicted();
+
+    default boolean hasExpectedReads() {
+        return getExpectedReadCount() >= 1;
+    }
 }
 ```
 
--   **`EntryReadCountHandler` interface**:
-    -   `int getExpectedReadCount()`
-    -   `boolean incrementExpectedReadCount(int increment)`
-    -   `boolean incrementExpectedReadCount()`
-    -   `void markRead()`: Decrements the count.
-    -   `void markEvicted()`: Sets count to a special "evicted" value (e.g., `Integer.MIN_VALUE / 2`) to stop further modifications and identify it as fully processed.
--   **`EntryReadCountHandlerImpl` class**:
-    -   Uses `AtomicIntegerFieldUpdater` for `expectedReadCount`.
-    -   Handles the special `EVICTED_VALUE`.
-
-### 4. Integration with `ManagedLedgerImpl` and `ManagedCursorContainer`
-
--   **`OpAddEntry`**:
-    -   When an entry is successfully added to a ledger:
-        ```java
-        if (!(ml instanceof ShadowManagedLedgerImpl)) {
-            int activeCursorCount = ml.getActiveCursors().size();
-            if (activeCursorCount > 0) {
-                int expectedReadCount = 0;
-                if (ml.getConfig().isCacheEvictionByExpectedReadCount()) {
-                    // For newly added entries, all active cursors are effectively "before" the added entry for future reads.
-                    expectedReadCount = activeCursorCount; 
-                }
-                EntryImpl entry = EntryImpl.create(ledgerId, entryId, data, expectedReadCount);
-                entry.setDecreaseReadCountOnRelease(false); // Cache owns the primary read count handling now
-                ml.entryCache.insert(entry);
-                entry.release();
-            }
-        }
-        ```
--   **`ManagedLedgerImpl.asyncReadEntry`**:
-    -   The `expectedReadCount` parameter (an `IntSupplier`) is passed down to `RangeEntryCacheImpl.asyncReadEntry0`.
-    -   The `IntSupplier` would resolve to `opReadEntry.cursor.getNumberOfCursorsAtSamePositionOrBefore()` when `cacheEvictionByExpectedReadCount` is true.
--   **`RangeEntryCacheImpl.readFromStorage`**:
-    -   Accepts an `IntSupplier expectedReadCount`.
-    -   When creating `EntryImpl` from `LedgerEntry`, it passes `expectedReadCount.getAsInt()` to `EntryImpl.create`.
--   **`RangeEntryCacheImpl.insert(Entry entry)`**:
-    -   If an entry with the same position already exists in the cache:
-        ```java
-        CachedEntry previousEntry = entries.get(position);
-        if (previousEntry != null) {
-            try {
-                if (entry.getReadCountHandler() != null) {
-                    // If the entry is already in the cache, increase the expected read count on the existing entry
-                    // by the count from the new entry (which represents new potential readers for this insert op)
-                    if (previousEntry.increaseReadCount(entry.getReadCountHandler().getExpectedReadCount())) {
-                        return false; // Successfully updated existing entry's read count
-                    }
-                }
-            } finally {
-                previousEntry.release();
-            }
-        }
-        // ... proceed to create new CachedEntryImpl and put it ...
-        CachedEntryImpl cacheEntry = CachedEntryImpl.create(position, cachedData, (EntryReadCountHandlerImpl) entry.getReadCountHandler());
-        ```
--   **`ManagedCursorContainer`**:
-    -   Needs an efficient way to count active cursors at or before a given position so that "expected read count" can be calculated for backlog reads. `ManagedCursorContainer` has been modified to use `TreeMap` so that cursors can be sorted by position.
+- **`EntryReadCountHandler` interface**:
+  - `int getExpectedReadCount()` returns the current expected read count.
+  - `void incrementExpectedReadCount()` increments the count.
+  - `void markRead()`: Decrements the count.
+- **`EntryReadCountHandlerImpl` class**:
+  - Uses `AtomicIntegerFieldUpdater` for `expectedReadCount`.
+
+#### 3. `ActiveManagedCursorContainer` and `ActiveManagedCursorContainerImpl` 
+
+To efficiently track the number of active cursors at or before a given position, a new `ActiveManagedCursorContainer` interface is introduced. The `ActiveManagedCursorContainerImpl` class implements tracking using a sorted linked list. JMH benchmarks show that this implementation is more efficient for the expected use cases, allowing O(1) access to the number of cursors at a given position. The `ActiveManagedCursorContainerImpl` is only used when the `cacheEvictionByExpectedReadCount` is [...]
+
+#### 4. Integration with `ManagedLedgerImpl`
+
+- **`OpAddEntry`**:
+  - When an entry is successfully added to a ledger:
     ```java
-    // ManagedCursorContainer
-    private final NavigableMap<Position, List<Item>> sortedByPosition = new TreeMap<>();
-    // ... methods to add/remove/update items in sortedByPosition ...
-    public int getNumberOfCursorsAtSamePositionOrBefore(ManagedCursor cursor) {
-        long stamp = rwLock.readLock();
-        try {
-            Item item = cursors.get(cursor.getName()); // cursors is the existing ConcurrentMap
-            if (item == null || item.position == null) { // item.position can be null if cursor is not reading
-                return 0;
-            } else {
-                int sum = 0;
-                // Iterate through positions up to and including the cursor's current read position
-                for (Map.Entry<Position, List<Item>> entry : sortedByPosition.headMap(item.position, true).entrySet()) {
-                    sum += entry.getValue().size();
-                }
-                return sum;
+    if (!(ml instanceof ShadowManagedLedgerImpl)) {
+        int activeCursorCount = ml.getActiveCursors().size();
+        if (activeCursorCount > 0) {
+            int expectedReadCount = 0;
+            if (ml.getConfig().isCacheEvictionByExpectedReadCount()) {
+                // For newly added entries, all active cursors are effectively "before" the added entry for future reads.
+                expectedReadCount = activeCursorCount; 
             }
-        } finally {
-            rwLock.unlockRead(stamp);
+            EntryImpl entry = EntryImpl.create(ledgerId, entryId, data, expectedReadCount);
+            entry.setDecreaseReadCountOnRelease(false); // Cache owns the primary read count handling now
+            ml.entryCache.insert(entry);
+            entry.release();
         }
     }
     ```
--   **`EntryImpl.release()`**:
-    -   A new flag `decreaseReadCountOnRelease` (default `false`) is added to `EntryImpl`.
-    -   When `EntryImpl.create(Entry other)` (copy constructor for dispatch) is called, this flag is set to `true` on the copy.
-    -   `EntryImpl.beforeDeallocate()`: if `decreaseReadCountOnRelease` is true and `readCountHandler` is not null, call `readCountHandler.markRead()`.
+- **`ManagedLedgerImpl.asyncReadEntry`**:
+  - The `expectedReadCount` parameter (an `IntSupplier`) is passed down to `RangeEntryCacheImpl.asyncReadEntry0`.
+  - The `IntSupplier` would resolve to `opReadEntry.cursor.getNumberOfCursorsAtSamePositionOrBefore()` when `cacheEvictionByExpectedReadCount` is true.
+- **`RangeEntryCacheImpl.readFromStorage`**:
+  - Accepts an `IntSupplier expectedReadCount`.
+  - When creating `EntryImpl` from `LedgerEntry`, it passes `expectedReadCount.getAsInt()` to `EntryImpl.create`.
+- **`EntryImpl.release()`**:
+  - A new flag `decreaseReadCountOnRelease` (default `false`) is added to `EntryImpl`.
+  - When `EntryImpl.create(Entry other)` (copy constructor for dispatch) is called, this flag is set to `true` on the copy.
+  - `EntryImpl.beforeDeallocate()`: if `decreaseReadCountOnRelease` is true and `readCountHandler` is not null, call `readCountHandler.markRead()`.
     This ensures that only when an entry copy created for dispatch is released, the read count on the original cached entry's handler is decremented.
 
-### 4. `RangeEntryCacheManagerImpl`
--   `doCacheEviction`: Called by the global eviction scheduler.
-    -   Invokes `evictionHandler.invalidateEntriesBeforeTimestampNanos`. Would also have to take separate `managedLedgerCacheEvictionTimeThresholdMillis` and `managedLedgerCacheEvictionTimeThresholdMillisMax` settings into account.
-    -   Calls `doEvictToWatermarkWhenOverThreshold()` to ensure cache size is within limits.
--   `triggerEvictionWhenNeeded()`: Called after an entry is added.
-    -   If size > threshold, schedules `doEvictToWatermarkWhenOverThreshold()` on `cacheEvictionExecutor`.
--   `doEvictToWatermarkWhenOverThreshold()`:
-    -   Calculates `sizeToEvict`.
-    -   Calls `evictionHandler.evictEntries(sizeToEvict, expirationTimestampNanosForNonEvictableEntries)`.
+#### 5. `RangeEntryCacheManagerImpl`
+- `doCacheEviction`: Called by the global eviction scheduler.
+  - Invokes `evictionHandler.invalidateEntriesBeforeTimestampNanos`. Would also have to take separate `managedLedgerCacheEvictionTimeThresholdMillis` and `managedLedgerCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes` settings into account.
+  - Calls `doEvictToWatermarkWhenOverThreshold()` to ensure cache size is within limits.
+- `triggerEvictionWhenNeeded()`: Called after an entry is added.
+  - If size > threshold, schedules `doEvictToWatermarkWhenOverThreshold()` on `cacheEvictionExecutor`.
+- `doEvictToWatermarkWhenOverThreshold()`:
+  - Calculates `sizeToEvict`.
+  - Calls `evictionHandler.evictEntries(sizeToEvict, expirationTimestampNanosForNonEvictableEntries)`.
 
-## Note about entry cache size and how it's related to broker cache total size
+### Note About Entry Cache Size and How It's Related to Broker Cache Total Size
 
 The calculation of the entry cache size is not accurate unless the `managedLedgerCacheCopyEntries` setting is set to `true`. This occurs because when the entry payload is read from BookKeeper or received from a Pulsar client publisher, it is "sliced" from an underlying buffer. The actual memory consumption can be higher when the other entries of the shared underlying buffer have been released and there is a single entry that retains the complete underlying buffer.
 
-For entries received from a Pulsar client publisher, the buffer size is between 16kB to 1MB. This is currently not configurable and it is set in code in [BrokerService.defaultServerBootstrap method](https://github.com/apache/pulsar/blob/19b4a05f888da35a584cf7ff6d594dd9df5e03d1/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java#L564-L565).
+For entries received from a Pulsar client publisher, the buffer size is between 16kB to 1MB. This is currently not configurable and is set in code in the [BrokerService.defaultServerBootstrap method](https://github.com/apache/pulsar/blob/19b4a05f888da35a584cf7ff6d594dd9df5e03d1/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java#L564-L565).
 
 ```java
         bootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR,
             new AdaptiveRecvByteBufAllocator(1024, 16 * 1024, 1 * 1024 * 1024));
 ```
 
-The actual underlying buffer size will also depend on Netty's LengthFieldBasedFrameDecoder (extends ByteToMessageDecoder) logic which will cumulate buffers by copying until the complete entry has been read. In that case, the underlying buffer size can be larger than 1MB. The cumulated buffer will shared be shared across multiple entries since entry buffers are sliced from the cumulated buffer.
+The actual underlying buffer size will also depend on Netty's LengthFieldBasedFrameDecoder (extends ByteToMessageDecoder) logic, which will cumulate buffers by copying until the complete entry has been read. In that case, the underlying buffer size can be larger than 1MB. The cumulated buffer will be shared across multiple entries since entry buffers are sliced from the cumulated buffer.
 
 For entries received from BookKeeper, it's similar. However, BookKeeper client defaults to Netty's [default channel config settings](https://github.com/netty/netty/blob/c3416d8ad2260f3c9bc98a9061d25bba92020f93/transport/src/main/java/io/netty/channel/DefaultChannelConfig.java#L74-L76) for RCVBUF_ALLOCATOR, which has a maximum buffer size of 64kB.
 
-It would be useful to make the Pulsar broker side `RCVBUF_ALLOCATOR` parameters configurable so that the parameters could be tuned to a smaller size. It is very unlikely that the 1MB maximum size improves performance significantly. There is a performance tradeoff in the case where messages are large and buffers would have to be merged by copying in the LengthFieldBasedFrameDecoder. To avoid this overhead, it would be possible to make LengthFieldBasedFrameDecoder to use composite buffers  [...]
+It would be useful to make the Pulsar broker side `RCVBUF_ALLOCATOR` parameters configurable so that the parameters could be tuned to a smaller size. It is very unlikely that the 1MB maximum size improves performance significantly. There is a performance tradeoff in the case where messages are large and buffers would have to be merged by copying in the LengthFieldBasedFrameDecoder. To avoid this overhead, it would be possible to make LengthFieldBasedFrameDecoder use composite buffers for [...]
 
-Since in many cases, the underlying buffer where the slice is taken for a single entry is shared with entries that have been published or retrieved together, it is also common that these entries will get evicted together. Making the RCVBUF_ALLOCATOR settings configurable in the Pulsar broker is a sufficient mitigation for the problem.
-In cases where shared buffers add a lot of overhead of consumed memory, it will be possible to reduce it by setting the maximum size for `AdaptiveRecvByteBufAllocator` to 64kB, with the tradeoff of possible unnecessary buffer copies for messages exceeding 64kB. In BookKeeper server, there are settings `byteBufAllocatorSizeInitial`, `byteBufAllocatorSizeMin` and `byteBufAllocatorSizeMax` to configure the `AdaptiveRecvByteBufAllocator` parameters. The naming of these parameters in BookKeep [...]
+Since in many cases, the underlying buffer where the slice is taken for a single entry is shared with entries that have been published or retrieved together, it is also common that these entries will get evicted together. Making the RCVBUF_ALLOCATOR settings configurable in the Pulsar broker is a sufficient mitigation for the problem. In cases where shared buffers add a lot of overhead of consumed memory, it will be possible to reduce it by setting the maximum size for `AdaptiveRecvByteB [...]
 
 The proposal would be to add these configuration parameters to `broker.conf` for controlling Broker's `AdaptiveRecvByteBufAllocator` parameters:
 ```properties
@@ -294,19 +226,44 @@ brokerAdaptiveRecvByteBufAllocatorMaximumSize=1048576
 ## Public-facing Changes
 
 ### Configuration
--   **New Configuration**: `broker.conf`
-    -   `cacheEvictionByExpectedReadCount` (boolean): Enables the new eviction strategy based on expected read count. When true, entries with `expectedReadCount > 0` are less likely to be evicted by size-based eviction unless they also meet timestamp expiration.
-        Default: `true`.
-    -   `managedLedgerCacheEvictionTimeThresholdMillisMax` (Integer): cache entry time-to-live for entries that have pending expected reads (applies only when `cacheEvictionByExpectedReadCount=true`)
-        Default: `5000`
-    -   `brokerAdaptiveRecvByteBufAllocatorMinimumSize` (int): Netty adaptive receive buffer allocator's minimum size in bytes. Default: `1024`.
-    -   `brokerAdaptiveRecvByteBufAllocatorInitialSize` (int): Netty adaptive receive buffer allocator's initial size in bytes. Default: `16384`.
-    -   `brokerAdaptiveRecvByteBufAllocatorMaximumSize` (int): Netty adaptive receive buffer allocator's maximum size in bytes. Tune this value to a lower value to reduce overhead of the entries cached in the Broker cache due to shared underlying buffers. Default: `1048576`.
--   **Modified Behavior of Existing Configurations**:
-    -   `managedLedgerCacheEvictionTimeThresholdMillis`: Applies only to entries which have reached the expected read count when `cacheEvictionByExpectedReadCount=true`.
-    -   `managedLedgerCacheSizeMB`: Still applies. The new size-based eviction will use the `RangeCacheRemovalQueue`.
-    -   `cacheEvictionByMarkDeletedPosition`: If `cacheEvictionByExpectedReadCount` is `true`, this setting's direct influence on preserving entries is diminished, as `expectedReadCount` provides a more granular control. However, mark-delete position updates still occur and will lead to `expectedReadCount` decrementing as cursors move past entries. If `cacheEvictionByExpectedReadCount` is `false`, this setting functions as before (though the underlying eviction for "up to slowest reader" [...]
-    -   `managedLedgerMinimumBacklogCursorsForCaching`, `managedLedgerMinimumBacklogEntriesForCaching`, `managedLedgerMaxBacklogBetweenCursorsForCaching`: These primarily affect whether a cursor is considered "active" for caching purposes (i.e., `ManagedCursorImpl.isCacheReadEntry()`). The `expectedReadCount` logic naturally incorporates this by only counting active cursors. If `cacheEvictionByExpectedReadCount` is true, these settings become less critical for direct cache retention deci [...]
+- **New Configuration**: `broker.conf`
+  - `cacheEvictionByExpectedReadCount` (boolean): Enables the new eviction strategy based on expected read count. When true, entries with `expectedReadCount > 0` are less likely to be evicted by size-based eviction unless they also meet timestamp expiration.
+    Default: `true`.
+  - `managedLedgerCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes` (int): Maximum number of times the cache can extend the TTL of an entry that has remaining expected reads. Only takes effect when `cacheEvictionByExpectedReadCount` is enabled. This helps optimize cache efficiency for scenarios like:
+    - Key_Shared subscription replays
+    - Catch-up reads for lagging consumers
+    - Consumers temporarily falling behind the tail
+      
+      Entries with remaining expected reads will have their TTL extended up to this many times before being eligible for eviction. The TTL will be extended by `managedLedgerCacheEvictionTimeThresholdMillis` each time.
+      Default: `5`.
+  - `managedLedgerCacheEvictionExtendTTLOfRecentlyAccessed` (boolean): Controls whether recently accessed entries in the managed ledger cache should have their lifetime extended before cache eviction.
+    - When enabled:
+      - During eviction check, if an entry has been accessed since the last check, its expiration time will be extended by `managedLedgerCacheEvictionTimeThresholdMillis`
+      - Makes the cache behave like a Least Recently Used (LRU) cache by keeping frequently accessed entries longer
+      - Helps optimize performance for frequently accessed entries while still allowing old unused entries to be evicted
+      - Minimum eviction time is 2x `managedLedgerCacheEvictionTimeThresholdMillis`
+    - When disabled:
+      - Cache behaves more like a FIFO queue with time-based and size-based eviction
+      - Minimum eviction time is `managedLedgerCacheEvictionTimeThresholdMillis`
+    - Default: `true`.
+  - `managedLedgerContinueCachingAddedEntriesAfterLastActiveCursorLeavesMillis` (long):
+    - This setting configures the duration of continuing to cache added entries while there are no active cursors, 
+      when the last active cursor has left or immediately after initialization when the persistent topic and the
+      managedledger gets loaded. This setting is ignored unless `cacheEvictionByExpectedReadCount` is enabled.
+    - Default: The default value is 2 * `managedLedgerCacheEvictionTimeThresholdMillis`.
+
+- **Modified Behavior of Existing Configurations**:
+  - `managedLedgerCacheEvictionTimeThresholdMillis` (long): Controls time-to-live (TTL) for entries in the managed ledger (broker) cache. The TTL can be extended in two ways:
+    - When `cacheEvictionByExpectedReadCount` is enabled: TTL is extended for entries with remaining expected reads. The maximum number of extensions is controlled by `managedLedgerCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes`.
+    - When `cacheEvictionExtendTTLOfRecentlyAccessed` is enabled: TTL is extended for entries accessed since the last expiration check.
+    - Default: `1000`.
+  - `managedLedgerCacheSizeMB`: Still applies. The new size-based eviction will use the `RangeCacheRemovalQueue`.
+  - `cacheEvictionByMarkDeletedPosition`: If `cacheEvictionByExpectedReadCount` is `true`, this setting's direct influence on preserving entries is diminished, as `expectedReadCount` provides more granular control. However, mark-delete position updates still occur. If `cacheEvictionByExpectedReadCount` is `false`, this setting functions as before (though the underlying eviction for "up to slowest reader" now also uses the central queue).
+  - `managedLedgerCursorBackloggedThreshold`, `managedLedgerMinimumBacklogCursorsForCaching`, `managedLedgerMinimumBacklogEntriesForCaching`, `managedLedgerMaxBacklogBetweenCursorsForCaching`: These settings primarily determine whether a cursor is considered "active" for caching purposes (i.e., `ManagedCursorImpl.isCacheReadEntry()`).
+      - If `cacheEvictionByExpectedReadCount` is true, these settings are ignored since they would conflict with the expected read count implementation and would not benefit the caching algorithm.
+      - The current implementation using `managedLedgerCursorBackloggedThreshold` and related configuration parameters relies on periodic checks of `checkBackloggedCursors` and `checkCursorsToCacheEntries`, which occur once per minute during statistics updates.
+          - This approach is insufficient for handling scenarios where a large number of consumers with backlogged subscriptions connect simultaneously. The calculation logic is also costly to perform for a large number of topics and cursors.
+          - The `cacheEvictionByExpectedReadCount` solution eliminates the need for these settings since the efficient eviction algorithm can adapt to cases where far-behind backlogged cursors unnecessarily cause entries to be cached longer than necessary. The size-based eviction will efficiently remove entries in a prioritized manner, ensuring that entries with a positive expected read count are retained longer, while entries with no expected reads are evicted more aggressively.
 
 ### Metrics & Monitoring
 
@@ -316,20 +273,20 @@ Existing broker cache metrics will continue to function, reflecting the behavior
 
 There are no compatibility concerns since the broker cache is handled at runtime, in a single broker.
 
-## Pulsar Geo-Replication Upgrade & Downgrade/Rollback Considerations
--   This PIP does not directly interact with or change geo-replication mechanisms. Cache behavior is local to each broker in each cluster. Compatibility considerations are the same as for a standalone broker.
+### Pulsar Geo-Replication Upgrade & Downgrade/Rollback Considerations
+- This PIP does not directly interact with or change geo-replication mechanisms. Cache behavior is local to each broker in each cluster. Compatibility considerations are the same as for a standalone broker.
 
-# Alternatives
+## Alternatives
 
-1.  **Simple LRU (Least Recently Used) for Global Cache**:
-    -   A global LRU cache could be considered.
-    -   **Reason for rejection**: Standard LRU doesn't naturally account for Pulsar's specific read patterns where an entry might be predictably read multiple times (e.g., by different consumers in a shared subscription after a backlog is cleared, or by Key_Shared consumers). The "expected read count" provides a more domain-specific heuristic. Moreover, managing a global LRU across all topics with high concurrency and varying entry sizes can be complex and may require significant locking [...]
+1. **Simple LRU (Least Recently Used) for Global Cache**:
+   - A global LRU cache could be considered.
+   - **Reason for rejection**: Standard LRU doesn't naturally account for Pulsar's specific read patterns where an entry might be predictably read multiple times (e.g., by different consumers in a shared subscription after a backlog is cleared, or by Key_Shared consumers). The "expected read count" provides a more domain-specific heuristic. Moreover, managing a global LRU across all topics with high concurrency and varying entry sizes can be complex and may require significant locking or [...]
 
-2.  **Priority Queue for `RangeCacheRemovalQueue`**:
-    -   Instead of a simple FIFO queue, a priority queue ordered by `expectedReadCount` (ascending) and then timestamp (ascending) could be used.
-    -   **Reason for rejection (for this PIP)**: While potentially more precise, managing priorities in a queue where `expectedReadCount` can change dynamically for entries already in the queue adds significant complexity. Updates would require O(log N) or O(N) operations depending on the queue implementation and how updates are handled. The current MPSC queue with a "stash" for non-evictable items offers a simpler, lower-overhead approach for the initial refactoring. Performance of MPSC [...]
+2. **Priority Queue for `RangeCacheRemovalQueue`**:
+   - Instead of a simple FIFO queue, a priority queue ordered by `expectedReadCount` (ascending) and then timestamp (ascending) could be used.
+   - **Reason for rejection (for this PIP)**: While potentially more precise, managing priorities in a queue where `expectedReadCount` can change dynamically for entries already in the queue adds significant complexity. Updates would require O(log N) or O(N) operations depending on the queue implementation and how updates are handled. The current MPSC queue with a "stash" for non-evictable items offers a simpler, lower-overhead approach for the initial refactoring. Performance of MPSC qu [...]
 
-# Links
+## Links
 
 * Mailing List discussion thread: https://lists.apache.org/thread/o1ozbg468kxfd38pxk2ppzsstdnxnok2
-* Mailing List voting thread: https://lists.apache.org/thread/p6jroxhtjpk7hbgqt3hzjtjd2233zmxy
+* Mailing List voting thread: https://lists.apache.org/thread/p6jroxhtjpk7hbgqt3hzjtjd2233zmxy
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 14817a25baf..79d334efb38 100644
--- a/pom.xml
+++ b/pom.xml
@@ -118,7 +118,11 @@ flexible messaging model and an intuitive client API.</description>
       -Xshare:off <!-- suppress sharing warning -->
       ${test.additional.args.jdk24}
       ${test.netty.args}
+      ${test.asyncprofiler.args}
+      ${test.jvm.args}
     </test.additional.args>
+    <!-- arguments to pass to test JVMs -->
+    <test.jvm.args></test.jvm.args>
     <test.additional.args.jdk24></test.additional.args.jdk24>
     <!-- Netty related JVM args, including Netty leak detection enabled in tests -->
     <test.netty.args>
@@ -148,6 +152,7 @@ flexible messaging model and an intuitive client API.</description>
       -Dio.netty.leakDetection.acquireAndReleaseOnly=true
       -Dio.netty.leakDetection.samplingInterval=32
     </test.netty.leakdetection.args>
+    <test.asyncprofiler.args></test.asyncprofiler.args>
     <testExitJvmOnLeak>false</testExitJvmOnLeak>
     <testExitJvmOnLeakDelayMillis>1000</testExitJvmOnLeakDelayMillis>
     <testLeakDetectionLevel>paranoid</testLeakDetectionLevel>
@@ -204,6 +209,7 @@ flexible messaging model and an intuitive client API.</description>
     <bouncycastle.bc-fips.version>1.0.2.6</bouncycastle.bc-fips.version>
     <jackson.version>2.17.2</jackson.version>
     <fastutil.version>8.5.16</fastutil.version>
+    <jctools.version>4.0.5</jctools.version>
     <reflections.version>0.10.2</reflections.version>
     <swagger.version>1.6.2</swagger.version>
     <puppycrawl.checkstyle.version>10.14.2</puppycrawl.checkstyle.version>
@@ -970,6 +976,12 @@ flexible messaging model and an intuitive client API.</description>
         <version>${fastutil.version}</version>
       </dependency>
 
+      <dependency>
+        <groupId>org.jctools</groupId>
+        <artifactId>jctools-core</artifactId>
+        <version>${jctools.version}</version>
+      </dependency>
+
       <dependency>
         <groupId>org.codehaus.jettison</groupId>
         <artifactId>jettison</artifactId>
@@ -2016,6 +2028,8 @@ flexible messaging model and an intuitive client API.</description>
                 <exclude>**/zk-3.5-test-data/*</exclude>
                 <exclude>**/*_pb2.py</exclude>
                 <exclude>**/*_pb2_grpc.py</exclude>
+                <exclude>jmh-result-*</exclude>
+                <exclude>profile-results/**</exclude>
                 <!--
                 Exclude any files from license checks that are in hidden directories (name starting with ".")
                 at the top level. This helps exclude directories used by AI tools
@@ -3039,6 +3053,56 @@ flexible messaging model and an intuitive client API.</description>
        <test.netty.leakdetection.args></test.netty.leakdetection.args>
       </properties>
     </profile>
+
+    <!-- This profile is used to run tests with Async Profiler profiling.
+         Download Async Profiler from https://github.com/async-profiler/async-profiler/releases and
+         after installing set LIBASYNCPROFILER_PATH environment variable to the full path of the
+         libasyncProfiler.so (Linux) or libasyncProfiler.dylib (MacOS) file.
+         The profiler will generate a JFR file in the target directory with the name test_profile_<timestamp>_<forkNumber>.jfr
+         Async Profiler's jfrconv tool can be used to convert the JFR file to different type of flamegraphs.
+         For example, to convert the JFR file to a CPU flamegraph, run the following command:
+         jfrconv \-\-cpu target/test_profile_<timestamp>_<forkNumber>_<pid>.jfr
+         the result will be in a .html file in the target directory.
+         You can also use Eclipse Mission Control (https://adoptium.net/jmc) to open the JFR file and analyze it.
+         example usage:
+         ENABLE_MANUAL_TEST=true mvn -PtestAsyncProfiler test -pl pulsar-broker -Dtest=BrokerEntryCacheRollingRestartTest
+         On Linux, it's recommended to pass -Dtest.asyncprofiler.event=cpu to use the most optimal method for method profiling:
+         ENABLE_MANUAL_TEST=true mvn -PtestAsyncProfiler -Dtest.asyncprofiler.event=cpu test -pl pulsar-broker -Dtest=BrokerEntryCacheRollingRestartTest
+    -->
+    <profile>
+      <id>testAsyncProfiler</id>
+      <properties>
+        <!-- On Linux, pass -Dtest.asyncprofiler.event=cpu to use the most optimal method for method profiling.
+        See https://github.com/async-profiler/async-profiler/blob/master/docs/CpuSamplingEngines.md for more details.
+        -->
+        <test.asyncprofiler.event>itimer</test.asyncprofiler.event>
+        <!-- Assumes Async Profiler 4.1+ to be installed, which supports "all" option -->
+        <test.asyncprofiler.opts>event=${test.asyncprofiler.event},all,jfrsync=profile</test.asyncprofiler.opts>
+        <test.asyncprofiler.args>
+          -XX:+UnlockDiagnosticVMOptions
+          -XX:+DebugNonSafepoints
+          -Xms${testMaxHeapSize} -XX:+AlwaysPreTouch
+          -agentpath:${env.LIBASYNCPROFILER_PATH}=start,${test.asyncprofiler.opts},jfr,file=${pulsar.basedir}/target/test_profile_${maven.build.timestamp}_${surefire.forkNumber}_%p.jfr
+          -Dpulsar.test.logging.appender=FILE
+          -Dpulsar.test.logging.file=target/test_${maven.build.timestamp}_${surefire.forkNumber}.log
+        </test.asyncprofiler.args>
+        <testForkCount>1</testForkCount>
+        <testRetryCount>0</testRetryCount>
+        <redirectTestOutputToFile>false</redirectTestOutputToFile>
+        <test.netty.leakdetection.args></test.netty.leakdetection.args>
+      </properties>
+    </profile>
+    <!-- profile that redirects log4j logs to a file -->
+    <profile>
+      <id>testLoggingToFile</id>
+      <properties>
+        <test.jvm.args>
+        -Dpulsar.test.logging.appender=FILE
+        -Dpulsar.test.logging.file=target/test_${maven.build.timestamp}_${surefire.forkNumber}.log
+        </test.jvm.args>
+        <redirectTestOutputToFile>false</redirectTestOutputToFile>
+      </properties>
+    </profile>
   </profiles>
 
   <repositories>
diff --git a/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/ServiceConfiguration.java b/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/ServiceConfiguration.java
index a1c7134eadf..fd8dce934b9 100644
--- a/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/ServiceConfiguration.java
+++ b/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/ServiceConfiguration.java
@@ -2148,13 +2148,54 @@ public class ServiceConfiguration implements PulsarConfiguration {
             doc = "Configure the cache eviction interval in milliseconds for the managed ledger cache, default is 10ms")
     private long managedLedgerCacheEvictionIntervalMs = 10;
 
-    @FieldContext(category = CATEGORY_STORAGE_ML,
-            dynamic = true,
-            doc = "All entries that have stayed in cache for more than the configured time, will be evicted")
+    @FieldContext(category = CATEGORY_STORAGE_ML, dynamic = true, doc =
+            "Controls time-to-live (TTL) for entries in the managed ledger (broker) cache.\n"
+                    + "The TTL can be extended in two ways:\n"
+                    + "1. When cacheEvictionByExpectedReadCount is enabled: TTL is extended for entries with "
+                    + "remaining\n"
+                    + "expected reads. The maximum number of extensions is controlled by\n"
+                    + "managedLedgerCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes.\n"
+                    + "2. When cacheEvictionExtendTTLOfRecentlyAccessed is enabled: TTL is extended for entries\n"
+                    + "accessed since the last expiration check.\n"
+                    + "Default value is 1000ms.")
     private long managedLedgerCacheEvictionTimeThresholdMillis = 1000;
+
+    @FieldContext(category = CATEGORY_STORAGE_ML, dynamic = true, doc = "Maximum number of times the cache can extend\n"
+            + "the TTL of an entry that has remaining expected reads.\n"
+            + "Only takes effect when cacheEvictionByExpectedReadCount is enabled.\n"
+            + "This helps optimize cache efficiency for scenarios like:\n"
+            + "- Key_Shared subscription replays\n"
+            + "- Catch-up reads for lagging consumers\n"
+            + "- Consumers temporarily falling behind the tail\n"
+            + "Entries with remaining expected reads will have their TTL extended up to this many times\n"
+            + "before being eligible for eviction. The TTL will be extended by\n"
+            + "managedLedgerCacheEvictionTimeThresholdMillis each time.\n"
+            + "Default is 5.")
+    private int managedLedgerCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes = 5;
+
+    @FieldContext(category = CATEGORY_STORAGE_ML, dynamic = true,
+            doc = "Controls whether recently accessed entries in the managed ledger cache should have their "
+                    + "lifetime extended before cache eviction.\n"
+                    + "When enabled:\n"
+                    + " - During eviction check, if an entry has been accessed since the last check, its expiration"
+                    + " time will be extended by managedLedgerCacheEvictionTimeThresholdMillis\n"
+                    + " - Makes the cache behave like a Least Recently Used (LRU) cache by keeping frequently "
+                    + "accessed entries longer\n"
+                    + " - Helps optimize performance for frequently accessed entries while still allowing old unused "
+                    + "entries to be evicted\n"
+                    + " - Minimum eviction time is 2x managedLedgerCacheEvictionTimeThresholdMillis\n"
+                    + "When disabled:\n"
+                    + " - Cache behaves more like a FIFO queue with time-based and size-based eviction\n"
+                    + " - Minimum eviction time is managedLedgerCacheEvictionTimeThresholdMillis\n"
+                    + "Default is true, to behave like a LRU cache."
+    )
+    private boolean managedLedgerCacheEvictionExtendTTLOfRecentlyAccessed = true;
+
     @FieldContext(category = CATEGORY_STORAGE_ML,
             doc = "Configure the threshold (in number of entries) from where a cursor should be considered 'backlogged'"
-                    + " and thus should be set as inactive.")
+                    + " and thus should be set as inactive.\n"
+                    + "Set to -1 to disable this behavior.\n"
+                    + "This setting has no effect when cacheEvictionByExpectedReadCount is enabled.")
     private long managedLedgerCursorBackloggedThreshold = 1000;
     @FieldContext(
         category = CATEGORY_STORAGE_ML,
@@ -2429,20 +2470,23 @@ public class ServiceConfiguration implements PulsarConfiguration {
             dynamic = true,
             category = CATEGORY_STORAGE_ML,
             doc = "Minimum cursors that must be in backlog state to cache and reuse the read entries."
-                    + "(Default =0 to disable backlog reach cache)"
+                    + "(Default =0 to disable backlog reach cache)\n"
+                    + "This has no effect when cacheEvictionByExpectedReadCount is enabled."
     )
     private int managedLedgerMinimumBacklogCursorsForCaching = 0;
 
     @FieldContext(
             dynamic = true,
             category = CATEGORY_STORAGE_ML,
-            doc = "Minimum backlog entries for any cursor before start caching reads"
+            doc = "Minimum backlog entries for any cursor before start caching reads\n"
+                    + "This has no effect when cacheEvictionByExpectedReadCount is enabled."
     )
     private int managedLedgerMinimumBacklogEntriesForCaching = 1000;
     @FieldContext(
             dynamic = true,
             category = CATEGORY_STORAGE_ML,
-            doc = "Maximum backlog entry difference to prevent caching entries that can't be reused"
+            doc = "Maximum backlog entry difference to prevent caching entries that can't be reused\n"
+                    + "This has no effect when cacheEvictionByExpectedReadCount is enabled."
     )
     private int managedLedgerMaxBacklogBetweenCursorsForCaching = 1000;
 
@@ -3466,11 +3510,29 @@ public class ServiceConfiguration implements PulsarConfiguration {
 
     @FieldContext(
             category = CATEGORY_STORAGE_ML,
-            doc = "Evicting cache data by the slowest markDeletedPosition or readPosition. "
-                    + "The default is to evict through readPosition."
+            doc = "Evicting cache data by the slowest markDeletedPosition (true) or slowest read position (false)."
+                    + "This setting is ignored when cacheEvictionByExpectedReadCount is true."
     )
     private boolean cacheEvictionByMarkDeletedPosition = false;
 
+    @FieldContext(
+            category = CATEGORY_STORAGE_ML,
+            doc = "Evicting cache data by expected read count. Expected read count is calculated by the number of "
+                    + "active cursors with a read position that is behind the position of the cached entry. "
+                    + "This setting will override the cacheEvictionByMarkDeletedPosition setting."
+    )
+    private boolean cacheEvictionByExpectedReadCount = true;
+
+    @FieldContext(
+            category = CATEGORY_STORAGE_ML,
+            doc = "This setting configures the duration of continuing to cache added entries while there are no "
+                    + "active cursors, when the last active cursor has left or immediately after initialization when "
+                    + "the persistent topic and the managed ledger gets loaded. "
+                    + "This setting is ignored unless cacheEvictionByExpectedReadCount is enabled."
+                    + "The default value is 2 * managedLedgerCacheEvictionTimeThresholdMillis."
+    )
+    private Long managedLedgerContinueCachingAddedEntriesAfterLastActiveCursorLeavesMillis;
+
     /**** --- Transaction config variables. --- ****/
     @FieldContext(
             category = CATEGORY_TRANSACTION,
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/ManagedLedgerClientFactory.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/ManagedLedgerClientFactory.java
index e1561bda303..b199db88304 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/ManagedLedgerClientFactory.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/ManagedLedgerClientFactory.java
@@ -77,6 +77,17 @@ public class ManagedLedgerClientFactory implements ManagedLedgerStorage {
         managedLedgerFactoryConfig.setCacheEvictionIntervalMs(conf.getManagedLedgerCacheEvictionIntervalMs());
         managedLedgerFactoryConfig.setCacheEvictionTimeThresholdMillis(
                 conf.getManagedLedgerCacheEvictionTimeThresholdMillis());
+        Long continueCachingAddedEntriesAfterLastActiveCursorLeavesMillis =
+                conf.getManagedLedgerContinueCachingAddedEntriesAfterLastActiveCursorLeavesMillis();
+        if (continueCachingAddedEntriesAfterLastActiveCursorLeavesMillis != null) {
+            managedLedgerFactoryConfig.setContinueCachingAddedEntriesAfterLastActiveCursorLeavesMillis(
+                    continueCachingAddedEntriesAfterLastActiveCursorLeavesMillis);
+        } else {
+            // default to 2 * managedLedgerCacheEvictionTimeThresholdMillis if the value is unset
+            managedLedgerFactoryConfig.setContinueCachingAddedEntriesAfterLastActiveCursorLeavesMillis(
+                    2 * conf.getManagedLedgerCacheEvictionTimeThresholdMillis()
+            );
+        }
         managedLedgerFactoryConfig.setCopyEntriesInCache(conf.isManagedLedgerCacheCopyEntries());
         long managedLedgerMaxReadsInFlightSizeBytes = conf.getManagedLedgerMaxReadsInFlightSizeInMB() * 1024L * 1024L;
         if (managedLedgerMaxReadsInFlightSizeBytes > 0 && conf.getDispatcherMaxReadSizeBytes() > 0
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java
index 1f2e02da77e..6976b4f2fce 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java
@@ -2107,9 +2107,14 @@ public class BrokerService implements Closeable {
             managedLedgerConfig.setInactiveOffloadedLedgerEvictionTime(
                     serviceConfig.getManagedLedgerInactiveOffloadedLedgerEvictionTimeSeconds(),
                     TimeUnit.SECONDS);
-
-            managedLedgerConfig.setCacheEvictionByMarkDeletedPosition(
-                    serviceConfig.isCacheEvictionByMarkDeletedPosition());
+            if (serviceConfig.isCacheEvictionByExpectedReadCount()) {
+                managedLedgerConfig.setCacheEvictionByMarkDeletedPosition(false);
+                managedLedgerConfig.setCacheEvictionByExpectedReadCount(true);
+            } else {
+                managedLedgerConfig.setCacheEvictionByMarkDeletedPosition(
+                        serviceConfig.isCacheEvictionByMarkDeletedPosition());
+                managedLedgerConfig.setCacheEvictionByExpectedReadCount(false);
+            }
             managedLedgerConfig.setMinimumBacklogCursorsForCaching(
                     serviceConfig.getManagedLedgerMinimumBacklogCursorsForCaching());
             managedLedgerConfig.setMinimumBacklogEntriesForCaching(
@@ -2875,7 +2880,20 @@ public class BrokerService implements Closeable {
                     defaultManagedLedgerFactory.updateCacheEvictionTimeThreshold(MILLISECONDS
                             .toNanos((long) cacheEvictionTimeThresholdMills));
                 });
-
+        //  add listener to notify broker managedLedgerCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes
+        //  dynamic config
+        registerConfigurationListener("managedLedgerCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes",
+                (managedLedgerCacheEvictionMaxRequeueCount) -> {
+                    defaultManagedLedgerFactory.updateCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes(
+                            (int) managedLedgerCacheEvictionMaxRequeueCount);
+                });
+        //  add listener to notify broker managedLedgerCacheEvictionExtendTTLOfRecentlyAccessed
+        //  dynamic config
+        registerConfigurationListener("managedLedgerCacheEvictionExtendTTLOfRecentlyAccessed",
+                (extendTTLOfRecentlyAccessed) -> {
+                    defaultManagedLedgerFactory.updateCacheEvictionExtendTTLOfRecentlyAccessed(
+                            (boolean) extendTTLOfRecentlyAccessed);
+                });
 
         // add listener to update message-dispatch-rate in msg for topic
         registerConfigurationListener("dispatchThrottlingRatePerTopicInMsg", (dispatchRatePerTopicInMsg) -> {
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/EntryAndMetadata.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/EntryAndMetadata.java
index cf1802e3853..33abddc300b 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/EntryAndMetadata.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/EntryAndMetadata.java
@@ -23,6 +23,7 @@ import io.netty.buffer.ByteBuf;
 import java.util.function.ToIntFunction;
 import lombok.Getter;
 import org.apache.bookkeeper.mledger.Entry;
+import org.apache.bookkeeper.mledger.EntryReadCountHandler;
 import org.apache.bookkeeper.mledger.Position;
 import org.apache.pulsar.common.api.proto.MessageMetadata;
 import org.apache.pulsar.common.protocol.Commands;
@@ -136,4 +137,9 @@ public class EntryAndMetadata implements Entry {
     public Entry unwrap() {
         return entry;
     }
+
+    @Override
+    public EntryReadCountHandler getReadCountHandler() {
+        return entry.getReadCountHandler();
+    }
 }
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentStickyKeyDispatcherMultipleConsumers.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentStickyKeyDispatcherMultipleConsumers.java
index 9e92a2ab40d..080f5acbf16 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentStickyKeyDispatcherMultipleConsumers.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentStickyKeyDispatcherMultipleConsumers.java
@@ -466,6 +466,10 @@ public class PersistentStickyKeyDispatcherMultipleConsumers extends PersistentDi
                     // the entry is blocked by hash, add the consumer to the blocked set
                     blockedByHashConsumers.add(consumer);
                 }
+                if (entry.getReadCountHandler() != null) {
+                    // increment the expected read count for the entry, so that it can be cached for a longer time
+                    entry.getReadCountHandler().incrementExpectedReadCount();
+                }
                 // add the message to replay
                 addMessageToReplay(entry.getLedgerId(), entry.getEntryId(), stickyKeyHash);
                 // release the entry as it will not be dispatched
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentSubscription.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentSubscription.java
index 5f3f9f3aba4..ac54957f0c6 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentSubscription.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentSubscription.java
@@ -244,6 +244,16 @@ public class PersistentSubscription extends AbstractSubscription {
         return pendingAckHandle.pendingAckHandleFuture().thenCompose(future -> {
             synchronized (PersistentSubscription.this) {
                 cursor.updateLastActive();
+                if (!cursor.isActive()
+                        && (topic.getManagedLedger().getConfig().isCacheEvictionByExpectedReadCount()
+                        // set the cursor active when it connects, if the backlog size is less than the threshold.
+                        // this is configured with the managedLedgerCursorBackloggedThreshold setting
+                        || topic.getBackloggedCursorThresholdEntries() < 0
+                        || cursor.getNumberOfEntries() <= topic.getBackloggedCursorThresholdEntries())) {
+                    // If the cursor is not active, we need to set it active
+                    cursor.setActive();
+                }
+
                 if (IS_FENCED_UPDATER.get(this) == TRUE) {
                     log.warn("Attempting to add consumer {} on a fenced subscription", consumer);
                     return FutureUtil.failedFuture(new SubscriptionFencedException("Subscription is fenced"));
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentTopic.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentTopic.java
index 0e40894cbd1..7ed459daafb 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentTopic.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentTopic.java
@@ -239,6 +239,7 @@ public class PersistentTopic extends AbstractTopic implements Topic, AddEntryCal
     private Optional<DispatchRateLimiter> dispatchRateLimiter = Optional.empty();
     private final Object dispatchRateLimiterLock = new Object();
     private Optional<SubscribeRateLimiter> subscribeRateLimiter = Optional.empty();
+    @Getter
     private final long backloggedCursorThresholdEntries;
     public static final int MESSAGE_RATE_BACKOFF_MS = 1000;
 
@@ -3510,9 +3511,14 @@ public class PersistentTopic extends AbstractTopic implements Topic, AddEntryCal
     }
 
     private void checkBackloggedCursor(PersistentSubscription subscription) {
+        // ignore cursor inactivating a cursor based on backlog size when eviction is based on expected read count
+        if (getManagedLedger().getConfig().isCacheEvictionByExpectedReadCount()) {
+            return;
+        }
         // activate caught up cursor which include consumers
         if (!subscription.getConsumers().isEmpty()
-                && subscription.getCursor().getNumberOfEntries() < backloggedCursorThresholdEntries) {
+                && (backloggedCursorThresholdEntries < 0
+                || subscription.getCursor().getNumberOfEntries() <= backloggedCursorThresholdEntries)) {
             subscription.getCursor().setActive();
         } else {
             subscription.getCursor().setInactive();
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/buffer/impl/TransactionEntryImpl.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/buffer/impl/TransactionEntryImpl.java
index ab18373d859..51b4b37081e 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/buffer/impl/TransactionEntryImpl.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/buffer/impl/TransactionEntryImpl.java
@@ -20,6 +20,7 @@ package org.apache.pulsar.broker.transaction.buffer.impl;
 
 import io.netty.buffer.ByteBuf;
 import org.apache.bookkeeper.mledger.Entry;
+import org.apache.bookkeeper.mledger.EntryReadCountHandler;
 import org.apache.bookkeeper.mledger.Position;
 import org.apache.pulsar.broker.transaction.buffer.TransactionEntry;
 import org.apache.pulsar.client.api.transaction.TxnID;
@@ -136,4 +137,9 @@ public class TransactionEntryImpl implements TransactionEntry {
     public boolean release() {
         return this.entry.release();
     }
+
+    @Override
+    public EntryReadCountHandler getReadCountHandler() {
+        return entry.getReadCountHandler();
+    }
 }
diff --git a/pulsar-broker/src/test/java/org/apache/bookkeeper/mledger/impl/cache/RangeCacheTestUtil.java b/pulsar-broker/src/test/java/org/apache/bookkeeper/mledger/impl/cache/RangeCacheTestUtil.java
new file mode 100644
index 00000000000..efd7f7164a1
--- /dev/null
+++ b/pulsar-broker/src/test/java/org/apache/bookkeeper/mledger/impl/cache/RangeCacheTestUtil.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.bookkeeper.mledger.impl.cache;
+
+import java.util.function.Consumer;
+import org.apache.bookkeeper.mledger.ReferenceCountedEntry;
+import org.apache.pulsar.broker.PulsarService;
+
+public class RangeCacheTestUtil {
+    /**
+     * Iterates over all cached entries in the RangeEntryCacheManager and applies the provided consumer to each entry.
+     *
+     * @param pulsarService the PulsarService instance to access the entry cache manager
+     * @param consumer      the consumer to apply to each cached entry
+     */
+    public static void forEachCachedEntry(PulsarService pulsarService,
+                                          Consumer<ReferenceCountedEntry> consumer) {
+        RangeEntryCacheManagerImpl entryCacheManagerImpl =
+                (RangeEntryCacheManagerImpl) pulsarService.getDefaultManagedLedgerFactory().getEntryCacheManager();
+        entryCacheManagerImpl.forEachEntry(entry -> consumer.accept(entry.value));
+    }
+}
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/MultiBrokerBaseTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/MultiBrokerBaseTest.java
index 2a3e705dca1..a218310cc37 100644
--- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/MultiBrokerBaseTest.java
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/MultiBrokerBaseTest.java
@@ -18,17 +18,23 @@
  */
 package org.apache.pulsar.broker;
 
+import java.time.Duration;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.Optional;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.bookkeeper.client.PulsarMockBookKeeper;
+import org.apache.bookkeeper.common.util.OrderedExecutor;
 import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest;
+import org.apache.pulsar.broker.testcontext.NonClosableMockBookKeeper;
 import org.apache.pulsar.broker.testcontext.PulsarTestContext;
 import org.apache.pulsar.client.admin.PulsarAdmin;
 import org.apache.pulsar.client.admin.PulsarAdminBuilder;
 import org.apache.pulsar.client.admin.PulsarAdminException;
 import org.apache.pulsar.client.api.PulsarClient;
 import org.apache.pulsar.client.api.PulsarClientException;
+import org.apache.pulsar.common.util.GracefulExecutorServicesShutdown;
 import org.apache.pulsar.common.util.PortManager;
 import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
@@ -39,22 +45,61 @@ public abstract class MultiBrokerBaseTest extends MockedPulsarServiceBaseTest {
     protected List<PulsarService> additionalBrokers;
     protected List<PulsarAdmin> additionalBrokerAdmins;
     protected List<PulsarClient> additionalBrokerClients;
+    protected PulsarMockBookKeeper mockBookKeeper;
+    protected int mainBrokerPort;
+    protected List<Integer> additionalBrokerPorts = new ArrayList<>();
 
     protected int numberOfAdditionalBrokers() {
         return 2;
     }
 
+    protected boolean useDynamicBrokerPorts() {
+        return true;
+    }
+
     @BeforeClass(alwaysRun = true)
     @Override
     public final void setup() throws Exception {
+        beforeSetup();
+        if (!useDynamicBrokerPorts()) {
+            mainBrokerPort = PortManager.nextLockedFreePort();
+        }
+        OrderedExecutor mockBookKeeperExecutor = OrderedExecutor.newBuilder().numThreads(1)
+                .name(MultiBrokerBaseTest.class.getSimpleName() + "-bk-executor").build();
+        registerCloseable(() -> GracefulExecutorServicesShutdown.initiate()
+                .timeout(Duration.ZERO)
+                .shutdown(mockBookKeeperExecutor)
+                .handle().get());
+        mockBookKeeper = new NonClosableMockBookKeeper(mockBookKeeperExecutor);
+        registerCloseable(() -> {
+            ((NonClosableMockBookKeeper) mockBookKeeper).reallyShutdown();
+        });
         super.internalSetup();
         additionalBrokersSetup();
         pulsarResourcesSetup();
         additionalSetup();
     }
 
+    protected void beforeSetup() {
+
+    }
+
+    @Override
+    protected void customizeMainPulsarTestContextBuilder(PulsarTestContext.Builder pulsarTestContextBuilder) {
+        pulsarTestContextBuilder.bookKeeperClient(mockBookKeeper);
+    }
+
+    @Override
+    protected void doInitConf() throws Exception {
+        super.doInitConf();
+        if (!useDynamicBrokerPorts()) {
+            conf.setBrokerServicePort(Optional.of(mainBrokerPort));
+        }
+    }
+
     protected void additionalSetup() throws Exception {
         // override this method to add any additional setup logic
+
     }
 
     protected void pulsarResourcesSetup() throws PulsarAdminException {
@@ -69,6 +114,13 @@ public abstract class MultiBrokerBaseTest extends MockedPulsarServiceBaseTest {
         additionalBrokerAdmins = new ArrayList<>(numberOfAdditionalBrokers);
         additionalBrokerClients = new ArrayList<>(numberOfAdditionalBrokers);
         additionalPulsarTestContexts = new ArrayList<>(numberOfAdditionalBrokers);
+        additionalBrokerPorts = new ArrayList<>(numberOfAdditionalBrokers);
+        if (!useDynamicBrokerPorts()) {
+            for (int i = 0; i < numberOfAdditionalBrokers; i++) {
+                int port = PortManager.nextLockedFreePort();
+                additionalBrokerPorts.add(port);
+            }
+        }
         for (int i = 0; i < numberOfAdditionalBrokers; i++) {
             PulsarTestContext pulsarTestContext = createAdditionalBroker(i);
             additionalPulsarTestContexts.add(i, pulsarTestContext);
@@ -84,12 +136,33 @@ public abstract class MultiBrokerBaseTest extends MockedPulsarServiceBaseTest {
         }
     }
 
+    protected void restartAdditionalBroker(int i) throws Exception {
+        additionalBrokerAdmins.get(i).close();
+        additionalBrokerClients.get(i).close();
+        additionalPulsarTestContexts.get(i).close();
+        PulsarTestContext pulsarTestContext = createAdditionalBroker(i);
+        additionalPulsarTestContexts.set(i, pulsarTestContext);
+        PulsarService pulsarService = pulsarTestContext.getPulsarService();
+        additionalBrokers.set(i, pulsarService);
+        PulsarAdminBuilder pulsarAdminBuilder =
+                PulsarAdmin.builder().serviceHttpUrl(pulsarService.getWebServiceAddress() != null
+                        ? pulsarService.getWebServiceAddress()
+                        : pulsarService.getWebServiceAddressTls());
+        customizeNewPulsarAdminBuilder(pulsarAdminBuilder);
+        additionalBrokerAdmins.set(i, pulsarAdminBuilder.build());
+        additionalBrokerClients.set(i, newPulsarClient(pulsarService.getBrokerServiceUrl(), 0));
+    }
+
     protected ServiceConfiguration createConfForAdditionalBroker(int additionalBrokerIndex) {
         return getDefaultConf();
     }
 
     protected PulsarTestContext createAdditionalBroker(int additionalBrokerIndex) throws Exception {
-        return createAdditionalPulsarTestContext(createConfForAdditionalBroker(additionalBrokerIndex));
+        ServiceConfiguration conf = createConfForAdditionalBroker(additionalBrokerIndex);
+        if (!useDynamicBrokerPorts()) {
+            conf.setBrokerServicePort(Optional.of(additionalBrokerPorts.get(additionalBrokerIndex)));
+        }
+        return createAdditionalPulsarTestContext(conf);
     }
 
     @AfterClass(alwaysRun = true)
@@ -102,6 +175,14 @@ public abstract class MultiBrokerBaseTest extends MockedPulsarServiceBaseTest {
             log.warn("Exception during additional cleanup", e);
         }
         super.internalCleanup();
+        if (!useDynamicBrokerPorts()) {
+            if (mainBrokerPort > 0) {
+                PortManager.releaseLockedPort(mainBrokerPort);
+            }
+            for (Integer port : additionalBrokerPorts) {
+                PortManager.releaseLockedPort(port);
+            }
+        }
     }
 
     protected void additionalCleanup() throws Exception {
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/MultiBrokerTestZKBaseTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/MultiBrokerTestZKBaseTest.java
index 06c28c3b3ce..6a50f66dacb 100644
--- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/MultiBrokerTestZKBaseTest.java
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/MultiBrokerTestZKBaseTest.java
@@ -39,7 +39,9 @@ public abstract class MultiBrokerTestZKBaseTest extends MultiBrokerBaseTest {
     @Override
     protected void doInitConf() throws Exception {
         super.doInitConf();
-        testZKServer = new TestZKServer();
+        if (testZKServer == null) {
+            testZKServer = new TestZKServer();
+        }
     }
 
     @Override
@@ -64,13 +66,18 @@ public abstract class MultiBrokerTestZKBaseTest extends MultiBrokerBaseTest {
     }
 
     @Override
-    protected PulsarTestContext.Builder createPulsarTestContextBuilder(ServiceConfiguration conf) {
-        return super.createPulsarTestContextBuilder(conf)
-                .spyNoneByDefault()
-                .localMetadataStore(createMetadataStore(MetadataStoreConfig.METADATA_STORE))
+    protected void configureMetadataStores(PulsarTestContext.Builder builder) {
+        builder.localMetadataStore(createMetadataStore(MetadataStoreConfig.METADATA_STORE))
                 .configurationMetadataStore(createMetadataStore(MetadataStoreConfig.CONFIGURATION_METADATA_STORE));
     }
 
+    @Override
+    protected PulsarTestContext.Builder createAdditionalPulsarTestContextBuilder(ServiceConfiguration conf) {
+        return createPulsarTestContextBuilder(conf)
+                .reuseSpyConfig(pulsarTestContext)
+                .bookKeeperClient(pulsarTestContext.getBookKeeperClient());
+    }
+
     @NonNull
     protected MetadataStoreExtended createMetadataStore(String metadataStoreName)  {
         try {
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/auth/MockedPulsarServiceBaseTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/auth/MockedPulsarServiceBaseTest.java
index 31a484bb6f2..6fdfee86b9f 100644
--- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/auth/MockedPulsarServiceBaseTest.java
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/auth/MockedPulsarServiceBaseTest.java
@@ -242,16 +242,20 @@ public abstract class MockedPulsarServiceBaseTest extends TestRetrySupport {
     }
 
     protected void doInitConf() throws Exception {
-        this.conf.setBrokerShutdownTimeoutMs(0L);
-        this.conf.setLoadBalancerOverrideBrokerNicSpeedGbps(Optional.of(1.0d));
-        this.conf.setBrokerServicePort(Optional.of(0));
-        this.conf.setAdvertisedAddress("localhost");
-        this.conf.setWebServicePort(Optional.of(0));
-        this.conf.setNumExecutorThreadPoolSize(5);
-        this.conf.setExposeBundlesMetricsInPrometheus(true);
+        configureInitialConfig(conf);
+    }
+
+    protected void configureInitialConfig(ServiceConfiguration conf) {
+        conf.setBrokerShutdownTimeoutMs(0L);
+        conf.setLoadBalancerOverrideBrokerNicSpeedGbps(Optional.of(1.0d));
+        conf.setBrokerServicePort(Optional.of(0));
+        conf.setAdvertisedAddress("localhost");
+        conf.setWebServicePort(Optional.of(0));
+        conf.setNumExecutorThreadPoolSize(5);
+        conf.setExposeBundlesMetricsInPrometheus(true);
         // Disable the dispatcher retry backoff in tests by default
-        this.conf.setDispatcherRetryBackoffInitialTimeInMs(0);
-        this.conf.setDispatcherRetryBackoffMaxTimeInMs(0);
+        conf.setDispatcherRetryBackoffInitialTimeInMs(0);
+        conf.setDispatcherRetryBackoffMaxTimeInMs(0);
     }
 
     protected final void init() throws Exception {
@@ -510,15 +514,30 @@ public abstract class MockedPulsarServiceBaseTest extends TestRetrySupport {
      */
     protected PulsarTestContext createAdditionalPulsarTestContext(ServiceConfiguration conf,
                                               Consumer<PulsarTestContext.Builder> builderCustomizer) throws Exception {
-        var builder = createPulsarTestContextBuilder(conf)
-                .reuseMockBookkeeperAndMetadataStores(pulsarTestContext)
-                .reuseSpyConfig(pulsarTestContext);
+        var builder = createAdditionalPulsarTestContextBuilder(conf);
+        customizeAdditionalPulsarTestContextBuilder(builder);
         if (builderCustomizer != null) {
             builderCustomizer.accept(builder);
         }
         return builder.build();
     }
 
+    /**
+     * Customize the PulsarTestContext.Builder instance used for creating the PulsarTestContext
+     * for an additional PulsarService instance.
+     *
+     * @param pulsarTestContextBuilder the PulsarTestContext.Builder instance to customize
+     */
+    protected void customizeAdditionalPulsarTestContextBuilder(PulsarTestContext.Builder pulsarTestContextBuilder) {
+
+    }
+
+    protected PulsarTestContext.Builder createAdditionalPulsarTestContextBuilder(ServiceConfiguration conf) {
+        return createPulsarTestContextBuilder(conf)
+                .reuseSpyConfig(pulsarTestContext)
+                .reuseMockBookkeeperAndMetadataStores(pulsarTestContext);
+    }
+
     protected void waitForZooKeeperWatchers() {
         try {
             Thread.sleep(3000);
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/cache/AbstractBrokerEntryCacheMultiBrokerTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/cache/AbstractBrokerEntryCacheMultiBrokerTest.java
new file mode 100644
index 00000000000..862ef6719cb
--- /dev/null
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/cache/AbstractBrokerEntryCacheMultiBrokerTest.java
@@ -0,0 +1,296 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.broker.cache;
+
+import com.google.common.collect.Sets;
+import io.netty.channel.Channel;
+import io.netty.channel.EventLoopGroup;
+import java.net.InetSocketAddress;
+import java.time.Instant;
+import java.time.ZoneOffset;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.LongSupplier;
+import java.util.stream.Collectors;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.bookkeeper.client.PulsarMockBookKeeper;
+import org.apache.bookkeeper.client.api.LedgerEntries;
+import org.apache.bookkeeper.mledger.Entry;
+import org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl;
+import org.apache.commons.lang3.BooleanUtils;
+import org.apache.pulsar.broker.MultiBrokerTestZKBaseTest;
+import org.apache.pulsar.broker.ServiceConfiguration;
+import org.apache.pulsar.broker.testcontext.PulsarTestContext;
+import org.apache.pulsar.client.api.InjectedClientCnxClientBuilder;
+import org.apache.pulsar.client.api.PulsarClient;
+import org.apache.pulsar.client.api.SizeUnit;
+import org.apache.pulsar.client.impl.PulsarClientImpl;
+import org.apache.pulsar.common.policies.data.TenantInfoImpl;
+import org.apache.pulsar.common.util.netty.ChannelFutures;
+import org.testng.SkipException;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+@Test(groups = "broker-api")
+@Slf4j
+public abstract class AbstractBrokerEntryCacheMultiBrokerTest extends MultiBrokerTestZKBaseTest {
+    public enum BrokerEntryCacheType {
+        PIP430("PIP430", AbstractBrokerEntryCacheMultiBrokerTest::configurePIP430),
+        PIP430Disabled("PIP430disabled", AbstractBrokerEntryCacheMultiBrokerTest::disablePIP430),
+        CacheEvictionByMarkDeletedPosition("cacheEvictionByMarkDeletedPosition",
+                AbstractBrokerEntryCacheMultiBrokerTest::configureCacheEvictionByMarkDeletedPosition),
+        PR12258Caching("PR12258", AbstractBrokerEntryCacheMultiBrokerTest::configurePR12258Caching);
+
+        private final String description;
+        private final java.util.function.Consumer<ServiceConfiguration> configurer;
+
+        BrokerEntryCacheType(String description, java.util.function.Consumer<ServiceConfiguration> configurer) {
+            this.description = description;
+            this.configurer = configurer;
+        }
+
+        public void configure(ServiceConfiguration config) {
+            configurer.accept(config);
+        }
+
+        public String getDescription() {
+            return description;
+        }
+    }
+
+    /**
+     * Configures PIP430 caching explicitly, although it's enabled by default.
+     * @param conf ServiceConfiguration instance to be configured
+     */
+    private static void configurePIP430(ServiceConfiguration conf) {
+        // use cache eviction by expected read count, this is the default behavior so it's not necessary to set it,
+        // but it makes the test more explicit
+        conf.setCacheEvictionByExpectedReadCount(true);
+        // LRU cache eviction behavior is enabled by default, but we set it explicitly
+        conf.setManagedLedgerCacheEvictionExtendTTLOfRecentlyAccessed(true);
+    }
+
+    /**
+     * Disables PIP430 caching features.
+     * @param conf ServiceConfiguration instance to be configured
+     */
+    private static void disablePIP430(ServiceConfiguration conf) {
+        conf.setCacheEvictionByExpectedReadCount(false);
+        conf.setManagedLedgerCacheEvictionExtendTTLOfRecentlyAccessed(false);
+    }
+
+    /**
+     * Configures ServiceConfiguration for cache eviction based on the slowest markDeletedPosition.
+     * This method disables cache eviction by expected read count and enables eviction by markDeletedPosition.
+     * Related to https://github.com/apache/pulsar/pull/14985 - "Evicting cache data by the slowest markDeletedPosition"
+     *
+     * @param conf ServiceConfiguration instance to be configured
+     */
+    private static void configureCacheEvictionByMarkDeletedPosition(ServiceConfiguration conf) {
+        disablePIP430(conf);
+        conf.setCacheEvictionByMarkDeletedPosition(true);
+    }
+
+    /**
+     * Configures ServiceConfiguration with settings to test PR12258 behavior for caching to drain backlog consumers.
+     * This method sets configurations to enable caching for cursors with backlogged messages.
+     * To make PR12258 effective, there's an additional change made in the broker codebase to
+     * activate the cursor when a consumer connects, instead of waiting for the scheduled task to activate it.
+     * Check org.apache.pulsar.broker.service.persistent.PersistentSubscription#addConsumerInternal method
+     * for the change.
+     * @param conf ServiceConfiguration instance to be modified
+     */
+    private static void configurePR12258Caching(ServiceConfiguration conf) {
+        disablePIP430(conf);
+        conf.setManagedLedgerMinimumBacklogCursorsForCaching(1);
+        conf.setManagedLedgerMinimumBacklogEntriesForCaching(1);
+        conf.setManagedLedgerMaxBacklogBetweenCursorsForCaching(Integer.MAX_VALUE);
+        conf.setManagedLedgerCursorBackloggedThreshold(Long.MAX_VALUE);
+    }
+
+    AtomicInteger bkReadCount = new AtomicInteger(0);
+    AtomicInteger bkReadEntryCount = new AtomicInteger(0);
+    protected static final DateTimeFormatter CSV_DATETIME_FORMATTER =
+            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
+
+    protected final BrokerEntryCacheType cacheType;
+
+    public AbstractBrokerEntryCacheMultiBrokerTest(BrokerEntryCacheType cacheType) {
+        this.cacheType = cacheType;
+    }
+
+    @Override
+    protected void beforeSetup() {
+        if (!BooleanUtils.toBoolean(System.getenv("ENABLE_MANUAL_TEST")) && !isRunningInIntelliJ()) {
+            throw new SkipException("This test requires setting ENABLE_MANUAL_TEST=true environment variable.");
+        }
+    }
+
+    static boolean isRunningInIntelliJ() {
+        // Check for IntelliJ-specific system properties
+        return System.getProperty("idea.test.cyclic.buffer.size") != null
+                || System.getProperty("java.class.path", "").contains("idea_rt.jar");
+    }
+
+    @Override
+    protected void additionalSetup() throws Exception {
+        super.additionalSetup();
+        admin.tenants().createTenant("my-property",
+                new TenantInfoImpl(Sets.newHashSet("appid1", "appid2"), Sets.newHashSet("test")));
+        admin.namespaces().createNamespace("my-property/my-ns");
+        beforeMethod();
+        PulsarMockBookKeeper mockBookKeeper = pulsarTestContext.getMockBookKeeper();
+        mockBookKeeper.setReadHandleInterceptor(
+                (long ledgerId, long firstEntry, long lastEntry, LedgerEntries entries) -> {
+                    bkReadCount.incrementAndGet();
+                    int numberOfEntries = (int) (lastEntry - firstEntry + 1);
+                    bkReadEntryCount.addAndGet(numberOfEntries);
+                    logBKRead(ledgerId, firstEntry, lastEntry, numberOfEntries);
+                    return mockBookKeeper.getJfrReadHandleInterceptor()
+                            .interceptReadAsync(ledgerId, firstEntry, lastEntry, entries);
+                });
+        // disable delays in bookkeeper writes and reads since they can skew the test results
+        mockBookKeeper.setDefaultAddEntryDelayMillis(0L);
+        mockBookKeeper.setDefaultReadEntriesDelayMillis(0L);
+    }
+
+    protected void logBKRead(long ledgerId, long firstEntry, long lastEntry, int numberOfEntries) {
+        log.info("BK read for ledgerId {}, firstEntry {}, lastEntry {}, numberOfEntries {}",
+                ledgerId, firstEntry, lastEntry, numberOfEntries);
+    }
+
+    @Override
+    protected void customizeMainPulsarTestContextBuilder(PulsarTestContext.Builder pulsarTestContextBuilder) {
+        super.customizeMainPulsarTestContextBuilder(pulsarTestContextBuilder);
+        customizeAllPulsarTestContextBuilders(pulsarTestContextBuilder);
+    }
+
+    @Override
+    protected void customizeAdditionalPulsarTestContextBuilder(PulsarTestContext.Builder pulsarTestContextBuilder) {
+        super.customizeAdditionalPulsarTestContextBuilder(pulsarTestContextBuilder);
+        customizeAllPulsarTestContextBuilders(pulsarTestContextBuilder);
+    }
+
+    protected void customizeAllPulsarTestContextBuilders(PulsarTestContext.Builder pulsarTestContextBuilder) {
+        pulsarTestContextBuilder.spyNoneByDefault();
+        pulsarTestContextBuilder.entryCacheEntryLengthFunction(this::calculateEntryLength);
+    }
+
+    protected int calculateEntryLength(ManagedLedgerImpl ml, Entry entry) {
+        return entry.getLength();
+    }
+
+    @Override
+    protected int numberOfAdditionalBrokers() {
+        // test with 2 brokers in the cluster
+        return 1;
+    }
+
+    @Override
+    protected boolean useDynamicBrokerPorts() {
+        return false;
+    }
+
+    @BeforeMethod(alwaysRun = true)
+    public final void doBeforeMethod() {
+        beforeMethod();
+    }
+
+    protected void beforeMethod() {
+        bkReadCount.set(0);
+        bkReadEntryCount.set(0);
+    }
+
+    @Override
+    protected void doInitConf() throws Exception {
+        super.doInitConf();
+        configureDefaults(conf);
+    }
+
+    private void configureDefaults(ServiceConfiguration conf) {
+        // configure ledger rollover to avoid OOM in test runs and slowdowns due to GC happening frequently when
+        // available heap memory is low
+        conf.setManagedLedgerMinLedgerRolloverTimeMinutes(0);
+        conf.setManagedLedgerMaxLedgerRolloverTimeMinutes(1);
+        conf.setManagedLedgerMaxEntriesPerLedger(100000);
+
+        // set cache size
+        // the entry sizes will be simulated using an injected EntryLengthFunction which
+        // maps to the calculateEntryLength method in this class.
+        conf.setManagedLedgerCacheSizeMB(getManagedLedgerCacheSizeMB());
+
+        // Adjust dispatcher retry backoff to save CPU with minimal latency
+        conf.setDispatcherRetryBackoffInitialTimeInMs(1);
+        conf.setDispatcherRetryBackoffMaxTimeInMs(1);
+
+        // configure the cache type
+        cacheType.configure(conf);
+    }
+
+    protected int getManagedLedgerCacheSizeMB() {
+        return 500;
+    }
+
+    @Override
+    protected ServiceConfiguration createConfForAdditionalBroker(int additionalBrokerIndex) {
+        ServiceConfiguration conf = super.createConfForAdditionalBroker(additionalBrokerIndex);
+        configureDefaults(conf);
+        return conf;
+    }
+
+    String serviceUrlForFixedPorts() {
+        return "pulsar://127.0.0.1:" + mainBrokerPort + "," + additionalBrokerPorts
+                .stream()
+                .map(port -> "127.0.0.1:" + port)
+                .collect(Collectors.joining(",")) + "/";
+    }
+
+
+    protected PulsarClientImpl createPulsarClient(EventLoopGroup eventLoopGroup, LongSupplier connectionDelaySupplier)
+            throws Exception {
+        return InjectedClientCnxClientBuilder.builder()
+                .eventLoopGroup(eventLoopGroup)
+                .clientBuilder(
+                        PulsarClient.builder()
+                                .serviceUrl(serviceUrlForFixedPorts())
+                                .statsInterval(0, TimeUnit.SECONDS)
+                                .memoryLimit(32, SizeUnit.MEGA_BYTES)
+                )
+                .connectToAddressFutureCustomizer((Channel channel, InetSocketAddress inetSocketAddress) -> {
+                    // Introduce a delay to simulate network latency in connecting to the broker.
+                    long delayMillis = connectionDelaySupplier.getAsLong();
+                    if (delayMillis > 0) {
+                        return CompletableFuture.supplyAsync(() -> null,
+                                        CompletableFuture.delayedExecutor(delayMillis, TimeUnit.MILLISECONDS))
+                                .thenCompose(
+                                        __ -> ChannelFutures.toCompletableFuture(channel.connect(inetSocketAddress)));
+                    } else {
+                        return ChannelFutures.toCompletableFuture(channel.connect(inetSocketAddress));
+                    }
+                })
+                .build();
+    }
+
+    protected static String formatTimestampForCsv(long ts) {
+        return CSV_DATETIME_FORMATTER.format(ZonedDateTime.ofInstant(Instant.ofEpochMilli(ts), ZoneOffset.UTC));
+    }
+}
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/cache/BacklogConsumerTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/cache/BacklogConsumerTest.java
index 5bd2a213523..585c14b422b 100644
--- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/cache/BacklogConsumerTest.java
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/cache/BacklogConsumerTest.java
@@ -26,6 +26,7 @@ import java.util.concurrent.TimeUnit;
 import lombok.Cleanup;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl;
+import org.apache.pulsar.broker.ServiceConfiguration;
 import org.apache.pulsar.broker.service.persistent.PersistentTopic;
 import org.apache.pulsar.client.api.Consumer;
 import org.apache.pulsar.client.api.Message;
@@ -57,6 +58,13 @@ public class BacklogConsumerTest extends ProducerConsumerBase {
         internalCleanup();
     }
 
+    @Override
+    protected ServiceConfiguration getDefaultConf() {
+        ServiceConfiguration defaultConf = super.getDefaultConf();
+        defaultConf.setCacheEvictionByExpectedReadCount(false);
+        return defaultConf;
+    }
+
     @DataProvider(name = "ackReceiptEnabled")
     public Object[][] ackReceiptEnabled() {
         return new Object[][] { { true }, { false } };
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/cache/BrokerEntryCacheRollingRestartTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/cache/BrokerEntryCacheRollingRestartTest.java
new file mode 100644
index 00000000000..4dca51b9cec
--- /dev/null
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/cache/BrokerEntryCacheRollingRestartTest.java
@@ -0,0 +1,405 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.broker.cache;
+
+import static org.testng.Assert.assertTrue;
+import io.netty.channel.EventLoopGroup;
+import java.io.Closeable;
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.LongSupplier;
+import java.util.stream.Collectors;
+import lombok.Cleanup;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.bookkeeper.mledger.Entry;
+import org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl;
+import org.apache.commons.io.FileUtils;
+import org.apache.pulsar.broker.BrokerTestUtil;
+import org.apache.pulsar.broker.qos.AsyncTokenBucket;
+import org.apache.pulsar.client.api.Consumer;
+import org.apache.pulsar.client.api.InjectedClientCnxClientBuilder;
+import org.apache.pulsar.client.api.Message;
+import org.apache.pulsar.client.api.MessageId;
+import org.apache.pulsar.client.api.Producer;
+import org.apache.pulsar.client.api.PulsarClient;
+import org.apache.pulsar.client.api.PulsarClientException;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.api.SubscriptionInitialPosition;
+import org.apache.pulsar.client.api.SubscriptionType;
+import org.apache.pulsar.client.impl.PulsarClientImpl;
+import org.testng.annotations.Factory;
+import org.testng.annotations.Test;
+
+/**
+ * This test class current contains test cases that are exploratory in nature and are not intended to be run
+ * as part of the regular test suite. This might change later.
+ * The current intent is to show how the PIP-430 caching results in better cache hit rates in rolling restarts
+ * when there's an active producer producing messages to a topic.
+ * To avoid OOME, run this test on the command line (after enabling the test case) with this command:
+ * ENABLE_MANUAL_TEST=true NETTY_LEAK_DETECTION=off mvn -pl pulsar-broker test -Dtest=BrokerEntryCacheRollingRestartTest
+ */
+@Slf4j
+public class BrokerEntryCacheRollingRestartTest extends AbstractBrokerEntryCacheMultiBrokerTest {
+    // comment out next line to run a single test run for the default cache type
+    @Factory
+    public static Object[] createTestInstances() {
+        return Arrays.stream(BrokerEntryCacheType.values()).map(BrokerEntryCacheRollingRestartTest::new)
+                .toArray();
+    }
+
+    volatile MessageId lastPublishedMessageId;
+    volatile boolean restarting;
+
+    public BrokerEntryCacheRollingRestartTest() {
+        this(BrokerEntryCacheType.PIP430);
+    }
+
+    public BrokerEntryCacheRollingRestartTest(BrokerEntryCacheType cacheType) {
+        super(cacheType);
+    }
+
+    @Override
+    public void beforeMethod() {
+        super.beforeMethod();
+        lastPublishedMessageId = null;
+        restarting = false;
+    }
+
+    @Override
+    protected void logBKRead(long ledgerId, long firstEntry, long lastEntry, int numberOfEntries) {
+        log.info(
+                "BK read for ledgerId {}, firstEntry {}, lastEntry {}, numberOfEntries {}, "
+                        + "last msgid {}, restarting={}",
+                ledgerId, firstEntry, lastEntry, numberOfEntries, lastPublishedMessageId, restarting);
+    }
+
+    private static final int CACHE_SIZE_MB = 250;
+    private static final int KB = 1024;
+    private static final int MESSAGE_SIZE = 8 * KB; // 8 KB
+
+    @Override
+    protected int getManagedLedgerCacheSizeMB() {
+        return CACHE_SIZE_MB;
+    }
+
+    @Override
+    protected int calculateEntryLength(ManagedLedgerImpl ml, Entry entry) {
+        // simulate message size for cache entry size calculation
+        return MESSAGE_SIZE;
+    }
+
+    @Test(invocationCount = 5)
+    public void testTailingReadsRollingRestart() throws Exception {
+        // this description is showed in result CSV files
+        String testConfigDescriptionInResult = cacheType.getDescription();
+
+        @Cleanup("shutdownGracefully")
+        EventLoopGroup eventLoopGroup = InjectedClientCnxClientBuilder.createEventLoopGroup(2, false);
+
+        @Cleanup
+        PulsarClient producerPulsarClient = createPulsarClient(eventLoopGroup, () -> 0L);
+
+        final String topicName =
+                BrokerTestUtil.newUniqueName("persistent://my-property/my-ns/rolling-restart-cache-test-topic");
+        final String subscriptionName = "sub";
+        final int numConsumers = 10;
+        final int receiverQueueSize = 200;
+        int testTimeInSeconds = 30;
+        int numberOfRestarts = 3;
+        int producerRatePerSecond = 50000;
+        int numberOfPartitions = 10;
+        if (numberOfPartitions > 1) {
+            admin.topics().createPartitionedTopic(topicName, numberOfPartitions);
+        }
+
+        AtomicInteger messagesInFlight = new AtomicInteger();
+        final int targetMessagesInFlight = 500000;
+        AtomicInteger numberOfMessagesConsumed = new AtomicInteger(0);
+
+        @Cleanup
+        Producer<Long> producer = producerPulsarClient.newProducer(Schema.INT64)
+                .topic(topicName)
+                .enableBatching(false)
+                .blockIfQueueFull(true)
+                .create();
+
+        // Create consumers in paused state with receiver queue size of 50
+        Consumer<Long>[] consumers = new Consumer[numConsumers];
+        List<PulsarClient> consumerPulsarClients = new ArrayList<>();
+        @Cleanup
+        Closeable consumerPulsarClientsCloseable = () -> {
+            for (PulsarClient consumerPulsarClient : consumerPulsarClients) {
+                consumerPulsarClient.close();
+            }
+        };
+
+        // introduce 40-100ms delay in consumer connection to simulate network latency
+        LongSupplier consumerClientConnectionDelaySupplier = () -> ThreadLocalRandom.current().nextLong(20L, 40L);
+        for (int i = 0; i < numConsumers; i++) {
+            PulsarClientImpl consumerPulsarClient =
+                    createPulsarClient(eventLoopGroup, consumerClientConnectionDelaySupplier);
+            consumerPulsarClients.add(consumerPulsarClient);
+            consumers[i] = consumerPulsarClient.newConsumer(Schema.INT64)
+                    .topic(topicName)
+                    .subscriptionName(subscriptionName + "-" + i)
+                    .subscriptionType(SubscriptionType.Shared)
+                    .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)
+                    .startPaused(true) // start consumers in paused state
+                    .receiverQueueSize(receiverQueueSize)
+                    .poolMessages(true)
+                    // use shorter ack group time to lose less acks in restarts
+                    .acknowledgmentGroupTime(5, TimeUnit.MILLISECONDS)
+                    .ackTimeout(0, TimeUnit.SECONDS)
+                    .subscribe();
+        }
+
+        long endTimeMillis = System.currentTimeMillis() + testTimeInSeconds * 1000;
+
+        CompletableFuture<Long> numberOfMessagesProducedFuture = new CompletableFuture<>();
+
+        @Cleanup("interrupt")
+        Thread producerThread = new Thread(() -> {
+            long messageId = 0;
+            log.info("Starting producer thread");
+            AsyncTokenBucket producerTokenBucket = AsyncTokenBucket.builder()
+                    .rate(producerRatePerSecond)
+                    .build();
+            try {
+                while (!Thread.currentThread().isInterrupted()) {
+                    producerTokenBucket.consumeTokens(1);
+                    producer.sendAsync(messageId++).thenApply(msgId -> {
+                        lastPublishedMessageId = msgId;
+                        return msgId;
+                    });
+                    // each consumer has a unique subscription
+                    messagesInFlight.addAndGet(numConsumers);
+                    if (messagesInFlight.get() >= targetMessagesInFlight) {
+                        try {
+                            // start throttling message production
+                            Thread.sleep(100L);
+                        } catch (InterruptedException e) {
+                            Thread.currentThread().interrupt();
+                        }
+                    }
+                    // limit the rate by throttling message production if necessary
+                    long throttlingDurationNanos = producerTokenBucket.calculateThrottlingDuration();
+                    if (throttlingDurationNanos > 0) {
+                        try {
+                            Thread.sleep(TimeUnit.NANOSECONDS.toMillis(throttlingDurationNanos));
+                        } catch (InterruptedException e) {
+                            Thread.currentThread().interrupt();
+                        }
+                    }
+                }
+            } finally {
+                log.info("Producer thread interrupted, stopping message production");
+                numberOfMessagesProducedFuture.complete(messageId);
+            }
+        });
+
+        producerThread.start();
+
+        // Unpause all consumers
+        for (Consumer<Long> consumer : consumers) {
+            consumer.resume();
+        }
+
+        AtomicInteger actualNumberOfRestarts = new AtomicInteger(0);
+
+        @Cleanup("interrupt")
+        Thread rollingRestartInjector = new Thread(() -> {
+            List<Runnable> restartRunnables = new ArrayList<>();
+            restartRunnables.add(() -> {
+                try {
+                    long startTimeMillis = System.currentTimeMillis();
+                    log.info("Starting restarting main broker");
+                    restartBroker();
+                    log.info("Finished restarting main broker, took {} ms",
+                            System.currentTimeMillis() - startTimeMillis);
+                } catch (Exception e) {
+                    log.error("Error while restarting broker ", e);
+                }
+            });
+            for (int i = 0; i < numberOfAdditionalBrokers(); i++) {
+                int brokerIndex = i;
+                restartRunnables.add(() -> {
+                    try {
+                        long startTimeMillis = System.currentTimeMillis();
+                        log.info("Starting restarting additional broker {}", brokerIndex);
+                        restartAdditionalBroker(brokerIndex);
+                        log.info("Finished restarting additional broker {}, took {} ms", brokerIndex,
+                                System.currentTimeMillis() - startTimeMillis);
+                    } catch (Exception e) {
+                        log.error("Error while restarting additional broker {}", brokerIndex, e);
+                    }
+                });
+            }
+            long targetIntervalBetweenRestarts = testTimeInSeconds * 1000 / (numberOfRestarts + 1);
+            log.info("Rolling restart injector will restart the brokers every {} ms", targetIntervalBetweenRestarts);
+            long restartsStartTimeMillis = System.currentTimeMillis();
+            while (!Thread.currentThread().isInterrupted()) {
+                try {
+                    for (Runnable restartRunnable : restartRunnables) {
+                        if (Thread.currentThread().isInterrupted()
+                                || System.currentTimeMillis() >= endTimeMillis - targetIntervalBetweenRestarts) {
+                            return;
+                        }
+                        long waitingTimeMillis =
+                                targetIntervalBetweenRestarts - ((System.currentTimeMillis() - restartsStartTimeMillis)
+                                        % targetIntervalBetweenRestarts);
+                        log.info("Waiting for {} ms before restarting broker", waitingTimeMillis);
+                        // Wait for some time before restarting the broker
+                        Thread.sleep(waitingTimeMillis);
+                        // Now restart the next broker
+                        try {
+                            restarting = true;
+                            restartRunnable.run();
+                        } finally {
+                            restarting = false;
+                        }
+                        actualNumberOfRestarts.incrementAndGet();
+                    }
+                } catch (InterruptedException e) {
+                    log.info("rolling restart injector interrupted");
+                    Thread.currentThread().interrupt();
+                }
+            }
+        });
+        rollingRestartInjector.start();
+
+        // Start consumer threads to read the catch-up messages
+        CountDownLatch consumersLatch = new CountDownLatch(numConsumers);
+
+        List<Thread> consumerThreads = new ArrayList<>();
+
+        for (int i = 0; i < numConsumers; i++) {
+            final int consumerId = i;
+            Consumer<Long> consumer = consumers[consumerId];
+            Thread consumerThread = new Thread(() -> {
+                try {
+                    while (!Thread.currentThread().isInterrupted()) {
+                        try {
+                            Message<Long> message = consumer.receive(2000, TimeUnit.MILLISECONDS);
+                            if (message == null) {
+                                break;
+                            }
+                            consumer.acknowledge(message);
+                            message.release();
+                            messagesInFlight.decrementAndGet();
+                            numberOfMessagesConsumed.incrementAndGet();
+                        } catch (PulsarClientException e) {
+                            // Continue on errors
+                        }
+                    }
+                } finally {
+                    consumersLatch.countDown();
+                }
+            });
+            consumerThread.start();
+            consumerThreads.add(consumerThread);
+        }
+        @Cleanup
+        Closeable consumerThreadsCloseable = () -> {
+            for (Thread consumerThread : consumerThreads) {
+                consumerThread.interrupt();
+            }
+            for (Thread consumerThread : consumerThreads) {
+                try {
+                    consumerThread.join();
+                } catch (InterruptedException e) {
+                }
+            }
+            for (Consumer<Long> consumer : consumers) {
+                consumer.close();
+            }
+        };
+
+        long waitingTimeMillis = endTimeMillis - System.currentTimeMillis();
+        if (waitingTimeMillis > 0) {
+            log.info("Waiting for {} ms for test to complete.", waitingTimeMillis);
+            Thread.sleep(waitingTimeMillis);
+        }
+
+        log.info("Stopping rolling restart injector.");
+        rollingRestartInjector.interrupt();
+        rollingRestartInjector.join();
+
+        log.info("Stopping producer thread.");
+        producerThread.interrupt();
+        producerThread.join();
+
+        log.info("Waiting up to 5 seconds for consumers to complete.");
+        boolean allCompleted = consumersLatch.await(5, TimeUnit.SECONDS);
+        log.info("All consumers completed: {} (latch count: {})", allCompleted, consumersLatch.getCount());
+
+        log.info("Stopping consumers.");
+        consumerThreadsCloseable.close();
+        // Wait for all consumers to complete
+        assertTrue(consumersLatch.await(2, TimeUnit.SECONDS),
+                "All consumers should have been completed");
+
+        logAndRecordResultsToCsv(testConfigDescriptionInResult, numberOfMessagesProducedFuture.getNow(0L),
+                numberOfMessagesConsumed.get(), numConsumers, actualNumberOfRestarts.get());
+    }
+
+    private void logAndRecordResultsToCsv(String testConfigDescriptionInResult, long numberOfMessagesProduced,
+                                          int numberOfMessagesConsumed, int numConsumers, int actualNumberOfRestarts)
+            throws InterruptedException, ExecutionException, TimeoutException, IOException {
+        int readEntryCount = bkReadEntryCount.get();
+        double cacheHitPercentage = 100.0d * (numberOfMessagesConsumed - readEntryCount)
+                / numberOfMessagesConsumed;
+        double cacheMissPercentage = 100.0d - cacheHitPercentage;
+        int readCount = bkReadCount.get();
+
+        Object[] msgArgs = {testConfigDescriptionInResult, numberOfMessagesProduced,
+                numberOfMessagesConsumed, numConsumers, readCount, readEntryCount,
+                String.format("%.2f", cacheHitPercentage), String.format("%.2f", cacheMissPercentage),
+                actualNumberOfRestarts};
+        log.info("[{}] Produced {} and Consumed {} messages (across {} consumers with unique subscriptions) in total. "
+                + "Number of BK reads {} with {} entries. Cache hits {}%, Cache misses {}%, Number of "
+                + "restarts {}", msgArgs);
+
+        // record results in csv files for later processing with DuckDB, for example:
+        // { cat pulsar-broker/target/rolling_restarts_result_header.txt; \
+        //   cat pulsar-broker/target/rolling_restarts_result_*.csv } \
+        //   | duckdb -c "select * from read_csv('/dev/stdin')" | cat
+        File resultHeaderFile = new File("target/rolling_restarts_result_header.txt");
+        String resultHeader =
+                "description\tproduced\tconsumed\tconsumers\tbk_reads\tbk_read_entries\thits\tmisses\trestarts\tts\n";
+        FileUtils.write(resultHeaderFile, resultHeader, StandardCharsets.UTF_8);
+        long ts = System.currentTimeMillis();
+        File resultFile = new File("target/rolling_restarts_result_" + ts + ".csv");
+        List<String> csvColumns = new ArrayList<>();
+        Arrays.stream(msgArgs).map(Object::toString).forEach(csvColumns::add);
+        csvColumns.add(formatTimestampForCsv(ts));
+        String resultMessage = csvColumns.stream().collect(Collectors.joining("\t")) + "\n";
+        FileUtils.write(resultFile, resultMessage, StandardCharsets.UTF_8);
+    }
+}
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/cache/BrokerEntryCacheTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/cache/BrokerEntryCacheTest.java
new file mode 100644
index 00000000000..f857c5c2ef6
--- /dev/null
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/cache/BrokerEntryCacheTest.java
@@ -0,0 +1,760 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.broker.cache;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import lombok.Cleanup;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.bookkeeper.client.api.LedgerEntries;
... 1729 lines suppressed ...