Slow query message on MongoDB ends up with a RAM overload

141 Views Asked by At

I'm running a MongoDB server on a VM, which has 4gb of ram, 2 vCPU and a SSD disk.
The database is connected to an API, which is becoming more and more widely used, so requests to the database are increasing. I also cache some data (API side).

The problem is that my server's RAM is only decreasing, as the MongoDB process is using more and more RAM (until my server crashed from lack of RAM). So I looked for a way to limit MongoDB's use of RAM, and came across this solution. I put cacheSizeGB to 2. This didn't solve my problem.

So I redirected myself to the logs to see if an error could explain my problem. In /var/log/mongodb/mongod.log, I've come across a lot of Slow query. I was surprised, because I use indexes.

Here's a typical message from my logs:

{"t":{"$date":"2023-08-26T10:25:21.010+00:00"},"s":"I",  "c":"COMMAND",  "id":51803,   "ctx":"conn4","msg":"Slow query","attr":{"type":"command","ns":"mspretro.chatlogs","appName":"App Service","command":{"insert":"chatlogs","documents":[{"ActorId":26020,"LogId":728151,"RoomId":36835,"Date":{"$date":"2023-08-26T10:25:20.660Z"},"IPId":119334,"Message":"swearing at me isnt going to help u","_id":{"$oid":"64e9d310f52dc2de618eb2a3"},"__v":0}],"ordered":true,"lsid":{"id":{"$uuid":"1014a7bb-d128-4d9c-92ab-cb65239c4719"}},"$db":"mspretro"},"ninserted":1,"keysInserted":4,"numYields":0,"reslen":45,"locks":{"ParallelBatchWriterMode":{"acquireCount":{"r":1}},"FeatureCompatibilityVersion":{"acquireCount":{"w":1}},"ReplicationStateTransition":{"acquireCount":{"w":1}},"Global":{"acquireCount":{"w":1}},"Database":{"acquireCount":{"w":1}},"Collection":{"acquireCount":{"w":1}},"Mutex":{"acquireCount":{"r":1}}},"flowControl":{"acquireCount":1,"timeAcquiringMicros":1},"storage":{"data":{"bytesRead":9734855,"timeReadingMicros":26751},"timeWaitingMicros":{"handleLock":3,"schemaLock":9503}},"remote":"10.0.0.254:56786","protocol":"op_msg","durationMillis":344}}

db.chatlogs.getIndexes() returns me:

[
  { v: 2, key: { _id: 1 }, name: '_id_' },
  { v: 2, key: { ActorId: 1 }, name: 'ActorId_1' },
  { v: 2, key: { IPId: 1 }, name: 'IPId_1' },
  { v: 2, key: { LogId: 1 }, name: 'LogId_1' }
]

To prevent my app from crashing all the time, I have to restart MongoDB regularly, which isn't a sustainable solution.

Can you please help me?

0

There are 0 best solutions below