How to do a queue operation with Redis
Reids is a more advanced open-source Key-value storage System, implemented with ANSI C. It is similar to memcached, but supports persistent data storage, while value supports multiple types: string (value in the same memcached), list, set (set), ordered set (Orderset), and hash. All value types support atomic operations, such as appending pop-up elements to the list, inserting removed elements into the collection, and so on. Most of the data in Rdids is in memory and reads and writes efficiently, providing both AOF (Append action log files) and dump (periodic data backup) persistence. Redis supports a custom VM (virtual memory) mechanism that can store part of the value in a file when the data capacity exceeds memory. At the same time Redis supports the master-slave mechanism, which can replicate data.
Redis's list structure can be used as a queue.
From the scenario and the role of Redis above, for our current development activities, can we introduce redis into those scenes instead of turning such a nice thing into a "redis" and a tragic situation? Of course, the specific problem of specific analysis, this is really important ha.
Cache? Distributed cache? Queue? Distributed queues?
Some system applications (for example, telecommunications, banking and large Internet applications, etc.) will be used, of course, now the memcache is a good proof, but on the one hand, whether the memcache can take two of them, and can do better (no actual application, so just throw). But from Redis, I can feel that Redis can include both the queue and the cache, and it doesn't bother with concurrency, because operations in Redis are atomic operations.
As for the comment on the pros and cons of the two are exempt, the existence is the reason, the choice is the best.
Start playing with the queue (distributed) design in Redis yy, please give me a lot of advice.
Condition Scenario:
Now the project is deployed on multiple servers, or more than one IP, and the foreground is distributed through F5, so the user's request falls on that server, it is not certain. For the project, there was a second kill design, which was initially not considered for this kind of deployment, but also the easiest way to handle the database table lock Row Records (on Oracle). It can be said that for different application deployment, and only one database server, very "easy" to solve the concurrency problem. So now consider whether to move to the application, to avoid the database server is also doped into the business.
For example, there are now 2 application servers and 1 database servers. The idea is that Redis is deployed on the database server, where both servers operate the concurrent cache or queue, get the proxy objects on the two application servers, and then do the listed columns from the Redis server.
See Code implementation (PHP)
into the queue operation file list_push.php
The code is as follows:
<?php $redis = Getredisinstance ();//Redis instance from Redis server $redis->connect (' Redis server IP ', 6379); while (true) {$redis->lpush (' List1 ', ' A_ '. Date (' y-m-d h:i:s ')); Sleep (rand ()%3);}?>
Execute # PHP list_push.php &
Out Queue Operation list_pop.php file
The code is as follows:
<?php $redis = Getredisinstance ();//Redis instance from Redis server $redis->pconnect (' Redis server IP ', 6379); while (true) {try {var_export ($redis->blpop (' List1 ')),} catch (Exception $e) {//echo $e;}}
Implementation method (Python)
1. Into the queue (write.py)
The code is as follows:
#!/usr/bin/env python import time from Redis import Redis Redis = Redis (host= ' 127.0.0.1 ', port=6379) while True:now = Tim E.strftime ("%y/%m/%d%h:%m:%s") redis.lpush (' Test_queue ', now) time.sleep (1)
2. Out of queue (read.py)
The code is as follows:
#!/usr/bin/env python import sys from redis import redis Redis = Redis (host= ' 127.0.0.1 ', port=6379) while true:res = Redi S.rpop (' test_queue ') if res = = None:pass else:print str (RES)
Example code:
1. Queue operation
<?php$redis = new Redis (), $redis->connect (' 127.0.0.1 ', 6379), while (True) { try{ $value = ' value_ '. Date ( ' y-m-d h:i:s '); $redis->lpush (' Key1 ', $value); Sleep (rand ()%3); echo $value. " \ n "; } catch (Exception $e) { echo $e->getmessage (). " \ n "; }}? >
2. The team operation
<?php$redis = new Redis (), $redis->pconnect (' 127.0.0.1 ', 6379), while (True) { try{ echo $redis Lpop (' Key1 '). " \ n "; } catch (Exception $e) { echo $e->getmessage (). " \ n "; } Sleep (rand ()%3); >