---
title: "Compare Exchange in Cluster-Wide Session"
url: "https://docs.ravendb.net/6.2/client-api/session/cluster-transaction/compare-exchange"
version: "6.2"
language: "python"
---

# Compare Exchange in Cluster-Wide Session

> Client language: Python. Also available for [C#](https://docs.ravendb.net/6.2/client-api/session/cluster-transaction/compare-exchange.md), [PHP](https://docs.ravendb.net/6.2/client-api/session/cluster-transaction/compare-exchange.php.md), [Node.js](https://docs.ravendb.net/6.2/client-api/session/cluster-transaction/compare-exchange.nodejs.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

> **Note**
>
> - Compare-Exchange items can be created and managed on the advanced session (`session.advanced`)  
>   Other options are listed in this [compare-exchange overview](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/overview.python.md#how-to-create-and-manage-compare-exchange-items).
>
> - When working with compare-exchange items from the session,  
>   the session **must be opened as a [cluster-wide session](https://docs.ravendb.net/6.2/client-api/session/cluster-transaction/overview.python.md#open-a-cluster-transaction)**.
>
> - In this page:
>   - [Create compare-exchange](#create-compare-exchange)
>   - [Get compare-exchange](#get-compare-exchange)
>   - [Delete compare-exchange](#delete-compare-exchange)

## Create compare-exchange

> **Note**
>
> #### Example
>
> ```python
> # The session must be first opened with cluster-wide mode
> session.advanced.cluster_transaction.create_compare_exchange_value(
>   key="Best NoSQL Transactional Database",
>   item="RavenDB",
> )
>
> session.save_changes()
> ```
>
> - `save_changes()` throws a `ConcurrencyException` if the key already exists.
> - A `RuntimeError` exception is thrown if the session was Not opened as **cluster-wide**.

> **Note**
>
> #### Syntax
>
> ```python
> session.advanced.cluster_transaction.create_compare_exchange_value(key, value)
> ```
>
> | Parameters | Type | Description |
> | - | - | - |
> | **key** | `str` | The key for the compare-exchange item to be created<br />This string can be up to 512 bytes |
> | **value** | `T` | The value to associate with this key |
>
> | Return Value | Description |
> | - | - |
> | `CompareExchangeValue[T]` | The new compare-exchange item is returned |

> **Note**
>
> #### The CompareExchangeValue
>
> | Parameters | Type | Description |
> | - | - | - |
> | **key** | `str` | The compare-exchange item key<br />This string can be up to 512 bytes |
> | **value** | `T` | The value associated with the key |
> | **index** | `int` | Index for concurrency control |

## Get compare-exchange

> **Note**
>
> #### Get single value
>
> ```python
> session.advanced.cluster_transaction.get_compare_exchange_value(key)
> ```
>
> | Parameters | Type | Description |
> | - | - | - |
> | **key** | `str` | The key for a compare-exchange item whose value is requested |
>
> | Return Value | Description |
> | - | - |
> | `CompareExchangeValue[T]` | The requested value<br />If the key doesn't exist the value associated with it will be `None` |

> **Note**
>
> #### Get multiple values
>
> ```python
> session.advanced.cluster_transaction.get_compare_exchange_values(keys)
> ```
>
> | Parameters | Type | Description |
> | - | - | - |
> | **keys** | `List[str]` | An array of compare-exchange keys whose values are requested |
>
> | Return Value | Description |
> | - | - |
> | `Dict[str, CompareExchangeValue[T]]` | A dictionary of requested values<br />If a key doesn't exist the value associated with it will be `None` |

> **Note**
>
> #### Get compare-exchange lazily
>
> ```python
> # Single value
> session.advanced.cluster_transaction.lazily.get_compare_exchange_value(key)
>
> # Multiple values
> session.advanced.cluster_transaction.lazily.get_compare_exchange_values(keys)
> ```
>
> | Parameters | Type | Description |
> | - | - | - |
> | **key** | `str` | The key for a compare-exchange item whose value is requested |
> | **keys** | `List[str]` | An array of compare-exchange keys whose values are requested |
>
> | Return Value | Description |
> | - | - |
> | `Lazy[CompareExchangeValue[T]]` | The requested value<br />If the key doesn't exist the value associated with it will be `None` |
> | `Lazy[Dict[str, CompareExchangeValue[T]]]` | A dictionary of requested values<br />If a key doesn't exist the value associated with it will be `None` |

## Delete compare-exchange

To delete a compare exchange item, use either of the following methods.

> **Note**
>
> ```python
> # Delete by key & index
> session.advanced.cluster_transaction.delete_compare_exchange_value(key, index)
>
> # Delete by compare-exchange item
> session.advanced.cluster_transaction.delete_compare_exchange_value(item)
> ```
>
> | Parameters | Type | Description |
> | - | - | - |
> | **key** | `str` | The key for the compare-exchange item to delete |
> | **index** | `int` | The index for the compare-exchange item to delete |
> | **item** | `CompareExchangeValue[T]` | The compare-exchange item to delete |

## See also

- [Compare Exchange: Overview](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/overview.python.md)
- [Get a Compare-Exchange Value](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/get-compare-exchange-value.md)
- [Get Compare-Exchange Values](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/get-compare-exchange-values.md)
- [Put a Compare-Exchange Value](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/delete-compare-exchange-value.md)
