5

Imagine having a tun of hashes in a Redis Database, each representing a user object. How would you get the user with for example the lowest registration date? The user where ts_registered is the lowest. I searched a bit on the Commands Page on redis.io but didn't seem to find a command for it there.

I don't have much experience with Redis and I'm basically just checking out how it works.

5
+250

Redis doesn't come out of the box with the ability to search/index inside hashes, but it gives you all the tools you need to accomplish your task. Your challenge could be solved, for example, by keeping a key in the database called last_user_registered or similar, and SETting it every time that a user registers to that user's ID. When you want to fetch the last user that had registered at any given moment - just GET it, get it?

But you (and Redis!) can do much better: for example, you could use a Redis list and LPUSH/RPOP members to keep scores with the users who last joined. Or, you could use a sorted set where each member is a user id and the score is the timestamp... ah, the possibilities are endless and you're only limited by your imagination (and time, resources, etc...) ;)

I think this is exactly why Redis 'aficionados' lovingly refer to it as a database building tool rather than as just a database - you can actually make it do whatever you want and if what you want isn't in Redis already, there's always Lua that you can use to extend with or even developing your own fork and putting in a pull request to the github.

  • Bounty awarded for this and your other contributions in the [redis] tag. – Paul White Aug 31 '15 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.