What is the Meteor concurrency model?

concurrency, fibers, javascript, meteor, multithreading

Solution

Alright, I looked through the Meteor source and here's how things work:

1) On the server-side, Meteor exclusively uses fibers to handle concurrency. Fibers are like threads, except that context must be explicitly yielded. This makes reasoning about concurrency easier, at the (potential) cost of some fibers starving others.

2) All calls to Meteor.call, Meteor.setInterval, and any Collection operations are wrapped in fibers. This means that all of these calls yield context.

3) In addition, any use of the fibers/futures module yields.

The upshot of this structure is that if you want to write atomic operations, just avoid accessing the objects provided by Meteor framework in the block of code that you want to make atomic. If this block really needs (say) a DB access, then you can implement in-memory locks without trouble, but for my application, this knowledge is enough. My core update function just needs to be called with all of the documents it needs from Mongo already read.

Problem

I'm writing server-side logic for a Meteor app that has to update in-memory state in response to requests from the client. This application needs strong concurrency guarantees - in particular, I want to be sure that there is only one update executed at a time. I'm trying to figure out if Meteor's concurrency model supports this. The documentation mentions that Meteor is multithreaded (which would be a problem), but after searching around, I get the impression that Meteor is actually uses fibers (explicitly scheduled threads). If that's true, then I'm safe as long as the part of my code that needs to run atomically doesn't make any Meteor calls (which involve IO and thus yield the execution lock). Is this the case? Where can I find more information on Meteor's concurrency model?

Original source