> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/cdf/latest/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/components/cdf/latest/operation-guides/storage.md).

# Configure storage backend

When CDF Receiver receives Accounting Records (ACRs) from the SMF, it converts them to CDRs and publishes them to a Cennso Data Mesh (CDM) stream. The CDM stream acts as a queue where CDRs wait until CDF Sender retrieves and forwards them to the CGF. This interim storage ensures no CDRs are lost if CDF Sender temporarily cannot send to the CGF.

Storage configuration happens at two levels:

1. Configure a physical storage using the `Mesh` Custom Resource, which defines the total disk space available for all CDM data.
2. Configure the CDM stream - either using the `CDFReceiver` Custom Resource (recommended) or directly via Helm chart values.

## Steps

You can configure the storage backend for CDF using either Custom Resources or Helm charts. Follow the steps in the tab that corresponds to your deployment method.

### Steps (Custom Resource)

Follow these steps to configure the storage backend for CDF using Custom Resources:

1. Configure the physical storage capacity in the `Mesh` CR to ensure CDM has enough space to store incoming CDRs and operational data. At minimum, you must specify the following parameters:

   - **size** - total disk space allocated for CDM. This includes CDR streams, key-value stores, and all CDM data. Set this based on your expected CDR volume and retention period.
   - **memoryStoreSize** - in-memory cache size that improves read/write performance for frequently accessed data
   - **accessModes** - Kubernetes storage access mode. Use `ReadWriteOnce` for single-node access or `ReadWriteMany` if your storage supports multi-node access.

   For example:

   ```yaml
   apiVersion: cdm.cennso.com/v1alpha1
   kind: Mesh
   metadata:
     name: example-cdf
     namespace: default
   spec:
     forProvider:
       persistence:
         size: "100Gi"              # Total disk space for CDM storage
         memoryStoreSize: "2Gi"     # In-memory cache size
         accessModes:
           - ReadWriteOnce
         pvcRetentionPolicy:
           whenDeleted: "Retain"
           whenScaled: "Retain"
       connectionSecretRef:
         name: core-cluster
         namespace: cennso-infra
       clusterName: example-cdf
       mode: HA
       namespace: cdf
       resources:
         cpu: "1500m"
         memory: "2564Mi"
   ```

2. Configure the CDM stream in the `CDFReceiver` CR to manage how CDRs are stored and retained. At minimum, the stream configuration requires these parameters:

   - **url** - CDM server address (must match the `Mesh` service URL from step 1)
   - **subjects** - CDM subject where CDF Receiver publishes CDRs (use the same value as **cdmServices.workQueue.subject**)
   - **name** - stream identifier (must match **cdmServices.workQueue.streamName** so CDF Sender knows which stream to consume from)

   For example:

   ```yaml
   apiVersion: cdf.cennso.com/v1alpha1
   kind: CDFReceiver
   metadata:
     name: cdf-receiver
     namespace: default
   spec:
     connectionSecretRef:
       name: core-cluster
       namespace: cennso-infra
     targetNamespace: cdf
     config:
       hostname: cdf-receiver.example.com
       nodeID: cdf-rx-01
       cdmServices:
         workQueue:
           url: nats://example-cdf.cdf.svc.cluster.local:4237
           subject: CDR.arrived
           streamName: CDR
           consumerName: CDF
           credentialsSecretName: example-cdf-user
       cdmBucketsCreate:
         stream:
           url: nats://example-cdf.cdf.svc.cluster.local:4237
           subjects: "CDR.arrived"
           name: CDR
           storage: file
           retention: workqueue
           maxBytes: 107374182400      # 100 GB maximum stream size
           maxMsgs: 50000000           # Maximum 50 million messages
           maxAge: 86400000000000      # Delete messages older than 24 hours (in nanoseconds)
           discard: old                # When limit reached, delete oldest messages
           numReplicas: 3              # Store 3 copies for high availability
           credentialsSecretName: example-cdf-user
         consumer:
           url: nats://example-cdf.cdf.svc.cluster.local:4237
           streamName: CDR
           durableName: CDF
           ackPolicy: explicit
           ackWait: 30000000000        # Wait 30 seconds for acknowledgement before redelivery (in nanoseconds)
           deliverPolicy: all          # Process all messages from the beginning
           maxAckPending: 1000         # Maximum unacknowledged messages before pausing delivery
           credentialsSecretName: example-cdf-user
   ```

### Steps (Helm chart)

Follow these steps to configure the storage backend for CDF using Helm charts:

