A Million-user Comet Application with Mochiweb, Part 2

In Part 1, we built a (somewhat useless) mochiweb comet application that sent clients a message every 10 seconds. We tuned the Linux kernel, and built a tool to establish a lot of connections in order to test performance and memory usage. We found that it took around 45KB per connection.

Part 2 is about turning the application into something useful, and saving memory:

  • Implement a message router with a login/logout/send API
  • Update the mochiweb app to receive messages from the router
  • Setup a distributed erlang system so we can run the router on a different node/host to mochiweb
  • Write a tool to spam the router with lots of messages
  • Graph memory usage over 24hrs, and optimise the mochiweb app to save memory.

This means we are decoupling the message sending logic from the mochiweb app. In tandem with the floodtest tool from part 1, we can benchmark a setup closer to a production scenario.

Implementing the message router

The router API is just 3 functions:

  • login(Id, Pid) register a process (of pid Pid) to receive messages for Id
  • logout(Pid) to stop receiving messages
  • send(Id, Msg) sends the message Msg to any client logged in as Id

Note that, by design, it is possible for one process to login with multiple different Ids.

This example router module uses 2 ets tables to store bidirectional mappings between Pids and Ids. (pid2id and id2pid in the #state record below.)

router.erl:

  1. -module(router).
  2. -behaviour(gen_server).
  3.  
  4. -export([start_link/0]).
  5. -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
  6.      terminate/2, code_change/3]).
  7.  
  8. -export([send/2, login/2, logout/1]).
  9.  
  10. -define(SERVER, global:whereis_name(?MODULE)).
  11.  
  12. % will hold bidirectional mapping between id <–> pid
  13. -record(state, {pid2id, id2pid}).
  14.  
  15. start_link() ->
  16.     gen_server:start_link({global, ?MODULE}, ?MODULE, [], []).
  17.  
  18. % sends Msg to anyone logged in as Id
  19. send(Id, Msg) ->
  20.     gen_server:call(?SERVER, {send, Id, Msg}).
  21.  
  22. login(Id, Pid) when is_pid(Pid) ->
  23.     gen_server:call(?SERVER, {login, Id, Pid}).
  24.  
  25. logout(Pid) when is_pid(Pid) ->
  26.     gen_server:call(?SERVER, {logout, Pid}).
  27.  
  28. %%
  29.  
  30. init([]) ->
  31.     % set this so we can catch death of logged in pids:
  32.     process_flag(trap_exit, true),
  33.     % use ets for routing tables
  34.     {ok, #state{
  35.                 pid2id = ets:new(?MODULE, [bag]),
  36.                 id2pid = ets:new(?MODULE, [bag])
  37.                }
  38.     }.
  39.  
  40. handle_call({login, Id, Pid}, _From, State) when is_pid(Pid) ->
  41.     ets:insert(State#state.pid2id, {Pid, Id}),
  42.     ets:insert(State#state.id2pid, {Id, Pid}),
  43.     link(Pid), % tell us if they exit, so we can log them out
  44.     io:format("~w logged in as ~w\n",[Pid, Id]),
  45.     {reply, ok, State};
  46.  
  47. handle_call({logout, Pid}, _From, State) when is_pid(Pid) ->
  48.     unlink(Pid),
  49.     PidRows = ets:lookup(State#state.pid2id, Pid),
  50.     case PidRows of
  51.         [] ->
  52.             ok;
  53.         _ ->
  54.             IdRows = [ {I,P} || {P,I} <- PidRows ], % invert tuples
  55.             % delete all pid->id entries
  56.             ets:delete(State#state.pid2id, Pid),
  57.             % and all id->pid
  58.             [ ets:delete_object(State#state.id2pid, Obj) || Obj <- IdRows ]
  59.     end,
  60.     io:format("pid ~w logged out\n",[Pid]),
  61.     {reply, ok, State};
  62.  
  63. handle_call({send, Id, Msg}, _From, State) ->
  64.     % get pids who are logged in as this Id
  65.     Pids = [ P || { _Id, P } <- ets:lookup(State#state.id2pid, Id) ],
  66.     % send Msg to them all
  67.     M = {router_msg, Msg},
  68.     [ Pid ! M || Pid <- Pids ],
  69.     {reply, ok, State}.
  70.  
  71. % handle death and cleanup of logged in processes
  72. handle_info(Info, State) ->
  73.     case Info of
  74.         {‘EXIT’, Pid, _Why} ->
  75.             % force logout:
  76.             handle_call({logout, Pid}, blah, State);
  77.         Wtf ->
  78.             io:format("Caught unhandled message: ~w\n", [Wtf])
  79.     end,
  80.     {noreply, State}.
  81.  
  82. handle_cast(_Msg, State) ->
  83.     {noreply, State}.
  84. terminate(_Reason, _State) ->
  85.     ok.
  86. code_change(_OldVsn, State, _Extra) ->
  87.     {ok, State}.


Updating the mochiweb application

Let’s assume a user is represented by an integer Id based on the URL they connect to mochiweb with, and use that id to register with the message router. Instead of blocking for 10 seconds then sending something, the mochiweb loop will block on receiving messages from the router, and send an HTTP chunk to the client for every message the router sends it:

  • Client connects to mochiweb at http://localhost:8000/test/123
  • Mochiweb app registers the pid for that connection against the id ‘123′ with the message router
  • If you send a message to the router addressed to id ‘123′, it will be relayed to the correct mochiweb process, and appear in the browser for that user

Here’s the updated version of mochiconntest_web.erl:

  1. -module(mochiconntest_web).
  2.  
  3. -export([start/1, stop/0, loop/2]).
  4.  
  5. %% External API
  6.  
  7. start(Options) ->
  8.     {DocRoot, Options1} = get_option(docroot, Options),
  9.     Loop = fun (Req) ->
  10.                    ?MODULE:loop(Req, DocRoot)
  11.            end,
  12.     % we’ll set our maximum to 1 million connections. (default: 2048)
  13.     mochiweb_http:start([{max, 1000000}, {name, ?MODULE}, {loop, Loop} | Options1]).
  14.  
  15. stop() ->
  16.     mochiweb_http:stop(?MODULE).
  17.  
  18. loop(Req, DocRoot) ->
  19.     "/" ++ Path = Req:get(path),
  20.     case Req:get(method) of
  21.         Method when Method =:= ‘GET’; Method =:= ‘HEAD’ ->
  22.             case Path of
  23.                 "test/" ++ Id ->
  24.                     Response = Req:ok({"text/html; charset=utf-8",
  25.                                       [{"Server","Mochiweb-Test"}],
  26.                                       chunked}),
  27.                     % login using an integer rather than a string
  28.                     {IdInt, _} = string:to_integer(Id),
  29.                     router:login(IdInt, self()),
  30.                     feed(Response, IdInt, 1);
  31.                 _ ->
  32.                     Req:not_found()
  33.             end;
  34.         ‘POST’ ->
  35.             case Path of
  36.                 _ ->
  37.                     Req:not_found()
  38.             end;
  39.         _ ->
  40.             Req:respond({501, [], []})
  41.     end.
  42.  
  43. feed(Response, Id, N) ->
  44.     receive
  45.     {router_msg, Msg} ->
  46.         Html = io_lib:format("Recvd msg #~w: ‘~s’", [N, Msg]),
  47.         Response:write_chunk(Html)
  48.     end,
  49.     feed(Response, Id, N+1).
  50.  
  51. %% Internal API
  52.  
  53. get_option(Option, Options) ->
  54.     {proplists:get_value(Option, Options), proplists:delete(Option, Options)}.


It’s Alive!

Now let’s bring it to life – we’ll use 2 erlang shells, one for mochiweb and one for the router. Edit start-dev.sh, used to start mochiweb, and add the following additional parameters to erl:

  • -sname n1 to name the erlang node ‘n1′
  • +K true to enable kernel-poll. Seems daft not to when dealing with lots of connections
  • +P 134217727 the default maximum number of processes you can spawn is 32768. Considering we need one process per connection (and I don’t know of any good reason not to) I suggest just setting this to the maximum possible value. 134,217,727 is the max according to “man erl”.

Now run make && ./start-dev.sh and you should see a prompt like this: (n1@localhost)1> – your mochiweb app is now running and the erlang node has a name.

Now run another erlang shell like so:
erl -sname n2
Currently those two erlang instances don’t know about each other, fix that:
(n2@localhost)1> nodes().
[]
(n2@localhost)2> net_adm:ping(n1@localhost).
pong
(n2@localhost)3> nodes().
[n1@localhost]

Now compile and start the router from this shell:
(n2@localhost)4> c(router).
{ok,router}
(n2@localhost)5> router:start_link().
{ok,<0.38.0>}

Now for the fun bit, go to http://localhost:8000/test/123 in your browser (or use lynx --source "http://localhost:8000/test/123" from the console). Check the shell you launched the router in, you should see it logged in one user.

You can now send messages to the router and watch them appear in your browser. Only send strings for now, because we are using ~s to format them with io_lib:format in the feed function, and atoms will crash it:

Just borrow the shell you used to launch the router:

(n2@localhost)6> router:send(123, "Hello World").
(n2@localhost)7> router:send(123, "Why not open another browser window too?").
(n2@localhost)8> router:send(456, "This message will go into the void unless you are connected as /test/456 too").

Check your browser, you’ve got comet :)

Running in a distributed erlang system

It makes sense to run the router and mochiweb front-end(s) on different machines. Assuming you have a couple of spare machines to test this on, you should start the erlang shells as distributed nodes, i.e. use -name n1@host1.example.com instead of -sname n1 (and the same for n2). Make sure they can see each other by using net_adm:ping(...) as above.

Note that on line 16 of router.erl, the name of the router process (’router’) is registered globally, and that because we are using the following macro to identify/locate the router in calls to gen_server, it will already work fine in a distributed system:

-define(SERVER, global:whereis_name(?MODULE)).

A global name registry for processes in a distributed system is just one of the things you get for free with Erlang.

Generating lots of messages

In a real environment we might see a long-tail like usage pattern, with some very active users and many infrequent users. However for this test we’ll just indiscriminately spam random users with fake messages.

msggen.erl:

  1. -module(msggen).
  2. -export([start/3]).
  3.  
  4. start(0, _, _) -> ok;
  5. start(Num, Interval, Max) ->
  6.     Id = random:uniform(Max),
  7.     router:send(Id, "Fake message Num = " ++ Num),
  8.     receive after Interval -> start(Num -1, Interval, Max) end.



This will send Num messages to random user Ids between 1 and Max, waiting Interval ms between each send.

You can see this in action if you run the router and the mochiweb app, connect with your browser to http://localhost:8000/test/3 then run:

erl -sname test
(test@localhost)1> net_adm:ping(n1@localhost).
pong
(test@localhost)2> c(msggen).
{ok,msggen}
(test@localhost)3> msggen:start(20, 10, 5).
ok

This will send 20 messages to random Ids between 1-5, with a 10ms wait between messages. Chances are Id 3 will receive a message or four.

We can even run a few of these in parallel to simulate multiple sources for messages. Here’s an example of spawning 10 processes that each send 20 messages to ids 1-5 with a 100ms delay between each message:

[ spawn(fun() -> msggen:start(20, 100, 5), io:format("~w finished.\n", [self()]) end) || _ <- lists:seq(1,10) ].
[<0.97.0>,<0.98.0>,<0.99.0>,<0.100.0>,<0.101.0>,<0.102.0>,
<0.103.0>,<0.104.0>,<0.105.0>,<0.106.0>]
<0.101.0> finished.
<0.105.0> finished.
<0.106.0> finished.
<0.104.0> finished.
<0.102.0> finished.
<0.98.0> finished.
<0.99.0> finished.
<0.100.0> finished.
<0.103.0> finished.
<0.97.0> finished.

C10K again, with feeling

We have the pieces we need to run another larger-scale test now; clients connect to our mochiweb app, which registers them with the message router. We can generate a high volume of fake messages to fire at the router, which will send them to any registered clients. Let’s run the 10,000 concurrent-user test again from Part 1, but this time we’ll leave all the clients connected for a while while we blast lots of messages through the system.

Assuming you followed the instructions in Part 1 to tune your kernel and increase your max files ulimit etc, this should be easy. You already have the mochiweb app and router running, so let’s dump more traffic on it.

Without any clients connected, the mochiweb beam process uses around 40MB (resident):

$ ps -o rss= -p `pgrep -f 'sname n1'`
40156

This greps for the process ID of the command with ’sname n1′ in it, which is our mochiweb erlang process, then uses some formatting options to ps to print the RSS value – the resident memory size (KB)

I concocted this hideous one-liner to print the timestamp (human readable and a unixtime in case we need it later), current memory usage of mochiweb (resident KB), and the number of currently established connections every 60 seconds – leave this running on the mochiweb machine in a spare terminal:

$ MOCHIPID=`pgrep -f 'name n1'`; while [ 1 ] ; do NUMCON=`netstat -n | awk '/ESTABLISHED/ && $4=="127.0.0.1:8000"' | wc -l`; MEM=`ps -o rss= -p $MOCHIPID`; echo -e "`date`\t`date +%s`\t$MEM\t$NUMCON"; sleep 60; done | tee -a mochimem.log

If anyone knows a better way to plot memory usage for a single process over time please leave a comment..

Now launch the floodtest tool from Part 1 in a new erl shell:
erl> floodtest:start("/tmp/mochi-urls.txt", 10).

This will establish 100 new connections per second until all 10,000 clients are connected.
You’ll see it quickly reaches 10k connections:
erl> floodtest:start("/tmp/mochi-urls.txt", 10).
Stats: {825,0,0}
Stats: {1629,0,0}
Stats: {2397,0,0}
Stats: {3218,0,0}
Stats: {4057,0,0}
Stats: {4837,0,0}
Stats: {5565,0,0}
Stats: {6295,0,0}
Stats: {7022,0,0}
Stats: {7727,0,0}
Stats: {8415,0,0}
Stats: {9116,0,0}
Stats: {9792,0,0}
Stats: {10000,0,0}
...

Check the hideous memory usage one-liner output:
Mon Oct 20 16:57:24 BST 2008 1224518244 40388 1
Mon Oct 20 16:58:25 BST 2008 1224518305 41120 263
Mon Oct 20 16:59:27 BST 2008 1224518367 65252 5267
Mon Oct 20 17:00:32 BST 2008 1224518432 89008 9836
Mon Oct 20 17:01:37 BST 2008 1224518497 90748 10001
Mon Oct 20 17:02:41 BST 2008 1224518561 90964 10001
Mon Oct 20 17:03:46 BST 2008 1224518626 90964 10001
Mon Oct 20 17:04:51 BST 2008 1224518691 90964 10001

It reached 10k concurrent connections (plus one I had open in firefox) and the resident memory size of mochiweb is around 90MB (90964KB).

Now unleash some messages:

erl> [ spawn(fun() -> msggen:start(1000000, 100, 10000) end) || _ <- lists:seq(1,100) ].
[<0.65.0>,<0.66.0>,<0.67.0>,<0.68.0>,<0.69.0>,<0.70.0>,
<0.71.0>,<0.72.0>,<0.73.0>,<0.74.0>,<0.75.0>,<0.76.0>,
<0.77.0>,<0.78.0>,<0.79.0>,<0.80.0>,<0.81.0>,<0.82.0>,
<0.83.0>,<0.84.0>,<0.85.0>,<0.86.0>,<0.87.0>,<0.88.0>,
<0.89.0>,<0.90.0>,<0.91.0>,<0.92.0>,<0.93.0>|...]

That’s 100 processes each sending a million messages at a rate of 10 messages a second to random Ids from 1 to 10,000. That means the router is seeing 1000 messages per second, and on average each of our 10k clients will get one message every 10 seconds.

Check the output in the floodtest shell, and you’ll see clients are receiving http chunks (remember it was {NumConnected, NumClosed, NumChunksRecvd}):
...
Stats: {10000,0,5912}
Stats: {10000,0,15496}
Stats: {10000,0,25145}
Stats: {10000,0,34755}
Stats: {10000,0,44342}
...

A million messages at a rate of 10 per second per process will take 27 hours to complete. Here’s how the memory usage looks after just 10 mins:
Mon Oct 20 16:57:24 BST 2008 1224518244 40388 1
Mon Oct 20 16:58:25 BST 2008 1224518305 41120 263
Mon Oct 20 16:59:27 BST 2008 1224518367 65252 5267
Mon Oct 20 17:00:32 BST 2008 1224518432 89008 9836
Mon Oct 20 17:01:37 BST 2008 1224518497 90748 10001
Mon Oct 20 17:02:41 BST 2008 1224518561 90964 10001
Mon Oct 20 17:03:46 BST 2008 1224518626 90964 10001
Mon Oct 20 17:04:51 BST 2008 1224518691 90964 10001
Mon Oct 20 17:05:55 BST 2008 1224518755 90980 10001
Mon Oct 20 17:07:00 BST 2008 1224518820 91120 10001
Mon Oct 20 17:08:05 BST 2008 1224518885 98664 10001
Mon Oct 20 17:09:10 BST 2008 1224518950 106752 10001
Mon Oct 20 17:10:15 BST 2008 1224519015 114044 10001
Mon Oct 20 17:11:20 BST 2008 1224519080 119468 10001
Mon Oct 20 17:12:25 BST 2008 1224519145 125360 10001

You can see the size already crept up from 40MB to 90MB when all 10k clients were connected, and to 125MB after running a bit longer.

It’s worth pointing out that the floodtest shell is almost CPU-bound, the msggen shell is using 2% CPU and the router and mochiweb less than 1%. (ie, only simulating lots of clients is using much CPU – the server app itself is very light on the CPU). It helps to have multiple machines, or a multicore CPU for testing.

Results after running for 24 hours

I ran this for 24 hours, whilst logging memory usage of the mochiweb process to mochimem.log. This is with 10,000 connected clients, and 1000 messages per second being sent to random clients.

The following bit of bash/awk was used to trick gnuplot into turning the mochimem.log file into a graph:

(echo -e "set terminal png size 500,300\nset xlabel \"Minutes Elapsed\"\nset ylabel \"Mem (KB)\"\nset title \"Mem usage with 10k active connections, 1000 msg/sec\"\nplot \"-\" using 1:2 with lines notitle" ; awk 'BEGIN{FS="\t";} NR%10==0 {if(!t){t=$2} mins=($2-t)/60; printf("%d %d\n",mins,$3)}' mochimem.log ; echo -e "end" ) | gnuplot > mochimem.png

Graph of memory usage with c10k, 1000msg/sec, 24hrs

Memory usage with c10k, 1000msg/sec, 24hrs

This graph shows the memory usage (with 10k active connections and 1000 msgs/sec) levels off at around 250MB over a 24 hour period. The two big drops, once near the start and once at the end of the test, are when I ran this in the mochiweb erlang process, just out of curiosity:

erl> [erlang:garbage_collect(P) || P <- erlang:processes()].

This forces all processes to garbage collect, and reclaimed around 100MB of memory – next up we investigate ways to save memory without resorting to manually forcing garbage collection.

Reducing memory usage in mochiweb

Seeing as the mochiweb app is just sending messages and then immediately forgetting them, the memory usage shouldn’t need to increase with the number of messages sent.

I’m a novice when it comes to Erlang memory management, but I’m going to assume that if I can force it to garbage collect more often, it will allow us to reclaim much of that memory, and ultimately let us serve more users with less overall system memory. We might burn a bit more CPU in the process, but that’s an acceptable trade-off.

Digging around in the erlang docs yields this option:

erlang:system_flag(fullsweep_after, Number)

Number is a non-negative integer which indicates how many times generational garbages collections can be done without forcing a fullsweep collection. The value applies to new processes; processes already running are not affected.
In low-memory systems (especially without virtual memory), setting the value to 0 can help to conserve memory.
An alternative way to set this value is through the (operating system) environment variable ERL_FULLSWEEP_AFTER.

Sounds intriguing, but it only applies to new processes and would affect all processes in the VM, not just our mochiweb processes.

Next up is this:

erlang:system_flag(min_heap_size, MinHeapSize)

Sets the default minimum heap size for processes. The size is given in words. The new min_heap_size only effects processes spawned after the change of min_heap_size has been made. The min_heap_size can be set for individual processes by use of spawn_opt/N or process_flag/2.

Could be useful, but I’m pretty sure our mochiweb processes need a bigger heap than the default value anyway. I’d like to avoid needing to patch the mochiweb source to add spawn options if possible.

Next to catch my eye was this:

erlang:hibernate(Module, Function, Args)

Puts the calling process into a wait state where its memory allocation has been reduced as much as possible, which is useful if the process does not expect to receive any messages in the near future.

The process will be awaken when a message is sent to it, and control will resume in Module:Function with the arguments given by Args with the call stack emptied, meaning that the process will terminate when that function returns. Thus erlang:hibernate/3 will never return to its caller.

If the process has any message in its message queue, the process will be awaken immediately in the same way as described above.

In more technical terms, what erlang:hibernate/3 does is the following. It discards the call stack for the process. Then it garbage collects the process. After the garbage collection, all live data is in one continuous heap. The heap is then shrunken to the exact same size as the live data which it holds (even if that size is less than the minimum heap size for the process).

If the size of the live data in the process is less than the minimum heap size, the first garbage collection occurring after the process has been awaken will ensure that the heap size is changed to a size not smaller than the minimum heap size.

Note that emptying the call stack means that any surrounding catch is removed and has to be re-inserted after hibernation. One effect of this is that processes started using proc_lib (also indirectly, such as gen_server processes), should use proc_lib:hibernate/3 instead to ensure that the exception handler continues to work when the process wakes up.

This sounds reasonable – let’s try hibernating after every message and see what happens.

Edit mochiconntest_web.erl and change the following:

  • Make the last line of the feed(Response, Id, N) function call hibernate instead of calling itself
  • Call hibernate immediately after logging into the router, rather than calling feed and blocking on receive
  • Remember to export feed/3 so hibernate can call back into the function on wake-up

Updated mochiconntest_web.erl with hibernation between messages:

  1. -module(mochiconntest_web).
  2.  
  3. -export([start/1, stop/0, loop/2, feed/3]).
  4.  
  5. %% External API
  6.  
  7. start(Options) ->
  8.     {DocRoot, Options1} = get_option(docroot, Options),
  9.     Loop = fun (Req) ->
  10.                    ?MODULE:loop(Req, DocRoot)
  11.            end,
  12.     % we’ll set our maximum to 1 million connections. (default: 2048)
  13.     mochiweb_http:start([{max, 1000000}, {name, ?MODULE}, {loop, Loop} | Options1]).
  14.  
  15. stop() ->
  16.     mochiweb_http:stop(?MODULE).
  17.  
  18. loop(Req, DocRoot) ->
  19.     "/" ++ Path = Req:get(path),
  20.     case Req:get(method) of
  21.         Method when Method =:= ‘GET’; Method =:= ‘HEAD’ ->
  22.             case Path of
  23.                 "test/" ++ IdStr ->
  24.                     Response = Req:ok({"text/html; charset=utf-8",
  25.                                       [{"Server","Mochiweb-Test"}],
  26.                                       chunked}),
  27.                     {Id, _} = string:to_integer(IdStr),
  28.                     router:login(Id, self()),
  29.                     % Hibernate this process until it receives a message:
  30.                     proc_lib:hibernate(?MODULE, feed, [Response, Id, 1]);
  31.                 _ ->
  32.  
  33.  
  34.                     Req:not_found()
  35.             end;
  36.         ‘POST’ ->
  37.             case Path of
  38.                 _ ->
  39.                     Req:not_found()
  40.             end;
  41.         _ ->
  42.             Req:respond({501, [], []})
  43.     end.
  44.  
  45. feed(Response, Id, N) ->
  46.     receive
  47.     {router_msg, Msg} ->
  48.         Html = io_lib:format("Recvd msg #~w: ‘~w’<br/>", [N, Msg]),
  49.         Response:write_chunk(Html)
  50.     end,
  51.     % Hibernate this process until it receives a message:
  52.     proc_lib:hibernate(?MODULE, feed, [Response, Id, N+1]).
  53.  
  54.  
  55. %% Internal API
  56.  
  57. get_option(Option, Options) ->
  58.     {proplists:get_value(Option, Options), proplists:delete(Option, Options)}.


I made these changes, ran make to rebuild mochiweb, then redid the same c10k test (1000msgs/sec for 24hrs).

Results after running for 24 hours w/ proc_lib:hibernate()

Memory usage with c10k, 1000msg/sec, 24hrs, using hibernate()

Memory usage with c10k, 1000msg/sec, 24hrs, using hibernate()

Judicious use of hibernate means the mochiweb application memory levels out at 78MB Resident with 10k connections, much better than the 450MB we saw in Part 1. There was no significant increase in CPU usage.

Summary

We made a comet application on Mochiweb that lets us push arbitrary messages to users identified by an integer ID. After pumping 1000 msgs/sec through it for 24 hours, with 10,000 connected users, we observed it using 80MB, or 8KB per user. We even made pretty graphs.

This is quite an improvement from the 45KB per used we saw in Part 1. The savings are attributed to making the application behave in a more realistic way, and use of hibernate for mochiweb processes between messages.

Next Steps

In Part 3, I’ll turn it up to 1 million connected clients. I will be deploying the test app on a multi-cpu 64-bit server with plenty of RAM. This will show what difference, if any, running on a 64-bit VM makes. I’ll also detail some additional tricks and tuning needed in order to simulate 1 million client connections.

The application will evolve into a sort of pub-sub system, where subscriptions are associated to user Ids and stored by the app, rather than provided by clients when they connect. We’ll load in a typical social-network dataset: friends. This will allow a user to login with their user Id and automatically receive any event generated by one of their friends.

UPDATED: Part 3 is now online.

Tags: , ,

Thursday, October 23rd, 2008 programming

21 Comments to A Million-user Comet Application with Mochiweb, Part 2

  1. [...] Part 2 is online [...]

  2. A Million-user Comet Application with Mochiweb, Part 1 | Richard Jones, Esq. on October 23rd, 2008
  3. Wow. A great follow-up to your part one. Thank you! This is incredibly helpful.

  4. scott on October 23rd, 2008
  5. Nice. I have been hacking Erlang code for a several years now, and read most of the Erlang related resources I can find. This is the most useful I’ve seen in a while.

  6. dsmith on October 23rd, 2008
  7. Excellent series! I’m not only learning some nice Erlang tricks, but some scripting ones as well. Thanks!

  8. daveb on October 24th, 2008
  9. [...] A Million-user Comet Application with Mochiweb, Part 2 (tags: erlang mochiweb comet blog programming) [...]

  10. links for 2008-10-24 « Bloggitation on October 24th, 2008
  11. Thanks you for such a great article!
    I found this one particularly helpful, I’ve just read it and already applying some of tips!

  12. pablo on October 24th, 2008
  13. [...] update: 第二篇的原文在[这里]。 [...]

  14. Erlang-China » Comet and Erlang, A perfect match on October 25th, 2008
  15. Very nice article. I’m also using Erlang at work and this article has given me some additional ideas. I’m also thinking about making a Comet application with Erlang/Mochiweb and definitely look forward to your results with 1 million clients.

  16. Thijs (Shenzhen) on October 26th, 2008
  17. Yes, please do the part 3, :)

  18. John Wright on October 30th, 2008
  19. [...] 1 and Part 2 in this series showed how to build a comet application using mochiweb, and how to route messages to [...]

  20. A Million-user Comet Application with Mochiweb, Part 3 | Richard Jones, Esq. on November 4th, 2008
  21. Richard, this is a great series. I’ve been diving in and out of learning Erlang because I needed Mnesia rather than SQL, and this has to be the greatest leg-up that I’ve come across.

    Within in the next three months I should finally be able to finish my f*****g application!

  22. Mark on January 1st, 2009
  23. Toward a million-user long-poll HTTP application - nginx + erlang + mochiweb :) « Alexey’s Random Notes on January 11th, 2009
  24. [...] 原文:A Million-user Comet Application with Mochiweb, Part 2 [...]

  25. 用Mochiweb打造百万级Comet应用,第二部分 - IDISC的生活 on January 22nd, 2009
  26. [...] of posts about creating a “Million user Comet application with MochiWeb” (part I, part II, part [...]

  27. Playing with Erlang « Kai Lautaportti on February 3rd, 2009
  28. Thanks for your article,I copy the code and test to send messages.Mochiweb report me a error:
    {mochiweb_socket_server,235,
    {child_error,{noproc,{gen_server,call,[undefined,{login,123,}]}}}}

    what’s the meaning

  29. xuzhe on August 3rd, 2009
  30. Anyone know how to detect client disconnects not using a timeout? In this code, it would not “logout” immediately… I think it involves making the socket active instead of passive, but can’t figure it out. Richard, any thoughts? Thanks!

  31. Shayne on November 5th, 2009
  32. Richard, first thank you for this post. It is one of my favourites on the web.

    I took the code from these examples and made an attempt to do long-polling using your method. I ran into a huge problem after adding in the proc_lib:hibernate(..) code to save on memory. I’m not sure if perhaps I messed something up or have a different version of Mochiweb than the one you were using, but it seems with that code, the Mochiweb process will never release its socket_acceptor process which handles the next incoming request. Meaning that after the 1M requests, Mochiweb stops accepting any new connections, even if you logout.

    Perhaps you may not have noticed this because “mochiweb_http:start([{max, 1000000}” is set so high. As a test, I set the max to 3 concurrent requests. After the 3rd one (even if the previous ones disconnected), I received a message from Mochiweb telling me it was no longer accepting any new connections and I found no way to recover from that.

    Removing the proc_lib:hibernate(..) code from the loop(..) and feed(..) functions in mochiconntest_app.erl resolved this.

    I was just wondering if it was something you ran into as well or if perhaps I had just messed something up.

  33. Nemanja on January 20th, 2010
  34. Nemanja: that isn’t something I ran into, since as you said I set the max very high, and restarted in between tests.

    It sounds like when a connection is closed, the mochiweb proc doesn’t notice since it’s hibernating. Perhaps you can put the socket into active mode once you’ve accepted the connection, so that the proc is sent a msg on close/error (to wake it up from hibernation).

    Been a while since I looked at this code, let me know how you get on. I’m RJ2 on irc.freenode.org #erlang if you want to chat.

  35. RJ on January 20th, 2010
  36. Richard, thanks for the (superfast) reply. I figured it had been a while since you looked at this so I thought it was a shot in the dark.

    I’ll experiment with it some more and let you know if I find a work-around.

    Thanks again and see you on IRC. :)

  37. Nemanja on January 20th, 2010
  38. Richard, I think I got the active socket portion working.

    Just replace the following in mochiconntest_web.erl:
    loop(Req, DocRoot) ->
    “/” ++ Path = Req:get(path),
    case Req:get(method) of
    Method when Method =:= ‘GET’; Method =:= ‘HEAD’ ->

    with:
    loop(Req, DocRoot) ->
    Socket = Req:get(socket),
    inet:setopts(Socket, [{active, once}]),
    “/” ++ Path = Req:get(path),
    case Req:get(method) of
    Method when Method =:= ‘GET’; Method =:= ‘HEAD’ ->

    That will grab the socket of the current request and set it to be active. Once that is done at the beginning of the loop to process the request, it is then possible to do the proc_lib:hibernate(..) without any issues and the socket adapters no longer hang! I have not tested it with the olde C10k test to see that the memory consumption savings remain, but I can’t see any reason that would change.

    Shayne, this might help you since you were looking for a way to make the sockets active to detect disconnects without relying on timeouts.

  39. Nemanja on January 20th, 2010
  40. Thanks for the tutorial. But i’m having issues running it. I copied and pasted above example but cant seem to get to work properly.

    Connecting via browser to
    http://localhost:8000/test/123

    I get message on router side. But router:send(123, “testing”) does not deliver message to browser until I abort mochiweb VM.

    Running on OSX.

  41. erlnewb on February 4th, 2010

Leave a comment