mongodbspring-webfluxchangestreamspring-data-mongodb-reactive

Sping MongoDB Reactive - how many change streams is it recommended to have in an application?


I try to study the topic of Spring Reactive and MongoDB change streams.

And i run with quite a lot of change streams as i develop my application (up to 10 so far - but still) - so far so good, but i believe when being used by multiple users it can affect memory usage/performance or anything.

Can you, please, advise me on the following items:

  1. I have websockets in the set-up and i am assuming that if the WS session is completed - then the corresponding change stream is garbage collected - is that right?

  2. Is there any particular recommended way to centrally manage change streams for different collections?

  3. And what could be maximum and preferred number for change streams in general?


Solution

  • I would recommend to create as few change streams as possible, because they can cause a lot of query-load on your database.

    I don't know your specific use case, but I would recommend a maximum of one change stream per collection.

    All of your subscribers should subscribe to the same change stream.

    If subscribers require different subsets of changes, it would be their responsibility to filter the stream to their specific needs.