09-16-06 06:43 PM
abhi147@gmail.com wrote:
> I have a program which has 1 client and 4 servers . Client
> sends messages(every second) to all these 4 servers and recieves a
> response from these servers . It has load balaning and failover logic
> in it .
> Now my question is , is it a good practice to connect to these servers
> outside the while loop or should I connect these inside the loop(so
> that client connect to server everytime a message is being sent ) ..
Assuming you use TCP, connecting to a server for each request results
in overhead of 6 ip datagrams (total: 3 for establishing a connection,
3 for tearing down). Whether it's an issue depends on your environment.
If it is, you might consider using udp or sctp to reduce the overhead.
> Wouldn't connecting and closing the socket every second be a load on
> the system ?
It depends on how often you do so. I once had an in-memory database
server on linux os. Protocol was simple: a client (php) connects to the
server, sends a request (add, get, update, delete), the server
responds, the client disconnects (i.e. a tcp connection for each
request). The request and response were less than 512 bytes long.
Serving 2500 tcp connection per second server used no more than 30% of
(one) cpu time (the server actually worked on a 4-core amd opteron, but
it only ever used one thread).
[ Post a follow-up to this message ]
|