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

同步 · dev.to / @markyu

RedisJSON: Enhancing JSON Data Handling in Redis

Introduction JSON has become the standard format for data exchange in modern...

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

Introduction

Image description

JSON has become the standard format for data exchange in modern applications. However, traditional relational databases may face performance challenges when handling JSON data. To address this issue, Redis introduced the RedisJSON module, which allows developers to store, query, and manipulate JSON data directly within the Redis database. This article will delve into RedisJSON's working principles, key operations, performance advantages, and usage scenarios.

Table of Contents

  1. Introduction to RedisJSON
  2. How RedisJSON Works
  3. Installing RedisJSON
  4. Basic Operations with RedisJSON
    • Storing JSON Data
    • Retrieving JSON Data
    • Getting JSON Data Type
    • Modifying JSON Data
    • Deleting JSON Data
    • Adding or Updating JSON Fields
    • Adding Elements to a JSON Array
    • JSONPath Queries
    • Getting JSON Array Length
    • Retrieving All Keys from a JSON Object
    • Deleting Fields in JSON
    • Complex Queries
  5. Performance Advantages
  6. Use Cases
  7. Conclusion

1. Introduction to RedisJSON

RedisJSON is an extension module for Redis that provides native support for JSON data. With RedisJSON, developers can store JSON documents in Redis and perform efficient queries and operations on them. This module simplifies data processing workflows and significantly enhances the performance of JSON data handling.

2. How RedisJSON Works

Data Storage Format

RedisJSON stores data in an optimized binary format rather than simple text. This binary format allows for fast serialization and deserialization of JSON data, improving read and write performance.

Serialization and Deserialization

Before storing data in Redis, JSON data is serialized into a compact binary string. When reading data, this binary string is deserialized back into its original JSON format for easy use by applications.

Internal Data Structure

RedisJSON uses a tree-like structure, known as a Rax tree (Redis tree), to manage JSON data. This ordered dictionary tree allows for efficient key sorting and quick insertion, deletion, and lookup operations.

Query and Operation Optimization

RedisJSON supports JSONPath syntax for complex queries, enabling developers to filter and sort JSON data efficiently. All operations on JSON data are atomic, ensuring data consistency and integrity in high-concurrency environments.

Integration with Redis Ecosystem

RedisJSON seamlessly integrates with other Redis features and tools, such as transactions, pub/sub, and Lua scripting, providing a comprehensive solution for managing JSON data.

Performance Characteristics

Despite adding support for JSON data, RedisJSON maintains Redis's high performance. Optimized internal representation and efficient query algorithms ensure fast response times even with large datasets.

3. Installing RedisJSON

Prerequisites

Ensure that Redis is installed with a version of 6.0 or higher.

Downloading RedisJSON Module

Download the RedisJSON module from the Redis website or GitHub repository. Choose the version suitable for your operating system.

Loading RedisJSON Module

In the Redis configuration file (redis.conf), add a line to load the RedisJSON module using the loadmodule directive, followed by the module file path.

Example:

loadmodule /path/to/module/rejson.so

Verifying Installation

Start the Redis server and ensure there are no errors. Connect to the Redis server using the redis-cli tool and run MODULE LIST to verify that RedisJSON is loaded successfully.

4. Basic Operations with RedisJSON

Storing JSON Data

Use JSON.SET to store JSON data.

JSON.SET user $ '{"name":"HuYiDao","age":18}'

Retrieving JSON Data

Use JSON.GET to retrieve JSON data.

JSON.GET user

Getting JSON Data Type

Use JSON.TYPE to get the type of JSON data.

JSON.TYPE user

Modifying JSON Data

Use JSON.NUMINCRBY to modify numeric fields in JSON data.

JSON.NUMINCRBY user $.age 2

Deleting JSON Data

Use the DEL command to delete a key storing JSON data.

DEL user

Adding or Updating JSON Fields

Use JSON.SET with a path to add or update fields in a JSON object.

