Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
have uploaded the Rest API on two different server. One is primary and one is secondary. Now I am calling for these APIs from ajax call in jQuery.

If primary server is down or not responding then APIs on secondary server should get called from jQuery. How to manage that? Is it proper architecture?

What I have tried:

I have set two base url in constant.js, and on ajax call in fail, called same method to secondary server. It's working, but I think there should be any other option, as it is very time consuming and I don't feel it's proper way of coding.
Posted
Updated 12-Sep-21 23:39pm

1 solution

This approach might be called either "failover" or "load balancing", which is where two servers provide the same service but allow for distribution in the event of problems. If the secondary server here is more for redundancy then it's a "failover" system.

Typically you wouldn't have your client code accessing two different URLs, instead you'd normally have some middleware proxy (like a load balancer, or a service like Cloudflare, Apigee, AWS ELB) which sits inbetween providing the automated routing. Your client should access one proxy address and the proxy service should then determine which server it should be routing the request to.

The expense here is at the client-level, because a proxy service will normally have some functionality to perform health checks to see whether a server is up and functional, and that means it knows (in advance) where to route the request to. Having the client run this check means your users could experience delays because you'll have to wait for the primary requests to timeout before using the failover server.

Consider having a look at failover proxy solutions, or routing your API through something like Apigee which is fairly flexible and easy to setup.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900