A cart stored as a serialized string under one Redis key is updated by several services; how do `JSON.NUMINCRBY`, `JSON.ARRAPPEND` and `JSON.DEL` change that?
answer
- who does the parsing now
- lost updates between GET and SET
- one command, one atomic step
- replies carry only the change
- several commands are not one
basics
~20 sEach command changes one part of the document inside the server as one atomic step, so services stop overwriting each other's read-modify-write cycles. Only the change travels, though an invariant spanning several commands still needs a transaction.
solid answer
~40 sWith a serialized string, each service runs `GET`, parses, edits and `SET`s the whole value, so two services working at once lose one update. With the JSON type the server makes the change. `JSON.NUMINCRBY cart $.items[0].qty 1` adds to one number, `JSON.ARRAPPEND cart $.items '{...}'` adds a line, and `JSON.DEL cart $.items[0]` removes one, each executing whole, so concurrent commands cannot interleave inside it. Only the command and a short reply cross the network, and in the 8.10 source replicas receive the command as sent, not the whole document. On a single matched value `JSON.NUMINCRBY` and `JSON.ARRAPPEND` are documented as O(1). Atomicity covers one command only: changing a line and a total together needs `MULTI`/`EXEC` or a script, and a read-then-decide needs `WATCH` or a script.
code
plaintext · 9 lines> MULTI
OK
> JSON.NUMINCRBY cart:42 '$.items[?(@.sku=="B7")].qty' 1
QUEUED
> JSON.NUMINCRBY cart:42 $.totalCents 500
QUEUED
> EXEC
1) "[2]"
2) "[4498]"go deeper
Name the three commands and what each changes: add to a number, add to an array, remove a value. Say that the server applies each one, so no service rewrites the whole document.
Explain the lost update in a GET, parse and SET cycle, then predict each command's reply with a dollar-sign path and its documented cost on one match.
Draw the atomicity boundary precisely: one command, one key. Say when MULTI and EXEC, WATCH or a script are needed, and why money should be stored as integers.
Decide whether services may share a document at all. Partial updates remove lost writes, but shared ownership of one document's shape still needs a schema owner and a review habit.
## What goes wrong with a serialized string Take a shopping cart kept as JSON text under one key. Pricing updates totals, the storefront adds lines and inventory removes lines that sold out. To Redis the value is only bytes, so every change takes a cycle in the service: `GET` the text, parse it, edit it, serialize it and `SET` it back. That cycle is the **lost update**. Two services read the same version, each makes its own edit and each writes back the whole document, so the second write erases the first: 1. Pricing reads the cart with `qty` at 2 and `totalCents` at 3998. 2. The storefront reads the same text and adds a line. 3. The storefront writes its version back. 4. Pricing writes its version back, and the new line is gone. Neither service saw an error, and the cart looks valid. A `WATCH` retry loop can prevent that, but it still moves the full document across the network on every attempt, and busy carts retry often. ## What the JSON type changes The **JSON** type keeps the document parsed on the server and offers commands that act on a path inside it. Redis runs commands one at a time, so one of these commands completes before another begins. The Redis docs describe these as **atomic partial updates**. ```text > JSON.SET cart:42 $ '{"totalCents":3998,"items":[{"sku":"A1","qty":2,"priceCents":1999}]}' OK > JSON.NUMINCRBY cart:42 '$.items[?(@.sku=="A1")].qty' 1 "[3]" > JSON.ARRAPPEND cart:42 $.items '{"sku":"B7","qty":1,"priceCents":500}' 1) (integer) 2 > JSON.DEL cart:42 '$.items[?(@.sku=="A1")]' (integer) 1 ``` Replies above are RESP2, with `redis-cli`'s default protocol. | Command | What it does | Reply with a `$` path | Documented cost on one match | |---|---|---|---| | `JSON.NUMINCRBY` | adds to a number | JSON array of new values | O(1) | | `JSON.ARRAPPEND` | adds to the end of an array | array of new lengths | O(1) | | `JSON.DEL` | removes values at the path | count of paths deleted | O(N), N the deleted value's size | | `JSON.SET` | replaces or adds a value | `OK` or nil | O(M+N), old and new value size | These costs assume the path evaluates to a single value. With several matches the documented cost grows to the size of the key. Finding the location also takes work, since the path page counts a search for every level and a scan of the keys in each parent object, with array access O(1). ## What you gain - **No lost updates inside one command.** Two simultaneous increments of 1 on a quantity of 2 always end at 4. - **Less traffic.** The request carries only the change, and the reply only the result. A read can also fetch one member instead of the whole document. - **Smaller replication traffic.** In the 8.10 source a JSON write is replicated verbatim, so replicas and the AOF receive the short command, not a rewritten document. - **No rewriting of other services' parts.** Services still agree on the document's shape, but each one changes only the path it owns. ## What atomic does not cover One command is one step, and that boundary is easy to overstate. 1. **Two related values.** Raising a line's `qty` and the cart's `totalCents` takes two commands, because `JSON.NUMINCRBY` applies a single amount per call. Wrap them in `MULTI`/`EXEC` or a Lua script, or drop the stored total and derive it when the cart is read. 2. **Read, then decide.** If a service reads the quantity and acts on what it saw, another service can change it in between. `WATCH` on the key detects that, at whole-key granularity. 3. **Several documents.** `JSON.MSET` writes key, path and value triplets atomically, but it assigns values and cannot increment them, and on Redis Cluster all its keys must share a hash slot. 4. **Money as a float.** Two integers stay an integer, with an overflow error at the 64-bit limit. Any float makes the result a double, with binary rounding, so store cents as integers. The practical rule: put each independent change in one JSON command, and reach for a transaction only where two values really must move together. Most cart changes turn out to be independent, which is why this move removes so many retry loops.
- Does `JSON.NUMINCRBY` create a member that does not exist, the way `INCR` creates a missing key?No. `INCR` treats a missing key as 0, but `JSON.NUMINCRBY` on a missing key is an error, and with a `$` path that matches nothing it returns an empty array and changes nothing. Create the member first, for example with `JSON.SET cart $.items[0].qty 0 NX`, or include it when the document is created.
- How does `JSON.MERGE` compare with several `JSON.SET` calls for updating a cart's fields?`JSON.MERGE` applies a JSON merge patch at a path as one command, so several members can change together: members in the patch are added or replaced, and a member set to `null` is deleted. It suits patching a known shape. It still assigns values, so it cannot express an increment, and that stays with `JSON.NUMINCRBY`.
- Why is `JSON.ARRAPPEND` a better fit than `JSON.ARRINSERT` for adding a line to a large array?`JSON.ARRAPPEND` writes at the end and is documented as O(1) for one match, because elements stay where they are. `JSON.ARRINSERT` is documented as O(N) in the array's size, since later elements have to shift. Shifting also moves every later element's index, which matters to any service still addressing lines by position.
A serialized string is a paper order form that each clerk photocopies, edits at their desk and uses to replace the original, so the last clerk erases the others' changes. The JSON type keeps one form at a counter, where each clerk's single-line change is made in turn. A change touching two lines is still two visits unless the clerk reserves the counter.
- Two simultaneous JSON.NUMINCRBY calls on one number can still lose an increment.
- MULTI is needed to make a single JSON.NUMINCRBY atomic.
- One JSON.NUMINCRBY can apply different amounts to a line and to the total.
- Floating-point prices are safe in JSON.NUMINCRBY because the type stores exact decimals.
- JSON.GET must transfer the whole document even when you ask for one member.