sgon00
sgon00

Reputation: 5777

How to calculate hash-max-ziplist-value properly?

My question example: HMSET myhash field1 value1 field2 value2 and myhash only has these two fields.

The main question is how to calculate hash-max-ziplist-value so that my hash will not exceed the value to maintain my hash in a compressed format to minimize the memory usage.

Thank "Kevin Christopher Henry" very much for his detail explanation, help and time. Due to my limited English, I will summarize Kevin's answer here. Please correct me if what I understand is wrong.

(1) To meet hash-max-ziplist-value, I need to calculate max(field1, value1, field2, value2). Let's assume value1 has the biggest size. Then I just need to make sure the size of value1 does not exceed hash-max-ziplist-value.

(2) To calculate value1, I just need to calculate the number of bytes in size. Because hash-max-ziplist-value is the number of bytes for the string value before any compression.

(3) To calculate the number of bytes for value1, there are many ways and one of them is as follows: First, convert value1 to UTF8 encoding if it's not. Second, check the length of it by using the client language. Because The length of UTF8 encoded string is the number of bytes in Size. (for instance: utf8.encode(value1).length).

Originl Post

For example, HMSET myhash field1 value1 field2 value2

Is the above example one entry or two entries because it has two fields?

(a) MEMORY USAGE myhash

(b) the sum size of field1, value1, field2, value2

(c) the sum size of value1 and value2.

(d) max(value1, value2)?

(e) max(field1+value1, field2+value2)

Thank you very much for your help.

Upvotes: 3

Views: 3635

Answers (1)

Kevin Christopher Henry
Kevin Christopher Henry

Reputation: 48972

These values are briefly described in the redis.conf file, as well as the memory optimization documentation.

# Hashes are encoded using a memory efficient data structure when they have a
# small number of entries, and the biggest entry does not exceed a given
# threshold. These thresholds can be configured using the following directives.
hash-max-ziplist-entries 512
hash-max-ziplist-value 64

Using these default values as an example, if the hash has 512 or fewer entries, and each is 64 bytes or smaller, the hash will be encoded using a ziplist.

Although the documentation doesn't say exactly how the size of a hash entry is calculated, a look at the source code indicates that both the field name and the value must have a size less than or equal to the threshold. You should be able to determine the size by computing the length, in bytes, of the binary string.

To answer some of your specific questions:

Is the above example one entry or two entries because it has two fields?

Two.

What is hash-max-ziplist-value?

Using your terminology, this would be max(field1, value1, field2, value2).

Is that utf-8 encoded string calculation?

Redis works with binary strings. It's up to you (or your client) to decide what encoding to use.

Is there any easy way to calculate myhash value in bytes for hash-max-ziplist-value? Is there an existing command in redis for this calculation?

Not that I know of, but the length of the binary string representation of the value should be approximately right.

Upvotes: 4

Related Questions