Skip to content

[BUG] RDB load asymmetrically applies encoding conversions #4203

Open

Description

@stockholmux

Describe the bug

In working on #3479, I observed that Valkey doesn't apply some conversions between encodings during RDB load. It appears that conversion logic is just missing.

When you load an RDB file with a hash encoded as a hashtable and the current hash-max-listpack-value configuration indicates that it should actually be listpack it will convert the key to listpack. However, if you have key encoded as a listpack it won't convert to a hashtable on RDB load even if the configuration hash-max-listpack-value indicates it should. So, it's an asymmetrical conversion.

If you create or modify a key while the server is running that exceeds hash-max-listpack-value, the key will be encoded properly.

The related configuration hash-max-listpack-entries works symmetrically. Bothhashtable -> listpack and listpack ->hashtable both work on RDB load.

While I haven't benchmarked this, the current behaviour may be more performant as I think evaluating the length of all the fields and values of a listpack would be an O(n) operation.

To reproduce

First, start Valkey with default configuration.

$ ./src/valkey-server ./valkey.conf

Then run the following commands:

flushall
hset a 1 a
hset a 2 b
hset a 3 c
hset b 1 aaaaaaaa
save

If you run the following:

object encoding a
object encoding b

It will return listpack for both

Then, kill the Valkey-server.

Then run the following:

$ echo "hash-max-listpack-entries 1\r\nhash-max-listpack-value 1\r\n" | cat valkey.conf - | ./src/valkey-server -

This will start the server with hash-max-listpack-entries and hash-max-listpack-value to 1 but with otherwise default configuration.

Then it will load the RDB file and, in theory, should do the conversion of both of these keys to hashtable since key a exceeds the hash-max-listpack-entries configuration and key b exceeds the hash-max-listpack-value configuration.

Running object encoding a returns "hashtable", which is expected.
However, running object encoding b returns "listpack", which is unexpected.

Expected behaviour

In the above test case, object encoding b should return "listpack".

Indeed, after the server is started with the lower configuration values, running hset b 1 aaaaaaaa again and object encoding b will return the expected "hashtable"

Additional information

I believe that this is happening because when RDB encounters RDB_TYPE_HASH_LISTPACK it only evaluates the number of entries. It should also convert the size of each field and value.

valkey/src/rdb.c

Line 2596 in 6b006c9

if (hashTypeLength(o) > server.hash_max_listpack_entries) hashTypeConvert(o, OBJ_ENCODING_HASHTABLE);

