title: Is there a way to share memory among workers/threads/something in Node.JS?
body: <p>I have a Node app which accesses a static, large (>100M), complex, in-memory data structure, accepts queries, and then serves out little slices of that data to the client over HTTP.</p>
  
  <p>Most queries can be answered in tenths of a second. Hurray for Node!</p>
  
  <p>But, for certain queries, searching this data structure takes a few seconds. This sucks because everyone else has to wait.</p>
  
  <p>To serve more clients efficiently, I would like to use some sort of parallelism. </p>
  
  <p>But, because this data structure is so large, I would like to share it among the workers or threads or what have you, so I don't burn hundreds of megabytes. This would be perfectly safe, because the data structure is not going to be written to. A typical 'fork()' in any other language would do it.</p>
  
  <p>However, as far as I can tell, all the standard ways of doing parallelism in Node explicitly make this impossible. For safety, they don't want you to share anything.</p>
  
  <p>But is there a way?</p>
  
  <p>Background:</p>
  
  <p>It is impractical to put this data structure in a database, or use memcached, or anything like that.</p>
  
  <p>WebWorker API libraries and similar only allow short serialized messages to be passed in and out of the workers.</p>
  
  <p>Node's Cluster uses a call named 'fork', but it is not really a fork of the existing process, it is spawning a new one. So once again, no shared memory.</p>
  
  <p>Probably the really correct answer would be to use filesystem-like access to shared memory, aka tmpfs, or mmap. There are some node libraries that make mount() and mmap() available for exactly something like this. Unfortunately then one has to implement complex data structure access on top of synchronous seeks and reads. My application uses arrays of arrays of dicts and so on. It would be nice to not have to reimplement all that.</p>