Website responds very slow using remote database

I want two same websites to share one database. One server is in Asia, hosting a website and the database. Another server is in the US, hosting the same web via remote database. However, the web in the US responds very slow but when moving the database to the local server(US server), the web responds fast. How to speed up the connection between the server in the US and the database in Asia?

I am using Centos7+Nginx+MySQL.


How to speed up the connection between the server in the US and the database in Asia?

You probably can't. US-Asia is a long distance, and there's latency involved. Simply put: the light has a finite speed.

You should probably look into alternatives to querying a remote database, such as caching assets in multiple locations, using a CDN, or using a database replication scheme so that you can have local copies of the data.


Does your web application make multiple separate database queries? Does one query depend on the previous query, or are there independent queries that could both be in flight in parallel to overlap their latency?

If the latter, make sure that's actually happening in whatever programming language you used, not unnecessarily serializing your queries.

If that's not sufficient or impossible, you'll need to reduce the round-trip latency between web server and database somehow, either by replicating the DB so you can have a local copy, caching some "hot" parts of it that don't change often, or various other things like caching the final output of the web server or other things a CDN can do (see vidarlo's answer).


You can't improve on the speed of light. {citation needed}

Instead of querying intercontinentally, you might need to look into whatever replication MySQL offers. You'd need a webserver and a database server in Asia and duplicated in each location (US, EU, Africa, ME, etc). When a webserver writes, the local DB is updated immediately, and that update is then pushed to the remote DB servers by MySQL internally.

Positives:

  • User's sessions could flip from one site to another and their database records would be consistent.
  • You only need to back up one database server to get the lot.
  • Can add redundancy and fault tolerance by having a primary site, and if it goes down then fail over to a remote site.
  • Can scale out the webservers horizontally at each site to allow for downtime maintenance like updates/reboots
  • You can even have a backup DB server locally to each site, again allowing for redundancy without having to fail over

Negatives:

  • Added complexity
  • Increased cost of more resources

Ultimately this is an infrastructure design issue, and not really a webserver thing.