Geospatial Indexing with Redis & Sinatra for a Fac

2019-04-11 21:54发布

I'm rebuilding Lovers on Facebook with Sinatra & Redis, and I want:

  • Set operations for managing requests & relationships between users
  • Geospatial indexing to display nearby app users

Current Redis Implementation

Each user has two Redis ordered sets (reqSent & reqRecv) that store uids. The SCORE that we order the requests by is the time (UNIX timestamp) the request was made. I am using ordered sets instead of lists because a user may only make The request type (rid) is encoded as a number and prepended to the uid. (rid|uid) E.g., for the user with uid=100, we might have:

100:reqSent => ["1|123", "2|123", "2|134"]  # format: ["rid|tid"]
100:reqRecv => ["3|343", "5|142", "4|2224"] # format: ["rid|uid"]

MongoDB supports geospatial indexing natively, so I'm thinking of switching to that.

Otherwise, how should I implement geospatial indexing with Redis? Should I do it with Sunspot (localsolr)? Apparently, you can use Sunspot with Redis.

2条回答
来,给爷笑一个
2楼-- · 2019-04-11 22:36

I like to avoid multiple platforms and have implemented something similar using redis. Geospatial indexing isn't all that different to indexing anything else in redis. You just need a function to convert lat/long to a single number covering an appropriate area, then you use than number as the key to a set containing all users in that area. If you chose the area right, retrieving that set and possibly some of its neighbours should get you an appropriate number of users to run actual distance calculations on for final filtering/sorting.

The general case of geospatial queries would be quite hard to implement, but you don't need it and redis is the wrong platform for ad hoc queries anyway.

查看更多
男人必须洒脱
3楼-- · 2019-04-11 22:45

Implementing geospatial indexing in Redis sounds prohibitively time consuming if you think MongoDB is a valid option. Mongo is fast and very nice to work with. If the majority of the queries will be against the geospatial index there is no reason not to use Mongo, in my opinion.

I often find myself using both Mongo and Redis in the same project. They both have very useful properties, and some use cases work really well in Mongo, and some in Redis. However, the converse is also true, some things are really, really hard to get right in Redis, and other things aren't practically possible in Mongo. Geospatial indexes is an example of the former and set operations an example of the latter.

Also, without knowing very much about Sunspot, would it really use Redis? Doesn't it use Solr, which uses the filesystem for storage? It doesn't sound to me like you could get Redis in there, and unless I'm completely wrong I would say that you should definitely go with MongoDB.

查看更多
登录 后发表回答