Retention Policies

MynaDB automatically manages disk space by deleting old date-partitioned data. Configure retention policies to automatically expire data after a specified number of days, or manually trigger retention via the API.

📅 Date-Based Partitions

Retention works on date partitions (date=YYYY-MM-DD/). Data is stored in daily partitions, making it efficient to delete entire days of data at once.

Configuration

Configure automatic retention in config.toml:

toml
[retention]
# Enable automatic background retention
enabled = true

# Delete partitions older than N days
default_days = 7

# How often to check for expired data (seconds)
interval_secs = 3600

# Optional: Databases to exclude from retention
exclude_databases = ["_system", "archive"]

# Optional: Specific tables to exclude (catalog.schema.table)
exclude_tables = ["mydb.default.important_metrics"]

Configuration Options

Option Type Default Description
enabled bool false Enable/disable automatic retention
default_days u32 7 Delete data older than N days
interval_secs u64 3600 Background check interval (seconds)
exclude_databases array [] Databases to skip during retention
exclude_tables array [] Specific tables to skip (fully qualified)

How It Works

The retention manager runs as a background task and performs these steps:

🔍

1. Scan Storage

Scans all catalogs, schemas, and tables in the data directory

📅

2. Find Partitions

Discovers date partitions (date=YYYY-MM-DD/) in each table

🕘

3. Check Age

Compares partition dates against default_days threshold

🗑

4. Delete Old Data

Removes entire partition directories for expired data

Storage Structure

text
data/
└── mydb/                           # Catalog (database)
    └── default/                    # Schema
        └── cpu/                    # Table
            ├── date=2024-01-01/    # Partition (7+ days old - DELETE)
            │   └── L0_xxx.parquet
            ├── date=2024-01-05/    # Partition (3 days old - KEEP)
            │   └── L0_xxx.parquet
            └── date=2024-01-08/    # Partition (today - KEEP)
                └── L0_xxx.parquet

Retention API

Use the REST API for manual retention operations on specific tables.

Delete by Age

Delete partitions older than a specified number of days:

bash
# Delete partitions older than 30 days
curl -X POST http://localhost:7000/retention/mydb/default/cpu \
  -H "Content-Type: application/json" \
  -d '{"older_than_days": 30}'

Response

{
  "status": "ok",
  "table": "mydb.default.cpu",
  "partitions_deleted": 5,
  "bytes_freed": 1048576000,
  "cutoff_date": "2023-12-08"
}

Delete Before Date

Delete partitions before a specific date:

bash
# Delete partitions before January 1, 2024
curl -X POST http://localhost:7000/retention/mydb/default/cpu \
  -H "Content-Type: application/json" \
  -d '{"before_date": "2024-01-01"}'

List Partitions

View existing partitions before running retention:

bash
# List all partitions for a table
curl http://localhost:7000/partitions/mydb/default/cpu

Response

{
  "catalog": "mydb",
  "schema": "default",
  "table": "cpu",
  "partitions": [
    {
      "date": "2024-01-01",
      "files": 3,
      "size_bytes": 10485760
    },
    {
      "date": "2024-01-05",
      "files": 2,
      "size_bytes": 8388608
    },
    {
      "date": "2024-01-08",
      "files": 1,
      "size_bytes": 2097152
    }
  ]
}

Delete Specific Partition

Delete a single partition by date:

bash
# Delete specific date partition
curl -X DELETE http://localhost:7000/partitions/mydb/default/cpu/2024-01-01

Example Configurations

Short-Term Metrics (1 Week)

For high-frequency metrics where you only need recent data:

toml
[retention]
enabled = true
default_days = 7
interval_secs = 3600  # Check hourly

Medium-Term Analytics (30 Days)

For dashboards and trend analysis:

toml
[retention]
enabled = true
default_days = 30
interval_secs = 86400  # Check daily

Long-Term Archive (1 Year)

For compliance and historical analysis:

toml
[retention]
enabled = true
default_days = 365
interval_secs = 86400  # Check daily
exclude_databases = ["archive"]  # Keep archive forever

Multi-Tier Retention

For different retention periods per table, use the API to set up custom retention schedules via cron jobs:

bash
#!/bin/bash
# retention_cron.sh - Run daily via cron

# High-frequency metrics: 7 days
curl -X POST http://localhost:7000/retention/mydb/default/cpu \
  -H "Content-Type: application/json" -d '{"older_than_days": 7}'

curl -X POST http://localhost:7000/retention/mydb/default/memory \
  -H "Content-Type: application/json" -d '{"older_than_days": 7}'

# Business metrics: 90 days
curl -X POST http://localhost:7000/retention/mydb/default/orders \
  -H "Content-Type: application/json" -d '{"older_than_days": 90}'

# Audit logs: 1 year
curl -X POST http://localhost:7000/retention/mydb/default/audit \
  -H "Content-Type: application/json" -d '{"older_than_days": 365}'

Monitoring Retention

Log Output

The retention manager logs its activity. Enable info-level logging to monitor:

[logging]
level = "info"

# Log output:
# INFO retention: Starting retention scan
# INFO retention: Scanning catalog=mydb
# INFO retention: Deleted 3 partitions from mydb.default.cpu, freed 15.5 MB
# INFO retention: Retention scan complete: 5 partitions deleted, 42.3 MB freed

Disk Usage Monitoring

Monitor disk usage before and after retention:

bash
# Check data directory size
du -sh /data0/stats/data/

# Check size per database
du -sh /data0/stats/data/*/

# Check size per table
du -sh /data0/stats/data/mydb/default/*/

Best Practices

✅ Do
  • Start with automatic retention enabled
  • Set interval_secs appropriately (hourly for short retention, daily for long)
  • Monitor disk usage and adjust default_days as needed
  • Use exclusions for tables that need longer retention
  • Test retention on non-production data first
⚠ Don't
  • Set interval_secs too low (causes unnecessary disk I/O)
  • Forget to exclude critical historical tables
  • Run manual retention during peak query times
  • Delete partitions that are still being written to (current date)

Capacity Planning

Calculate expected storage based on your retention policy:

# Example calculation:
# Daily data volume: 10 GB
# Retention: 7 days
# Expected storage: 10 GB x 7 = 70 GB
# Add 20% buffer: 84 GB recommended

Troubleshooting

Retention Not Running

# Check if retention is enabled in config
grep -A 5 '\[retention\]' config.toml

# Should show:
# [retention]
# enabled = true

Data Not Being Deleted

Permission Errors

# Check file permissions
ls -la /data0/stats/data/mydb/default/cpu/

# Ensure mynadb user can delete
chown -R statsservice:statsservice /data0/stats/data/

Disk Space Not Freed

If disk space isn't freed after deletion, the files may be held open:

# Check for deleted but open files
lsof +D /data0/stats/data/ | grep deleted

# Restart service to release file handles
systemctl restart mynadb

High Availability Considerations

When running in Master-Slave HA mode, retention has special considerations:

⚠ HA Retention Rules
  • Master only: Retention should only run on the master node
  • Slave follows: Slaves receive retention operations via WAL replication
  • Same config: Use identical retention config on all nodes
  • After failover: Verify retention is running on the new master
toml
# Master config
[ha]
enabled = true
role = "master"

[retention]
enabled = true      # Retention runs on master
default_days = 7

# Slave config
[ha]
enabled = true
role = "slave"

[retention]
enabled = false     # Disable on slave - replicated from master
🛈 Related Guides

Compaction - Understand how compaction affects storage
High Availability - HA setup and configuration
Configuration - Full configuration reference