Golang / CosmosDB Pagination

Issue

I’m trying to implement pagination while selecting records from CosmosDB using cosmosapi package.
The azure documentation states that continuation tokens never expire and I’m trying to understand the semantics of that.

In How does Cosmos DB Continuation Token work? there is an agreement that

Documents created after serving the first page are observable on
subsequent pages

I tried to validate that point by running some experiments from a golang applicaiton, and something is not quite right. As a very high level example, if we insert three records to CosmosDB:

Insert record #1
Insert record #2
Insert record #3

Then if we try to select from the table (query = SELECT * FROM c ORDER BY c.dateField DESC) using this options:

opts := cosmosapi.QueryDocumentsOptions{
    IsQuery:              true,
    ContentType:          cosmosapi.QUERY_CONTENT_TYPE,
    ConsistencyLevel:     cosmosapi.ConsistencyLevelStrong,
    Continuation:         "",
    PartitionKeyValue:    partitionKeyValue,
    MaxItemCount:         2,
}

it returns:

record #1
record #2 
continuation token = "cont-token-1"

Now when selecting again with the same options, but different continuation token:

opts := cosmosapi.QueryDocumentsOptions{
    IsQuery:              true,
    ContentType:          cosmosapi.QUERY_CONTENT_TYPE,
    ConsistencyLevel:     cosmosapi.ConsistencyLevelStrong,
    Continuation:         "cont-token-1",
    PartitionKeyValue:    partitionKeyValue,
    MaxItemCount:         2,
}

It returns

record #3

Which is fairly logical.
Now when I try to insert record #4, and it gets inserted right after record #3, and try to fetch using "cont-token-1", record #4 does not show up. It only shows up when I regenerate the continuation tokens by selecting again using an empty opts.Continuation field.

If I try to select using an empty continuation token, then it fetches record #1 and record #2, and leads to a new token that fetches record #3 and record #4.

Is this the expected behavior? Or am I missing anything?
From my understanding, it should show up. The continuation token is like a bookmark, and it should see the results even when using the same continuation token.

Solution

A continuation token can only be used with the exact same query and will return the exact same answer every time, regardless of how you change the underlying data, you need to get a new token if your underlying data changes in such a way that would have been included in the first answer.

Answered By – Matt Douhan

Answer Checked By – Clifford M. (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.