zenna
zenna

Reputation: 9186

How to dynamically assign particular client (browser) to one of many servers?

I am building a service which requires me to dynamically launch and close servers at many locations around the world, (for example using AWS). When a user visits my domain they need to be assigned to a local server with the lowest latency.

By assignment, I mean that for example the client makes an ajax call to example.com/getData, it should go directly to one particular server that is has been assigned to. Different servers will be doing different computation, so it is not sufficient to have some kind of general load balancing.

What general mechanisms/technology would allow me to 1) Assess the latency between a particular client and any server under my control? 2) Assign a particular client to a particular server? I cannot use just the IP addresses for example, since javascript has domain name based restrictions.

Thanks

Upvotes: 4

Views: 172

Answers (1)

Silenteh
Silenteh

Reputation: 516

Note: I do not have enough reputation to link all the technologies in the response, therefore sometimes you will see the links copied in plain text.

1) Assign users to a local server with the lowest latency is not always possible.

Sometimes the geographically closest server to a user is unexpectedly the one with the highest latency.

To find the lowest latency between your (running) servers and the users is not an easy task. There might be many different hops (routers) between the client and the server, and any of them at any time can have problems, routes update, packet congestions and so on. The quickest way to assess the latency is a ping, but it can be that the firewalls block this.

So the best way to achieve this is to use the anycast

All the major CDN providers implement this method. Some use the TCP anycast, which seems to be not recommended, and others UDP anycast. It is an open debate.

Anyway in order to implement anycast you need to be able to peer with the ISP routers, and normally this is not possible. Additionally there are good peers and bad peers. Finally All this requires a deep knowledge of the routing protocols and the TCP/IP stack.

A quick and dirty solution could be to use BIND with the GEO-IP patch. So you can define specific dns query responses per country. What I mean is that, for instance, if you have a server in UK and one in US you can configure BIND to respond to users coming from europe to hit the UK server and users coming from US to hit the US server.

2) To assign a particular client to a particular server you can use the technique I described on the point 1 or you can use a proxy and sticky sessions. HA-Proxy is a good product to achieve this. (the URL: xy.1wt.eu )

3) if you use the point 1, you will not have problems with cross domain ajax calls. In fact it is completely transparant for the client. For instance for the same domain example.com a user coming from US will resolve it to 1.1.1.1 whereas a user coming from Germany will resolve example.com to 2.2.2.2 (ip addresses are fake and used just as an example).

On a side note, a solution to do cross domain ajax call is JSON-P which has though some drawbacks, like the lack of support for POST.

If I were you I would go with the BIND and GEO-IP, because it would solve all three problems in once. (a part for the latency because is not always true that the geographically closest server is the one with the lowest latency.)

Upvotes: 2

Related Questions