redis-cli
Memcached | Redis |
---|---|
Memcached only does caching information. | It provides some more functionalities like replication and persistence along with caching information. |
Memcached supports the functionality of LRU (Least Recently Used) eviction of values. | LRU is not supported by Redis. |
In Memcached, when they overflow memory, the one you have not used recently (LRU- Least Recently Used) will get deleted. | In Redis, there is a time set for each function, Three keys are maintained, the one, which is closest to expiry, will get deleted. |
CAS (Check and Set) is supported by Memcached. | CAS is not supported by Redis. |
Array objects are needed to be serialized in order to get saved. We need to unserialize them for their retrieval. | Redis has got stronger data structures; it can handle strings, binary safe strings, list of binary safe strings, sorted lists, etc. |
Memcached has at most 250 bytes length. | Redis has at most 2 GB key length. |
It is Multi-threaded | It is single threaded. |
Fsync ()
every time a new command is added to the append log file: It is safe but very slowFysnc()
one time every second : It is fast, but you may lose 1 second of data if system failsfsync()
: It is an unsafe method, and your data is in hand of Operating System$list = $redis ->lrange (“tutorials”, 0, 5);
print_r($list);
/*array ([0] => Mysql [1] => Mongodb [2] => Redis [3] => MySQL [4]=> PHP [5] => Mysql)*/
try
{
$redis = new Redis();
$redis -> connect (‘127.0.0.1’, 6379);
echo “Redis is running.”;
echo “Server is running:”. $redis -> ping()
}
catch (Exception $e)
{
echo $e -> getMessage();
}
Redis | MongoDB |
---|---|
It is a key-value store. | It is a document type of store. |
It is relatively faster. | It is relatively slower. |
It uses C language. | It uses C++ language. |
Lua is the server-side script here. | JavaScript is the server-side script here. |
Redis-CLI flushall
Redis-CLI flushdb
127.0.0.1:6379> ZADD "id" 1 "Mark"
(integer) 1
127.0.0.1:6379> ZADD "id" 2 "Arthur"
(integer) 1
127.0.0.1:6379> ZADD "id" 3 "Monica"
(integer) 1
127.0.0.1:6379> ZADD "id" 4 "Daniel"
(integer) 1
127.0.0.1:6379> ZADD "id" 5 "Sergio"
(integer) 1
ZSET
command ZRANGE
and identify that score by its order position.127.0.0.1:6379> ZRANGE "id" 0 0
1) "Mark"
ZRANGE ZSET
command returns a range of scores sorted from low to high.ZRANGE
to display every score in a Redis ZSET
like this:127.0.0.1:6379> ZRANGE "id" 0 -1
1) "Mark"
2) "Arthur"
3) "Monica"
4) "Daniel"
5) "Sergio"
127.0.0.1:6379> ZRANGE "id" 0 -1 WITHSCORES
1) "Mark"
2) "1"
3) "Arthur"
4) "2"
5) "Monica"
6) "3"
7) "Daniel"
8) "4"
9) "Sergio"
10) "5"
127.0.0.1:6379> ZRANGEBYSCORE "id" 0 2
1) "Mark"
2) "Arthur"
127.0.0.1:6379> ZRANGEBYSCORE "id" 0 2 withscores
1) "Mark"
2) "1"
3) "Arthur"
4) "2"
ZREM
Redis ZSET
command to delete a member and score in a ZSET
like this :127.0.0.1:6379> ZREM "id" "Mark"
(integer) 1
127.0.0.1:6379> ZREM "id" "Mark"
(integer) 0
ZREM
command removed the ZSET
item. You can tell because it looked for the member and score again and 0 was returned. This shows that you have successfully removed the item from the ZSET
. If you look for the item again, a 0 will display in the results.127.0.0.1:6379> SADD TEST_SET A
(integer) 1
127.0.0.1:6379> SADD TEST_SET B
(integer) 1
127.0.0.1:6379> SMEMBERS TEST_SET
1) "A"
2) "B"
127.0.0.1:6379> SISMEMBER TEST_SET A
(integer) 1 [1 indicates it's true; it's available]
127.0.0.1:6379> SPOP TEST_SET
"A"
127.0.0.1:6379> SMEMBERs TEST_SET
1) "B"
Fsysnc()
each time.Fsysnc()
in every second. Despite of the 1 second data lose in the case of system fails.127.0.0.1:6379> SET TEST_KEY TEST_VALUE
OK [TEST_VALUE is being set to TEST_KEY]
127.0.0.1:6379> GET TEST_KEY
"TEST_VALUE"
127.0.0.1:6379> SET TEST_KEY TEST_VALUE_UPDATED NX
(nil)
127.0.0.1:6379> GET TEST_KEY
"TEST_VALUE"
127.0.0.1:6379> SET TEST_KEY TEST_VALUE_XX_UDPATED XX
OK
127.0.0.1:6379> GET TEST_KEY
"TEST_VALUE_XX_UDPATED"
127.0.0.1:6379> brpop A {timeout}
...
...
2³² — 1 (4294967295, more than 4 billion of members per set)
.127.0.0.1:6379> SMEMBERS TEST_KEY_A
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> SMEMBERS TEST_KEY_B
1) "2"
2) "6"
127.0.0.1:6379> SUNION TEST_KEY_A TEST_KEY_B
1) "1"
2) "2"
3) "3" [prints union of two sets]
4) "6"
127.0.0.1:6379> SINTER TEST_KEY_A TEST_KEY_B
1) "2" [print intersection of two sets]
O(log(N))
for each item added, where N is the number of elements in the sorted set.O(M*log(N))
with N being the number of elements in the sorted set and M the number of elements to be removed.O(log(N)+M)
with N being the number of elements in the sorted set and M the number of elements returned.O(log(N)+M)
with N being the number of elements in the sorted set and M the number of elements being returned. If M is constant (e.g. always asking for the first 10 elements with LIMIT), you can consider it O(log(N))
.127.0.0.1:6379> ZADD TEST_ZKEY 1 A
(integer) 1
127.0.0.1:6379> ZADD TEST_ZKEY 2 B
(integer) 1
127.0.0.1:6379> ZADD TEST_ZKEY 1 C
(integer) 1
127.0.0.1:6379> ZRANGE TEST_ZKEY 0 -1
1) "A"
2) "C"
3) "B"
127.0.0.1:6379> ZREM TEST_ZKEY B
(integer) 1
127.0.0.1:6379> ZRANGE TEST_ZKEY 0 -1
1) "A"
2) "C"
127.0.0.1:6379> ZRANGEBYSCORE TEST_ZKEY 1 2 {Here 1 is min rank, 2 is max rank- gets a data from rank of 1 to 2}
1) "A"
LREM
and replacing it if it was found.SET
in conjunction with your LIST
LIST
until you find the item or reach the end.SET
. Regardless, make sure your actions are atomic with MULTI-EXEC
or Lua scripts.O(N)
appends, for instance, instead of having quadratic behavior.OOM
killer, crash with an error, or will start to slow down. With modern operating systems malloc()
returning NULL
is not common, usually the server will start swapping (if some swap space is configured), and Redis performance will start to degrade, so you'll probably notice there is something wrong.maxmemory
option in the configuration file to put a limit to the memory Redis can use. If this limit is reached Redis will start to reply with an error to write commands (but will continue to accept read-only commands), or you can configure it to evict keys when the max memory limit is reached in the case where you are using Redis for caching.