Redis or Mongo for determining if a number falls within ranges?

前端 未结 3 1851
余生分开走
余生分开走 2020-12-15 01:26

I need a way to quickly check if an IP address falls into one of many forbidden IP ranges.

I currently use iptables to check if the IP falls into a specified range.

3条回答
  •  隐瞒了意图╮
    2020-12-15 02:13

    If you are dealing with ranges, mongodb will be better than redis for that. If you are dealing with specific IP addresses, redis will be the best choice.

    Why?

    Mongodb can build indexes on the start and end ranges of ip addresses which you can query in O(log n) time whereas redis is simply a key value store.

    You could use a redis hash if every single ip that you wanted to check against was in a hash table and look them up in O(1) time but because you are using ranges I would say mongo is your best bet. I don't think you are going to get better than log n time with any of the data structures in the redis toolkit. O(n) with sorted set or list maybe but not O(log n).

提交回复
热议问题