How to reduce latency of your Google Cloud Endpoints APIs

Philippe BOISNEY
Google Cloud - Community
3 min readJan 30, 2017

--

Credit: Tom Eversley

It’s been 2 years now since I’ve used Google Cloud Endpoint in order to build powerful APIs for endpoints like Android & IOS.

If you’ve never heard about Google Cloud Endpoints you’ve probably heard about Google Cloud Platform (GCP), that is basically the equivalent of Amazon Web Services, made by Google.

So, Google Cloud Endpoints (one of the services provided by GCP) helps you create easily your own APIs, to allow your endpoints (Android, IOS, Javascript, etc…) to access to your backend.

However, there are some optimisations to take full advantage of Google Cloud Endpoints and improve performances of your own APIs.

1. Keep alive your instances

As you may know, Google Cloud Endpoints runs on Google App Engine instances. Thereby, when an instance is not used, then it shuts down itself after some time, and start up when it’s necessary (Incoming http request, etc…).

But the startup time for an appengine instance can take long . I’ve noticed that sometimes, it can exceed 7 seconds…

To handle that problem, a simple trick is to create a cron task that keep alive instance in cron.xml:

<cron>
<url>/Cron/KeepAliveInstance</url>
<description>Keep alive instance</description>
<schedule>every 1 minutes</schedule>
</cron>

Also, you can tell Google when startup a new instance if there are too many requests to handle, with “min-pending-latency” parameter in appengine-web.xml. This value depending on your average latency time you can notice on your APIs :

<automatic-scaling>
<min-pending-latency>3000ms</min-pending-latency>
</automatic-scaling>

2. Use Memcache with caution

I highly recommend to read this documentation that explains when using Memcache. Back in the day, I set up every of my Datastore Entities in Memcache, telling myself that it will be more faster… #Fail

Basically, if you serve datas that never (or rarely) change, use Memcache for those datas. Otherwise, don’t.

For example, on my app CookMinute, every recipe is daily downloaded and stored in user’s phone (thanks Realm). So I’ve only set up RECIPE entity to be part of Memcache.

3. Increase instances performances (Not required)

If you have high processing on your APIs, you can increase the instance class of your appengine instances. However, this will increase the cost too.

<appengine-web-app>
<application>${app.id}</application>
<instance-class>F2</instance-class>
</appengine-web-app>

Results

I’ve tested those optimisations with API that sign-up a new user to my app. Stats are generated with Stackdriver Trace (Console => Stackdriver => Trace) and the instance class is F1.

Before optimisation

After optimisation

As you can see, latency has decreased and is more smooth, especially because there is always an instance alive that can handle a request.

Oh, and about the cost, well, I don’t pay anything yet because I never exceeded the free quotas… (which is really huge)

If you have some advice or tips about Google Cloud Endpoints API, let me know in the comments !

Phil, Founder @ CookMinute and mobile enthusiast.

--

--