Catfish
Catfish

Reputation: 19284

Mongodb tailable cursor - Isn't that bad practice to have a continually spinning loop?

I'm looking into the best way to have my app get notified when a collection is updated in mongo. From everything I read on the interwebs, the standard practice is to use a capped collection with a tailable cursor and here's a snippet from mongodb's docs on tailable cursors.

I notice in there snippet that they have a continuous while loop that never stops. Doesn't this seem like a bad practice? I can't imagine this would perform well when scaling.

Does anyone have any insight as to how this could possibly scale and still be performant? Is there something i'm not understanding?

EDIT

So this is a good example where i see the stream is just open and since the stream is never closed, it just has a listener listening. That makes sense to me i guess.

I'm also looking at this mubsub implementation where they use a setTimeout with 1 second pause.

Aren't these typically bad practices - to leave a stream open or to use a setTimeout like that? Am i just being old school?

Upvotes: 1

Views: 542

Answers (1)

mnemosyn
mnemosyn

Reputation: 46291

I notice in there snippet that they have a continuous while loop that never stops. Doesn't this seem like a bad practice?

Looks like it to me as well, yes.

Does anyone have any insight as to how this could possibly scale and still be performant?

You can set the AwaitData flag and make the more() call blocking for some time - it won't block until data is available though, but it will block for some time. Requires server support (from v. 1.6 or so) That is also what's being done in the node.js example you posted ({awaitdata:true}).

where they use a setTimeout with 1 second pause.

The way I read it, they retry to get the cursor back when lost in regular intervals and return an error iff that failed for a full second.

Aren't these typically bad practices - to leave a stream open [...]?

You're not forgetting the stream (that would be bad), you keep using it - that's pretty much the definition of a tailable cursor.

Upvotes: 2

Related Questions