Large sitemap

3 min read We built a social network-type site recently with quite a large number of public-facing pages, and like any good site, we created a sitemap with links to every page. All fine on launch, every page we tested finished in under 3ms of logic processing, and everyone was very happy. A few weeks pass and everything looks great, until all of a sudden all our request times go through the roof, and the database is maxing out and we’re scratching our heads at what’s causing all the load. No abnormal requests,…

RavenDB "In" operation

< 1 min read RavenDB supports an “in” operator, although vaguely documented.
First, include the namespace

using Raven.Client.Linq;

Usage

var ids = new[] { "myobjects/1", "myobjects/2" };
session
  .Query()
  .Where(o => o.Id.In(ids));

RavenDB Load Balancing

< 1 min read Somewhat counter-intuitive, this behavior is set at the client level, not the server. When servers are setup for replication, they create system documents of the other servers involved in replication. When the client accesses the primary server, it downloads and caches the replication information, so the client can “fail over” properly.
To set this up, you need to assign the FailoverBehavior convention in your DocumentStore.

RavenDB Lucene Query

< 1 min read How to build a Lucene query using extension methods, and not have the request go out until ToList() is called.

private List GetMembers(string nickname)
{
  var query = DocumentSession.Advanced.LuceneQuery(AllMembersIndex.Name);
  // Search for nickname
  if (!nickname.IsNullOrWhiteSpace())
    query = query.Search("Nickname", nickname);
  // Execute query
  return query.ToList();
}

RavenDB Create static index

2 min read

/// 
/// Gets the document store.
/// 
/// The document store.
///
/// Do this only once per AppDomain load. It's very expensive.
private static IDocumentStore GetDocumentStore()
{
    // Create the DocumentStore (expensive operation).
    IDocumentStore documentStore = new DocumentStore
    {
        ConnectionStringName = "RavenDB",
        Credentials = System.Net.CredentialCache.DefaultNetworkCredentials // For "trusted connections": see comments at http://ravendb.net/docs/client-api/connecting-to-a-ravendb-datastore
    };
    // Read from and write to all servers.
    documentStore.Conventions.FailoverBehavior = FailoverBehavior.ReadFromAllServers
        | FailoverBehavior.AllowReadsFromSecondariesAndWritesToSecondaries;
    // Initialize the store (must be done before creating indexes)
    documentStore = documentStore.Initialize();
    // Create static indexes
    CreateRavenStaticIndexes(documentStore);
    // Return document store
    return documentStore;
}