skip to content

After moving carts from serialized strings to the Redis JSON type, memory use grew sharply; how do you measure the per-document cost and explain the growth?

level: seniorimportance: should knowfreq 33%

answer

  1. text versus a tree of values
  2. a floor under every value
  3. arrays reserve more than they use
  4. one reading counts strings repeatedly
  5. deleting does not shrink a container

basics

~20 s

A Redis JSON document is a tree, so each value costs at least 8 bytes on 64-bit and arrays reserve spare capacity. JSON.DEBUG MEMORY and MEMORY USAGE read one key but over-count reused strings, so confirm with used_memory on real data.

solid answer

~50 s

A serialized string costs roughly its text length. A JSON document is stored as binary values, and the docs say that form is often larger than the text: every value takes at least 8 bytes on 64-bit, strings add their length, and an array carries metadata and capacity that doubles as it grows. The docs' table shows a 381-byte file at 1,069 bytes and a 46,333-byte file at 57,513, so small nested documents can pay most. Measure with `JSON.DEBUG MEMORY key [path]` to find the heavy subtree, and `MEMORY USAGE key` to include the key's overhead. Both over-count strings, because Redis stores a repeated string once and reports it each time. For the real figure, load a representative batch on a test instance and compare `used_memory`. Removing values does not free a container's allocation either.

code

plaintext · 8 lines
plaintext
> JSON.SET arr . '["", "", "", ""]'
OK
> JSON.DEBUG MEMORY arr
(integer) 56
> JSON.SET arr . '["", "", "", "", ""]'
OK
> JSON.DEBUG MEMORY arr
(integer) 88

go deeper

for a junior

Remember that the JSON type usually takes more memory than the same text in a string, and that JSON.DEBUG MEMORY reports a value's size in bytes.

for a middle

Explain where the bytes go: an 8-byte floor per value on 64-bit, string lengths, array metadata and doubling capacity. Say why small nested documents have the worst ratio.

for a senior

Distinguish the three readings, explain why string reuse makes per-key figures overstate, and show the batch method with used_memory. Mention that deleted values do not release a container's capacity.

for a principal

Turn the numbers into a budget: bytes per document times expected documents, measured on your Redis version, weighed against what partial updates save in traffic and retries.

## Why the same data takes more room A **serialized string** is the JSON text plus a small header. The Redis **JSON** type parses that text and stores **binary values in a tree**, trading space for the ability to reach inside the document. The docs say this form is often more expensive, size-wise, than the serialized text. The documented costs, with examples measured on Redis 8: - **Every value** occupies at least 8 bytes on a 64-bit build, as a thin wrapper around a pointer. - **Nulls, booleans, small integers** and empty strings, arrays or objects need only those 8 bytes. - **Strings** add their length: `"bar"` reports 11 bytes. - **Arrays** add 16 bytes of metadata plus 8 bytes per slot of capacity. Capacity starts at 4 and doubles, so one to four elements report 56 bytes and a fifth jumps to 88. Because of ongoing optimizations, the docs warn that these numbers vary by version, and the 8.10 release notes list further JSON footprint improvements. Read them as the shape of the cost, not as constants, and measure your own version. ## How much bigger The docs' own table compares file sizes with stored size: | File size in bytes | Stored as JSON type | |---|---| | 381 | 1,069 | | 1,387 | 2,190 | | 3,718 | 5,469 | | 22,466 | 26,901 | | 46,333 | 57,513 | The ratio runs from about 2.8 times at the smallest file to about 1.2 times at the largest. File size is not the whole story, since these files differ in shape. The per-value floor means **many short values in nested containers** weigh most, and that is what a small cart document is. The official comparison also ranks JSON as generally the most demanding of the three document types, followed by hashes and then strings. ## Measuring it ```text > JSON.SET arr . '["", "", "", "", ""]' OK > JSON.DEBUG MEMORY arr (integer) 88 > JSON.DEBUG MEMORY arr $ 1) (integer) 88 ``` The 88 is the docs' figure on Redis 8, and the second form's array reply is from the 8.10 source. Three readings answer different questions: 1. **`JSON.DEBUG MEMORY key [path]`** reports a value's size in bytes. With a `$` path the 8.10 source returns an array of sizes, and with no path or a legacy path a single integer. Pointing it at sub-paths finds the heavy subtree. 2. **`MEMORY USAGE key`** adds the key and its administrative overhead. In the 8.10 source the JSON part of it comes from the same accounting function, so it inherits the same behaviour with strings. 3. **`used_memory`** in `INFO memory` is what the allocator reports for the whole instance, and the only one of the three that reflects string reuse. The reason for the third reading is the **string reuse mechanism**. A string that appears repeatedly, within a document or across documents on the same node, is stored once and referenced. `JSON.DEBUG MEMORY` still counts every instance, so the docs warn that real consumption can be much lower than it reports. Summing per-key readings therefore overstates a fleet full of repeated values such as `"EUR"`. The sound method: load a representative batch into a test instance, compare `used_memory` before and after, and divide by the document count. Then do the same with the strings you have today. ## Why it does not come back The docs state that deleting values from containers **does not free the container's allocated memory** in the current version. A cart that once held many lines keeps its capacity after they are removed. Replacing the document with a root `JSON.SET` builds the containers afresh, which is one way to reclaim it on long-lived documents. ## What to do about it - **Check the premise.** If most reads and writes take the whole document, a string costs less and delivers the same. - **Shrink the document.** Keep large rarely-read blobs out of it, and drop fields a service can derive. - **Prefer a hash for flat data.** Redis's guidance says hashes carry lower overhead when nesting is not needed. - **Expire idle documents.** A key-level TTL on abandoned carts can reclaim more than a format change. - **Budget per document.** Set a size target, and sample `JSON.DEBUG MEMORY` against it in a job.

  • Why might summing `JSON.DEBUG MEMORY` across all carts give more than `used_memory` shows?
    Because the command reports each string as if stored independently, while Redis keeps one copy of a repeated string per node and references it. Carts sharing currency codes and SKU strings can therefore sum to more than the server really allocated. Use per-key figures to compare documents and find heavy subtrees, and use `used_memory` deltas on a representative batch for capacity planning.
  • Why does `JSON.DEBUG MEMORY cart:42 $.items` return an array when `JSON.DEBUG MEMORY cart:42` returns a number?
    A `$` path can match several locations, so the 8.10 source replies with one size per match, while no path or a legacy path gives a single integer. That makes it a useful probe: `$.items[*]` returns a size for each line, which shows whether one oversized line or the sheer number of lines drives the cost.

  • JSON type documents always take less memory because the binary form is compressed.
  • JSON.DEBUG MEMORY is the exact real allocation for a key.
  • Removing array elements with JSON.DEL immediately shrinks the array's allocation.
  • The per-value byte figures in the docs are fixed for every Redis version.
  • Summing JSON.DEBUG MEMORY across keys gives the instance's real JSON memory.

Assumes Redis 8.10; the docs' byte figures were measured on Redis 8 and warn that JSON memory use varies by version, and 8.10's release notes list footprint improvements.