简体   繁体   中英

MongoDB Change Streams very slow

I am encountering a delay of 5 to 10 seconds from when the operation happens in MongoDB until I capture it in a Change Stream in NodeJS.

Are these times normal, what parameters could I check to see if any are impacting this?

Here are a couple of examples and some suspicions (to be tested).

Here we try to catch changes only in the fields of the Users collection that interest us, I do not know if doing this to avoid unwanted events may be causing delay in the reception of the ChangeStream and it would be convenient to receive more events and filter in code the updated fields.

I do not know, also if the "and" of the type of operation would have to be put before or it is irrelevant.

userChangeStreamQuery: [{
    $match: {
        $and: [
            {$or:[
                { "updateDescription.updatedFields.name": { $exists: true } },
                { "updateDescription.updatedFields.email": { $exists: true } },
                { "updateDescription.updatedFields.organization": { $exists: true } },
                { "updateDescription.updatedFields.displayName": { $exists: true } },
                { "updateDescription.updatedFields.image": { $exists: true } },
                { "updateDescription.updatedFields.organizationName": { $exists: true } },
                { "updateDescription.updatedFields.locationName": { $exists: true } }
            ]},
            { operationType: "update" }]
    }
}],

Of this other one, that waits for events on the Plans collection, I worry that it does not have aggregate defined and it is when receiving the event where it is filtered if the operation arrives type 'insert', 'update', 'delete'. This one is giving us a delay of 7~10 seconds.

startChangeStream({
    streamId: 'plans',
    collection: 'plans',
    query: '',
    resumeTokens
});
...
const startChangeStream = ({ streamId, collection, query, resumeTokens }) => {

    const resumeToken = resumeTokens ? resumeTokens[streamId] || undefined : undefined;

    nativeMongoDbFactory.setChangeStream({
        streamId,
        collection,
        query,
        resumeToken
    });
}

In no case are massive operations, normally they are operations performed by the user through web forms.

when the collection is sharding, using change streams the mongos server need to wait until all shards have data to return, if some shards no data to write, the idle primary mongod writes a no-op to the oplog every 10 ( idlewriteperiodms ) seconds. that is why you delay is 7~10 seconds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM