Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-4902

Include shard key when reloading documents for improved query performance with Atlas Global Write clusters

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 7.2.0.rc1
    • Affects Version/s: 7.0.8
    • Component/s: None
    • None
    • Environment:
      Ubuntu 18, Phusion Passenger, Ruby 2.5.8 Mongo Atlas with Global Write Enabled
    • When Mongoid reloads models it will use the shard key declared for the model, if one exists, in addition to the id field in the find query.

      Given a Global Write enabled Mongo Atlas cluster with the following compound shard keys:

      { location: 1, token: 1 }
      

      The reload mechanism for my document takes around 200 ms. This severely affects the performance of my application since all of the invocations of delete_all/destroy_all seems to utilize the reload mechanism on the document.

      It also appears that the "identity" of the document is not compatible with Global Write clusters, since it only utilizes the document's ID

      Here's some code to demonstrate the performance issues:

      mc = nil
      Benchmark.bm(100) do |bm|
        bm.report("Initialize Mongo connection and get Mongo Cart") do 
          mc = MongoCart.where(location: "CA", token: "4d8e7b38d7ac73ba68cf6401299b918f").first
        end 
      
        bm.report("Re-fetch Mongo Cart") do 
          mc = MongoCart.where(location: "CA", token: "4d8e7b38d7ac73ba68cf6401299b918f").first
        end 
      
        bm.report("Reload Mongo Cart") do 
          mc.reload
        end
      
        bm.report("Get Mongo Cart by Token") do 
          mc = MongoCart.find_by(token: "4d8e7b38d7ac73ba68cf6401299b918f")
        end
      
        bm.report("Get Mongo Cart by ID") do 
          mc = MongoCart.find(mc._id)
        end
      
        bm.report("Get Mongo Cart by Location + ID") do 
          mc = MongoCart.where(location: "CA", _id: mc._id).first
        end
      end
      

      And the resulting benchmarks are attached.

      Can there be a way to have the reload mechanism include shard key fields to improve its performance?

            Assignee:
            emily.giurleo@mongodb.com Emily Giurleo (Inactive)
            Reporter:
            schuyler@jewlr.com Schuyler Jager
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: