Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Robust MongoDB Change Stream implementation
Woovi profile imageSibelius Seraphini
Sibelius Seraphini forWoovi

Posted on • Edited on

     

Robust MongoDB Change Stream implementation

Change streams allow applications to access real-time data changes without the prior complexity and risk of manually tailing theoplog.

Change streams enable you to update other data sources like a search index in elastic search, or publish events to subscribers.

A naive implementation would listen to some collections, but won't handle the case your service that is listening crashes or has some downtime when doing a rollout deployment.

This article provides a more robust solution that will avoid making you lose any change stream event, even when your service crashes or get some downtime.

Robust Change Streams using a Resume Token

conststream=model.watch([],{fullDocument:'updateLookup',});stream.on('change',(data)=>{})
Enter fullscreen modeExit fullscreen mode

A stream will emit a ChangeStreamData for each data change in your collection.

For each data change it will also provide aresumeToken, that will let you resume the processing from this point in time.

AresumeToken is like a cursor in cursor-based pagination.

The implementation to use a resume token is easy as that:

constchangeStreamListen=async<Textendsany>(model:Model<T>,fn:(data:ChangeStreamData<T>)=>void,)=>{constname=model.collection.name;constkey=`resumetoken:${name}`;constresumeToken=awaitRedisCache.get(key);constgetResumeOptions=()=>{if(resumeToken){return{resumeAfter:resumeToken,};}return{};};conststream=model.watch([],{fullDocument:'updateLookup',...getResumeOptions(),});stream.on('change',changeStreamMiddleware(name,fn)).on('error',(err)=>{// eslint-disable-next-lineconsole.log('change error:',err);Sentry.setExtra('error',err);Sentry.captureException(err);});};
Enter fullscreen modeExit fullscreen mode

This implementation saves the resume token for a given collection in Redis, and tries to resume from it if available.

In Summary

Change Streams can simplify a lot your software architecture.
Providing a robust implementation of it is important to make sure you won't lose any data change.

References


Woovi
Woovi is a Startup that enables shoppers to pay as they like. Woovi provides instant payment solutions for merchants to accept orders to make this possible.

If you want to work with us, we arehiring!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

More fromWoovi

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp