Original: http://segmentfault.com/q/1010000000316112
What is Redis?
Redis is a warehouse that stores key-value key-value pairs, how to use Redis to understand the E-R model of the system you need to design, and then properly plan the database structure of Redis
Scene
Let me give you an example of a simple messaging system, business requirements: server-side sending of messages to users
E-r Model:
1. User (Uid,nickname,phone,mood)
2. Message (MID,TITLE,CONTENT,TS)
A relationship between a user and a message is N, a message can be sent to multiple users, each user can have multiple messages
Redis Database Design
The key to the design of Redis database is the design of key, I usually use fixed prefix + unique suffix way, for example:
1. The message entity is a two-tiered structure, consider using the REDIS hash data structure for storage (PS: With the basic string is also no problem), key is msg_[$mid],mid similar to the main key of MySQL table, in the current Redis database to ensure global unique, Can be implemented using Redis's incr atomic operation, value can be an array (' title ' = ' + ' caption ', ' content ' = ' contents ', ' ts ' = ' release timestamp '). This way, each time a message is generated on the server side, Constructs such a Key-value key-value pair to represent the contents of the message itself
2. User entity, because the user's information needs more stable persistent storage, so it is recommended to store directly in MySQL, do not need to migrate to Redis
3. User-message relationship, you can consider Redis's sets data structure. Key is unread_mids_[$uid] and read_mids_[$uid],value is a collection of mid. Each time a message is sent to the specified user, simply place the message in the corresponding collection
This will represent the E-R model of the message system.
Summarize
How Redis databases are used is key to how you design and understand your system's E-R model, figuring out the relationships between entities, and designing the corresponding Key-value key pairs in Redis. As for those data structures, it's not very useful for you to design a Redis database, just a more convenient effect, as memcache can do.
For Redis provides several data structures, I suggest don't float on the surface just will tune an API, a little technical content is not, suggest to see the source implementation, recommended links: http://redisbook.com/
Redis database Design (GO)