Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoServerError: WriteConflict error on MongoDB Atlas #4583

Closed
tyteen4a03 opened this issue Dec 21, 2023 · 6 comments
Closed

MongoServerError: WriteConflict error on MongoDB Atlas #4583

tyteen4a03 opened this issue Dec 21, 2023 · 6 comments
Assignees
Labels
db-mongodb @payloadcms/db-mongodb status: needs-repro If an issue does not include a reproduction status: needs-triage Possible bug which hasn't been reproduced yet

Comments

@tyteen4a03
Copy link
Contributor

Link to reproduction

No response

Describe the Bug

When using MongoDB Atlas, sometimes writes fail with "WriteConflict" error.

To Reproduce

See: https://discord.com/channels/967097582721572934/1182227009603117116/1182227009603117116

Payload Version

2.5.0

Adapters and Plugins

No response

@tyteen4a03 tyteen4a03 added the status: needs-triage Possible bug which hasn't been reproduced yet label Dec 21, 2023
@tyteen4a03
Copy link
Contributor Author

Note: I get this issue most frequently when I am doing a lot of writes at the same time.

@tyteen4a03 tyteen4a03 changed the title MongoServerError: WriteConflict error MongoServerError: WriteConflict error on MongoDB Atlas Dec 21, 2023
@AlessioGr AlessioGr added the db-mongodb @payloadcms/db-mongodb label Dec 26, 2023
@DanRibbens DanRibbens self-assigned this Jan 2, 2024
@AlessioGr AlessioGr added the status: needs-repro If an issue does not include a reproduction label Jan 11, 2024
@PrimarchAlpharius
Copy link

I had the same problem doing an update in the afterLogin hook.
It was persistently throwing a WriteConflict, even though it was the only place I was explicity writing to the database.

Not running Atlas, but a self-hosted replica set.
Considering I had no issues in my development environment, I found in the docs that Payload switches to Mongo Transactions by default when connected to a replica set.

After disabling transactions in payload.config.ts with transactionOptions: false everything started working fine.

@DanRibbens
Copy link
Contributor

DanRibbens commented Jan 29, 2024

@PrimarchAlpharius Can you confirm that the latest update to the db-mongodb adapter fixes your issue if you reenable transactions?

@PrimarchAlpharius
Copy link

@DanRibbens Confirming it works, thanks!

@DanRibbens
Copy link
Contributor

Note: I get this issue most frequently when I am doing a lot of writes at the same time.

@tyteen4a03 Are you still running into this on the latest version of Payload and db-mongodb?

If you are using hooks that use the Payload local API, make sure you pass the req through to any payload local API calls so that the transaction is used. If you call the database models directly, you can also pass the session in the options as: payload.db.collections[slug].findOneAndUpdate(query, data, { session: payload.db.sessions?.[req.transactionID] } })

I hope this helps! I'm closing this issue, but I will re-open if somebody can provide steps for reproduction and tag me so I see it.

@BrianJM
Copy link

BrianJM commented Feb 22, 2024

Mark suggested disabling transactions in discord.

Could be related to #4350.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
db-mongodb @payloadcms/db-mongodb status: needs-repro If an issue does not include a reproduction status: needs-triage Possible bug which hasn't been reproduced yet
Projects
None yet
Development

No branches or pull requests

5 participants