Search code examples
apigoogle-mapsserver-error

Sometimes the Google Geocoding API returns a 500 server error


Sometimes the Google Maps API returns a 500 server error response according to German postal codes and i cannot understand why.

I hope it is specific enough. Any ideas?

https://maps.googleapis.com/maps/api/geocode/json?key={api_key}&address={postal_code}&language=de&region=de&components=country:DE&sensor=false


Solution

  • Since you specify that the problem is not a given address but a seemingly "random" behavior, this may fall under a documented behavior of other "famous" API.

    As for other cases, the recommended strategy is Exponential backoff for the Geocoding API, which basically means that you have to retry after a certain delay.

    In case the above link goes down or changes, I'm quoting the article:

    Exponential Backoff

    In rare cases something may go wrong serving your request; you may receive a 4XX or 5XX HTTP response code, or the TCP connection may simply fail somewhere between your client and Google's server. Often it is worthwhile re-trying the request as the followup request may succeed when the original failed. However, it is important not to simply loop repeatedly making requests to Google's servers. This looping behavior can overload the network between your client and Google causing problems for many parties.

    A better approach is to retry with increasing delays between attempts. Usually the delay is increased by a multiplicative factor with each attempt, an approach known as Exponential Backoff.

    For example, consider an application that wishes to make this request to the Google Maps Time Zone API:

    https://maps.googleapis.com/maps/api/timezone/json?location=39.6034810,-119.6822510&timestamp=1331161200&key=YOUR_API_KEY

    The following Python example shows how to make the request with exponential backoff:

    import json
    import time
    import urllib
    import urllib2
    
    def timezone(lat, lng, timestamp):
        # The maps_key defined below isn't a valid Google Maps API key.
        # You need to get your own API key.
        # See https://developers.google.com/maps/documentation/timezone/get-api-key
        maps_key = 'YOUR_KEY_HERE'
        timezone_base_url = 'https://maps.googleapis.com/maps/api/timezone/json'
    
        # This joins the parts of the URL together into one string.
        url = timezone_base_url + '?' + urllib.urlencode({
            'location': "%s,%s" % (lat, lng),
            'timestamp': timestamp,
            'key': maps_key,
        })
    
        current_delay = 0.1  # Set the initial retry delay to 100ms.
        max_delay = 3600  # Set the maximum retry delay to 1 hour.
    
        while True:
            try:
                # Get the API response.
                response = str(urllib2.urlopen(url).read())
            except IOError:
                pass  # Fall through to the retry loop.
            else:
                # If we didn't get an IOError then parse the result.
                result = json.loads(response.replace('\\n', ''))
                if result['status'] == 'OK':
                    return result['timeZoneId']
                elif result['status'] != 'UNKNOWN_ERROR':
                    # Many API errors cannot be fixed by a retry, e.g. INVALID_REQUEST or
                    # ZERO_RESULTS. There is no point retrying these requests.
                    raise Exception(result['error_message'])
    
            if current_delay > max_delay:
                raise Exception('Too many retry attempts.')
            print 'Waiting', current_delay, 'seconds before retrying.'
            time.sleep(current_delay)
            current_delay *= 2  # Increase the delay each time we retry.
    
    tz = timezone(39.6034810, -119.6822510, 1331161200)
    print 'Timezone:', tz
    

    Of course this will not resolve the "false responses" you mention; I suspect that depends on data quality and does not happen randomly.