Storing JSON in REDIS

Storing JSON in REDIS
Photo by Ferenc Almasi / Unsplash

The project I was working was a turn based multiplayer game, and what I needed to store was game state. And the game state had multiple fields to be stored for a single game. That is, I had an object of game state that needed to be accessible from players in other servers, and fast too. So I was suggested to go with Redis and store JSON.

The project I was working was a turn based multiplayer game, and what I needed to store was game state. The game state had multiple fields to be stored for a single game. I had an object of game state that needed to be fast as well as accessible from players in other servers. So I was suggested to go with Redis and store JSON.

const jsonData = {
    name: "xyz",
    players: [1,2,4,7],
    positions: [
        [/*position of 1*/],
        [/*position of 2*/],
        [/*position of 4*/],
        [/*position of 7*/]
    ],
    gameStart: true,
    fee: 100,
    turnIndex: 12
}
json data to store

Storing as string

Redis stores data as key-value pair. One approach is to convert JSON to string and save in the key. While getting and working, we had to parse the string back to JSON.

// storing the data
const jsonString = JSON.stringify(jsonData)
const gameName = "roomName12"
redis.set(gameName, jsonString)

// retrieving the data
const data = redis.get(gameName)
const parsedJsonData = JSON.parse(data)
storing and retrieving in redis using javascript

The good things with this approach are :

  1. easy to work with
  2. JSON string and JSON parse was relatively easy and understandable

The bad things with this approach are :

  1. heavy to parse and make string because my use-case would do that frequently
  2. if I needed to check one value, eg: turnIndex, I had to get whole gameState from REDIS and again set whole gameState after being modified.

Storing as hash set

Redis Hashes are maps between string fields and string values, so they are the perfect data type to represent objects.

The good things with this approach are :

  1. we can get all or individual fields based on our need
  2. it eliminates the whole parsing thing with former approach

The bad things with this approach are :

  1. can store only numbers or strings as values
  2. eg: if I need to store a array, I need to store it as string, then retrieve and parse the string again to array.

Storing as JSON using Redis-JSON module

RedisJSON provides in-memory manipulation of JSON documents at high velocity and volume. With RedisJSON, you can natively store document data in a hierarchical, tree-like format to scale and query documents efficiently, significantly improving performance over storing and manipulating JSON with Lua and core Redis data structures.

The good things with this approach are :

  1. natively supports JSON

The bad things I found at the time of writing were :

  1. not a good npm package to work with
  2. redis-json module doesn't come default with redis installation, it is required to be installed as docker image or downloading and loading module into redis.

Finally

I shared just an experience and with not much details. If you want to try it out then follow redis documentation and modules.