Using Redis in Real-time Statistics Application

Background


In our application, there is a producer produce cid(a unique string id)s into a message queue, knowing as concurrent user.

Requirement


Provide real-time statistics of user.

Designment


1. Extract the cid from message queue and save it into Redis's Sorted-Set data structure, with timestamp as score.

Time complexity: O(log(N)) where N is the number of elements in the sorted set.

2. Run an backend thread to remove the expired cids(which is not actived in the last five minutes) every one second.

Time complexity: O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements removed by the operation.

3. Provide data api to count the number of elements in the sorted set.

Time complexity: O(1)

4. UI refreshes every five seconds to call data api to provide real-time concurrent user data.

Optimization


Instead of saving cid into sorted-set directly, holding a mapping of cid to unique int id can save memory since the cid will be reused.

So change #1 step as follows:

1.1 Extract the cid from message queue, save it into Redis's Hash data structure, using cid as key, an auto-increase unique int id as vaule.

Time complexity: O(1)

1.2 Save the int id into Redis's Sorted-Set data structure, with timestamp sa score.

Time complexity: O(log(N)) where N is the number of elements in the sorted set.

By this optimization, there is also a good way to calculate AU and NU in real-time. See http://blog.csdn.net/gaoyingju/article/details/9671283

Further More


For #2 step, record the number of elements every second and update when the next second has more elements, PCU is also available.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,034评论 0 23
  • 我不能把世界送给你 我不能当你忠实的犬仆 我能为你做的 就是离开 尽早 也许你不明白我这样的人 你在星光下 我是躲...
    翔于阅读 195评论 0 2
  • sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换、删除、新增、选...
    天天向上的小M阅读 253评论 0 0
  • Songkran Festival April 13th is a special day for Thai. T...
    月下海棠花阅读 1,006评论 0 0