samuelbrody1249
samuelbrody1249

Reputation: 4767

Redis numeric type

How are numbers usually stored in redis? For example, it seems to support a limited number of scalars, with the primary one being string type. Or, are numeric operations just done in the client, i.e., something like:

> SET id 4

>>> int(r.get(id)) + ...

Upvotes: 1

Views: 990

Answers (1)

for_stack
for_stack

Reputation: 22946

In order to be memory efficient, Redis has a complex encoding mechanism.

The value of SET command is of string type. To save a string in Redis, we need a memory block for the string, and a pointer to the memory block. However if the value can be converted to an integer, Redis will convert the string value to be an integer, and use the pointer to save the integer. So that it can save lots of memory, i.e. no need to allocate an extra memory block the string.

Also Redis preallocates 10000 objects for integer 0 to 9999. If values of keys are between 0 to 9999, Redis will make these keys SHARE the value, i.e. a pointer to these preallocated object, instead of creating new object for these keys.

Upvotes: 4

Related Questions