Redis as a JSON document store
Itamar Haber
What is JSON (emphasis mine)
ECMA-404 The JSON Data Interchange Standard
JSON (JavaScript Object Notation) is a lightweight
data-interchange format. It is easy for humans to
read and write. It is easy for machines to parse and
generate.
http://json.org/
2
Not a session about the JSON specification...
object
{}
{ members }
members
pair
pair , members
pair
string : value
array
[]
[ elements ]
elements
value
value , elements
3
value
string
number
object
array
true
false
null
string
""
" chars "
chars
char
char chars
char
any-Unicode-
Character-except-
"-or--or-control-
character
"

/
b
f
n
r
t
u four-hex-digits
number
int
int frac
int exp
int frac exp
Int
digit
digit1-9 digits
- digit
- digit1-9 digits
frac
. digits
exp
e digits
digits
digit
digit digits
e
e
e+
e-
E
E+
E-
...though it does fit nicely in a single slide
There are three kinds of lies: lies, damned
lies, and JSON standards.
tl;dr Parsing JSON is a Minefield
Fact #1: everyone uses JSON
Fact #2: many also use Redis
7
JSON Redis
Us
?
Two ways for storing JSON in Redis
Serialized, as a raw String
● Just SET/GET it, client
encodes/decodes
● Great for "forklift" ops
● And with Lua scripting:
○ Sub-element access
○ Atomic updates
8
Deserialized, in a Hash
● Decode->HMSET,
HMGET->Encode
● O(1) access
● Follows the Redis
spirit...
ReJSON = Redis + JSON
ReJSON is
● A custom JSON data type for Redis (v4 Modules API)
● Keys can contain any valid JSON value
○ Scalars, objects or arrays
○ Nested or not
● Data is stored decoded, in binary format
● JSONPath-like syntax for direct access to elements
● Strongly-typed atomic commands
127.0.0.1:6379> JSON.SET scalar . '"Hello JSON!"'
OK
127.0.0.1:6379> JSON.SET object . '{"foo": "bar",
"ans": 42}'
OK
127.0.0.1:6379> JSON.GET object
"{"foo":"bar","ans":42}"
127.0.0.1:6379> JSON.GET object .ans
"42"
ReJSON - basic SET and GET
JSON value -> ReJSON tree data structure
redis> JSON.SET j .
'{
"foo": "bar",
"ans": 42
}'
Type: object
foo
ans
Type: string
"bar"
Type: number
42
root
streaming lexer
Performance: 3468 bytes, 3 nesting levels
Throughput Average latency
ReJSON Raw JSON & Lua MessagePack & Lua
General JSON.DEL, JSON.GET, JSON.MGET, JSON.SET &
JSON.TYPE
Numbers JSON.NUMINCRBY & JSON.NUMMULTBY
Strings JSON.STRAPPEND & JSON.STRLEN
Objects JSON.OBJKEYS & JSON.OBJLEN
Arrays JSON.ARRAPPEND, JSON.ARRINDEX,
JSON.ARRINSERT, JSON.ARRLEN, JSON.ARRPOP &
JSON.ARRTRIM
Other JSON.RESP
ReJSON commands
http://rejson.io - Thank you!
Itamar Haber
Chief OSS Education Officer
Redis Labs
@itamarhaber

RedisConf17 - Redis as a JSON document store

  • 1.
    Redis as aJSON document store Itamar Haber
  • 2.
    What is JSON(emphasis mine) ECMA-404 The JSON Data Interchange Standard JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. http://json.org/ 2
  • 3.
    Not a sessionabout the JSON specification... object {} { members } members pair pair , members pair string : value array [] [ elements ] elements value value , elements 3 value string number object array true false null string "" " chars " chars char char chars char any-Unicode- Character-except- "-or--or-control- character " / b f n r t u four-hex-digits number int int frac int exp int frac exp Int digit digit1-9 digits - digit - digit1-9 digits frac . digits exp e digits digits digit digit digits e e e+ e- E E+ E- ...though it does fit nicely in a single slide
  • 4.
    There are threekinds of lies: lies, damned lies, and JSON standards. tl;dr Parsing JSON is a Minefield
  • 5.
  • 6.
    Fact #2: manyalso use Redis
  • 7.
  • 8.
    Two ways forstoring JSON in Redis Serialized, as a raw String ● Just SET/GET it, client encodes/decodes ● Great for "forklift" ops ● And with Lua scripting: ○ Sub-element access ○ Atomic updates 8 Deserialized, in a Hash ● Decode->HMSET, HMGET->Encode ● O(1) access ● Follows the Redis spirit...
  • 9.
  • 10.
    ReJSON is ● Acustom JSON data type for Redis (v4 Modules API) ● Keys can contain any valid JSON value ○ Scalars, objects or arrays ○ Nested or not ● Data is stored decoded, in binary format ● JSONPath-like syntax for direct access to elements ● Strongly-typed atomic commands
  • 11.
    127.0.0.1:6379> JSON.SET scalar. '"Hello JSON!"' OK 127.0.0.1:6379> JSON.SET object . '{"foo": "bar", "ans": 42}' OK 127.0.0.1:6379> JSON.GET object "{"foo":"bar","ans":42}" 127.0.0.1:6379> JSON.GET object .ans "42" ReJSON - basic SET and GET
  • 12.
    JSON value ->ReJSON tree data structure redis> JSON.SET j . '{ "foo": "bar", "ans": 42 }' Type: object foo ans Type: string "bar" Type: number 42 root streaming lexer
  • 13.
    Performance: 3468 bytes,3 nesting levels Throughput Average latency ReJSON Raw JSON & Lua MessagePack & Lua
  • 14.
    General JSON.DEL, JSON.GET,JSON.MGET, JSON.SET & JSON.TYPE Numbers JSON.NUMINCRBY & JSON.NUMMULTBY Strings JSON.STRAPPEND & JSON.STRLEN Objects JSON.OBJKEYS & JSON.OBJLEN Arrays JSON.ARRAPPEND, JSON.ARRINDEX, JSON.ARRINSERT, JSON.ARRLEN, JSON.ARRPOP & JSON.ARRTRIM Other JSON.RESP ReJSON commands
  • 15.
    http://rejson.io - Thankyou! Itamar Haber Chief OSS Education Officer Redis Labs @itamarhaber