1. Configure the physical storage capacity in the `Mesh` CR to ensure CDM has enough space to store incoming CDRs and operational data. At minimum, you must specify the following parameters:

   - **size** - total disk space allocated for CDM. This includes CDR streams, key-value stores, and all CDM data. Set this based on your expected CDR volume and retention period.
   - **memoryStoreSize** - in-memory cache size that improves read/write performance for frequently accessed data
   - **accessModes** - Kubernetes storage access mode. Use `ReadWriteOnce` for single-node access or `ReadWriteMany` if your storage supports multi-node access.

   For example:

   ```yaml
   apiVersion: cdm.cennso.com/v1alpha1
   kind: Mesh
   metadata:
     name: example-cdf
     namespace: default
   spec:
     forProvider:
       persistence:
         size: "100Gi"              # Total disk space for CDM storage
         memoryStoreSize: "2Gi"     # In-memory cache size
         accessModes:
           - ReadWriteOnce
         pvcRetentionPolicy:
           whenDeleted: "Retain"
           whenScaled: "Retain"
       connectionSecretRef:
         name: core-cluster
         namespace: cennso-infra
       clusterName: example-cdf
       mode: HA
       namespace: cdf
       resources:
         cpu: "1500m"
         memory: "2564Mi"
   ```

2. Configure the CDM stream in your Helm `values.yaml` file to manage how CDRs are stored and retained. At minimum, the stream configuration requires these parameters:

   - **url** - CDM server address (must match the `Mesh` service URL from step 1)
   - **subjects** - CDM subject where CDF Receiver publishes CDRs (use the same value as **work_queue.subject**)
   - **name** - stream identifier (must match **work_queue.stream_name** so CDF Sender knows which stream to consume from)

   For example:

   ```yaml
   # values.yaml
   receiver:
     enabled: true
     hostname: cdf-receiver.example.com
     node_id: cdf-rx-01

   nats_services:
     work_queue:
       url: nats://example-cdf.cdf.svc.cluster.local:4237
       subject: CDR.arrived
       stream_name: CDR
       consumer_name: CDF
       creds: example-cdf-user

   nats_create:
     stream:
       enabled: true
       url: nats://example-cdf.cdf.svc.cluster.local:4237
       subjects: "CDR.arrived"
       name: CDR
       storage: file
       retention: workqueue
       max_bytes: 107374182400         # 100 GB maximum stream size
       max_msgs: 50000000              # Maximum 50 million messages
       max_age: 86400000000000         # Delete messages older than 24 hours (in nanoseconds)
       discard: old                    # When limit reached, delete oldest messages
       num_replicas: 3                 # Store 3 copies for high availability
       creds: example-cdf-user
     consumer:
       enabled: true
       url: nats://example-cdf.cdf.svc.cluster.local:4237
       stream_name: CDR
       durable_name: CDF
       ack_policy: explicit
       ack_wait: 30000000000           # Wait 30 seconds for acknowledgement before redelivery (in nanoseconds)
       deliver_policy: all             # Process all messages from the beginning
       max_ack_pending: 1000           # Maximum unacknowledged messages before pausing delivery
       creds: example-cdf-user
   ```

### Storage limits

You must configure at least one storage limit to prevent the stream from growing indefinitely. The maximum stream size limit (**maxBytes** in CR, **max_bytes** in Helm) controls how many bytes the stream can hold — when reached, CDM applies the discard policy you configure. For example, `107374182400` (100 GB) can store approximately 33 million CDRs at 3 KB each. The maximum message count limit (**maxMsgs** / **max_msgs**) sets the maximum number of messages regardless of their size. The maximum age limit (**maxAge** / **max_age**, in nanoseconds) automatically deletes messages older than the specified age — use `86400000000000` for 24 hours or `259200000000000` for 3 days. You can combine multiple limits; CDM will apply whichever limit is reached first.

### Storage behavior

The **storage** parameter determines where CDM persists the stream data. Choose `file` to persist data to disk, which is recommended for production as it survives pod restarts. Choose `memory` to store data in RAM only, which provides faster performance but loses all data if the CDM pod restarts — use this only for testing or when data loss is acceptable.

The **retention** policy controls when CDM removes messages from the stream. Use `workqueue` (recommended for CDR streams) to keep messages until CDF Sender acknowledges them, ensuring no CDRs are lost. This is the safest option for production. Use `limits` to keep messages until storage limits are reached, regardless of acknowledgement — older messages are deleted when limits are hit. Use `interest` to keep messages only while consumers are actively subscribed — when all consumers disconnect, messages are removed.

The **discard** parameter defines what happens when storage limits are reached. Use `old` (recommended) to automatically delete the oldest messages and make room for new ones—this ensures CDF continues processing new CDRs even when storage is full. Use `new` to reject new messages when limits are reached — this stops accepting new CDRs until old ones are removed, which can cause data loss if CDF Receiver cannot publish CDRs.

The number of replicas parameter (**numReplicas** / **num_replicas**) sets how many copies of each message CDM stores across different nodes. Use `3` for production deployments to ensure high availability — if one or two nodes fail, CDRs remain accessible. Use `1` only for testing or development environments where data loss is acceptable. Higher replica counts provide better availability but require more storage space and network bandwidth.
