0

I am using MongoDB 4.0 as 3 nodes replica-set. 2 data nodes and 1 arbiter. My application (ASP.NET Core 2.2) uses change stream. Consider this scenario, my primary node goes down, my secondary node becomes primary and I replace in my replica-set the node that is down with a new node and it starts to be synced. What will happen to my change stream in this case?

And my second question is, in my application, readPreference is secondary and when my secondary node goes down I expect MongoDB to automatically read from primary but it didn't happen and my application had issue in read. Is it normal?

Mohammad Taherian
  • 1,622
  • 1
  • 15
  • 34

1 Answers1

0

Changestreams are resilient against replica set elections and will continue as if nothing happened. From the changestream manual:

The change stream cursor remains open until one of the following occurs:

  • The cursor is explicitly closed.
  • An invalidate event occurs; for example, a collection drop or rename.
  • The connection to the MongoDB deployment is closed.
  • If the deployment is a sharded cluster, a shard removal may cause an open change stream cursor to close, and the closed change stream cursor may not be fully resumable.

All other events like replica set election will not affect the changestream.

For your second question, this is expected since your read preference is explicitly secondary. To read from the primary if the secondary is not available, you want to use the secondaryPreferred setting.

kevinadi
  • 13,365
  • 3
  • 33
  • 49
  • Thank you for your help. But I had an issue, when I added a new node, during syncing new node with primary, when new node was in `STARTUP2` state, my change stream didn't work until it became `secondary`. – Mohammad Taherian Jan 08 '20 at 02:48
  • Changestreams will only return documents that are persisted by the majority of the replica set. Since you have a primary-secondary-arbiter, if the secondary hasn't replicated the write yet, it won't be returned by the changestream. Once it has a secondary status, it can replica the write, and the changestream can return the document. – kevinadi Jan 08 '20 at 03:18