title: Mongodb 4.2.8: Unable to add session into the cache because the number of active sessions is too high
body: <p>Suddenly we started to experience following problem during connection to Mongo: <code>{u'code': 261, u'ok': 0.0, u'$clusterTime': {u'clusterTime': Timestamp(1614532995, 3141), u'signature': {u'keyId': 0L, u'hash': Binary('\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', 0)}}, u'codeName': u'TooManyLogicalSessions', u'operationTime': Timestamp(1614532995, 3141), u'errmsg': u'Unable to add session into the cache because the number of active sessions is too high'}</code>.</p>
  <p>This happens for any connection driver we use:</p>
  <ul>
  <li>Mongo shell</li>
  <li>pymongo 3.6</li>
  <li>pymongo 3.11</li>
  </ul>
  <p>And happens not for every query, but roughly for 30-40% of all queries.</p>
  <p>Meanwhile, <code>maxSession</code> has default value (1000000) and I have following data from db status:</p>
  <pre><code>&quot;logicalSessionRecordCache&quot; : {
          &quot;activeSessionsCount&quot; : 2244,
          &quot;sessionsCollectionJobCount&quot; : 48430,
          &quot;lastSessionsCollectionJobDurationMillis&quot; : 0,
          &quot;lastSessionsCollectionJobTimestamp&quot; : ISODate(&quot;2021-02-28T16:56:03.438Z&quot;),
          &quot;lastSessionsCollectionJobEntriesRefreshed&quot; : 0,
          &quot;lastSessionsCollectionJobEntriesEnded&quot; : 0,
          &quot;lastSessionsCollectionJobCursorsClosed&quot; : 0,
          &quot;transactionReaperJobCount&quot; : 49566,
          &quot;lastTransactionReaperJobDurationMillis&quot; : 1,
          &quot;lastTransactionReaperJobTimestamp&quot; : ISODate(&quot;2021-02-28T17:03:17.631Z&quot;),
          &quot;lastTransactionReaperJobEntriesCleanedUp&quot; : 0,
          &quot;sessionCatalogSize&quot; : 33
      },
  </code></pre>
  <p>Once the issue has been discovered, I checked periodically a number of sessions in <code>config.system.sessions</code>. It varied from 11k to 560k (most of the time it was between 80k and 350k), which seems to be quite high.
  However, the problem remained disregard of the amount of sessions.</p>
  <p>An error is sudden, we have the same load as before (I don't know the number of sessions we used to have before but we didn't add any new clients - we have about 3k connections.</p>
  <p>There is no sharding, only a replica (one primary and one secondary).</p>
  <p>I would really appreciate any advice on how to overcome this problem.</p>
  <p>UPD: another thing that looks weird for me:</p>
  <pre><code>&gt; db.system.sessions.count()
  416068
  &gt; db.currentOp(true).inprog.length
  2911
  </code></pre>
  <p>how is it possible to have such a difference?</p>