Realm does not seem to close mongodb connections on app redeployment

Hello everybody! I’m very excited to be of assistance!

I’m Brock, I am one of the Technical Service Engineers for MongoDB Realm. Please allow me to explain what in particular you’re seeing if I may.

Short summary:
So simply put, the connections themselves aren’t client connections that you’re seeing. What you’re seeing is a series of redundant connection pathways that are waiting on more and more clients. As you get more clients the connections will begin to even out and disperse to balance the loads of an ever increasing number of clients. (Clients = Users/Devices/Instances of your app)

General points:

  • End user applications connect to Realm and Realm will connect to Atlas and share/manage connections as your app grows. Sometimes this leads to higher numbers of connections when numbers of users are small, but connections are efficiently shared at scale.
  • Sometimes a cluster change, Realm app deployment, or change in application behavior can cause a change in connections, but Realm releases unused connections regularly.
  • Users can scale up clusters for higher connection limits/resources.

In long summary:
If you are using Realm Triggers/functions/Sync, connection count in Atlas connections will increase but that should stabilise after some time(10-15 minutes) of no activity.

If you continue to experience any further issues with the connections not being released by the Realm application please let us know Realm app link and we would be happy to look into this further.

Realm internally proxies connection by default and keeps them open across multiple invocations so this will definitely be more efficient with connection utilization and may actually be more performant on average (less of a latency hit for opening new connections). Realm generally opens a single connection pool per Realm host and manages connections at the Realm host-level.

Further new devices connecting to Realm may not result in a new connection being made to Atlas from Realm Servers, it may re-use a connection.

So in another way to explain in short summary, connections count is not directly proportional on either the number of clients or the number of currently open Realms. Some unused connections can stay active on Atlas and later be reused by another Realm client.

To minimize the number of concurrent listening clients and open change streams:

  • Start Watch and Sync operations only when necessary.
  • Close any open Watch and Sync streams immediately when they’re no longer necessary.
  • Avoid data models that require you to Watch or Sync an unbounded number of collections.