What are the consequences of using autoCreate and autoIndex? #5545
-
I'm using Payload CMS with Amazon AWS DocumentDB, as I've described in an answer to another discussion #652 (comment). I noticed, however, that only the primary is receiving traffic, while the replica is sitting idle. I used the mongodb://USER:[email protected]:27017/payload
?tls=true
&tlsCAFile=eu-central-1-bundle.pem
&tlsInsecure=true
&directConnection=true
&retryWrites=false
+ &readPreference=secondaryPreferred Once I did that, I started receiving the following error:
Then I tried adding the aforementioned mongodb://USER:[email protected]:27017/payload
?tls=true
&tlsCAFile=eu-central-1-bundle.pem
&tlsInsecure=true
&directConnection=true
&readPreference=secondaryPreferred
&retryWrites=false
+ &autoCreate=true
+ &autoIndex=true …but I got the exact same error. Then, in the Mongoose types in export default buildConfig({
// ...
db: mongooseAdapter({
// Fixes https://github.com/payloadcms/payload/issues/3947
disableIndexHints: true,
connectOptions: {
useFacet: true,
autoCreate: false,
autoIndex: false,
},
url: process.env.MONGODB_URI,
}),
// ...
}); Now I started to receive this error:
It appears that only now the connection string options I had added earlier had started to make a difference, so I removed them: mongodb://USER:[email protected]:27017/payload
?tls=true
&tlsCAFile=eu-central-1-bundle.pem
&tlsInsecure=true
&directConnection=true
&readPreference=secondaryPreferred
&retryWrites=false
- &autoCreate=true
- &autoIndex=true Now the connection was successful and Payload started as usual. At the end, the solution was to:
But my question is, what are the downsides to using those settings, in the context of Payload? Everything seems to work now, but I don't want to get surprises on production… |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
I decided to go with this on our staging server and hit the following error:
So apparently, Payload can't work with replica instances. Is that right? @DanRibbens could you confirm? |
Beta Was this translation helpful? Give feedback.
-
Hey @hdodov, I appreciate the detailed explanation on the steps you took and problems you were facing. I'll share my knowledge and hopefully shed some light.
I haven't tested this but I think there is a difference in how documentDB and mongoDB replicasets are handling traffic. I agree with you on setting the readPreference. You could also experiment with the
Did you experiment with that?
If you have autoIndex: false, Payload, calling Mongoose, will not create the indexes that Payload normally makes for you, including the fields you have set in your config. When using this setting you will want to create your indexes inside migrations that can be run once, rather than anytime Payload starts. This is actually a better pattern than the default behavior of having autoIndex: true on in production, but it introduces more complexity in your deployments. I have a bigger concern with You could make a separate DB connection with whatever settings are needed from your migrations if you do continue on this path. Does this makes sense? Let us know what you find! |
Beta Was this translation helpful? Give feedback.
-
I'm trying to get a similar read-replica setup done on our end but I am running into a similar problem.
@DanRibbens, not sure if the above works. I believe
Unfortunately, this seems prone to error with updates and is not scalable. Is there any other workaround for achieving this? |
Beta Was this translation helpful? Give feedback.
Hey @hdodov,
I appreciate the detailed explanation on the steps you took and problems you were facing. I'll share my knowledge and hopefully shed some light.
I haven't tested this but I think there is a difference in how documentDB and mongoDB replicasets are handling traffic. I agree with you on setting the readPreference. You could also experiment with the
writeConcern
options. I would think you could further distribute the load and remove the disabling of autoCreate/autoIndex though I have not confirmed this.Did you experiment with that?