JSON.SET user $.address '{"city": "Beijing", "country": "China"}' NX

Adding Elements to a JSON Array

Use JSON.ARRAPPEND to add elements to a JSON array.

JSON.SET user $.hobbies '["reading"]'
JSON.ARRAPPEND user $.hobbies '"swimming"'

JSONPath Queries

Use JSONPath syntax to query JSON data.

JSON.GET user '$.name'

Getting JSON Array Length

Use JSON.OBJLEN to get the length of a JSON array.

JSON.OBJLEN user $.hobbies

Retrieving All Keys from a JSON Object

Use JSON.OBJKEYS to get all keys in a JSON object.

JSON.OBJKEYS user

Deleting Fields in JSON

Use JSON.DELPATH to delete specific fields in a JSON object.

JSON.DELPATH user $.address

Complex Queries

Use JSON.QUERY for advanced querying.

JSON.QUERY user '$[?(@.city=="Beijing")]'

5. Performance Advantages

RedisJSON offers several performance advantages:

  • In-Memory Storage: Storing data in memory ensures fast read and write speeds, outperforming traditional relational databases.
  • Tree Structure Storage: The tree structure enables quick access to sub-elements, enhancing query and operation efficiency.
  • Atomic Operations: All operations on JSON data are atomic, ensuring data consistency and preventing conflicts in concurrent environments.

6. Use Cases

RedisJSON is ideal for applications requiring real-time performance, such as:

  • Content Management Systems: Efficiently store and retrieve complex content structures and metadata.
  • Product Catalogs: Manage and search product attributes and SKU combinations effectively.
  • Mobile Applications: Synchronize data across client applications in real-time.
  • Session Management: Manage user session data efficiently in web applications.

Conclusion

RedisJSON provides a powerful solution for directly storing, querying, and manipulating JSON data within Redis. By leveraging RedisJSON's capabilities, developers can efficiently handle complex JSON data structures and meet the diverse needs of modern applications. Whether for content management, product catalogs, or mobile app development, RedisJSON offers a flexible and high-performance data storage and processing solution.

By understanding and utilizing RedisJSON, developers can take full advantage of Redis's speed and efficiency while working with JSON data.


References:

  • RedisJSON Documentation
  • GitHub Repository for RedisJSON
  • Redis Labs: RedisJSON Module

相关阅读

database

The True Cost of Poor Data Quality: Why It Matters and How to Improve It

In today’s fast-paced, data-driven world, businesses have more access to data than ever before....

ipaddresses

How to Determine the Network Address from a Known IP Address

Ever wondered how devices communicate within a network? Or perhaps you've come across terms like "IP...

java

Advanced Java: Simplifying Object Property Copy and Manipulation with BeanUtil

In Java programming, the BeanUtil utility class is a powerful and convenient tool for simplifying the...

原文发布

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

在 dev.to 继续阅读
上一篇Key Considerations for Effective Database Table DesignIntroduction In database design, the structure of tables is a critical element that...
返回档案
下一篇Advanced Java: Simplifying Object Property Copy and Manipulation with BeanUtilIn Java programming, the BeanUtil utility class is a powerful and convenient tool for simplifying the...
返回档案
频道开放·随时打个招呼 · 2026
--:--:--EST
联系

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

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

开启对话

订阅

偶尔收到一封简讯

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

作品

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

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

资源

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

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

工作室

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

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

社交

在常去的平台上找到我。

  • Facebook敬请期待
  • Instagram敬请期待
  • YouTube敬请期待
  • 领英敬请期待
M4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYU
始于 2024
ZhenXiao Mark YuZhenXiao Mark Yu
© 2026 ZhenXiao Mark Yu·加拿大 安大略
  • 邮件
  • GitHub
  • dev.to
  • 领英 (敬请期待)
  • 推特 / X (敬请期待)
  • Instagram (敬请期待)
由 Next.js 16 · React 19 · Tailwind 4 构建

由 Next.js 16 · React 19 · Tailwind 4 构建