Chandu
Chandu

Reputation: 2049

How to redirect connection to implement load balancer?

I have to write a load balancer for our custom server, not http. I have gone through lot of articles over internet. Every where its mentioned that load balancer redirects the connection to actual server. But no where is mentioned in how to redirect the connects. Can some body tell how to implement the connection redirection in C ?

Thanks

Upvotes: 0

Views: 928

Answers (1)

jmm
jmm

Reputation: 69

Redirecting connection in this context means creating a proxy between two connections - external (client facing), and internal (server facing). On one end you listen for incoming connections, on other you pick a backend server and redirect traffic from client connection there. In essence you're creating a flow from two ip tuples:

((external ip, external port, external interface) , (internal ip, internal port,internal interface))

The data flow is:

  client                load balancer                   server
[c1 sock]<--->[external socket | internal socket]<--->[s1 sock]

Basic operation mode would be:

  • When client connects, the load balancers picks a server from the servers pool.
  • When data is transferred on either end, the load balancer copies data between the two sockets.
  • When connection state changes on either end (is closed), the load balancer replicates the state to the other socket.
  • When a backed server is down, the load balancer excludes it from the pool (some kind of monitoring is required).

You can implement it without the usage of sockets, at the network layer, but that requires userspace TCP/IP stack implementaton and ability to read packets directly from network adapter queue.

nginx can load balance TCP and UDP connections. Why not use it instead of reinventing the wheel? It is probably way more tuned and battle tested that your solution will be in a few years.

Upvotes: 1

Related Questions