Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup a very fast node.js UDP server

I'm quite new to Node.js and I have a request for an application that would receive a payload of UDP packets and process it.

I'm talking about more than 400 messages per second, which would reach something like 200.000 messages/minute.

I have written a code to setup a UDP server (grabbed from docs here http://nodejs.org/api/all.html#all_udp_datagram_sockets actually) but it's losing something around 5% of the packets.

What I really need to develop is a server which would get the packet and send it to another worker do the job with the message. But looks like threading in node.js is a nightmare.

This is my core as is:

var dgram = require("dgram");
var fs = require("fs");
var stream = fs.createWriteStream("received.json",{ flags: 'w',
  encoding: "utf8",
  mode: 0666 });

var server = dgram.createSocket("udp4");
server.on("message", function (msg, rinfo) {
  console.log("server got: " + msg + " from " +
    rinfo.address + ":" + rinfo.port);
    stream.write(msg);
});

server.on("listening", function () {
  var address = server.address();
  console.log("server listening " +
      address.address + ":" + address.port);
});

server.bind(41234);
// server listening 0.0.0.0:41234
like image 217
Panthro Avatar asked Apr 19 '12 16:04

Panthro


1 Answers

You are missing concepts, NodeJS is not meant to be multi-thread in terms of you mean, requests should be handled in a cycle. No other thread exists so no context-switches happens. In a multi-core environment, you can create a cluster via node's cluster module, I have a blog-post about this here.

You set the parent proceses to fork child processes, and ONLY child processes should bind to a port. Your parent proceses will handle the load balancing between children.

Note: In my blog post, I made i < os.cpus().length / 2; but it should be i < os.cpus().length;

like image 189
Mustafa Avatar answered Oct 15 '22 00:10

Mustafa