sorting - Caching sortable/filterable data in Redis -
i have variety of data i've got cached in standard redis hashmap, , i've run situation need respond client requests ordering , filtering. order rankings name, average rating, , number of reviews can change regularly (multiple times minute, possibly). can advise me on proper strategy attacking problem? consider following example understand i'm looking for:
- client makes api request /api/v1/cookbooks?orderby=name&limit=20&offset=0
- i should respond first 20 entries, ordered name
strategies i've considered far:
- for each type of hashmap store (cookbooks, recipes, etc), creating sorted set each ordering scheme (alphabetical, average rating, etc) postgres order by; pulling out zrange slices based on limit , offset
- storing ordering data directly json string data each key.
- hitting postgres select id table order _, , using ids pull directly hashmap store
any additional thoughts or advice on how best address issue? in advance.
so, mentioned in comment below sorted sets great way implement sorting , filtering functionality in cache. take following example idea of how 1 might solve issue of needing order objects in hash:
given hash called "movies" scheme of bucket:objectid -> object, json string representation (read "bucketing" hashes performance here.
create sorted set called "movieratings", each member objectid "movies" hash, , score average of rating values (computed database). use numerical representation of whatever you're trying sort, , redis gives lot of flexibility on how can extract slices need.
this simple scheme has lot of flexibility in can achieved - ask sorted set set of keys fit requirements, , keys hmget "movies" hash. 2 swift redis calls, problem solved.
rinse , repeat whatever type of ordering need, such "number of reviews", "alphabetically", "actor count", etc. filtering can done in manner, normal sets quite sufficient purpose.
Comments
Post a Comment