[erlang-questions] Sharing a UDP socket between processes

Dmitry Kolesnikov dmkolesnikov@REDACTED
Fri Jun 22 17:19:47 CEST 2018


> On 22 Jun 2018, at 18.00, Roger Lipscombe <roger@REDACTED> wrote:
> I need to send fire-and-forget UDP messages to a particular
> destination. I don't plan on receiving any incoming messages.
> I need to be able to send messages from several thousand Erlang
> processes. Ordinarily, I'd wrap the socket in a gen_server, but is
> there really any need?

You should design system where resource ownership is explicitly managed.
gen_server is simply allows to build a fault tolerance solution.

> Can I simply stash the socket in an ETS table and use it directly?
> What possible downsides are there to this?

A single gen_server process is able to process over 80K messages per second before it
became a bottleneck. I would start with single process and start scanning on the needs.

Personally, I do not like usage of ETS as shared resources pool. I prefer to go with
Process pool(s) manages by supervisor(s). One process owns a resource (UDP socket) other
Child processes uses this resource. My reasoning simple here, supervisors helps you to
react on failures.

> Or would it be sensible to simply open/send/close a UDP socket each
> time? Each process only needs to send a message at startup, and then
> every 2 minutes or so. But: there will be several thousand of these
> processes spawned at once.

I would not recommend this option because single open / close causes resource allocation from kernel.
However, some monitoring solution uses this principle.

Best Regards,
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: Message signed with OpenPGP
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20180622/cbfa9a9e/attachment.bin>

More information about the erlang-questions mailing list