Skip to content

Commit 92f7c5d

Browse files
author
gitName
committed
policy for session affinity
1 parent 765ec4b commit 92f7c5d

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

articles/api-management/backends.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,49 @@ API Management supports the following load balancing options for backend pools:
250250
251251
### Session awareness
252252

253-
With any of the preceding load balancing options, optionally enable **session awareness** (session affinity) to ensure that all requests from a specific user during a session are directed to the same backend in the pool. API Management sets a cookie to maintain session state. This option is useful, for example, in scenarios with backends such as AI chat assistants or other conversational agents to route requests from the same session to the same endpoint.
253+
With any of the preceding load balancing options, optionally enable **session awareness** (session affinity) to ensure that all requests from a specific user during a session are directed to the same backend in the pool. API Management sets a session ID cookie to maintain session state. This option is useful, for example, in scenarios with backends such as AI chat assistants or other conversational agents to route requests from the same session to the same endpoint.
254254

255255
> [!NOTE]
256256
> Session awareness in load-balanced pools is being released first to the **AI Gateway Early** [update group](configure-service-update-settings.md).
257257
258+
#### Manage cookies for session awareness
259+
260+
When using session awareness, the client must handle cookies appropriately. The client needs to store the `Set-Cookie` header value and send it with subsequent requests to maintain session state.
261+
262+
You can use API Management policies to help set cookies for session awareness. For example, for the case of Azure OpenAI Assistant API, the client needs to keep the session ID, extract the thread ID from the body, and keep the pair and send the right cookie for each call. Moreover, the client needs to know when to send a cookie or when not to send a cookie header. These requirements can be handled appropriately by defining the following example policies:
263+
264+
265+
```xml
266+
<policies>
267+
  <inbound>
268+
    <base />
269+
    <set-backend-service backend-id="APIMBackend" />
270+
  </inbound>
271+
  <backend>
272+
    <base />
273+
  </backend>
274+
  <outbound>
275+
    <base />
276+
    <set-variable name="gwSetCookie" value="@{
277+
      var payload = context.Response.Body.As<JObject>();
278+
      var threadId = payload["id"];
279+
      var gwSetCookieHeaderValue = context.Request.Headers.GetValueOrDefault("SetCookie", string.Empty);
280+
      if(!string.IsNullOrEmpty(gwSetCookieHeaderValue))
281+
      {
282+
        gwSetCookieHeaderValue = gwSetCookieHeaderValue + $";Path=/threads/{threadId};";
283+
      }
284+
      return gwSetCookieHeaderValue;
285+
    }" />
286+
    <set-header name="Set-Cookie" exists-action="override">
287+
      <value>Cookie=gwSetCookieHeaderValue</value>
288+
    </set-header>
289+
  </outbound>
290+
  <on-error>
291+
    <base />
292+
  </on-error>
293+
</policies>
294+
```
295+
258296
### Example
259297

260298
Use the portal, API Management [REST API](/rest/api/apimanagement/backend), or a Bicep or ARM template to configure a backend pool. In the following example, the backend *myBackendPool* in the API Management instance *myAPIM* is configured with a backend pool. Example backends in the pool are named *backend-1* and *backend-2*. Both backends are in the highest priority group; within the group, *backend-1* has a greater weight than *backend-2*.

0 commit comments

Comments
 (0)