A tip for dealing with Google API's

A tip for dealing with Google API's
Photo by Cecep Rahmat / Unsplash

The SaaS I'm helping build does a lot with Google API's. In fact, it currently works with 5 different APIs and ~15 endpoints. One of the issues that kept cropping up was us getting a 503 Service Unavailable response, more times than not. The timing wasn't consistent, it could happen on any request at any time.

We're using SaloonPHP to integrate with Google, which means all of the requests go through a Connector class, or in our case, one of 5 Connector classes. The beauty of the Connector class is that you can add logic into it, which will be used for any Request that goes through it.

So with this in mind, I edited all of the Connector classes to have 3 properties, let's take a look at the Connector that handles any Requests to the Google Maps API:

<?php

namespace App\Integrations\Google;

use Saloon\Http\Connector;
use Saloon\Traits\Plugins\AcceptsJson;

class MapsConnector extends Connector
{
    use AcceptsJson;

    public ?int $tries = 3;

    public ?int $retryInterval = 500;

    public ?bool $useExponentialBackoff = true;

    /**
     * The Base URL of the API
     */
    #[\Override]
    public function resolveBaseUrl(): string
    {
        return 'https://maps.googleapis.com/maps/api/';
    }
}

So each time we interact with a Google API, we:

  • Try the job 3 times, if it still doesn't work, then we class it as failed
  • Use Exponential Backoff, to give more time between each retry, based on the interval we set
    • 1st retry happens 500ms after the initial request
    • 2nd retry happens 1s after the 1st retry

And that's it! I can't remember the last time I saw a Google 503 response, but it was getting quite annoying seeing all of those failures coming through!