[erlang-questions] How to handle a massive amount of UDP packets?

John-Paul Bader <>
Sun Apr 15 23:41:01 CEST 2012


Ok, little update.

I've changed the implementation to use {active,false} and a manual 
gen_udp:recv loop but that made things only slightly faster. Then I 
commented out the part where I spawned the processes which compute and 
send back the response to the clients and this gave a _huge_ speed 
improvement.

I ran a benchmark on a fast server and could easily handle 100-200k 
packets per second without losing a single packet.

So clearly my problem is dispatching the packet/message to the worker 
processes. I will experiment with a worker pool next instead of spawning 
the processes on demand.

If you know how to do this dispatching with as little overhead as 
possible please let me know.

~ John



Magnus Klaar wrote:
> Hi!
>
> You only mention setting the {active, once} option. Are you also setting
> higher values for the recbuf, sndbuf and read_packets options? As with
> all non-10gen default settings they are set to a low but safe value that
> may not be optimal for all applications.
>
> MVH Magnus
>
> On Sun, Apr 15, 2012 at 8:08 PM, John-Paul Bader <
> <mailto:>> wrote:
>
>     Dear list,
>
>
>     I'm currently writing a bittorrent tracker in Erlang. While a naive
>     implementation of the protocol is quite easy, there are some
>     performance related challanges where I could use some help.
>
>     In the first test run as a replacement for a very popular tracker,
>     my erlang tracker got about 40k requests per second.
>
>     My initial approach was to initialize the socket in one process with
>     {active, once}, handle the message in handle_info with minimal
>     effort and pass the data asynchronously to a freshly spawned worker
>     processes which responds to the clients. After spawning the process
>     I'm setting the socket back to {active, once}.
>
>     Now when I switched the erlang tracker live the erlang vm was
>     topping at 100% CPU load. My guess is that the process handling the
>     udp packets from the socket could not keep up. Since I'm still quite
>     new to the world of erlang I'd like to know if there are some best
>     practices / patterns to handle this massive amount of packets.
>
>     For example using the socket in {active, once} might be too slow?
>     Also the response to the clients needs to come from the same port as
>     the request was coming in. Is it a problem to use the same socket
>     for that? Should I pre-spawn a couple of thousand workers and
>     dispatch the data from the socket to them rather than spawning them
>     on each packet?
>
>     It would be really great if you could give some advice or point me
>     into the right directions.
>
>     ~ John
>     _________________________________________________
>     erlang-questions mailing list
>      <mailto:>
>     http://erlang.org/mailman/__listinfo/erlang-questions
>     <http://erlang.org/mailman/listinfo/erlang-questions>
>
>



More information about the erlang-questions mailing list