Search code examples
mysqllaravelredisreal-timeioredis

Using Redis to cache data that is in use in a Real TIme Single Page App


I've got a web application, it has the normal feature, user settings etc these are all stored in MYSQL with the user etc.....

A particular part of the application a is a table of data for the user to edit.

I would like to make this table real time, across multiple users. Ie multiple users can open the page edit the data and see changes in real time done by other users editing the table.

My thinking is to cache the data for the table in Redis, then preform all the actions in redis like keeping all the clients up to date.

Once all the connection have closed for a particular table save the data back to mysql for persistence, I know Redis can be used as a persistent NoSQL database but as RAM is limited and all my other data is stored in MYSQL, mysql seems a better option.

Is this a correct use case for redis? Is my thinking correct?


Solution

  • It depends on the scalability. The number of records you are going to deal with and the structure you are going to use for saving it.

    I will discuss about pros and crons of using redis. The decision is up to you.

    Advantages of using redis:

        1) It can handle heavy writes and reads in comparison with MYSQL
        2) It has flexible structures (hashmap, sorted set etc) which can 
    localise your writes instead of blocking the whole table.
        3) Read queries will be much faster as it is served from cache.
    

    Disadvantages of using redis:

        1) Maintaining transactions. What happens if both users try to access a 
        particular cell at a time? Do you have a right data structure in redis to 
        handle this case?
        2) What if the data is huge? It will exceed the memory limit. 
        3) What happens if there is a outage?
        4) If you plan for persistence of redis. Say using RDB or AOF. Will you 
        handle those 5-10 seconds of downtime?
    

    Things to be focussed:

    1) How much data you are going to deal with? Assume for a table of 10000 rows wit 10 columns in redis takes 1 GB of memory (Just an assumption actual memory will be very much less). If your redis is 10GB cluster then you can handle only 10 such tables. Do a math of about how many rows * column * live tables you are going to work with and the memory it consumes.

    2) Redis uses compression for data within a range http://redis.io/topics/memory-optimization. Let us say you decide to save the table with a hashmap, you have two options, for each column you can have a hashmap or for each row you can have a hashmap. Second option will be the optimal one. because storing 1000 (hashmaps -> rows) * 20 (records in each hash map -> columns) will take 10 time less memory than storing in the other way. Also in this way if a cell is changed you can localize in hashmap of within 20 values.

    3) Loading the data back in your MYSQL. how often will this going to happen? If your work load is high then MYSQL begins to perform worse for other operations.

    4) How are you going to deal with multiple clients on notifying the changes? Will you load the whole table or the part which is changes? Loading the changed part will be the optimal one. In this case, where will you maintain the list of cells which have been altered?

    Evaluate your system with these questions and you will find whether it is feasible or not.