4

I am trying to use insertMany() to put 50 documents in a collection. On the local emulator this works fine. When I try the same code in Azure with a CosmosDB I get the following error:

2019-12-05T00:12:51.320 [Information] Connected to Cosmsos DB
2019-12-05T00:12:51.576 [Error] failed to create users, err: BulkWriteError: Error=16500, RetryAfterMs=72, Details='
2019-12-05T00:12:51.320 [Information] Connected to Cosmsos DB
2019-12-05T00:12

Here is the connection mechanism, schema, and the code that populates the collection initially.

const personSchema = new mongoose.Schema({
    firstName: String,
    lastName: String,
    addressNumber: Number,
    streetName: String,
    city: String,
    email: String
})

async open() {
    let host = this.conn
    this.log.info(`Host: ${JSON.stringify(host)}`)
    const url = `mongodb://${host.host}:${host.port}/${host.db}?ssl=true&replicaSet=globaldb&retryWrites=false`
    const options = {
        auth: {
            user: host.user,
            password: host.password
        }
    }
    try {
        await mongoose.connect(url, options)
        this.log.info('Connected to Cosmsos DB')
        this.db = mongoose
    } catch (err) {
        this.log.error('Unable to connect to DB', err)
    }
    this.users = mongoose.model('person', personSchema)
}

async populate(logger) {
    await this.connect(logger)
    try {
        await this.users.deleteMany({})
    } catch (err) {
        this.log.error(`cannot empty collection: ${err}`)
    }
    const uList = userData.map(u => this.users(u))
    try {
        const result = await this.users.collection.insertMany(uList)
        this.log.info(`users created: ${result}`)
    } catch (err) {
        this.log.error(`failed to create users, err: ${err}`)
    } finally {
        await this.close()
    }
}
1

1 Answer 1

6

The error you're getting, 16500, is "Too Many Requests." TL;DR you've hit your RU limit and are being throttled. Each insert is costing some level of RU, and your per-second RU rate isn't set high enough to handle a rapid-insert scenario like what you've set up.

You can work around this by either increasing your RU capacity or reducing the number of items you're trying to insert at the same time.

FYI I just recreated this error condition by calling db.collection.insertMany() from the mongo shell with a large array of documents (in this case, about 150 small documents), combined with a very low RU count (400 in my case).

Sign up to request clarification or add additional context in comments.

2 Comments

Would bulk insert help? Not quite clear how it is invoked. I have 50 tiny documents. I thought 400 RU would be plenty.
The documentation seems to indicate that in 3.6, as long as one uses an array. insertMany is the same as bulk writes. I guess increasing RU is the only way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.