Friday, October 17th, 2008

A Million-user Comet Application with Mochiweb

Category: Comet

Richard Jones has started an interesting series to talk about scaling comet web applications using MochiWeb:

In this series I will detail what I found out empirically about how mochiweb performs with lots of open connections, and show how to build a comet application using mochiweb, where each mochiweb connection is registered with a router which dispatches messages to various users. We end up with a working application that can cope with a million concurrent connections, and crucially, knowing how much RAM we need to make it work.

In part one:

  • Build a basic comet mochiweb app that sends clients a message every 10 seconds.
  • Tune the Linux kernel to handle lots of TCP connections
  • Build a flood-testing tool to open lots of connections (ye olde C10k test)
  • Examine how much memory this requires per connection.

Future posts in this series will cover how to build a real message routing system, additional tricks to reduce memory usage, and more testing with 100k and 1m concurrent connections.

Richard walks through the code and setup, step by step and measures the scalable performance, leading too:

That was a reasonable first attempt. 45KB per-connection seems a bit high – I could probably cook something up in C using libevent that could do this with closer to 4.5KB per connection (just a guess, if anyone has experience please leave a comment). If you factor in the amount of code and time it took to do this in Erlang compared with C, I think the increased memory usage is more excusable.

In future posts I’ll cover building a message router (so we can uncomment lines 25 and 41-43 in mochiconntest_web.erl) and talk about some ways to reduce the overall memory usage. I’ll also share the results of testing with 100k and 1M connections.

Posted by Dion Almaer at 6:26 am
3 Comments

++---
2.4 rating from 25 votes

3 Comments »

Comments feed TrackBack URI

Hi,

can we have the link to the article? :)

Best regards,

Comment by melo — October 17, 2008

The article is at: http://www.metabrew.com/article/a-million-user-comet-application-with-mochiweb-part-1/

Comment by credmp — October 17, 2008

“The resident size of the mochiweb beam process with 10,000 active connections was 450MB – that’s 45KB per connection. CPU utilization on the machine was practically nothing, as expected.”
CPU utilization – of course, but 10k is not what the title says. It says million.
At 1 million users, 1 message per 10 seconds per user, that’s 100k messages per second. So unless we are broadcasting the same message to a large portion of our clients, and we use a SQL database for each unique message, it has to be some badass SQL cluster to handle the job. Actually at that rate, even without SQL, only constructing the response in any meaningful way would probably toast the CPU for good.
But for things like replacing a static comments page (digg, slashdot) with a continuously updated discussion feed, this is a good start.

Comment by ypctx — October 18, 2008

Leave a comment

You must be logged in to post a comment.