Unreachable MongoDB once memory is allocated on the swap

I am employing a MongoDB (version 5.0.5) on a virtual machine, which is supposed to receive a large number of client connections. However, once the DB reaches the memory threshold of the VM (8Gb) and starts allocating memory on the Swap (very large), my clients start closing the connection and break. If I check the status of the DB in the very moment the clients close the connection I get:

MongoServerSelectionError: Server selection timed out after 2000 ms

and in the MongoDB logs I get the following HostUnreachable error:

{"t":{"$date":"2021-12-30T22:07:33.332+01:00"},"s":"I",  "c":"NETWORK",  "id":22989,   "ctx":"conn12237","msg":"Error sending response to client. Ending connection from remote","attr":{"error":{"code":6,"codeName":"HostUnreachable","errmsg":"Connection reset by peer"},"remote":"","connectionId":12237}}

Is there anything I can do to prevent this issue or is this a hard limitation on the relative low amount of available RAM?


How to create an ORM in python?

A plugin enables you to import a Markdown file as various formats on your vite project