<section>
 <h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
  Read Preference
  <code class="leafygreen-ui-1l06pbn">
   maxStalenessSeconds
  </code>
  <a class="headerlink leafygreen-ui-1kepx47" href="#read-preference-maxstalenessseconds" title="Permalink to this heading">
   <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
    <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
    </path>
    <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
    </path>
   </svg>
   <div class="css-1btu0w3 evvkmar0" id="read-preference-maxstalenessseconds">
   </div>
  </a>
 </h1>
 <p class="leafygreen-ui-1kp3ins">
  Replica set members can lag behind the
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
   primary
  </a>
  due to network
congestion, low disk throughput, long-running operations, etc. The read
preference
  <code class="leafygreen-ui-1l06pbn">
   maxStalenessSeconds
  </code>
  option lets you specify a maximum
replication lag, or "staleness", for reads from
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-secondary">
   secondaries
  </a>
  . When a secondary's estimated staleness exceeds
  <code class="leafygreen-ui-1l06pbn">
   maxStalenessSeconds
  </code>
  , the client stops using it for read operations.
 </p>
 <div class="leafygreen-ui-85da11" role="note">
  <h2 class="leafygreen-ui-1ac4g5h">
   Important
  </h2>
  <div class="leafygreen-ui-9mycqc">
   <p class="leafygreen-ui-1kp3ins">
    The
    <code class="leafygreen-ui-1l06pbn">
     maxStalenessSeconds
    </code>
    read preference option is intended for
applications that read from secondaries and want to avoid reading
from a secondary that has fallen overly far behind in replicating
the primary's writes. For example, a secondary might stop
replicating due to a network outage between itself and the primary.
In that case, the client should stop reading from the secondary
until an administrator resolves the outage and the secondary catches
up.
   </p>
  </div>
 </div>
 <div class="leafygreen-ui-15mdk4" role="note">
  <h2 class="leafygreen-ui-6n7b7d">
   Note
  </h2>
  <div class="leafygreen-ui-9mycqc">
   <p class="leafygreen-ui-1kp3ins">
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/replication/#std-label-replication-flow-control">
     Flow control
    </a>
    limits the rate at which
the primary applies its writes with the goal of keeping
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/replSetGetStatus/#mongodb-data-replSetGetStatus.optimes.lastCommittedOpTime">
     <code class="leafygreen-ui-1l06pbn">
      majority
committed
     </code>
    </a>
    lag under a
specified maximum value.
   </p>
  </div>
 </div>
 <p class="leafygreen-ui-1kp3ins">
  You can specify
  <code class="leafygreen-ui-1l06pbn">
   maxStalenessSeconds
  </code>
  with the following read
preference modes:
 </p>
 <ul class="leafygreen-ui-rioki0">
  <li class="leafygreen-ui-d7p7b6">
   <p class="leafygreen-ui-1kp3ins">
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference/#mongodb-readmode-primaryPreferred">
     <code class="leafygreen-ui-1l06pbn">
      primaryPreferred
     </code>
    </a>
   </p>
  </li>
  <li class="leafygreen-ui-d7p7b6">
   <p class="leafygreen-ui-1kp3ins">
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference/#mongodb-readmode-secondary">
     <code class="leafygreen-ui-1l06pbn">
      secondary
     </code>
    </a>
   </p>
  </li>
  <li class="leafygreen-ui-d7p7b6">
   <p class="leafygreen-ui-1kp3ins">
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference/#mongodb-readmode-secondaryPreferred">
     <code class="leafygreen-ui-1l06pbn">
      secondaryPreferred
     </code>
    </a>
   </p>
  </li>
  <li class="leafygreen-ui-d7p7b6">
   <p class="leafygreen-ui-1kp3ins">
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference/#mongodb-readmode-nearest">
     <code class="leafygreen-ui-1l06pbn">
      nearest
     </code>
    </a>
   </p>
  </li>
 </ul>
 <p class="leafygreen-ui-1kp3ins">
  Max staleness is not compatible with mode
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference/#mongodb-readmode-primary">
   <code class="leafygreen-ui-1l06pbn">
    primary
   </code>
  </a>
  and only
applies when
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference-mechanics/#std-label-replica-set-read-preference-behavior-member-selection">
   selecting
  </a>
  a
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-secondary">
   secondary
  </a>
  member of a set for a read operation.
 </p>
 <p class="leafygreen-ui-1kp3ins">
  When selecting a server for a read operation with
  <code class="leafygreen-ui-1l06pbn">
   maxStalenessSeconds
  </code>
  , clients
estimate how stale each secondary is by comparing the secondary's last
write to that of the primary. The client will then direct the read
operation to a secondary whose estimated lag is less than or equal to
  <code class="leafygreen-ui-1l06pbn">
   maxStalenessSeconds
  </code>
  .
 </p>
 <p class="leafygreen-ui-1kp3ins">
  If there is no primary, the client uses the secondary with the most
recent write for the comparison.
 </p>
 <p class="leafygreen-ui-1kp3ins">
  By default, there is no maximum staleness and clients will not consider a
secondary's lag when choosing where to direct a read operation.
 </p>
 <p class="leafygreen-ui-1kp3ins">
  You must specify a
  <code class="leafygreen-ui-1l06pbn">
   maxStalenessSeconds
  </code>
  value of 90 seconds or
longer: specifying a smaller
  <code class="leafygreen-ui-1l06pbn">
   maxStalenessSeconds
  </code>
  value will raise
an error. Clients estimate secondaries' staleness by periodically
checking the latest write date of each replica set member. Since these
checks are infrequent, the staleness estimate is coarse. Thus, clients
cannot enforce a
  <code class="leafygreen-ui-1l06pbn">
   maxStalenessSeconds
  </code>
  value of less than 90 seconds.
 </p>
</section>
