According to the docs, change streams are also a subject to read preferences when the primary is down.
Let’s say I opened my connection using primaryPreferred.
- I opened the change stream using the connection to the primary
- Then there was a server restart due to maintenance window (as mentioned here for example of the free tier)
- Since I chose primaryPreferred in my connection string, it will try to reestablish the connection to secondary that meet my read preferences criteria (like maxStalenessSeconds).
- Assuming it found one, it connects to it and receiving events again.
So far I believe I understood it correctly, if not please do tell
Now for my question - what happens when the primary is BACK online?
For read operations the primaryPreferred option says:
“When the primaryPreferred read preference includes a maxStalenessSeconds value and there is no primary from which to read, the client estimates how stale each secondary is by comparing the secondary’s last write to that of the secondary with the most recent write. The client then directs the read operation to a secondary…”
So for regular read operations it sounds like when the primary is back the operations will be directed to it again.
But how about a change stream? Will it be redirected again to the primary? Or keep working vs the secondary which has a delay in data (based on the value of maxStalenessSeconds) ?
Since a change stream opens a connection individually with the DB, I suspect it will NOT start working again with the primary and will continue working with the secondary?
- What is actually happening in the situation I described? (and where is the official documentation about it for change streams? Couldn’t find it)
- If it continues working with the secondary, is there a way for me to force it to go back to the primary?
(I’m working with the mongodb package in Node.js if it helps)