rdb

repository·master·Indexed 20 days ago

https://github.com/hdt3213/rdb

A Go-based Redis RDB parser for secondary development and memory analysis. It provides a CLI tool to convert RDB files to JSON or AOF, generate memory reports, identify big and hot keys, and visualize memory distribution via FlameGraphs. It includes features for filtering keys by regex, size, and expiration, as well as an encoder package for programmatically building RDB files in Go.

Tokens
7.6K
Snippets
30
Records
34
Agent score
51%

What's inside rdb

  1. Use the rdb CLI

    master

    The rdb command is a tool for parsing Redis RDB files (supporting versions 1 through 12, up to Redis 7.2). It provides various commands for memory analysis, data conversion, and key searching.

    Run rdb without arguments to see the full usage manual.

    $ rdb
  2. Analyze RDB files for big keys, hot keys, and memory reports

    master

    The rdb tool provides several analysis modes:

    • Memory Report: Generate a CSV memory report using -c memory.
    • AOF Conversion: Convert RDB to an AOF file using -c aof.
    • Big Keys: Find the largest keys using -c bigkey. Use -n to limit the number of results.
    • Hot Keys: Find the most frequently used keys via LFU frequency using -c hotkey. This requires the Redis instance to have been configured with maxmemory-policy allkeys-lfu or volatile-lfu.
    • Prefix Analysis: Analyze memory usage by key prefixes using -c prefix. Use -prefix-sep to specify a separator for constant memory analysis.
    rdb -c memory -o memory.csv dump.rdb
    rdb -c aof -o dump.aof dump.rdb
    rdb -c bigkey -n 10 dump.rdb
    rdb -c hotkey -n 50 dump.rdb
  3. Install the rdb tool

    master

    You can install the rdb tool using go install if you have a Go environment set up, or by downloading the executable from the releases page and adding it to your PATH.

    go install github.com/hdt3213/rdb@latest
  4. Visualize memory usage with FlameGraphs

    master

    You can generate a FlameGraph to analyze which types of keys occupy the most memory. This starts a web service on a specified port.

    Use the -c flamegraph command. You can specify the port with -port and the key separator with -sep.

    rdb -c flamegraph -port 16379 -sep : dump.rdb
  5. Convert RDB to JSON format

    master

    Convert a Redis RDB file into a JSON array of key-value objects. You can control the concurrency of the conversion using the -concurrent flag.

    Use -show-global-meta to include Redis metadata (like version and creation time) and Lua function definitions in the output.

    # Basic conversion
    rdb -c json -o dump.json dump.rdb
    
    # Conversion with 8 concurrent workers
    rdb -c json -o intset_16.json -concurrent 8 cases/intset_16.rdb
    
    # Conversion including global metadata
    rdb -c json -o function.json -show-global-meta cases/function.rdb
  6. Find big keys and hot keys

    master

    Use the bigkey command to find the largest keys in the RDB file. Use the hotkey command to find the most frequently accessed keys based on LFU (Least Frequently Used) frequency.

    Note: hotkey analysis requires that the original Redis instance was configured with maxmemory-policy allkeys-lfu or volatile-lfu.

    # Get top 10 largest keys
    rdb -c bigkey -n 10 dump.rdb
    
    # Get top 50 hottest keys
    rdb -c hotkey -n 50 -o hotkey.csv dump.rdb
  7. Install the rdb CLI

    master

    You can install the rdb tool using Go, Homebrew, or by downloading pre-built binaries.

    Using Go

    If you have Go installed, run:

    go install github.com/hdt3213/rdb@latest

    Using Homebrew

    brew install rdb

    Using Binaries

    Download the executable binary from the releases page and add it to your PATH.

    go install github.com/hdt3213/rdb@latest
  8. Visualize memory with a flamegraph

    master

    Generate a flamegraph to visualize which types of keys are consuming the most memory. This starts a web service on the specified port.

    You can use the -sep flag to define how keys should be split for the flamegraph visualization.

    rdb -c flamegraph -port 16379 -sep : dump.rdb
  9. Convert RDB to JSON

    master

    To convert a Redis RDB file to a JSON format, use the json command. You can optionally specify the output path and the number of concurrent converters to speed up the process.

    To include global metadata (like redis-ver, ctime, used-mem, and functions), add the -show-global-meta flag.

    rdb -c json -o dump.json dump.rdb
    
    # With concurrency and metadata
    rdb -c json -o function.json -show-global-meta cases/function.rdb
  10. Generate a memory usage report

    master

    Generate a CSV report estimating the memory usage of each key in the RDB file based on its encoded size. The report includes columns for database index, key, type, size (in bytes), human-readable size, and element count.

    rdb -c memory -o mem.csv cases/memory.rdb
  11. Analyze key prefixes

    master

    Analyze the distribution of keys by their prefixes. This is useful for understanding key naming patterns and their impact on memory.

    • Standard mode: Uses a radix tree for analysis.
    • Constant memory mode: Use the -prefix-sep flag to specify a separator. This uses separator-based analysis which is more memory-efficient for large datasets.
    # Standard prefix analysis (top 10, max depth 3)
    rdb -c prefix -n 10 -max-depth 3 -o prefix-report.csv dump.rdb
    
    # Constant memory prefix analysis using ':' as separator
    rdb -c prefix -n 10 -max-depth 3 -prefix-sep : -o prefix-report.csv dump.rdb
  12. Configure Prefix Analysis

    master

    Prefix analysis (-c prefix) helps understand memory distribution by key prefixes.

    Key Options:

    • -n <number>: Number of results to return.
    • -max-depth <int>: Maximum depth of the prefix tree.
    • -o <path>: Output file path (e.g., for a CSV report).
    • -prefix-sep <separator>: Use this flag to enable flat-map mode (constant memory). Instead of a radix tree, it uses the specified separator to perform analysis. You can provide multiple separators by repeating the flag.

    Example: To perform prefix analysis using : as a separator in constant memory mode: rdb -c prefix -n 10 -max-depth 3 -prefix-sep : -o prefix-report.csv dump.rdb