Fransebas
Fransebas

Reputation: 341

What is better practice, create one unix socket with multiple connections or multiple sockets with one connection?

I'm making the design of a program that is going to create multiple process with exec and then create a connection to them using sockets and I have multiple alternatives but I don't know which one is better.

Every child process is going to have around 3 services it would like to communicate to the server.

  1. Should I create a 3 sockets and connect every child to those sockets and distinguish between them sending the ID when the connection starts /temp/service{1|2|3}.sock.
  2. Should I create 3 new sockets for every child /temp/{ID}/service{1|2|3}.sock.

enter image description here

The second options seems a bit better because I don't have to tell the sever who I'm when the connection starts, it is implicit in the name of the socket and each service will have it's own socket butI don't know if it will be inefficient to create a 3 sockets for every child.

Upvotes: 1

Views: 1253

Answers (1)

Cosmin Ioniță
Cosmin Ioniță

Reputation: 4045

Interesting question. Here are my thoughts around it:

(Around option 1)

If you have heavy traffic flowing through those sockets, then at some point they may become a bottleneck. If that's not the case (low traffic), then option 1 would work.

(Around option 2)

Let N be the number of child processes created in a given timeframe. If N * 3 > (total number of file descriptors on your machine), for the same timeframe, then definitely option 2 doesn't seem to be the right fit.

If you can also account for a file descriptor recycling rate, that would give more accuracy to the overall evaluation.

(Overall)

I would think about those 2 tradeoffs and make a decision based on that. Without some numbers around it would be hard to take an informed decision.

Upvotes: 1

Related Questions