Search code examples
mongodbspring-bootredischangestream

How do change streams work in mongo across multiple instances?


Let's assume I have two data centers each with 3 instances running the same code connected to a single MongoDB and separate Redis databases.

Redis A <----> Data Center A
                            |
                            |--------> MongoDB
                            |
Redis B <-----> Data Center B

The expectation is that when a certain collection in Mongo is updated, a change stream in both Data Center A and B will be triggered to clear the redis cache. Will it work with change streams?


Solution

  • When you have a changestream code running on n nodes/instances each instance will receive the change event and based on that event & data from changestream you can do your businesslogic/redis cleanup.

    1. Let say you have changestream listener(CS1) running on Data Center A and this data center has redis server A which you want to clean up on certain condition from DB change event.
    2. Let say you have another changestream listener(CS2) running on Data Center B and this data center has redis server B which you want to clean up on certain condition from DB change event.
    3. Both changestream listeners(CS1 & CS2) listening to same DB and same collection.
    4. When any change happens on the collection the change event will be received by both changestream listeners(CS1 & CS2)
    5. CS1 will have logic to cleanup redis A & CS2 will have logic to cleanup redis B so when change event comes and condition met your both redis server will be cleaned.