priyank
priyank

Reputation: 4724

Datamodel design for an application using Redis

I am new to redis and I am trying to figure out how redis can be used. So please let me know if this is a right way to build an application.

I am building an application which has got only one data source. I am planning to run a job on nightly basis to get data into a file.

Now I have a front end application, that needs to render this data in different formats.

Example application use case
Download processed applications by a university on nightly basis.
Display how many applications got approved or rejected.
Display number of applications by state.
Let user search for an application by application id.

Instead of using postgres/mysql like relational database, I am thinking about using redis. I am planning to store data in following ways.

  1. Application id -> Application details
  2. State -> List of application ids
  3. Approved -> List of application ids (By date ?)
  4. Declined -> List of application ids (By date ?)

Is this correct way to store data into redis?

Also if someone queries for all applications in california for a certain date, I will be able to pull application ids in one call but to get details for each application, do I need to make another request?

Upvotes: 2

Views: 269

Answers (2)

Itamar Haber
Itamar Haber

Reputation: 50112

Word of caution:

Instead of using postgres/mysql like relational database, I am thinking about using redis.

Why? Redis is an amazing database, but don't use the right hammer for the wrong nail. Use Redis if you need real time performance at scale, but don't try make it replace an RDBMS if that's what you need.

Answer:

Fetching data efficiently from Redis to answer your queries depends on how you'll be storing it. Therefore, to determine the "correct" data model, you first need to define your queries. The data model you proposed is just a description of the data - it doesn't really say how you're planning to store it in Redis. Without more details about the queries, I would store the data as follows:

  1. Store the application details in a Hash (e.g. app:<id>)
  2. Store the application IDs in a per state in Set (e.g. apps:<state>)
  3. Store the approved/rejected applications in two Sorted Sets, the id being the member and the date being the score

Also if someone queries for all applications in california for a certain date, I will be able to pull application ids in one call but to get details for each application, do I need to make another request?

Again, that depends on the data model but you can use Lua scripts to embed this logic and execute it in one call to the database.

Upvotes: 2

Lugg
Lugg

Reputation: 197

First of all you can use a Hash to store structured Data. With Lists (ZSets) and Sets you can create indexes for an ordered or unordered access. (Depending on your requirements of course. Make a list of how you want to access your data).

It is possible to get all data as json of an index in one go with a simple redis script (example using an unordered set):

local bulkToTable = function(bulk)
    local retTable = {};
    for index = 1, #bulk, 2 do
        local key = bulk[index];
        local value = bulk[index+1];
        retTable[key] = value;
    end
    return retTable;
end

local functionSet = redis.call("SMEMBERS", "app:functions")
local returnObj = {} ;
for index = 1, #functionSet, 1 do
    returnObj[index] = bulkToTable(redis.call("HGETALL", "app:function:" .. functionSet[index]));
    returnObj[index]["functionId"] = functionSet[index];
end
return cjson.encode(returnObj);

more information about redis scripts see here : http://www.redisgreen.net/blog/intro-to-lua-for-redis-programmers/

Upvotes: 0

Related Questions