skip to content

After moving carts to the Redis JSON type, `JSON.NUMINCRBY` calls report no error, yet some quantities never change and other lines get the increment; how do you diagnose it?

level: seniorimportance: should knowfreq 30%

answer

  1. success looks different from no error
  2. empty array versus array of null
  3. what a delete does to positions
  4. address the line, not its slot
  5. retries are not free

basics

~20 s

Read the replies: with a $ path, no match returns an empty array and a non-number returns null, and neither is an error. Increments on the wrong line point to positional paths like $.items[2] after another service's delete shifted the array.

solid answer

~40 s

Start with the reply, because JSONPath writes do not raise errors for misses. On RESP2 `JSON.NUMINCRBY` with a `$` path returns `"[]"` when nothing matched and `"[null]"` when the match is not a number, so a client that only checks for errors counts both as success. The wrong-line symptom comes from positional paths: `JSON.DEL` on an earlier line shifts every later index, so `$.items[2]` now names a different line, with no error. Fix it by addressing lines by identity, using a filter such as `$.items[?(@.sku=="A1")].qty` or an object keyed by SKU. Then check the neighbours: a retried `JSON.ARRAPPEND` duplicates a line, a `JSON.DEL` that empties the root deletes the key, and an old writer's `SET` replaces the document outright.

code

plaintext · 8 lines
plaintext
> JSON.SET cart:42 $ '{"items":{"A1":{"qty":2}}}'
OK
> JSON.SET cart:42 $.items.B7 '{"qty":1}' NX
OK
> JSON.SET cart:42 $.items.B7 '{"qty":1}' NX
(nil)
> JSON.NUMINCRBY cart:42 $.items.B7.qty 1
"[2]"

go deeper

for a junior

Remember that a JSON command can return an empty array, a null or a nil without any error, and that each of those means nothing was changed there.

for a middle

Predict the reply for no match, a non-number and a missing intermediate, and explain why deleting one array element changes what later positional paths mean.

for a senior

Run the diagnosis in order: raw replies, key type, value types, positional paths, retries. Fix by addressing lines by identity, not by adding more error handling around stale indexes.

for a principal

Make the failure unrepresentable: a shared client helper that rejects empty and null replies, a document shape keyed by identity, and a retry policy agreed per command.

## Start from what the replies say The Redis **JSON** commands report most misses in the reply, not as an error, when the path is **JSONPath** (starts with `$`). A client that only catches exceptions treats every line below as success. ```text > JSON.SET cart:42 $ '{"items":[{"sku":"A1","qty":2},{"sku":"B7","qty":"1"}]}' OK > JSON.NUMINCRBY cart:42 '$.items[?(@.sku=="Z9")].qty' 1 "[]" > JSON.NUMINCRBY cart:42 '$.items[1].qty' 1 "[null]" > JSON.SET cart:42 '$.items[5].qty' 1 (nil) ``` | Call and situation | Reply on RESP2 | Meaning | |---|---|---| | `JSON.NUMINCRBY` with `$`, no match | `"[]"` | nothing changed | | `JSON.NUMINCRBY` with `$`, match is not a number | `"[null]"` | nothing changed at that match | | `JSON.NUMINCRBY`, key missing | error | no document | | `JSON.ARRAPPEND` with `$`, match is not an array | `1) (nil)` | nothing changed at that match | | `JSON.SET`, intermediate element missing | nil | nothing changed | | `JSON.SET` with a filter, no match | wrong static path error | nothing changed | | `JSON.SET` with a filter and `XX`, no match | nil | nothing changed | The `"[null]"` case is common after a migration. A quantity written as the string `"1"` by one service's serializer is not a number, so increments on it do nothing. ## Why the wrong line changes A path such as `$.items[2].qty` names a **position**, not a line. Each command is atomic, but the service built that path from an earlier read. If another service runs `JSON.DEL cart:42 '$.items[0]'` in between, every later element moves down one place, and `$.items[2]` now means what used to be the fourth line, or nothing at all. No command failed. The stale index was the bug. ## Addressing lines by identity There are two sound shapes: - **A filter over an array.** `$.items[?(@.sku=="A1")].qty` finds the line when the command runs, so it can't go stale. It examines the array's elements, which is fine at cart sizes. - **An object keyed by SKU.** `$.items.A1.qty` needs no filter, and `JSON.SET cart:42 $.items.A1 '{"qty":1}' NX` adds a line only if it is absent. Use bracket notation for SKUs with special characters, as in `$.items["A-1"]`. ## The neighbouring traps 1. **Retries.** `JSON.ARRAPPEND` and `JSON.NUMINCRBY` are not idempotent. If a client times out after the server applied the command, a retry adds a second line or counts twice. The object shape with `NX` makes adding safe to repeat; for quantities, set the absolute value or carry a request ID. 2. **A vanishing key.** If `JSON.DEL` leaves the root object or array empty, the key is deleted too. A later `JSON.SET cart:42 $.items '[]'` then fails with `ERR new objects must be created at the root`. Removing the last element of `items` does not do this, because the root still holds `items`. 3. **Old writers.** Plain `SET` overwrites a key whatever its type, so a service not yet migrated replaces the document with a string. After that, JSON commands fail with `WRONGTYPE`. 4. **Partial application.** With a multi-match path, `JSON.SET` returns nil if **any** match failed, although others may have been applied. Re-read before assuming nothing changed. 5. **Hidden wrong types.** `JSON.MGET` returns nil for a key of another type, so a replaced cart looks merely absent. ## A diagnostic order 1. Log the raw reply of every JSON write for an affected cart, and fail on `[]`, `null` and nil. 2. Run `TYPE cart:42`, which should return `ReJSON-RL` for a JSON key. 3. Run `JSON.TYPE cart:42 '$.items[*].qty'` to find quantities stored as strings. 4. Search the code for numeric indexes in path strings, and for `SET` on the same key names. 5. Check retry settings on the client for the non-idempotent commands. ## Fixing it for good A patch to one call leaves the next service to rediscover the same trap, so make the fix structural: - **One client helper** for JSON writes, which turns `[]`, `null` and nil into a raised failure with the key and path attached. - **A metric on those failures**, so a rise after a deploy is visible before customers report it. - **A document shape keyed by identity** for anything several services change. - **A stated numeric type** for each number in the document, enforced where it is written, so that a string quantity cannot enter in the first place.

  • Why not switch the service to legacy paths, so a miss raises an error?
    A legacy path does fail loudly on a miss, and `JSON.NUMINCRBY` on a non-number errors too. But the switch keeps the stale-index bug, changes every reply shape and ties new code to the older syntax. The better fix is to keep `$`, treat `[]`, `null` and nil as failures in one client helper, and stop using positions.
  • A filter path matched two lines with the same SKU after a retry. What does `JSON.NUMINCRBY` do?
    It updates both and returns both new values, such as `"[3,2]"`, because a JSONPath write applies to every match. So a duplicate line created by a retried `JSON.ARRAPPEND` also corrupts later increments. Treat more than one element in the reply as a data fault and alert on it. Keying lines by SKU makes that duplicate impossible.

  • No error from a JSON write means the write was applied.
  • Atomic commands make positional array paths safe under concurrency.
  • A JSONPath increment that matches nothing fails with a path error.
  • A retried JSON.ARRAPPEND is safe because the command is atomic.
  • Deleting the last member from a JSON root leaves an empty document under the key.