M4RKYU.SYSEdition 2027
Skip to content
LOCZH/安大略 · 加拿大/▸logs · redisjson enhancing json data handling in redis 3b5h待机OK/--:--:--EST
M4M4RK_YUportfolio
  • 创作创作
    创作Overview
    • 作品精选案例与项目记录
    • 游戏可玩原型与游戏开发日志
  • 影像影像
    影像Overview
    • 照片影像合集与视觉实验
    • 商店印刷品、海报和限量物件
  • 写作写作
    写作Overview
    • 博客长篇开发日志与现场笔记
    • 笔记短观察、链接与代码片段
  • 资源资源
    资源Overview
    • 工具38 款浏览器内开发工具
    • 链接每日使用的开发与设计书签
  • 关于关于
  • 联系联系
EN

同步 · dev.to / @markyu

RedisJSON Is Useful When You Update Parts of a Document

A practical RedisJSON walkthrough: when to use it, when not to, and the commands that actually matter.

发布日期
May 24 '24
·
阅读时长
3 min read
·
点赞
20
·
评论数
1
redisjsonbackenddatabase
在 dev.to 查看评论

本页目录

  • Run It Locally
  • Store a Document
  • Update One Field
  • Use NX and XX Intentionally
  • Delete a Field
  • When I Would Use RedisJSON
  • The Practical Rule
  • Command Cheat Sheet

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
  }
}

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

Then connect:

redis-cli

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
  }
}'

Read the whole thing:

JSON.GET user:42

Read one field:

JSON.GET user:42 $.profile.city

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"'

Increment a counter:

JSON.NUMINCRBY user:42 $.usage.revisions 1

Append a skill:

JSON.ARRAPPEND user:42 $.profile.skills '"Three.js"'

Read it back:

JSON.GET user:42 $.plan $.usage $.profile.skills

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

Only update plan if it already exists:

JSON.SET user:42 $.plan '"team"' XX

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

Delete the whole document with regular Redis:

DEL user:42

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

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

相关阅读

Bad Data Quality Costs More Than a Slow Query

A practical data quality guide for engineers: validation, ownership, schema drift, observability, and fixing bad data before dashboards lie.

data

Database Table Design Starts With the Queries You Need

A practical database table design guide focused on queries, keys, indexes, normalization, constraints, and production tradeoffs.

database

Debug a Slow MySQL Query Before You Guess at Indexes

A practical MySQL workflow for finding slow queries, reading EXPLAIN output, and deciding whether an index actually helps.

mysql

原文发布

本文首发于 dev.to,评论与点赞保留在原站。

在 dev.to 继续阅读
上一篇Database Table Design Starts With the Queries You NeedA practical database table design guide focused on queries, keys, indexes, normalization, constraints, and production tradeoffs.
返回全部文章
下一篇Java BeanUtils Copying: Convenient, but Not FreeA practical Java guide to BeanUtils, shallow copy pitfalls, reflection overhead, and when MapStruct or manual mapping is a better choice.
返回档案
M4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYU
始于 2024
ZhenXiao Mark YuZhenXiao Mark Yu
联系

看到什么有意思的?和我聊聊。

这是一个作品集,不是服务 · 但每一条留言我都会看 — 如果哪里让你有所触动,或者只想打个招呼,欢迎写信过来。

开启对话
频道开放

随时打个招呼 · 2026

--:--:--EST加拿大 安大略
  • 邮件
  • GitHub
  • dev.to
  • 领英
  • 推特 / X
  • Instagram
  • Facebook
  • YouTube
  • CodePen
  • Spotify
  • Snapchat

订阅

偶尔收到一封简讯

来自 m4rkyu.com 的笔记与日志——简短、标注日期、没有杂音。随时可退订。

作品

线上发布、游戏作品与视觉档案。

  • 项目
  • 游戏
  • 档案
  • 日志

资源

每日好用的工具与个人收藏的链接库。

  • 搜索
  • 最新
  • 工具
  • 链接
  • 笔记
  • 主题
  • 商店
RSSJSON Feed

工作室

背景、联系方式以及合作渠道。

  • 关于
  • 联系
  • 更新日志
  • 技术说明
  • 简历筹备中

社交

在常去的平台上找到我。

  • GitHub
  • dev.to
  • 领英
  • 推特 / X
  • Instagram
  • Facebook
  • YouTube
  • CodePen
  • Spotify
  • Snapchat
  • 邮件
© 2026 ZhenXiao Mark Yumarkyu0615@gmail.com
  • 邮件
  • GitHub
  • dev.to
  • 领英
  • 推特 / X
  • Instagram
  • Facebook
  • YouTube
  • CodePen
  • Spotify
  • Snapchat
隐私条款由 Next.js 16 · React 19 · Tailwind 4 构建