# Checking Database Size and Related Issues

As your KeyDB data grows especially when using persistence modes like RDB or AOF—it’s essential to monitor how disk and memory resources are consumed. Uncontrolled growth can result in full disks, write failures, longer restarts, and issues with snapshot backups. While Elestio handles infrastructure hosting, managing storage cleanup and optimization is the user’s responsibility. This guide shows how to inspect keyspace usage, analyze persistence files, detect memory bloat, and tune your KeyDB deployment under Docker Compose.

## **Checking Keyspace Usage and Persistence File Size**

Like Redis, KeyDB doesn’t have schemas or tables but provides insights through built-in commands and memory metrics.

#### **Check total memory used by KeyDB**

Connect to the container:

```
docker-compose exec keydb keydb-cli INFO memory
```

Look at <span class="s1">used\_memory\_human</span> and <span class="s1">maxmemory</span> to understand current usage and configured limits.

#### **Inspect key count and TTL stats**

```
docker-compose exec keydb keydb-cli INFO keyspace
```

You’ll see entries like:

```
db0:keys=2400,expires=2100,avg_ttl=36000000
```

This helps identify how many keys are temporary and whether the dataset will grow indefinitely.

#### **View on-disk file sizes**

KeyDB writes persistence files to <span class="s1">/data</span> inside the container:

```
docker-compose exec keydb sh -c "ls -lh /data"
```

Check the size of <span class="s1">dump.rdb</span>, <span class="s1">appendonly.aof</span>, and any temporary files.

## **Detecting Bloat and Unused Space**

KeyDB supports Redis commands and adds multithreading, but it can still suffer from memory inefficiencies if not monitored properly.

#### **Estimate memory usage by key pattern**

```
docker-compose exec keydb keydb-cli --bigkeys
```

This reveals large keys by data type, helping you spot high-memory structures like oversized lists or sets.

#### **Analyze memory per key (manual sample)**

```
docker-compose exec keydb keydb-cli MEMORY USAGE some:key
```

This helps profile storage-heavy keys by prefix or type.

#### **Check memory fragmentation**

```
docker-compose exec keydb keydb-cli INFO memory | grep fragmentation
```

If <span class="s1">mem\_fragmentation\_ratio</span> is over 1.2, it may indicate inefficient memory allocation.

## **Optimizing and Reclaiming KeyDB Storage**

Once you’ve identified bloated memory areas or oversized persistence files, you can apply optimizations.

#### **Compact the AOF file**

```
docker-compose exec keydb keydb-cli BGREWRITEAOF
```

This rewrites and reduces the size of <span class="s1">appendonly.aof</span>.

#### **Delete unused keys or apply TTLs**

```
docker-compose exec keydb keydb-cli DEL obsolete:key
docker-compose exec keydb keydb-cli EXPIRE session:1234 3600
```

To bulk-delete keys by pattern (use with caution):

```
docker-compose exec keydb keydb-cli --scan --pattern "temp:*" | xargs -n 100 keydb-cli DEL
```

#### **Configure eviction policies**

In your mounted <span class="s1">keydb.conf</span>:

```
maxmemory 1gb
maxmemory-policy allkeys-lru
```

Restart the container to apply these changes. This ensures automatic cleanup when memory thresholds are exceeded.

## **Managing and Optimizing KeyDB Files on Disk**

KeyDB stores its persistent data under <span class="s2">/data</span>, which should be volume-mapped on the host system.

#### **Check disk usage**

```
docker system df
```

List all Docker volumes:

```
docker volume ls
```

Check usage for KeyDB volume:

```
sudo du -sh /var/lib/docker/volumes/<volume_name>/_data
```

#### **Clean up RDB snapshots and backups**

If using RDB snapshots, old <span class="s1">.rdb</span> files can be removed:

```bash
docker-compose exec keydb rm /data/dump-<timestamp>.rdb
```

Always offload backups to external storage rather than keeping them on the live host.

## **Best Practices for KeyDB Storage Management**

- **Use TTLs on non-permanent keys:** Set expiration on cache/session data to avoid unbounded key growth.
- **Avoid storing binary files in KeyDB:** Keep large files (images, documents, etc.) in object storage. Use KeyDB only for metadata.
- **Rotate container logs:** <span class="s2">In your </span>docker-compose.yml<span class="s2">:</span>

```yaml
logging:
  driver: "json-file"
  options:
    max-size: "10m"
    max-file: "3"
```

- **Use compact data structures:** Favor <span class="s1">HASH</span>, <span class="s1">SET</span>, or <span class="s1">ZSET</span> over storing entire JSON blobs as <span class="s1">STRING</span>.
- **Monitor and control AOF size:** Configure AOF rewrite frequency in <span class="s1">keydb.conf</span>:

```bash
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
```

- **Archive old analytical data:** Periodically move old metrics, logs, or time-series entries to cold storage.
- **Externalize backups:** Use Elestio’s backup features or configure external volumes/cloud storage to avoid accumulating snapshots on the same disk used for live data.