Search code examples
phpmysqlgoogle-app-enginegoogle-cloud-sql

Connecting to Cloud SQL from App Engine is slower than from localhost


So I've been trying to connect to Google Cloud SQL from my Google App Engine. Everything works, but the latency is very high. A simple select query is taking about 1.3 seconds. However, when I connect from localhost, it takes a much shorter time. The difference in connection is the following:

in App Engine yaml

MYSQL_DSN: /cloudsql/instanceName
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_DATABASE: dbname

in App Engine php

$servername = getenv('MYSQL_DSN');
$username = getenv('MYSQL_USER');
$password = getenv('MYSQL_PASSWORD');
$dbname = getenv('MYSQL_DATABASE');
$connection = new mysqli(null, $username, $password, $dbname, null, $servername);

when connecting to Cloud SQL in localhost I use:

$servername = "ip.address";
$username = getenv('MYSQL_USER');
$password = getenv('MYSQL_PASSWORD');
$dbname = getenv('MYSQL_DATABASE');
$connection = new mysqli($servername, $username, $password, $dbname);

When connecting from localhost, I have authorized my computer's ip address in my Cloud SQL dashboard, so connecting directly through ip address is no problem. My problem now is that when I am running my php files from localhost to connect to Cloud SQL, the latency is lower than when connecting from App Engine. Any idea why and how I can fix this?


Solution

  • This is a known issue affecting Cloud SQL instances in the asia-northeast1 region. It is tracked on the Cloud SQL public issue tracker under Issue 197. Please star the issue to support it getting appropriate attention.

    The underlying issue relates to scaling up the Japan (asia-northeast1) operation which just launched in 2016. There does not seem to be an ideal workaround at the moment. My only recommendation would be to try moving the Cloud SQL instance outside of asia-northeast1. You may be able to reduce the latency using asia-east1 (Taiwan) or a closer region still if that move is feasible in your situation.