Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Internally, Redis stores strings in the most efficient manner. Forcing integers into radix 10 strings will actually use more memory.</p> <p>Here is how Redis stores Strings - </p> <ol> <li>Integers less than 10000 are stored in a shared memory pool, and don't have any memory overheads. If you wish, you can increase this limit by changing the constant <a href="https://github.com/antirez/redis/blob/unstable/src/redis.h#L82">REDIS_SHARED_INTEGERS in redis.h</a> and recompiling Redis.</li> <li>Integers greater than 10000 and within range of a long consume 8 bytes. </li> <li>Regular strings take len(string) + 4 bytes for length + 4 bytes for marking free space + 1 byte for null terminator + 8 bytes for malloc overheads. </li> </ol> <p>In the example you quoted, its a question of 8 bytes for a long v/s 21 bytes for the string.</p> <p>EDIT :</p> <blockquote> <blockquote> <p>So if I have a set of numbers all less than 10,000 how does Redis store my set?</p> </blockquote> </blockquote> <p>It depends on how many elements you have.</p> <p>If you have less than 512 elements in your set (see <code>set-max-intset-entries</code>), then the set will be stored as an IntSet. An IntSet is a glorified name for a Sorted Integer Array. Since your numbers are less than 10000, it would use 16 bits per element. It is (almost) as memory efficient as a C array.</p> <p>If you have more than 512 elements, the set becomes a HashTable. Each element in the set is wrapped in a structure called <code>robj</code>, which has an overhead of 16 bytes. The <code>robj</code> structure has a pointer to the shared pool of integers, so you don't pay anything extra for the integer itself. And finally, the <code>robj</code> instances are stored in the hashtable, and the hashtable has an overhead that is proportional to the size of the set. </p> <p>If you are interested in exactly how much memory an element consumes, run <a href="https://github.com/sripathikrishnan/redis-rdb-tools">redis-rdb-tools</a> on your dataset. Or you can read the sourcecode for the class <a href="https://github.com/sripathikrishnan/redis-rdb-tools/blob/master/rdbtools/memprofiler.py#L80">MemoryCallback</a>, the comments explain how the memory is laid out.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload