You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Redis JSON stores JSON values as binary data after deserialization. This representation is often more
23
+
expensive, size-wise, than the serialized form. All JSON values occupy at least 8 bytes (on 64-bit architectures) because each is represented as a thin wrapper around a pointer. The type information is stored in the lower bits of the pointer, which are guaranteed to be zero due to alignment restrictions. This allows those bits to be repurposed to store some auxiliary data.
24
+
25
+
For some types of JSON values, 8 bytes is all that’s needed. Nulls and booleans don’t require any additional storage. Small integers are stored in static memory because they’re frequently used, so they also use only the initial 8 bytes. Similarly, empty strings, arrays, and objects don’t require any bookkeeping. Instead, they point to static instances of a _null_ string, array, or object. Here are some examples that use the [JSON.DEBUG MEMORY]({{< relref "/commands/json.debug-memory" >}}) command to report on memory consumption:
28
26
29
27
```
28
+
127.0.0.1:6379> JSON.SET boolean . 'true'
29
+
OK
30
+
127.0.0.1:6379> JSON.DEBUG MEMORY boolean
31
+
(integer) 8
32
+
33
+
127.0.0.1:6379> JSON.SET null . null
34
+
OK
35
+
127.0.0.1:6379> JSON.DEBUG MEMORY null
36
+
(integer) 8
37
+
30
38
127.0.0.1:6379> JSON.SET emptystring . '""'
31
39
OK
32
40
127.0.0.1:6379> JSON.DEBUG MEMORY emptystring
33
-
(integer) 24
34
-
```
41
+
(integer) 8
35
42
36
-
This RAM requirement is the same for all scalar values, but strings require additional space
37
-
depending on their actual length. For example, a 3-character string will use 3 additional bytes:
43
+
127.0.0.1:6379> JSON.SET emptyarr . '[]'
44
+
OK
45
+
127.0.0.1:6379> JSON.DEBUG MEMORY emptyarr
46
+
(integer) 8
38
47
39
-
```
40
-
127.0.0.1:6379> JSON.SET foo . '"bar"'
48
+
127.0.0.1:6379> JSON.SET emptyobj . '{}'
41
49
OK
42
-
127.0.0.1:6379> JSON.DEBUG MEMORY foo
43
-
(integer) 27
50
+
127.0.0.1:6379> JSON.DEBUG MEMORY emptyobj
51
+
(integer) 8
44
52
```
45
53
46
-
Empty containers take up 32 bytes to set up:
54
+
This RAM requirement is the same for all scalar values, but strings require additional space
55
+
depending on their length. For example, a 3-character string will use 3 additional bytes:
47
56
48
57
```
49
-
127.0.0.1:6379> JSON.SET arr . '[]'
50
-
OK
51
-
127.0.0.1:6379> JSON.DEBUG MEMORY arr
52
-
(integer) 32
53
-
127.0.0.1:6379> JSON.SET obj . '{}'
58
+
127.0.0.1:6379> JSON.SET foo . '"bar"'
54
59
OK
55
-
127.0.0.1:6379> JSON.DEBUG MEMORY obj
56
-
(integer) 32
60
+
127.0.0.1:6379> JSON.DEBUG MEMORY foo
61
+
(integer) 11
57
62
```
58
63
59
-
The actual size of a container is the sum of sizes of all items in it on top of its own
60
-
overhead. To avoid expensive memory reallocations, containers' capacity is scaled by multiples of 2
61
-
until a treshold size is reached, from which they grow by fixed chunks.
64
+
In the following four examples, each array requires 56 bytes. This breaks down as:
65
+
- 8 bytes for the initial array value pointer
66
+
- 16 bytes of metadata: 8 bytes for the allocated capacity and 8 bytes for the point-in-time size of the array
67
+
- 32 bytes for the array. The initial capacity of an array is 4. Therefore, the calculation is `4 * 8` bytes
62
68
63
-
A container with a single scalar is made up of 32 and 24 bytes, respectively:
64
69
```
65
70
127.0.0.1:6379> JSON.SET arr . '[""]'
66
71
OK
67
72
127.0.0.1:6379> JSON.DEBUG MEMORY arr
68
73
(integer) 56
69
74
```
70
75
71
-
A container with two scalars requires 40 bytes for the container (each pointer to an entry in the
72
-
container is 8 bytes), and 2 * 24 bytes for the values themselves:
73
76
```
74
77
127.0.0.1:6379> JSON.SET arr . '["", ""]'
75
78
OK
76
79
127.0.0.1:6379> JSON.DEBUG MEMORY arr
77
-
(integer) 88
80
+
(integer) 56
78
81
```
79
82
80
-
A 3-item (each 24 bytes) container will be allocated with capacity for 4 items, i.e. 56 bytes:
81
-
82
83
```
83
84
127.0.0.1:6379> JSON.SET arr . '["", "", ""]'
84
85
OK
85
86
127.0.0.1:6379> JSON.DEBUG MEMORY arr
86
-
(integer) 128
87
+
(integer) 56
87
88
```
88
89
89
-
The next item will not require an allocation in the container, so usage will increase only by that
90
-
scalar's requirement, but another value will scale the container again:
91
-
92
90
```
93
91
127.0.0.1:6379> JSON.SET arr . '["", "", "", ""]'
94
92
OK
95
93
127.0.0.1:6379> JSON.DEBUG MEMORY arr
96
-
(integer) 152
94
+
(integer) 56
95
+
```
96
+
97
+
Once the current capacity is insufficient to fit a new value, the array reallocates to double its capacity. An array with 5 elements will have a capacity of 8, therefore consuming `8 + 16 + 8 * 8 = 88` bytes.
Because reallocation operations can be expensive, Redis grows JSON arrays geometrically rather than linearly. This approach spreads the cost across many insertions.
107
+
108
+
This table gives the size (in bytes) of a few of the test files from the [module repo](https://github.com/RedisJSON/RedisJSON/tree/master/tests/files), stored using
109
+
JSON. The _MessagePack_ column is for reference purposes and reflects the length of the value when stored using [MessagePack](https://msgpack.org/index.html).
0 commit comments