问题描述
我试图了解通过.NET客户端SDK使用Azure DocumentDb时,会话一致性的实际含义,即定义(和限制)会话的含义.每次创建DocumentClient
的新实例时都会创建一个新的会话吗?如果使用IReliableReadWriteDocumentClient
包装器,行为是否会改变?
I am trying to understand what Sessions Consistency actually means when working with Azure DocumentDb via the .NET client SDK i.e. What defines (and bounds) a session. Is a new session created each time we create a new instance of DocumentClient
and if so does the behavior change if we are using the IReliableReadWriteDocumentClient
wrapper?
推荐答案
是的,每次您创建DocumentClient类的新实例时,都会创建一个新会话.每个DocumentClient实例维护一个集合映射->会话令牌映射.客户端保存从服务器收到的最新会话令牌,并在读取请求期间将其作为标头(x-ms-sessiontoken)回显.这使DocumentDB可以找到您集合的最新副本,以实现会话(或您自己写)的一致性.这与IReliableReadWriteDocumentClient相同,因为它是DocumentClient的包装器.
Yes, a new session is created each time you create a new instance of the DocumentClient class. Each DocumentClient instance maintains a map of collection -> session token mapping. The client saves the latest session token received from the server, and echoes it as a header (x-ms-sessiontoken) during read requests. This enables DocumentDB to locate an up-to-date replica of your collection to serve session (or read-your-writes) consistency. This is the same with IReliableReadWriteDocumentClient, since it's a wrapper over the DocumentClient.
注意:实现会话一致性的最简单方法是让一个DocumentClient实例自动为您管理.您还可以稍微复杂一些地管理多个DocumentClient实例之间的逻辑会话.例如,假设您有一个负载平衡的Web API,其中包含两个服务器,每个服务器都有一个DocumentClient实例,并且您希望这些服务器之间的会话一致性.
Note: the easiest way to achieve session consistency is to have a single DocumentClient instance manage it for you automatically. You can also manage a logical session across multiple DocumentClient instances with a little more complexity. For example, let's say that you have a load balanced Web API with two servers each with a DocumentClient instance, and you want session consistency across these servers.
- 客户端写入-> App Server 1-> DocumentDB
- 客户端读取-> App Server 2-> DocumentDB
您可以通过将步骤1中返回的x-ms-sessiontoken保存为客户端中的cookie,然后在读取请求中回显该x-ms-sessiontoken来实现此目的.通过往返会话令牌,可以获得会话一致性.
You can implement this by saving the x-ms-sessiontoken returned in step 1 by saving it as a cookie in the client, then echoing that x-ms-sessiontoken in the read request. By round-tripping the session token, you can get session consistency.
这篇关于会话一致性和.NET客户端SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!