SVerlinden
SVerlinden

Reputation: 23

Why and how is the quota "critial read requests" exceeded when using batchCreateContacts

I'm programming a contacts export from our database to Google Contacts using the Google People API. I'm programming the requests over URL via Google Apps Script.

The code below - using https://people.googleapis.com/v1/people:batchCreateContacts - works for 13 to about 15 single requests, but then Google returns this error message:

Quota exceeded for quota metric 'Critical read requests (Contact and Profile Reads)' and limit 'Critical read requests (Contact and Profile Reads) per minute per user' of service 'people.googleapis.com' for consumer 'project_number:***'.

For speed I send the request with batches of 10 parallel requests.

I have the following two questions regarding this problem:

  1. Why, for creating contacts, I would hit a quotum regarding read requests?
  2. Given the picture link below, why would sending 2 batches of 10 simultaneous requests (more precise: 13 to 15 single requests) hit that quotum limit anyway? quotum limit of 90 read requests per user per minute as displayed on console.cloud.google.com

Thank you for any clarification!

Further reading: https://developers.google.com/people/api/rest/v1/people/batchCreateContacts

  let payloads = [];
  let lengthPayloads;
  let limitPayload = 200;
/*Break up contacts in payload limits*/
  contacts.forEach(function (contact, index) /*contacts is an array of objects for the API*/
    {
      if(!(index%limitPayload))
      {
        lengthPayloads = payloads.push(
          {
            'readMask': "userDefined",
            'sources': ["READ_SOURCE_TYPE_CONTACT"],
            'contacts': []
          }
        );
      }
      payloads[lengthPayloads-1]['contacts'].push(contact);
    }
  );
Logger.log("which makes "+payloads.length+" payloads");
  let parallelRequests = [];
  let lengthParallelRequests;
  let limitParallelRequest = 10;
/*Break up payloads in parallel request limits*/
  payloads.forEach(function (payload, index)
    {
      if(!(index%limitParallelRequest))
        lengthParallelRequests = parallelRequests.push([]);
      parallelRequests[lengthParallelRequests-1].push(
        {
          'url': "https://people.googleapis.com/v1/people:batchCreateContacts",
          'method': "post",
          'contentType': "application/json",
          'payload': JSON.stringify(payload),
          'headers': { 'Authorization': "Bearer " + token }, /*token is a token of a single user*/
          'muteHttpExceptions': true
        }
      );
    }
  );
Logger.log("which makes "+parallelRequests.length+" parallelrequests");
  let responses;
  parallelRequests.forEach(function (parallelRequest)
    {
      responses = UrlFetchApp.fetchAll(parallelRequest); /* error occurs here*/
      responses = responses.map(function (response) { return JSON.parse(response.getContentText()); });
      
      responses.forEach(function (response)
        {
          if(response.error)
          {
            Logger.log(JSON.stringify(response));
            throw response;
          }
          else Logger.log("ok");
        }
      );

Output of logs:

which makes 22 payloads

which makes 3 parallelrequests

ok (15 times)

(the error message)

Upvotes: 2

Views: 1807

Answers (2)

Ranjani
Ranjani

Reputation: 1025

I had raised the same issue in Google's issue tracker.

Seems that the single BatchCreateContacts or BatchUpdateContacts call consumes six (6) "Critical Read Request" quota per request. Still did not get an answer why for creating/updating contacts, we are hitting the limit of critical read requests.

Upvotes: 3

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116868

Quota exceeded for quota metric 'Critical read requests (Contact and Profile Reads)' and limit 'Critical read requests (Contact and Profile Reads) per minute per user' of service 'people.googleapis.com' for consumer 'project_number:***'.

There are two types of quotas: project based quotas and user based quotas. Project based quotas are limits placed upon your project itself. User based quotes are more like flood protection they limit the number of requests a single user can make over a period of time.

When you send a batch request with 10 requests in it it counts as ten requests not as a single batch request. If you are trying to run this parallel then you are defiantly going to be overflowing the request per minute per user quota.

Slow down this is not a race.

Why, for creating contacts, I would hit a quota regarding read requests?

I would chock it up to a bad error message.

Given the picture link below, why would sending 13 to 15 requests hit that quota limit anyway? ((there are 3 read requests before this code)) quota limit of 90 read requests per user per minute as displayed on console.cloud.google.com

Well you are sending 13 * 10 = 130 per minute that would exceed the request per minute. There is also no way of knowing how fast your system is running it could be going faster as it will depend upon what else the server is doing at the time it gets your requests what minute they are actually being recorded in.

My advice is to just respect the quota limits and not try to understand why there are to many variables on Googles servers to be able to tack down what exactly a minute is. You could send 100 requests in 10 seconds and then try to send another 100 in 55 seconds and you will get the error you could also get the error after 65 seconds depend upon when they hit the server and when the server finished processing your initial 100 requests.

Again slow down.

Upvotes: 2

Related Questions