Frage

I have some voting data for an issue that I want to create some reports on.

I want to display the voting results for each issue following criteria

Age Sex Income Education Race

Different issues could be Abortion, Gun Control, etc..

How would I use Redis to store this voting data and then display reports on them? Here's one report I'm trying to create.

Here's what the report looks like when I want to view the voting data by Age

https://docs.google.com/spreadsheets/d/1N-C4pNN_fwb1kNGQck44TIrIAEn-jPZEpEsW6qQ8lh8/edit?usp=sharing

I want to create similar reports but they could also be by age and sex or age and income or income and education, etc..

Hope you understand what I'm trying to create. I want to let the end user select different criteria on the website and create this dynamic report on the fly as fast as I can which is why I dont' want to use MySQL for this. I know Redis can be used to solve this but I'm just not sure how to get started.

Thanks in advance for any pointers you can provide for me to get started.

War es hilfreich?

Lösung

Really, this is a problem most easily solved with a traditional RDBMS, like PostgreSQL/MySQL.

However, there are a few ways you could do this in Redis.

One way would be to simply store attributes for each vote in a hash.

redis.hmset "vote:123", "age", 26, "abortion", "yes", "gun_control", "undecided" #, ...

You would also want a redis SET (ie: "all_votes") containing all the vote ids, so you don't have to use redis.keys to search for votes.

The next step is making other sets. If you want to be able to look up by age ranges quickly, you will probably need to build a SET (ie: "vote_indexes:age:18-22") for each age range, populating it with the ids of any votes within that age range. Every time you add a vote or remove a vote you will need to add or remove them to/from the all_votes SET as well as its corresponding age range SET, and any other index SETs you build. If this sounds a lot like database indexes, it is exactly like that. Except you have to maintain them yourself, so that is quite a bit of extra code you wouldn't have to write with an RDBMS.

Now that you have your index sets, you can perform intersections of those sets to do some querying.

redis.sinter("indexes:age:18-22", "indexes:abortion:yes").count
# => 20

Instead of manually maintaining your own hand-built indexes, you could go the route of simply iterating through every vote and build the report as you go, hopefully in one pass. This would be pretty slow to implement within your application. The most performant option would likely be to use Lua scripting running within redis. Basically your Lua script would get passed to redis with the filter parameters and it would iterate through all votes and do the filtering, returning the matching results or even a final report.

That of course means you'll have to learn Lua. Its a nice little language and not difficult to pick up, but its a bit harder than a language you probably already know: SQL.

I love Redis, but not sure you have the need for it. An adhoc reporting system is something SQL was literally made for. Don't worry about performance issues until you have them. You'll be surprised how far SQL can get you. If you do hit some performance problems, Redis is an amazing way to cache your SQL results and give your RDBMS a break.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top