Serverless vs shared latency gap

Hi,

Just some basic question here. I set up mongodb atlas serverless and shared node(free tier) for my local testing. Both of them are set to set AWS oregon west datacenter. However, the read performance has a big gap. For my 1.8 document (the only document in the collection), the shared node latency is about 1.5 seconds where the serverless latency is about 5.6 seconds. Everything else is same.

I’m surprised by this latency gap and any insights is appreciated.

Hi @Weide_Zhang - Welcome to the community.

I presume the only thing you’re changing application end is the connection string (pointing to either the shared tier cluster or serverless instance) when testing. Please correct me if I am wrong here.

Besides that, would you be able to provide some information:

  • How many documents in the collection
  • sample document(s)
  • the operation being performed
  • If this latency gap is noticable every time you run the same operation

I’ll try reproduce this behaviour if possible.

Regards,
Jason

Hi Jason,

I’m attaching the document here. to answer your question.

  1. it is just a single document in the collection.
  2. https://codepo8.github.io/json-dummy-data/1.8MB.json
  3. find with no filter (using the nodejs mongo sdk)
  4. yes, it is repeatable.

Thanks,

Weide

Hi @Weide_Zhang,

I imported the document and I tried to test this via mongosh but got the below results from the execution stats output for both an M0 and serverless instance.

Serverless instance:

executionStats: {
    executionSuccess: true,
    nReturned: 1,
    executionTimeMillis: 0,
    totalKeysExamined: 0,
    totalDocsExamined: 1,
    executionStages: {
      stage: 'COLLSCAN',
      nReturned: 1,
      executionTimeMillisEstimate: 0,
      works: 2,
      advanced: 1,
      needTime: 0,
      needYield: 0,
      saveState: 0,
      restoreState: 0,
      isEOF: 1,
      direction: 'forward',
      docsExamined: 1
    }
  },
  command: { find: 'collection', filter: {}, '$db': 'db' },
  serverInfo: {
    host: 'serverlessinstance0-lb.redacted.mongodb.net',
    port: 27017,
    version: '6.2.0',
    gitVersion: '290112ef1396982a187217b4cb8a943416ad0db1'
  }

M0 instance:

executionStats: {
    executionSuccess: true,
    nReturned: 1,
    executionTimeMillis: 4,
    totalKeysExamined: 0,
    totalDocsExamined: 1,
    executionStages: {
      stage: 'COLLSCAN',
      nReturned: 1,
      executionTimeMillisEstimate: 4,
      works: 3,
      advanced: 1,
      needTime: 1,
      needYield: 0,
      saveState: 0,
      restoreState: 0,
      isEOF: 1,
      direction: 'forward',
      docsExamined: 1
    }
  },
  command: { find: 'collection', filter: {}, '$db': 'db' },
  serverInfo: {
    host: 'ac-iceu1mh-shard-00-01.redacted.mongodb.net',
    port: 27017,
    version: '5.0.15',
    gitVersion: '935639beed3d0c19c2551c93854b831107c0b118'
  }

The executionTimeMillis for both are <5ms.

Can you advise how you got these times? Was this from the execution stats output?

Regards,
Jason

Hi Jason,
Thanks for your reply.
Here is what my client code looks like, first i call connect for every request for my singleton mongodbclient and then i do the fetch

    let client = MongoDBClient.getInstance(); //this is a singleton 
    const start = new Date().getTime();
    await client.connect();
    let elapsed1 = new Date().getTime() - start;
    let fontservice = new FontsService();
    // this is the real mongodb call,       
    let data: any = await client.db!.collection("collectionname").findOne({});
    let elapsed2 = new Date().getTime() - start;
    console.log(`elapsed time is ${elapsed2} and connection taks ${elapsed1}`);
type or paste code here

so seems connection takes about 500ms and then the real difference is in the findOne call where I measured the latency (elapsed 2 and elapsed1) using the nodejs client sdk.

Thanks for clarifying.

Since your main purpose is the latency comparisons of the same find operation between the two types of instances, I believe you should try the following tests described below because it would compare the same operation executed server side:

Can you run the same queries against both instances using the explain output in “executionStats” verbosity and provide the output for each?:

let data: any = await client.db!.collection("collectionname").explain("executionStats").find({})

Note: .find() returns a cursor and can be used with .explain().

The above is to compare the execution times on both the serverless instance and shared tier instance. If these are relatively the same then the latency gap you’ve mentioned may exist elsewhere outside of the particular find operation being executed.

Please note : Your code is showing that it’s a findOne() and not a find().

Regards,
Jason