Skip to main content

A simple command-line tool for analyzing memory usage across keys in a Redis database.

Project description

redis-sizer

A simple command-line tool for analyzing memory usage across keys in a Redis database.

Example table

redis-sizer can help with the following problems:

  • Optimize caching strategy.
  • Gain insights into memory usage at different namespace levels.
  • Identify which parts of your application consume the most memory.

Installation

Install via pip:

pip install redis-sizer

Usage

Run redis-sizer by specifying the Redis host along with any desired options:

redis-sizer [OPTIONS] HOST

Options:

  • --port: Port number [default: 6379]
  • --db: DB number [default: 0]
  • --password: Password [default: None]
  • --socket-timeout: Socket timeout in seconds [default: 10]
  • --socket-connect-timeout: Socket connect timeout in seconds [default: 10]
  • --pattern: Pattern to filter keys [default: *]
  • --sample-size: Number of keys to sample [default: None]
  • --namespace-separator: Separator for key namespaces [default: :]
  • --namespace-level: Number of namespace levels to aggregate keys by. 0 means no aggregation. [default: 0]
  • --memory-unit: Memory unit for display in result table [default: B]
  • --top: Maximum number of rows to display in result table [default: 10]
  • --scan-count: COUNT option for scanning keys [default: 1000]
  • --batch-size: Batch size for calculating memory usage [default: 1000]

Aggregating Memory Usage by Key Groups

Many applications using Redis adopt hierarchical key naming conventions. redis-sizer leverages this structure to aggregate and analyze memory usage, helping you quickly identify which key groups consume the most memory.

For example, if your keys follow a colon-delimited format (e.g., users:profiles:001, users:logs:login:002), the hierarchy can be visualized like a tree structure:

DB
├── users:
│   ├── profiles:
│   │   ├── 001
│   │   └── 002
│   └── logs:
│       ├── 001
│       └── 002
├── sessions:
│   └── active:
...

Use --namespace-level 1 to aggregate memory usage at the top-level namespace:

  Key                │  Count  │ Size (MB) │ ... │ Memory Usage (%)  
╶────────────────────┼─────────┼───────────┼─────┼──────────────────╴
  users              │ 350000  │   2460.86 │ ... │           93.82  
  sessions           │  60000  │    140.23 │ ... │            5.35  
  cache              │  10000  │     20.07 │ ... │            0.77  
  tokens             │    700  │      1.75 │ ... │            0.07  
╶────────────────────┼─────────┼───────────┼─────┼──────────────────╴
  Total Keys Scanned │ 420700  │   2622.90 │ ... │          100.00  

Use --namespace-level 2 to aggregate memory usage at the second-level namespace:

  Key                │  Count  │ Size (MB) │ ... │ Memory Usage (%)  
╶────────────────────┼─────────┼───────────┼─────┼──────────────────╴
  users:profiles     │ 100000  │   1177.22 │ ... │            44.88  
  users:logs         │ 200000  │    988.01 │ ... │            37.67  
  sessions:active    │  50000  │    295.64 │ ... │            11.27  
  cache:images       │  40000  │    100.02 │ ... │             3.81  
  cache:queries      │  20000  │     40.21 │ ... │             1.53  
  tokens:auth        │    700  │      1.75 │ ... │             0.07  
╶────────────────────┼─────────┼───────────┼─────┼──────────────────╴
  Total Keys Scanned │ 420700  │   2622.90 │ ... |           100.00  

Considerations

  • Since data can change during analysis, the final result may not accurately reflect the database’s state at the beginning of the process.
  • For databases with large collections of keys, consider using the --sample-size option to balance processing time with accuracy. If --sample-size is not specified, redis-sizer will perform perform a full iteration.
  • Increasing --scan-count or --batch-size can reduce network overhead. However, this can increase the load on the Redis server without providing meaningful performance improvements and, in some cases, may even lead to server blocking. Adjust these values carefully based on your server’s capacity.

Redis Version Compatibility

  • This tool relies on the Redis MEMORY USAGE command, which has been available since version 4.0.0.
  • It has been tested and verified to work with Redis versions 4, 5, 6, and 7.

Dependencies

redis-sizer depends on:

  • redis-py: to communicate with the Redis database.
  • typer: to build CLI application, with output formatting supported by rich

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

redis_sizer-0.2.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

redis_sizer-0.2.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file redis_sizer-0.2.0.tar.gz.

File metadata

  • Download URL: redis_sizer-0.2.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.23.0 CPython/3.11.4 Darwin/24.3.0

File hashes

Hashes for redis_sizer-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8e3663ee7161a02d729a4b6ec31fd98bf67f9049aa0cebb31141f66d62b2f37b
MD5 6b616dd3328037f9ebcf6380a03341ed
BLAKE2b-256 1eaa8dcca9d4a6dcf8013783e1723137405380b4862b6200f4de79b5ab6a33b1

See more details on using hashes here.

File details

Details for the file redis_sizer-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: redis_sizer-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.23.0 CPython/3.11.4 Darwin/24.3.0

File hashes

Hashes for redis_sizer-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a6d8da147e4e7a4580eca2719053df2168d47b3862dcd32aceed3fb2ca357d45
MD5 da9b39cd1364627768b7ddf469f13951
BLAKE2b-256 bf2e724aaa449276005aeb8e426a3804359fdcc9012be93023e9d51ddd5a1230

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page