Reading from RavenDb immediately after writing to it returns inconsistent data
Asked Answered
T

1

7

I have a reconciliation process where by a background thread that periodically retrieves a list of object ids from an external webservice and attempts to add the missing entities to an embedded RavenDb database. The loop that performs this process is the following:

foreach (var pageId in listOfPageIds)
{
    if ( _contentService.GetPageByPageId(pageId) == null)
    {
        _contentService.AddPage(pageId);
    }
}

the implementation of the GetPageByPageId() and AddPage() are as follows:

public Page GetPageByPageId(string pageId)
{
    using (var session = DocumentStore.OpenSession())
    {
        return session.Query<Page>().FirstOrDefault(page => page.PageId == pageId);
    }
}

public bool AddPage(string pageId)
{
    var page = GetPageByPageId(pageId);
    if (page != null)
    {
        return false;
    }
    using (var session = DocumentStore.OpenSession())
    {
        var newPage = new Page() {PageId = pageId};
        session.Store(newPage);
        session.SaveChanges();
    }
    return true;
}

The problem is that if the list has duplicate ids, once it adds the first id and checks for that id again, the result comes back as empty. It is as if a finalization step is missing that would register the newly added entity. If I query the set from a different thread at a later time, the entity with that given id is returned. Can anyone see what the issue is here?

thanks,

Tamtama answered 1/11, 2011 at 17:22 Comment(0)
D
18

This is a result of the eventual consistency model that Raven adopts. Updates to the indexes as a result of writes happen asynchronously and therefore it is possible that performing a read shortly after will return stale results. You can alter your query to get non-stale results like this:

session.Query<Page>().Customize(x => x.WaitForNonStaleResultsAsOfNow()).FirstOrDefault(page => page.PageId == pageId)

There's a couple of other options that Ayende covers in this blog post.

Dovelike answered 1/11, 2011 at 17:41 Comment(1)
Thanks for the quick response, that was indeed the issue!Tamtama

© 2022 - 2024 — McMap. All rights reserved.