>_ IzzyLab

Hands-on task

This task is step 16 of the course “Redis in production”

A JSON document in Redis

easy~7 min

A user profile is in the file /root/user.json. Store it in Redis as a JSON document (the JSON module, not a string) under the key user:1, and then, without overwriting the whole document, modify it by JSON paths:

  1. increase stats.logins by 1 with JSON.NUMINCRBY;
  2. append the string "admin" to the end of the roles array with JSON.ARRAPPEND.

The other fields (name, email, stats.last_ip) must stay as in the file.

What you'll practise:
  • redis
  • RedisJSON
  • JSON.SET
  • JSON.GET
  • JSON.NUMINCRBY
  • JSON.ARRAPPEND
  • JSONPath

Topic breakdown

The JSON module (built into Redis 8) stores a document as a tree, not as a string: the key type is ReJSON-RL. That lets you read and change individual fields by JSONPath ($.stats.logins) atomically on the server — without the "read the whole string → parse it in the application → write it back" loop, in which concurrent clients lose updates. JSON.NUMINCRBY is a counter inside a document, JSON.ARRAPPEND/JSON.ARRPOP work with arrays, JSON.GET with paths returns only the pieces you need, saving traffic. The Query Engine indexes such documents too (FT.CREATE … ON JSON).

What's checked

  1. user:1 is a JSON document
  2. name, email, stats.last_ip as in the file
  3. stats.logins increased by 1
  4. admin added to roles
  5. fields changed by paths, not by overwriting

What it looks like