C# / .NETDevOpsMisc
C# / .NET
RavenDB Load Balancing
Alexandru Puiu
Alexandru Puiu
October 08, 2013
1 min

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. Here is how we’re doing it with Ninject:

// RavenDB Services
Bind().ToMethod(context =>
{
  // Create the DocumentStore (expensive operation).
  var documentStore = new DocumentStore
  {
    ConnectionStringName = "RavenDB",
    // For "trusted connections": see comments at
    // http://ravendb.net/docs/client-api/connecting-to-a-ravendb-datastore
    Credentials = System.Net.CredentialCache.DefaultNetworkCredentials
  };
  // When connecting to the primary database, the RavenDB client will learn
  // of any other servers used in replication. The following tells the client
  // that it can read and write to all servers.
  documentStore.Conventions.FailoverBehavior = FailoverBehavior.ReadFromAllServers
      | FailoverBehavior.AllowReadsFromSecondariesAndWritesToSecondaries;
  // Initialize the store and return it.
  return documentStore.Initialize();
}).InSingletonScope();

By default, all servers will have read access, but only the primary database can be written to. NOTE: If you have Master-to-Master replication configured, there is a small chance you’ll have to deal with conflicts.

References

Client Integration Master-to-Master replication Transitive Replication FailoverBehavior options and Load Balancing Windows Clustering Load balancing and efficient data reading when replication is available


Tags

utils
Alexandru Puiu

Alexandru Puiu

Engineer / Security Architect

Systems Engineering advocate, Software Engineer, Security Architect / Researcher, SQL/NoSQL DBA, and Certified Scrum Master with a passion for Distributed Systems, AI and IoT..

Expertise

.NET
RavenDB
Kubernetes

Social Media

githubtwitterwebsite

Related Posts

RavenDB Integration Testing
Using RavenDB in Integration Testing
December 24, 2022
2 min

Subscribe To My Newsletter

I'll only send worthwhile content I think you'll want, less than once a month, and promise to never spam or sell your information!
© 2023, All Rights Reserved.

Quick Links

Get In TouchAbout Me

Social Media