Activity

  1. added
    bug
    on Jul 17, 2026
  2. quanyeyang commented on Jul 18, 2026

    @quanyeyang
    Member

    Hi @stockholmux — I'd like to work on this if you don't already have a fix in progress as part of #3479.

    The plan would be to also check hash-max-listpack-value on the RDB_TYPE_HASH_LISTPACK load path (same idea as the hashtable→listpack conversion), and add a regression test for the repro in this issue.

    Happy to proceed if that sounds good; otherwise no worries if you'd rather handle it yourself.

  3. stockholmux commented on Jul 20, 2026

    @stockholmux
    MemberAuthor

    @quanyeyang I'd love the help but let's get some feedback from others if we even want to fix this first. It's one of those bugs that may be functionally undocumented, accepted behaviour, even if kind of wrong.

  4. quanyeyang commented on Jul 20, 2026

    @quanyeyang
    Member

    @stockholmux
    Sounds good — waiting for broader feedback makes sense.

    In the meantime, I can look into the implementation trade-offs, particularly the cost of scanning all fields and values in a listpack during RDB loading, and check whether there are similar conversion decisions elsewhere in the load path.

    If the consensus is that the conversion should be symmetric, I’d be happy to prepare the fix along with a regression test.

  5. JimB123 commented on Aug 7, 2026

    @JimB123
    Member

    This seems inconsistent, no matter how you slice it.

    IIRC, the reason that we don't want to expand listpacks at load time is that a snapshot might fail to load (maxmemory) when these configs are changed. Personally, I'd say that's expected, and don't change the config in that case.

    But regardless of that, the current state is inconsistent in that we DO convert based on number, which could also cause the same memory issues.

    @stockholmux do you want to raise this at Monday's meeting, to get a quick consensus? and unblock #4254

  6. stockholmux commented on Aug 9, 2026

    @stockholmux
    MemberAuthor

    @JimB123 Added. Kind of dreading it since it's kind of confusing to describe ;)

  7. ranshid commented on Aug 9, 2026

    @ranshid
    Member

    This seems like a bug indeed. I wonder why only hash related? is it not the same for zset (RDB_TYPE_ZSET_LISTPACK) set (RDB_TYPE_SET_LISTPACK)? I am not sure what about HyperLogLog sparse representation is also checked during RDB load? (maybe it also needs to be fixed)
    Overall - I agree this seems like an asymmetric - not planned bug and we should fix it.
    Regarding the backport - I see no harm in backporting it (unless I miss some potential breakage?)

  8. quanyeyang commented on Aug 9, 2026

    @quanyeyang
    Member

    This seems like a bug indeed. I wonder why only hash related? is it not the same for zset (RDB_TYPE_ZSET_LISTPACK) set (RDB_TYPE_SET_LISTPACK)? I am not sure what about HyperLogLog sparse representation is also checked during RDB load? (maybe it also needs to be fixed)
    Overall - I agree this seems like an asymmetric - not planned bug and we should fix it.
    Regarding the backport - I see no harm in backporting it (unless I miss some potential breakage?)

    Hi @ranshid, thanks for the review questions. I looked into all three while preparing #4371.

    set / zset — You're right, same bug. RDB_TYPE_SET_LISTPACK and RDB_TYPE_ZSET_LISTPACK had the same entries-only check. #4371 fixes all three together with a shared helper (lpMaxElementLength), plus regression tests in tests/unit/type/{hash,set,zset}.tcl that fail before and pass after. One nuance for zset: the listpack layout is [ele, score, ele, score, ...], and the runtime path (t_zset.c:1538) only checks sdslen(ele), never the score, so the helper skips every second entry for zsets to mirror that exactly.

    HyperLogLog — This one is a different shape of problem and I deliberately did not touch it. HLL is stored in RDB as a plain RDB_TYPE_STRING; sparse vs dense is an internal representation decided by the encoding byte in the HLL header, not an OBJ_ENCODING_* choice at the robj level. hll_sparse_max_bytes is only consulted on the PFADD write path (hyperloglog.c:887) to promote sparse→dense, and the RDB load path never re-evaluates it. Strictly speaking that's also asymmetric.

    Backport — Agreed, no breakage I can see. The load-path conversion is purely "stay as-is or upgrade to a bigger encoding", never the other direction, so a tighter config after the change can only promote more keys to hashtable/skiplist at load time.

  9. quanyeyang commented on Aug 9, 2026

    @quanyeyang
    Member

    A few more notes on scope, since they'll probably come up:

    Legacy ziplist branches (RDB_TYPE_HASH_ZIPLIST, RDB_TYPE_ZSET_ZIPLIST) — left unchanged. Current builds don't emit them (rdbGetObjectType only outputs listpack types), so they're only reachable from old RDB files and can't be exercised via DEBUG RELOAD. Happy to follow up in a separate PR if needed.(The coderabbitai point this out, so I change the code......)

    O(n) cost — same order as the existing RDB_TYPE_HASH_ZIPLIST branch (rdb.c:2392) which already scans for maxlen, and the field-by-field RDB_TYPE_HASH path. The entries check short-circuits first, so the scan only runs when entries alone wouldn't force a conversion.

    Vs #4254 / #4367 — those cover hash only; this one covers hash + set + zset (per your comment above), using a shared helper to avoid duplication.

  10. stockholmux commented on Aug 10, 2026

    @stockholmux
    MemberAuthor

    TSC meeting decision

  11. quanyeyang commented on Aug 10, 2026

    @quanyeyang
    Member

    Thanks,I'll close #4371 then.

  12. added a commit that references this issue on Oct 10, 2026
    2b4e0f4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions