DEV Community

Cover image for RedisJSON Is Useful When You Update Parts of a Document
Mark Yu
Mark Yu

Posted on • Edited on

RedisJSON Is Useful When You Update Parts of a Document

I would not use RedisJSON just because my payload happens to be JSON.

I would use it when I need fast reads and updates against parts of a document without fetching the whole blob, changing it in app code, and writing it back.

Here is the problem.

{
  "id": "user:42",
  "name": "Mark",
  "plan": "free",
  "profile": {
    "city": "Toronto",
    "skills": ["React", "Java", "Redis"]
  },
  "usage": {
    "posts": 23,
    "revisions": 4
  }
}
Enter fullscreen mode Exit fullscreen mode

If this lives as a plain string in Redis, changing usage.revisions means:

  1. GET user:42
  2. Parse JSON in the app
  3. Modify one field
  4. SET user:42 ...

That is boring code, and boring code becomes dangerous when two requests update different fields at the same time.

RedisJSON gives you path-level commands instead.

Run It Locally

Use Redis Stack if you want the quickest local setup:

docker run --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest
Enter fullscreen mode Exit fullscreen mode

Then connect:

redis-cli
Enter fullscreen mode Exit fullscreen mode

Store a Document

JSON.SET user:42 $ '{
  "id": "user:42",
  "name": "Mark",
  "plan": "free",
  "profile": {
    "city": "Toronto",
    "skills": ["React", "Java", "Redis"]
  },
  "usage": {
    "posts": 23,
    "revisions": 4
  }
}'
Enter fullscreen mode Exit fullscreen mode

Read the whole thing:

JSON.GET user:42
Enter fullscreen mode Exit fullscreen mode

Read one field:

JSON.GET user:42 $.profile.city
Enter fullscreen mode Exit fullscreen mode

That is the first real win. You can ask for the part you need.

Update One Field

Upgrade the user:

JSON.SET user:42 $.plan '"pro"'
Enter fullscreen mode Exit fullscreen mode

Increment a counter:

JSON.NUMINCRBY user:42 $.usage.revisions 1
Enter fullscreen mode Exit fullscreen mode

Append a skill:

JSON.ARRAPPEND user:42 $.profile.skills '"Three.js"'
Enter fullscreen mode Exit fullscreen mode

Read it back:

JSON.GET user:42 $.plan $.usage $.profile.skills
Enter fullscreen mode Exit fullscreen mode

This is where RedisJSON feels better than manual serialization. Your command says exactly what changed.

Use NX and XX Intentionally

When adding fields, be explicit.

Only set profile.website if it does not exist:

JSON.SET user:42 $.profile.website '"https://www.m4rkyu.com"' NX
Enter fullscreen mode Exit fullscreen mode

Only update plan if it already exists:

JSON.SET user:42 $.plan '"team"' XX
Enter fullscreen mode Exit fullscreen mode

I like this because it turns "oops, I accidentally created the wrong shape" into a failed command instead of silent data drift.

Delete a Field

JSON.DEL user:42 $.profile.website
Enter fullscreen mode Exit fullscreen mode

Delete the whole document with regular Redis:

DEL user:42
Enter fullscreen mode Exit fullscreen mode

When I Would Use RedisJSON

Good fits:

  • Session-like documents that change small fields often.
  • Feature flags or user settings with nested structure.
  • Product/catalog snapshots that need fast reads.
  • Cached API responses where you update selected paths.

Weak fits:

  • Long-term source-of-truth data with complex relational queries.
  • Analytics workloads.
  • Huge documents that grow without discipline.
  • Anything you need to join across many entities.

RedisJSON is not a relational database replacement. It is a sharp Redis tool for JSON-shaped data.

The Practical Rule

If your app always reads and writes the entire object, plain Redis strings are probably enough.

If your app frequently changes nested fields, increments values, appends to arrays, or reads selected paths, RedisJSON earns its place.

Command Cheat Sheet

JSON.SET user:42 $ '{"name":"Mark"}'
JSON.GET user:42 $
JSON.GET user:42 $.name
JSON.SET user:42 $.plan '"pro"'
JSON.NUMINCRBY user:42 $.usage.revisions 1
JSON.ARRAPPEND user:42 $.profile.skills '"Redis"'
JSON.DEL user:42 $.profile.website
JSON.TYPE user:42 $.profile.skills
Enter fullscreen mode Exit fullscreen mode

That is enough to start. Add search/indexing later only when you have a real query that needs it.

Top comments (1)

Collapse
 
brunsos_0e8f7092b2d5f1d72 profile image
Brunsos

goated forum!