---
schema_version: "1"
id: 0de4edcb81d30da5ea13cf846cd6d0f0c72c9b28b6b91194f2ca75c0cc7245eb
title: Configuring Data Storage
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/pdk/latest/policies-pdk-configure-features-data-storage.md
canonical_url: https://docs.mulesoft.com/pdk/latest/policies-pdk-configure-features-data-storage
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 881eea4e86f95534fa678a188f68f06c2982d27b00f8bf670455a237d4a882b4
description: MuleSoft Documentation Site
release: "1.10"
latest: true
---
---
product: Omni Gateway Policy Development Kit (PDK)
version: 1.10
is-latest-version: true
---

# Configuring Data Storage

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

Use PDK’s `DataStorageBuilder` to implement data storage in custom policies. Choose local in-memory
storage for single-replica deployments or shared Redis storage for multi-replica deployments or
deployments that require persistent data storage. Store and retrieve data with thread-safe
operations including Compare-And-Swap (CAS), Absent, and Always operations. Configure namespaces for
key isolation and set time-to-live values for automatic data expiration in shared storage.

For multi-replica deployments or persistent data storage, you must configure shared storage. To
configure shared storage, see:

- [Configuring Redis Shared Storage for Omni Gateway in Connected Mode](https://docs.mulesoft.com/gateway/latest/conn-shared-storage-config)

- [Configuring Redis Shared Storage for Omni Gateway in Local Mode](https://docs.mulesoft.com/gateway/latest/local-shared-storage-config)

To use data storage functionality, inject the `DataStorageBuilder` in your the `entrypoint` and pass
the storage instance to your request handlers:

```Rust
// for local storage
let storage = store_builder.local(namespace);
// or for remote storage
let storage = store_builder.remote(namespace, ttl_millis);
```

Storage builder parameters:

- `namespace`: a string prefix for key isolation (required)

- `ttl_millis`: time-to-live in milliseconds for automatic data expiration (only required for remote
  storage)

After creating the storage instance, store and retrieve data with support for CAS, Absent, and
Always operations for thread safety:

```Rust
// Store data with CAS operation
let (current_data, version) = storage.get::<MyData>(&key).await?;
let mode = StoreMode::Cas(version);
storage.store(&key, &mode, &new_data).await?;
// Retrieve data - Generic type parameter <MyData> specifies the expected data type
let (data, version) = storage.get::<MyData>(&key).await?;
```

The `StoreMode` enum supports these operations:

- `Cas` (Compare-And-Swap): Updates the value only if the current version matches the expected
  version. Use this operation for thread-safe conditional updates to ensure data hasn’t changed
  since you last read it.

- `Absent`: Stores the value only if the key doesn’t exist. Use this operation to initialize values
  without overwriting existing data.

- `Always`: Always stores the value, overwriting any existing value regardless of version. Use this
  operation to unconditionally update or create a key.

## Data Storage Configuration Examples

PDK provides the
[Data Storage Stats Policy Example](https://github.com/mulesoft/pdk-custom-policy-examples/tree/1.10.0/data-storage-stats)
to demonstrate how to configure data storage in Rust code.

Within the example policy, see these functions for further configuration details:

- [`entrypoint` function](https://github.com/mulesoft/pdk-custom-policy-examples/tree/1.10.0/data-storage-stats/src/lib.rs#L210)
  for how to configure the `DataStorageBuilder`.

- [`update_request_stats` function](https://github.com/mulesoft/pdk-custom-policy-examples/tree/1.10.0/data-storage-stats/src/lib.rs#L58)
  for how the policy uses CAS operations with retry logic for concurrent updates.

- [`get_all_stats` function](https://github.com/mulesoft/pdk-custom-policy-examples/tree/1.10.0/data-storage-stats/src/lib.rs#L111)
  for how the policy retrieves stored data.

## Redis Shared Storage Configuration Example

For an example of how to configure Redis shared storage for testing, see the example
[`playground/config/shared-storage-redis.yaml`](https://github.com/mulesoft/pdk-custom-policy-examples/blob/1.10.0/data-storage-stats/playground/config/shared-storage-redis.yaml)
and
[`playground/docker-compose.yaml`](https://github.com/mulesoft/pdk-custom-policy-examples/blob/1.10.0/data-storage-stats/playground/docker-compose.yaml)
files.

## See Also

- [Sharing Data Between Requests and Responses](https://docs.mulesoft.com/pdk/latest/policies-pdk-configure-features-share-data)

- [Rate Limiting Requests](https://docs.mulesoft.com/pdk/latest/policies-pdk-configure-features-rate-limiting)
