Tuesday 29 August 2017

CosmosDB change feed is not a record of ALL updates

Given I have two records, PottsA age 24 and PottsB age 30, the change feed processor returns the two records:

{
  "id": "PottsA",
  "FirstName": "Andrew",
  "LastName": "Potts",
  "Age": 24,
  "_rid": "As5lALWJOgABAAAAAAAAAA==",
  "_self": "dbs/As5lAA==/colls/As5lALWJOgA=/docs/As5lALWJOgABAAAAAAAAAA==/",
  "_etag": "\"00008e03-0000-0000-0000-59a53b790000\"",
  "_attachments": "attachments/",
  "_ts": 1504000889,
  "_lsn": 8
}


{
  "id": "PottsB",
  "FirstName": "Andrew",
  "LastName": "Potts",
  "Age": 30,
  "_rid": "As5lALWJOgADAAAAAAAAAA==",
  "_self": "dbs/As5lAA==/colls/As5lALWJOgA=/docs/As5lALWJOgADAAAAAAAAAA==/",
  "_etag": "\"00009203-0000-0000-0000-59a552970000\"",
  "_attachments": "attachments/",
  "_ts": 1504006807,
  "_lsn": 10
}

If I Upsert PottsB to age 50,

await client.UpsertDocumentAsync(
UriFactory.CreateDocumentCollectionUri(databaseName, collectionName),
new Person
{
Id = "PottsB",
FirstName = "Andrew",
LastName = "Potts",
Age = 50
});

Then the change feed detects the change:
ID: PottsB
{
  "id": "PottsB",
  "FirstName": "Andrew",
  "LastName": "Potts",
  "Age": 50,
  "_rid": "As5lALWJOgADAAAAAAAAAA==",
  "_self": "dbs/As5lAA==/colls/As5lALWJOgA=/docs/As5lALWJOgADAAAAAAAAAA==/",
  "_etag": "\"00009303-0000-0000-0000-59a553860000\"",
  "_attachments": "attachments/",
  "_ts": 1504007046,
  "_lsn": 11
}

However, if I reset the checkpoint back to null:
Only two results are returned. Only the latest change to PottsB is returned.

ID: PottsA
{
  "id": "PottsA",
  "FirstName": "Andrew",
  "LastName": "Potts",
  "Age": 24,
  "_rid": "As5lALWJOgABAAAAAAAAAA==",
  "_self": "dbs/As5lAA==/colls/As5lALWJOgA=/docs/As5lALWJOgABAAAAAAAAAA==/",
  "_etag": "\"00008e03-0000-0000-0000-59a53b790000\"",
  "_attachments": "attachments/",
  "_ts": 1504000889,
  "_lsn": 8
}
ID: PottsB
{
  "id": "PottsB",
  "FirstName": "Andrew",
  "LastName": "Potts",
  "Age": 50,
  "_rid": "As5lALWJOgADAAAAAAAAAA==",
  "_self": "dbs/As5lAA==/colls/As5lALWJOgA=/docs/As5lALWJOgADAAAAAAAAAA==/",
  "_etag": "\"00009303-0000-0000-0000-59a553860000\"",
  "_attachments": "attachments/",
  "_ts": 1504007046,
  "_lsn": 11
}

Thus the change feed does not provide a list of all of the changes that took place, just the final changes to the document.
To have an immutable history of changes to a document (e.g. in an event sourcing scenario) each change must have a unique document reference.
Simply appending changes to a document itself will not provide an immutable history.

No comments:

Post a Comment