Contact Us 1-800-596-4880

Configuring Data Storage

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:

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

// 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:

// 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 to demonstrate how to configure data storage in Rust code.

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

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 and playground/docker-compose.yaml files.