Redis
Remote directory Server (Redis), a persistent database storage system based on Key-value key-value pairs, has a richer storage of redis support data, including String,list,setzset.
These data types support a richer set of operations, such as push, Pop,add,remove, and intersection, the difference sets, and these operations are atomic, on the basis of which support different sorting methods, all the data is stored in from, But Redis's persistence service will also be cyclical, the update data written to disk and the modified operation record appended to the file record, more advantageous than memcached, Redis also supports Master-slave synchronization, which is similar to the relational database MySQL.
The emergence of redis, to some extent, make up for the memcached of such key-value memory cache service, in some cases can be complementary to the relational database
Official website www.redis.cn Domestic
Www.redis.io Foreign
Benefits of Redis
1, high performance: Redis can support more than 100K (10W) + read/write frequency per second
2, Rich data types: Redis support binary strings,lists,hashes,sets, and ordered sets, etc.
3, Atom: All operations of Redis are atomic, and Redis supports the atomic execution of several operations.
4, Rich features: Redis also supports pushlish subscribe, notifications, key expiration and other features
5,redis supports master-slave synchronous replication of different machines
6, unlike memcached, can persist data storage
######## #redis的应用场景
The traditional mysql+memcahed Web site architecture is experiencing problems:
MySQL database is actually suitable for massive data storage, plus through memcached to store hot data into memory cache, to speed up the number of visits to the purpose, most companies used to use such a structure, but as the volume of business data does not increase, And the continued growth of traffic to buy a lot of problems will leak out:
1, the need to continue to the MySQL to dismantle the library, the table, memcached also need to continue to follow the expansion, expansion and maintenance work occupy a large number of development operations time
2,memcached data consistency with MySQL is a persistent problem.
3,mencached's data hit rate is down, causing a lot of access to the database directly, resulting in MySQL being unable to support it. (This is the most deadly)
4, cross-room cache synchronization Consistency problem
########### #redis的最佳应用场景
1,redis Best Use scenario is all data in-memory
2,reids more scenes are used as substitutes for memcached
3, using Redis is more appropriate when you need more data type support outside of Key/value
4, when the stored data cannot be kicked out, the use of Redis is more suitable
Redis author on redis application scenario http://blog.nosqlfan.com/html/2235.html
Redis Data Summary topic: Very full http://blog.nosqlfan.com/html/3537.html
Using Redis bitmap for active user statistics:
Http://blog,nosqlfan.com/html/3501.html
Redis Operational Way: Http://blog.nosqlfan.com/html/2692.html?ref=rediszt
Summary of the Redis database:
1, increase the scalability of the DB line, just put the new data on the newly added server on it.
2, improved DB availability, only affects users accessing data on the Shard server
3. Improve the maintainability of DB, the upgrade and configuration of the system can be high according to Shard, the impact on the service is relatively small
4, when the maximum memory is reached, the key with expiration time will be emptied, the time key is not expired
5,redis with DB synchronous write, first write db, after write Redis, because write memory basically no problem
############################### #安装与部署
Redis Installation Deployment
1. :
$ wget http://redis.googlecode.com/files/redis-2.6.13.tar.gz
2. Unzip
$ tar xzf redis-2.6.13.tar.gz
3. Compile
$ CD redis-2.6.13
$ make
$make Install
$CP redis.conf/etc/
Parameter description:
When the make install command finishes executing, the executable file is generated in the/usr/local/bin directory.
Redis-server, Redis-cli, Redis-benchmark, redis-check-aof, Redis-check-dump, respectively,
Their role is as follows:
Redis-server:redis Server Daemon Startup program
Redis-cli:redis command-line operation tool. You can also use Telnet to manipulate it based on its plain text protocol.
Redis-benchmark:redis Performance Testing tool to test Redis's read and write performance under current system
REDIS-CHECK-AOF: Data Repair
Redis-check-dump: Check the Export tool
4. Modify the system configuration file, execute the command
A) echo Vm.overcommit_memory=1 >>/etc/sysctl.conf
b) Sysctl Vm.overcommit_memory=1 or perform echo Vm.overcommit_memory=1 >>/proc/sys/vm/overcommit_memory
Use numeric meanings:
0, indicates that the kernel will check for sufficient available memory to be used by the process, and if sufficient memory is available, the memory request is allowed; otherwise, the memory request fails and the error is returned to the application process.
1, which means that the kernel allows all physical memory to be allocated regardless of the current memory state.
2, which indicates that the kernel allows allocating more memory than the sum of all physical memory and swap space
5. Modifying a Redis configuration file
A) $ cd/etc
b) VI redis.conf
c) Modify Daemonize Yes---to enable the process to run in the background
Parameter description:
Daemonize: Whether to run daemon mode later
Pidfile:pid File Location
Port: Port number for listening
Timeout: Request time-out
Loglevel:log Information level
Logfile:log File Location
Databases: number of open databases
Save *: How often the snapshot is saved, the first * indicates how long, and the third * indicates how many times the write operation is performed. Snapshots are automatically saved when a certain number of writes are performed within a certain amount of time. You can set multiple conditions.
Rdbcompression: Whether to use compression
Dbfilename: Data Snapshot file name (only file name, excluding directory)
Dir: Save directory for Data snapshot (this is the directory)
AppendOnly: If the appendonlylog is turned on, each write will record a log, which will improve the data anti-risk ability, but affect the efficiency.
Appendfsync:appendonlylog How to sync to disk (three options, each write is forced to call Fsync, Fsync per second, do not call Fsync wait for the system to synchronize itself)
6. Start Redis
A) $ cd/usr/local/bin
b)./redis-server/etc/redis.conf
7. Check whether the start is successful
A) $ ps-ef | grep Redis
1. Turn off Redis service
[Email protected] ~]# REDIS-CLI
127.0.0.1:6379> shutdown
2,useage
[Email protected] ~]# REDIS-CLI--help
REDIS-CLI 3.0.3
USAGE:REDIS-CLI [OPTIONS] [cmd [arg [arg ...]]
-H
-P <port> Server port (default:6379).
-S <socket> Server socket (overrides hostname and port).
-A <password> password to use when connecting to the server.
-R <repeat> Execute specified command N times.
-I <interval> when-r is used, waits <interval> seconds per command.
It's possible to specify Sub-second times like-i 0.1.
-N <db> Database number.
-X Read last argument from STDIN.
-D <delimiter> multi-bulk delimiter in for raw formatting (default: \ n).
-C Enable Cluster mode (Follow-ask and-moved redirections).
--raw use raw formatting for replies (default when STDOUT is
Not a TTY).
--no-raw force formatted output even when STDOUT was not a tty.
--csv Output in CSV format.
--stat Print rolling stats about Server:mem, clients, ...
--latency Enter A special mode continuously sampling latency.
--latency-history like--latency but tracking latency changes over time.
Default time interval is the. Change it using-i.
--latency-dist shows latency as a spectrum, requires xterm-colors.
Default time interval is 1 sec. change it using-i.
--lru-test <keys> simulate a cache workload with an 80-20 distribution.
--slave simulate a slave showing commands received from the master.
--rdb <filename> Transfer An RDB dump from the remote server to local file.
--pipe Transfer Raw Redis protocol from stdin to server.
--pipe-timeout <n> in--pipe mode, abort with error if after sending all data.
No reply is received within <n> seconds.
Default timeout:30. Use the 0 to wait forever.
--bigkeys Sample Redis keys looking for big keys.
--scan List All keys using the Scan command.
--pattern <pat> useful with--scan to specify a scan pattern.
--intrinsic-latency <sec> Run A test to measure intrinsic system latency.
The test would run for the specified amount of seconds.
--eval <file> Send An eval command using the Lua script at <file>.
--help Output this Help and exit.
--version Output version and exit.
Examples:
cat/etc/passwd | Redis-cli-x Set MYPASSWD
REDIS-CLI Get MYPASSWD
Redis-cli-r Lpush mylist X
Redis-cli-r 100-i 1 Info | grep Used_memory_human:
REDIS-CLI--eval Myscript.lua key1 key2, arg1 arg2 arg3
Redis-cli--scan--pattern ' *:12345* '
[Email protected] ~]# cd/usr/local/bin/
[Email protected] bin]#./redis-server/etc/redis.conf
[Email protected] bin]# redis-cli-h 127.0.0.1-p 6379
127.0.0.1:6379>
127.0.0.1:6379> help get
GET Key
Summary:get the value of a key
since:1.0.0
Group:string
127.0.0.1:6379> Set Db.test.username Helloboy
Ok
127.0.0.1:6379> Get Db.test.username
"Helloboy"
127.0.0.1:6379> Set no002 Helloboy
Ok
127.0.0.1:6379> Get no002
"Helloboy"
[[email protected] ~]# redis-cli-h 127.0.0.1-p 6379 set 002num Lisia
Ok
[[email protected] ~]# redis-cli-h 127.0.0.1-p 6379 get 002num
"Lisia"
[[email protected] ~]# redis-cli-h 127.0.0.1-p 6379 get 002num
"Lisia"
[Email protected] ~]# redis-cli del 002num
(integer) 1
[[email protected] ~]# redis-cli-h 127.0.0.1-p 6379 get 002num
(nil)
[Email protected] ~]#
[[Email protected] ~]# telnet 127.0.0.1 6379
Trying 127.0.0.1 ...
Connected to 127.0.0.1.
Escape character is ' ^] '.
Set no003 Jihui
+ok
Get no003
$
Jihui
############## #字符串类型
This is the simplest type of redis, and if you only use this type, Redis is a MEMCACEHD server that can be persisted.
Here is the string type:
[[email protected] ~]# redis-cli set MyKey "My Binary Safe value"
Ok
[[email protected] ~]# REDIS-CLI get MyKey
"My Binary Safe value"
"My binary safe value" as you would normally set and get string values with Set and get
The value can be any kind of string, (including binary data), for example, you can save a JPEG image under a key, the value of the length of not more than 1GB
List
[Email protected] ~]# redis-cli rpush Messages "Hello old is You?"
(integer) 1
[A[[email protected] ~]# redis-cli rpush messages "im fine thank You"
(integer) 2
[Email protected] ~]# REDIS-CLI Rpush Messages "and you"
(integer) 3
Read List
[[email protected] ~]# REDIS-CLI lrange messages 0 2
1) "Hello old is You?"
2) "Im fine Thank You"
3) "And You"
[[email protected] ~]# REDIS-CLI lrange messages 0 1
1) "Hello old is You?"
2) "Im fine Thank You"
[[email protected] ~]# REDIS-CLI lrange messages 0 0
1) "Hello old is You?"
########################## #为php安装redis客户端
wget Https://github.com/nicolasff/phpredis/archive/master.zip
Unzip Master.zip
1005 CD phpredis-master/
1006/usr/local/php/bin/phpize
1007./configure--with-php-config=/usr/local/php/bin/php-config
1008 make
1009 Make Install
[[email protected] phpredis-master]# make install
Installing Shared extensions:/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
Modify PHP.ini, restart PHP
[Email protected] phpredis-master]# echo "extension=redis.so" >>/usr/local/php/etc/php.ini
[Email protected] phpredis-master]#
[Email protected] phpredis-master]#/etc/init.d/php-fpm restart
Gracefully shutting down php-fpm. Done
Starting php-fpm Done
Save to disk
Save 900 1 Every 900 seconds 1 records
Save 300 10 Every 300 seconds 10 records
#################### #redis Master-slave configuration:
Simply configure from a Redis server
Slaveof Master-ip Master-port
Slaveof 192.168.0.110 6379
Restart from Redis service
################## #redis负载均衡
You can use LVS, just like polling a Web server
This article is from the "Crazy_sir" blog, make sure to keep this source http://douya.blog.51cto.com/6173221/1680689
Redis--Beginner notes