http://blog.csdn.net/hpb21/article/details/7852934
Look for some information to see the next. The learning experience is as follows:
1 MySQL update Redis
MySQL update redis references memcache with MySQL communication, using MySQL UDF, each update operation triggered update Redis operation. Not enough in high concurrency when MySQL pressure is large, and for each table is required to increase the deletion trigger, and Redis server is not good to replace (do not know there is no method).
2 Redis update MySQL
The simplest is to read redis-> to write MySQL. But in high concurrency this is not a good fit, so consider using Message Queuing to update MySQL at regular intervals.
But when the primary key in the data is self-increment, the idea is as follows:
First, Redis uses tempid as Key,id to empty, overwriting data through MySQL UDF to cache such as, emptying cached temporary data.
Second, the Redis key value does not take an ID, uses the field UUID, and synchronizes the Redis cache ID with the MySQL UDF.
Third,Redis reads the MySQL table's latest auto -increment ID value, Redis pass ID.
Redis update MySQL It is important to note that Redis is positioned in the system, as a cache server, to plan what data is stored, how long it will be saved, and so on.
Concurrent access
Redis is a single-process single-threaded mode that uses queue mode to turn concurrent access into serial access. Redis itself does not have the concept of locking, and Redis does not compete for multiple client connections, but there are issues such as connection timeouts, data conversion errors, blocking, and client shutdown connections when Jedis clients have concurrent access to Redis, all due to client connectivity clutter. There are 2 ways to fix this:
1. Client angle, in order to ensure the normal and orderly communication between each client and Redis, the connection is pooled, while the client read and write Redis operation using internal lock synchronized.
2. Server angle, use SETNX to implement the lock. If a client wants to obtain a lock on a list of names, the client uses the following command to obtain it:
Setnx lock.list Current time + lock timeout
- If 1 is returned, the client obtains the lock and locks it. The list's key value is set to a time value indicating that the key is locked and the client can finally release the lock via del lock.list.
- A return of 0 indicates that the lock has been taken by another client, such that the other party completes or waits for the lock to time out.
3. Thread pool
MySQL Communication
data consistency is focused on the system Redis and data in MySQL. In order to ensure the consistency of the data, the following strategies can be adopted: for the important data, the system may take the first write the database to write the cache the way to guarantee, but for the general but the response speed request very high data can take the first write the cache then through the message queue again writes the database The way, At the same time do error logging and can recover data based on the log. (Jms/redis Message Queuing)
A problem exists:
(1) How does Redis synchronize with MySQL when the user adds an operation?
First the server starts, Redis reads the MySQL table with the latest self-key ID value, then Redis reads the ID and encapsulates the cache while keeping MySQL last completed by the queue MySQL update
(2) Message Queuing processing?
To locate a Redis operation:
Locate DAO: Define class, method correspondence rules, and update MySQL with the storage Redis key location method
Classification processing: By class, method.
Go Redis and MySQL Communication