15
Redis as a JSON document store Itamar Haber

RedisConf17 - Redis as a JSON document store

Embed Size (px)

Citation preview

Page 1: RedisConf17 - Redis as a JSON document store

Redis as a JSON document storeItamar Haber

Page 2: RedisConf17 - Redis as a JSON document store

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

Page 3: RedisConf17 - Redis as a JSON document store

Not a session about the JSON specification...object

{}{ members }

memberspairpair , members

pairstring : value

array[][ elements ]

elementsvaluevalue , elements

3

valuestringnumberobjectarraytruefalsenull

string""" chars "

charscharchar chars

charany-Unicode-Character-except-"-or-\-or-control-character\"\\\/\b\f\n\r\t\u four-hex-digits

numberintint fracint expint frac exp

Intdigitdigit1-9 digits - digit- digit1-9 digits

frac. digits

expe digits

digitsdigitdigit digits

eee+e-EE+E-

...though it does fit nicely in a single slide

Page 4: RedisConf17 - Redis as a JSON document store

There are three kinds of lies: lies, damned lies, and JSON standards.

tl;dr Parsing JSON is a Minefield

Page 5: RedisConf17 - Redis as a JSON document store

Fact #1: everyone uses JSON

Page 6: RedisConf17 - Redis as a JSON document store

Fact #2: many also use Redis

Page 7: RedisConf17 - Redis as a JSON document store

7

JSON Redis

Us

?

Page 8: RedisConf17 - Redis as a JSON document store

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

Page 9: RedisConf17 - Redis as a JSON document store

ReJSON = Redis + JSON

Page 10: RedisConf17 - Redis as a JSON document store

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

Page 11: RedisConf17 - Redis as a JSON document store

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

Page 12: RedisConf17 - Redis as a JSON document store

JSON value -> ReJSON tree data structure

redis> JSON.SET j .

'{

"foo": "bar",

"ans": 42

}'

Type: object

fooans

Type: string"bar"

Type: number42

root

streaming lexer

Page 13: RedisConf17 - Redis as a JSON document store

Performance: 3468 bytes, 3 nesting levels

Throughput Average latencyReJSON Raw JSON & Lua MessagePack & Lua

Page 14: RedisConf17 - Redis as a JSON document store

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

Page 15: RedisConf17 - Redis as a JSON document store

http://rejson.io - Thank you!

Itamar HaberChief OSS Education OfficerRedis Labs

@itamarhaber