Search code examples
google-app-enginegoogle-cloud-datastorebrowser-cacheapp-engine-ndb

Should I expect stale results after redirect on local environment?


After I post a new entity to the datastore, I redirect the page to a new URL that lists all of the entities in that group. When I redirect, the page shows stale results and I have to reload to see the new list of entities in the datastore.

I know about eventual consistency. Is that why I'm seeing the stale result?

For example,

my datastore my have one user - User 1 Then, in a form, I add a user - User 2 This entity is put to the datastore and then I redirect to a new url, i.e. 'get/users'

On the redirect I only see User 1, but if I refresh the page I see User 2. Any way I can guarantee or help to prevent the stale results?


Solution

  • Yes, this is caused by "eventual consistency" as you put it.

    I have a few recommendations:

    1. Use AJAX. Using a redirect results in unnecessary extra work:
      • an extra (unnecessary) HTTP request (network bandwidth, latency, server resources, mobile data costs, etc.)
      • an extra (unnecessary) datastore query to confirm what you already know
    2. Use JavaScript to update the list of users displayed to the user on success of the XMLHttpRequest; don't perform another query.
    3. If you really need the user object, you can do a get by key (not a query) from the datastore and this will be strongly consistent.
    4. If you really want a strongly consistent query, use an ancestor query, which is strongly consistent. Send the results of that query back in the success response and update your UI accordingly.
      • Note: use of ancestor queries requires an entity group, which is limited to ~ 1 write/second; this rate would be sufficient for, say, recording comments on a blog post, but would likely be insufficient for creation of new users in your application