Skip to content

Commit 33aa5e5

Browse files
committed
context adjusted for csharp client
1 parent 7efa5bb commit 33aa5e5

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

docs/client-api/configuration/load-balance/_load-balance-behavior-csharp.mdx

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ var documentStore = new DocumentStore
7777
LoadBalanceBehavior = LoadBalanceBehavior.UseSessionContext,
7878
7979
// Assign a method that sets the default context string
80-
// This string will be used for sessions that do Not provide a context string
80+
// This method will be used for sessions that do Not have their context string set explicitly
8181
// A sample GetDefaultContext method is defined below
8282
LoadBalancerPerSessionContextSelector = GetDefaultContext,
8383
@@ -92,11 +92,21 @@ var documentStore = new DocumentStore
9292
<TabItem value="LoadBalance_6" label="LoadBalance_6">
9393
<CodeBlock language="csharp">
9494
{`// A customized method for getting a default context string
95+
96+
public const string ContextKey = "RavenDB.Context";
97+
9598
private string GetDefaultContext(string dbName)
9699
\{
97-
// Method is invoked by RavenDB with the database name
98-
// Use that name - or return any string of your choice
99-
return "DefaultContextString";
100+
// Get the string set by the web application elsewhere.
101+
var ctx = System.Web.HttpContext.Current;
102+
if (ctx == null || ctx.Items.Contains(ContextKey) == false)
103+
{
104+
// return a safe default
105+
return dbName;
106+
}
107+
108+
// Return the context set elsewhere or default to the dbName
109+
return (ctx.Items[ContextKey] as string) ?? dbName;
100110
\}
101111
`}
102112
</CodeBlock>
@@ -120,12 +130,15 @@ using (var session = documentStore.OpenSession())
120130
{`// Open a session that will use a UNIQUE context string:
121131
using (var session = documentStore.OpenSession())
122132
\{
123-
// Call SetContext, pass a unique context string for this session
124-
session.Advanced.SessionInfo.SetContext("SomeOtherContext");
133+
// Call SetContext, pass a unique context string for this session.
134+
// In this case, a team identifier, where both employees belong to.
135+
session.Advanced.SessionInfo.SetContext("team/9-A");
125136
126137
// For all Read & Write requests made in this session,
127138
// node to access is calculated from the unique string & the seed defined on the store
128-
var employee = session.Load<Employee>("employees/1-A");
139+
var employee1 = session.Load<Employee>("employees/1-A");
140+
141+
var employee2 = session.Load<Employee>("employees/3-A");
129142
\}
130143
`}
131144
</CodeBlock>
@@ -250,15 +263,13 @@ using (documentStore)
250263

251264
## When to use
252265

253-
* Distributing _Read & Write_ requests among the cluster nodes can be beneficial
254-
when a set of sessions handle a specific set of documents or similar data.
255-
Load balancing can be achieved by routing requests from the sessions that handle similar topics to the same node, while routing other sessions to other nodes.
266+
* When a session handles a specific set of documents or data, that can be scoped under a shared context.
267+
Setting the context can greatly reduce chances for conflicts. It can be useful, when used with the [optimistic concurrency](../../../client-api/session/configuration/how-to-enable-optimistic-concurrency/),
268+
to ensure that requests for the same data are routed to the same node.
256269

257-
* Another usage example can be setting the session's context to be the current user.
258-
Thus spreading the _Read & Write_ requests per user that logs into the application.
270+
* When a user or a tenant owns their data and can benefit from having them served from one node. Also, in regards to the conflicts and the optimistic concurrency checks mentioned above.
259271

260-
* Once setting the load balance to be per session-context,
261-
in the case when detecting that many or all sessions send requests to the same node,
272+
* Once setting the load balance to be per session-context, in the case when detecting that many or all sessions send requests to the same node,
262273
a further level of node randomization can be added by changing the seed.
263274

264275

0 commit comments

Comments
 (0)