Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update DNS for domain name in AJAX request

We have DNS failover setup, with a TTL of 180 seconds. When a server fails, it updates the IP address on the A Host records to that of the working server. Currently, our website refreshes using AJAX requests on an interval. When one of the servers fails, the DNS is updated and failed over, so if you ping the domain it has the proper IP. For some reason though, inside the browser and long after the TTL has expired, the AJAX request still retains the old IP address assigned to the domain name (note the page cannot be reloaded in the browser). I am aware as of reading other questions that you cannot force a flush of the DNS cache on the client. My question is, do any of you know another way or trick to accomplish this using javascript or AJAX? Thanks.

like image 483
wayofthefuture Avatar asked Jun 04 '14 21:06

wayofthefuture


2 Answers

I would not attempt this client side, I would use a network load balancer, or some other server side virtual IP address.

To do as you request client side, would require that your AJAX calls are made in such a way as to either avoid browser security or fullly conform to cross domain requests. If you are happy to make these changes, then there are some JavaScript DNS libs, these can be used to resolve the DNS name to an IP address or CName, use the returned IP address or CName to adjust your AJAX url, and refresh the AJAX url using a new DNS lookup whenever an AJAX call fails.

I wouldn't want to do this myself

like image 82
Old fart Avatar answered Sep 18 '22 15:09

Old fart


Use two (or more) subdomains. eg. data1.example.com and data2.example.com that correspond to the physical servers.

However, for cross domain to work, you need to set document.domain which allows the communication between domains:

document.domain = "example.com";

Then, you need your script to fail over between the different servers manually. It is the only way that I know of and has been successful in many installs.

like image 29
cmroanirgo Avatar answered Sep 18 '22 15:09

cmroanirgo