Skip to content

RDB saving fails with large number of keys #1814

Closed

Description

@kevinmcgehee

If a hash table contains more than INT_MAX entries, then we've seen that no items are saved to disk during a bgsave/save. I believe the issue exists with the cast here https://github.com/antirez/redis/blob/unstable/src/dict.c#L579 because the hash table size is an unsigned long and the iterator index is a signed int.

Steps to reproduce:

  1. Fill a Redis 2.8 cluster (with activerehashing on) with > 1 billion keys. (I'm not sure the exact number needed to trigger the issue.) I used redis-benchmark to set random values.
  2. Perform a bgsave

The bgsave successfully finishes in 0 seconds, and upon inspection contains 0 items (i.e. the line linked to above is true for the first element). It looks like there isn't support for hash tables of size greater than INT_MAX even if there are fewer than INT_MAX elements in the database?

Activity

  1. mattsta commented on Jun 13, 2014

    @mattsta
    Contributor

    What OS and platform (32/64-bit) are you running? Sadly, Redis uses variable length type specifiers, so long on some platforms is 32 bits and 64 bits on others, but in either case, the iterator is too short.

    Can you checkout my dict-size branch and run your test again? I converted the iterator size to 64 bits instead of int and forced the dict size types to be 64 bits instead of unsigned long.

    Should be easy to clone and get working:

    git clone https://github.com/mattsta/redis trydict
    cd trydict
    git checkout dict-size
    make -j
    [your tests]
  2. kevinmcgehee commented on Jun 13, 2014

    @kevinmcgehee
    ContributorAuthor

    I'm using Amazon Linux 2014.03 64-bit on EC2. I'll try your branch and report back.

  3. mattsta commented on Jun 13, 2014

    @mattsta
    Contributor

    Good to know. Thanks for reporting the issue (and congrats on having so much data)! I'm looking forward to your results.

  4. kevinmcgehee commented on Jun 14, 2014

    @kevinmcgehee
    ContributorAuthor

    It's looking good. I'm able to save and load RDB files that have 1-2 billion items. Redis still segfaults when I get above 2^31 items (dictAddRaw is the culprit), but it looks like RDB dumping works as long as Redis works...

  5. mattsta commented on Jun 14, 2014

    @mattsta
    Contributor

    Oh, the int index in dictAddRaw? That's an easy fix too. I just added a fix for that to my dict-size branch.

    Great to hear it fixed the RDB dump problem.

    Did you run into the save failure actually in production or just during testing?

    For your tests, did you just run redis-benchmark with a very large -n and -r values? If you add your testing command(s) here I can double verify everything works too.

  6. antirez commented on Jun 16, 2014

    @antirez
    Contributor

    Do you have 2 billion of keys in a single instance!? :-)

    A few notes:

    • This was not done before since I assumed the dictht structure as using int, in order to save space, since it is unlikely you have an instance with a dictionary containing 2 billion items.
    • However it was broken as I used long instead, for some obscure reason. Probably because dict.h is so old that back I had no 64 bit system at all and did not cared enough.

    So now the thing is if to convert it to uint32_t and save memory, or to make it more future-proof given that the dictionary is used to hold the main hash table, not just the set/zset/hash types.

    I did a quick test, 2 million sets with a,b,c as elements. It is one of the use cases where the 32 bit integer make the biggest savings:

    • 32 bit int: 681.13M
    • 64 bit int: 708.80M

    So the difference is small... I'm more happy applying Matt's patch instead of saving this small amount of memory.

    However I'm still curious in what environment Kevin was able to store billion of keys in Redis!

  7. antirez commented on Jun 16, 2014

    @antirez
    Contributor

    @mattsta I see no pull request, I'm just merging your commit manually.

  8. antirez commented on Jun 16, 2014

    @antirez
    Contributor

    @mattsta p.s. thank you, as always...!

  9. antirez commented on Jun 16, 2014

    @antirez
    Contributor

    No wait... before merging, doesn't it actually make more sense to use unsigned long everywhere? So take the original type, but fix the iterator to also use unsigned long everywhere. You can't have billions of keys in a 32 bit instance, so why to use more memory than currently?

    unsigned long is convenient as it the same size of the memory address space in all our target architecture, so we can use a fat integer where it makes sense, and a smaller one where we'll never be able to exploit the additonal bits.

  10. mattsta commented on Jun 16, 2014

    @mattsta
    Contributor

    I see no pull request,

    Correct! I wasn't sure if these changes would even fix the problem and was half guessing at a fix (there could have been other keyspace or save size issues somewhere). If it looks correct to you, even better. :)

    fix the iterator to also use unsigned long everywhere

    The iterator currently uses -1 for index in some cases. If we remove the use of -1, the iterator/index values can be unsigned long too. I was just trying to make the code as clear as possible for immediate testing. 🐱

  11. kevinmcgehee commented on Jun 16, 2014

    @kevinmcgehee
    ContributorAuthor

    This problem was encountered during testing using a very large memory EC2 instance. 2 billion keys is a bit of a contrived test, but 1 billion keys is doable with 200+GB of RAM.

    redis-benchmark isn't the best way to reproduce this issue because it takes a while to get 1 billion unique random keys, but you can do something like

      redis-benchmark -r 2147483646 -n 5000000 -q -l -P 32 set __rand_int__ ''

    until you reach that number of keys.

  12. mattsta commented on Jul 28, 2014

    @mattsta
    Contributor

    Checking: did this fix get added? I can't track down in any commits.

  13. antirez commented on Aug 26, 2014

    @antirez
    Contributor

    Fixed in 064d5c9, I left the unsigned long approach in order to have an arch dependent type without side effects in 32 bit systems. Also switched the rehashing index to long, since otherwise it works for a quick test but then the dictionary is not able to rehash. Thanks.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions