Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis newbie - equivalent approach to mysql table

Tags:

redis

I'm new to Redis and was hoping for a "best practice" solution to implementing the equivalent of a "users" table in a mysql database, for handling users in a web app.

Would I create a users SET in Redis? Or a users database with a SET for each user?

like image 286
Dave Avatar asked Feb 06 '11 17:02

Dave


People also ask

Is Redis better than MySQL?

Overall, you don't really compare Redis and MySQL : if you want reliable relational storage, you use MySQL (or another DBMS) if you can afford to lose a few seconds of data in case of crash and you only need to fast store and retrieve key-value pairs, then Redis might be interesting.

How is Redis different from MySQL?

Redis also does not support Triggers, while MySQL allows Triggers. While MySQL supports the XML data format, Redis does not. When concerning indexes, both allow them. However, MySQL supports secondary indexes without any restrictions while Redis only supports secondary indexes with the RediSearch module.

Can I use Redis with MySQL?

Redis is an open-source and in-memory data structure store that can be used for caching, real-time analytics, searching, and machine learning. Integrate Redis with PHP and MySQL will improve your application performance because Redis stores data in RAM. You can use it with databases like MySQL or MariaDB.

For which cases is Redis more useful compared to SQL databases?

Redis is used only to store those small textual information which needs to be accessed, modified and inserted at a very fast rate. If you try to write bulk data more than the available memory then you will receive errors. RDBMS can hold large data which has less frequently usage and not required to be very fast.


1 Answers

My standard setup for users is a serialized object in a standard key u:userid - this object gets retrieved on each request, and there is never any need to access only one of the properties.

You could also use a hash rather than json for the user properties, but my setup includes everything getting used as strongly typed objects and redis running on a different server from the client, so using json makes it easier to use generic deserialization and minimizes any latency issues.

In addition to the user object itself, you will need an index for any fields you need to use to find a user - for example to allow a user to log in with email address you will need a key e:email => userid. A hash will also work here - the important thing is that you need something that is O(1) to get from an email to a user object.

Sometimes parts of the user data should have their own keys - for example a followers list is a perfect match to a redis set, so is best stored in that form.

like image 132
Tom Clarkson Avatar answered Sep 21 '22 04:09

Tom Clarkson