You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/api-management/backends.md
+39-1Lines changed: 39 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -250,11 +250,49 @@ API Management supports the following load balancing options for backend pools:
250
250
251
251
### Session awareness
252
252
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.
254
254
255
255
> [!NOTE]
256
256
> Session awareness in load-balanced pools is being released first to the **AI Gateway Early**[update group](configure-service-update-settings.md).
257
257
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-servicebackend-id="APIMBackend" />
270
+
</inbound>
271
+
<backend>
272
+
<base />
273
+
</backend>
274
+
<outbound>
275
+
<base />
276
+
<set-variablename="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);
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