2

Is the historyId in GMail APIs guaranteed to be monotonically increasing? The documentation is not very clear on that.

In history.list documentation I read:

History IDs increase chronologically but are not contiguous with random gaps in between valid IDs.

Is that valid for other APIs as well, eg. messages.list.

I guess what I am asking is, is it safe to always store the largest value for historyId I see?

599644
  • 561
  • 2
  • 15
  • By getting a list of message/emails based on the historyId with Users.History.List, it will give you all the changes in the message/emails. So for example, let say that a label is added in the message, it will also be returned. And this changes will be incremental for the historyId. So if you use [Messages.List](https://developers.google.com/gmail/api/v1/reference/users/messages/list), the changes will not returned by this a rather a new message. For more information, check this related [SO question](http://stackoverflow.com/questions/31797416). – KENdi Mar 24 '17 at 13:54

1 Answers1

0

I don't think the historyId for the message list of one specific user is guaranteed to be monotonically increasing.

After I checked my database containing a few gmail users' metadata. I found there is no evidence showing the historyId is monotonically increasing.

Here is part of the historyId and internalDate from my gmail data.

> db.gmail_threads.find({integrationEmail: 'me@test.com'}, {_id:0,historyId:1, internalDate:1, date:1}).limit(500).sort({internalDate:1})
{ "historyId" : "4745", "date" : "Sun, 11 Sep 2016 02:43:52 -0700 (PDT)", "internalDate" : NumberLong("1473587032000") }
{ "historyId" : "2766", "date" : "Sun, 11 Sep 2016 02:43:57 -0700", "internalDate" : NumberLong("1473587037000") }
{ "historyId" : "2745", "date" : "Sun, 11 Sep 2016 02:43:57 -0700", "internalDate" : NumberLong("1473587037000") }
{ "historyId" : "3116", "date" : "Sun, 11 Sep 2016 02:43:57 -0700", "internalDate" : NumberLong("1473587037000") }
{ "historyId" : "2245", "date" : "Sun, 11 Sep 2016 09:44:31 +0000", "internalDate" : NumberLong("1473587071000") }
{ "historyId" : "2246", "date" : "Sun, 11 Sep 2016 09:44:58 +0000", "internalDate" : NumberLong("1473587098000") }
{ "historyId" : "2254", "date" : "Sun, 11 Sep 2016 09:57:24 +0000", "internalDate" : NumberLong("1473587844000") }
{ "historyId" : "2249", "date" : "Sun, 11 Sep 2016 09:57:31 +0000", "internalDate" : NumberLong("1473587851000") }
{ "historyId" : "2244", "date" : "Sun, 11 Sep 2016 09:57:37 +0000", "internalDate" : NumberLong("1473587857000") }
{ "historyId" : "4749", "date" : "Sun, 11 Sep 2016 09:57:58 +0000", "internalDate" : NumberLong("1473587878000") }
{ "historyId" : "2248", "date" : "Sun, 11 Sep 2016 03:10:57 -0700 (PDT)", "internalDate" : NumberLong("1473588657000") }
{ "historyId" : "3067", "date" : "Sun, 11 Sep 2016 16:21:12 -0700 (PDT)", "internalDate" : NumberLong("1473636072000") }
{ "historyId" : "3026", "date" : "Sun, 11 Sep 2016 16:33:27 -0700 (PDT)", "internalDate" : NumberLong("1473636807000") }
{ "historyId" : "2989", "date" : "Sun, 11 Sep 2016 23:38:35 +0000", "internalDate" : NumberLong("1473637115000") }
{ "historyId" : "5091", "date" : "Sun, 11 Sep 2016 16:42:52 -0700 (PDT)", "internalDate" : NumberLong("1473637372000") }
{ "historyId" : "2322", "date" : "Mon, 12 Sep 2016 01:46:21 -0000", "internalDate" : NumberLong("1473644781000") }
{ "historyId" : "3137", "date" : "Mon, 12 Sep 2016 01:53:24 -0000", "internalDate" : NumberLong("1473645204000") }
{ "historyId" : "2560", "date" : "Sun, 11 Sep 2016 20:17:09 -0700 (PDT)", "internalDate" : NumberLong("1473650229000") }
{ "historyId" : "2678", "date" : "Sun, 11 Sep 2016 22:12:18 -0700 (PDT)", "internalDate" : NumberLong("1473657138000") }
{ "historyId" : "2931", "date" : "Mon, 12 Sep 2016 12:22:54 +0000 (UTC)", "internalDate" : NumberLong("1473682974000") }
Type "it" for more

According to Gmail API documents, here is the limitation:

History records are typically available for at least one week and often longer. However, the time period for which records are available may be significantly less and records may sometimes be unavailable in rare cases. If the startHistoryId supplied by your client is outside the available range of history records, the API returns an HTTP 404 error response. In this case, your client must perform a full sync as described in the previous section.

scozv
  • 211
  • 2
  • 4
  • Check the [History API](https://developers.google.com/gmail/api/v1/reference/users/history/list): _"History IDs increase chronologically but are not contiguous with random gaps in between valid IDs"_ – 599644 Dec 05 '17 at 08:05
  • `HistoryID` is `unsigned long`. What are you saving as? Only 4 digits is very suspicious. – 599644 Dec 05 '17 at 08:07