I have an Google Flex App Engine App that recieves POSTs with JSON data, then performs an insert statement using SQLAlchemy that writes the JSON data to a Cloud SQL database. The latency between POST device (in Dallas) and App Engine (us-east) is well above 300ms, sometimes spiking over 1500ms. I believe it may be due to poor INSERT write speed, although the load is hardly 'ridiculous' or anything. Roughly up to 5-8 reads/sec and 150-300 writes/sec
Using Flask, SQLAlchemy, MySQL 5.6 (on Cloud SQL), and 2nd generation Google Flex App Engine instance.
My problem is I need to drop the latency and I can't figure out what is causing it. I have run traceroute and ping commands to the IP that POSTs json data to the Flask App page being hosted by Google App Engine. The ping is fine, there is a hiccup in one hop but it is only one area. If I have too high of a latency, the IP will possibly drop POST.
from api import app
from sqlalchemy import create_engine
from datetime import datetime, timedelta
engine = create_engine(app.config.get("database_uri"))
Class Events(object):
@staticmethod
def add(event):
connection = engine.connect()
for observation in event["observations"]:
try:
connection.execute("""INSERT INTO events (
...
) VALUES (
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s
)""", (
...
))
except Exception as ex:
print(ex)
connection.close()
This is triggered by:
@app.route('/', methods=['POST'])
def events_post():
data = request.json
if data["secret"] != app.config.get("secret"):
return
Events.add(data["data"])
return "Success"
I have tried with and without connection.close() at the end and it doesn't seem to matter. This is a steady stream of data so closing and re-opening the connection every time seemed resource-heavy for the job at hand.
Thanks in advance.