Skip to content

Implement MEMORY USAGE command - #212

Open
surajrawat11 wants to merge 9 commits into
mansiverma897993:mainfrom
surajrawat11:feat/memory-usage
Open

surajrawat11 wants to merge 9 commits into
mansiverma897993:mainfrom
surajrawat11:feat/memory-usage

Conversation

@surajrawat11

@surajrawat11 surajrawat11 commented Nov 27, 2026 •

Copy link
Copy Markdown

This PR adds the MEMORY USAGE command to ReRu.

It returns an estimate of how much memory a key is using by reusing the existing size estimator instead of adding a separate calculation.

The optional SAMPLES argument is supported too. For now, it's accepted but doesn't affect the calculation, as mentioned in the issue.

A few other things covered in this PR:

  • Handles missing and expired keys.
  • Adds argument validation.
  • Registers the command in the command table.
  • Ensures that running MEMORY USAGE doesn't affect the LRU/LFU eviction state.

I've also added tests for different data types, invalid arguments, missing keys, transactions, and read-only behaviour.

Updated the compatibility documentation and changelog as well.

Testing

Ran the following checks:

  • cargo fmt --all -- --check
  • cargo test
  • cargo clippy --all-targets --all-features -- -D warnings
  • git diff --check

All checks are passing.

Reference: https://redis.io/docs/latest/commands/memory-usage/

MEMORY USAGE needs the per-key estimate that maxmemory accounting already
computes. Make eviction::entry_bytes pub(crate) so the keyspace module can
call it instead of adding a second estimator. The calculation is unchanged.

Tested with: cargo test.
MEMORY USAGE key [SAMPLES count] replies with the logical byte estimate
for one key by calling eviction::entry_bytes, or a nil bulk when the key is
missing or expired. SAMPLES is validated and then ignored, because the
estimate always covers the whole value: a missing or negative count and
any other option are a syntax error, and a non-integer count is rejected.

queue_error also checks MEMORY, so a missing or unknown subcommand and a
missing key abort MULTI when the command is queued, as for OBJECT.

Tested with: cargo test.
Eviction::touch treats the first argument of an unlisted command as its
key, so MEMORY USAGE would have refreshed a key named "USAGE". List MEMORY
next to OBJECT so reading an estimate never changes which key is evicted
next.

Tested with: cargo test.
Advertise memory|usage with arity -3 and its key at argument 2, and the
memory container with arity -2. COMMAND INFO, the MULTI queue-time arity
check and Cluster key routing now recognise the command.

The Keyspace row of the compatibility inventory gains MEMORY USAGE in the
same commit because a unit test keeps that table equal to the command
table.

Tested with: cargo test.
COMMAND INFO MEMORY must list memory|usage as a subcommand with arity -3
and key positions (2, 2, 1), next to the existing OBJECT assertion.

Tested with: cargo test.
For strings, lists, hashes, sets and sorted sets the estimate must grow
with the value, and the per-key numbers must add up to used_memory in
INFO memory, which sums the same estimator over the dataset. No byte
count is hardcoded.

Missing and expired keys reply nil. SAMPLES and lowercase spellings
report the same number, and reading the estimate leaves the value, the
TTL and another client's WATCH untouched.

Tested with: cargo test.
Pin the exact error replies for a missing subcommand, a missing key, an
unknown subcommand, malformed SAMPLES and unknown options, for both an
existing and a missing key.

Inside MULTI, a missing or unknown subcommand and a missing key are
rejected when queued and abort EXEC. A valid command queues and runs, and
a malformed SAMPLES queues and is reported by EXEC as that command's
reply.

Tested with: cargo test.
Under allkeys-lru the oldest key must still be the one evicted after
MEMORY USAGE measures it, and also when the oldest key is merely named
"USAGE" like the subcommand.

Tested with: cargo test.
Describe the reply in the compatibility notes: it is the key's share of
the logical dataset estimate, not allocator memory or process RSS, and
SAMPLES is accepted but ignored. List the MEMORY subcommands that are
not implemented and add the changelog entry.

Tested with: cargo test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant