1. download the latest redis version 2.2.14
CD/usr/local/src
Http://redis.googlecode.com/files/redis-2.2.14.tar.gz wget-C
Ii. Compile and install redis
Tar zxvf redis-2.2.14.tar.gz
CD redis-2.2.14
Make
After the make command is executed, five executable files are generated in the src directory, they are redis-server, redis-CLI, redis-benchmark, redis-check-Aof, and redis-check-dump. Their functions are as follows:
Redis-server: Daemon startup of the redis ServerProgram
Redis-CLI: redis command line operation tool. Of course, you can also use Telnet to operate based on its plain text protocol.
Redis-benchmark: redis performance testing tool to test the read/write performance of redis in your system and your configuration
Redis-check-Aof: Update log check
Redis-check-dump: used for local database check
Installation:
Make prefix =/usr/local install
3. Configure redis
Mkdir/etc/redis
CP redis. CONF/etc/redis. conf
Mkdir/var/lib/redis
You can download the modified redis. conf file from this directory.
1. redis. conf configuration parameters:
# Running as a daemon
Daemonize Yes
# If a later process runs, you must specify a PID. The default value is/var/run/redis. PID.
Pidfile redis. PID
# Bind the Host IP address. The default value is 127.0.0.1.
# Bind 127.0.0.1
# Redis default listening port
Port 6379
# How many seconds after the client is idle, disconnect. The default value is 300 (seconds)
Timeout 300
# Log record level, with four optional values: Debug, verbose (default), notice, and warning
Loglevel verbose
# Specify the log output file name. The default value is stdout. You can also set it to/dev/null to shield logs.
Logfile stdout
# Number of available databases; default value: 16; default value: 0
Databases 16
# Policy for saving data to disk
# If one of the keys data is changed, it will be refreshed to disk once every 900 seconds.
Save 900 1
# When 10 keys data items are changed, refresh them to disk once every 300 seconds
Save 300 10
# When pieces of keys data are changed, refresh to disk once every 60 seconds
Save 60 10000
# Whether to compress data objects when dump. RDB Databases
Rdbcompression Yes
# Local database file name, default value: dump. RDB
Dbfilename dump. RDB
# Local database storage path. The default value is ./
DIR/var/lib/redis/
########### Replication #####################
# Redis replication Configuration
# Slaveof <masterip> <masterport> when the local machine is a slave service, set the master service IP address and port
# Masterauth <master-Password> when the local machine is a slave service, set the master service connection password
# Connection password
# Requirepass foobared
# Maximum number of client connections, unlimited by default
# Maxclients 128
# Maximum memory usage settings. When the maximum memory is reached, redis will first try to clear expired or expiring keys. After this method is processed, any key that has reached the maximum memory settings will be cleared, no more write operations can be performed.
# Maxmemory <bytes>
# Whether to record logs after each update operation. If not enabled, data may be lost for a period of time during power failure. Because redis synchronizes data files according to the Save conditions above, some data will only exist in the memory for a period of time. The default value is no.
Appendonly No
# Update the log file name. The default value is appendonly. Aof.
# Appendfilename
# Update log conditions. There are three optional values. "No" indicates that data is cached and synchronized to the disk by the operating system. "Always" indicates that data is manually written to the disk by calling fsync () after each update operation. "everysec" indicates that data is synchronized once per second (default ).
# Appendfsync always
Appendfsync everysec
# Appendfsync No
############### Virtual memory ###########
# Whether to enable the VM function. The default value is no.
VM-enabled No
# VM-enabled Yes
# Virtual memory file path. The default value is/tmp/redis. Swap. It cannot be shared by multiple redis instances.
VM-Swap-file/tmp/redis. Swap
# Store all data greater than VM-max-memory into the virtual memory. No matter how small the VM-max-memory settings are, all the index data is stored in the memory (redis's index data is keys ), that is to say, when VM-max-memory is set to 0, all values exist on the disk. The default value is 0.
VM-max-memory 0
VM-page-size 32
VM-pages 134217728
VM-max-threads 4
############ Advanced config ###############
Glueoutputbuf Yes
Hash-max-zipmap-entries 64
Hash-max-zipmap-value 512
# Whether to reset the hash table
Activerehashing Yes
Note: The official redis documentation provides some suggestions on VM usage:
- When your key is very small and the value is very large, it will be better to use the VM, because the memory saved is relatively large.
- When your key is not hour, you can consider using some very method to convert a large key into a large value. For example, you can consider combining the key and value into a new value.
- It is best to use Linux ext3 and other file systems that support sparse files to save your swap files.
- The VM-max-threads parameter can be used to set the number of threads accessing the swap file. It is best to set the number of threads not to exceed the number of machine cores. if it is set to 0, all operations on swap files are serial. it may cause a long delay, but it guarantees data integrity.
2. Adjust System Kernel Parameters
If the memory is insufficient, you need to set the kernel parameters:
Echo 1>/proc/sys/Vm/overcommit_memory
Here we will talk about the meaning of this configuration:/proc/sys/Vm/overcommit_memory
This file specifies the kernel memory allocation policy. The value can be 0, 1, or 2.
0 indicates that the kernel will check whether there is enough available memory for use by the process. If there is enough available memory, the memory application will be allowed; otherwise, the memory application will fail, and return the error to the application process.
1 indicates that the kernel allows all physical memory allocation regardless of the current memory status.
2. indicates that the kernel is allowed to allocate more memory than the total physical memory and swap space.
Redis generates a sub-process when dumping data. In theory, the memory occupied by the child process is the same as that of the parent. For example, the parent occupies 8 GB of memory, at this time, 8 GB of memory should also be allocated to the child. If the memory is not enough, it will often cause the redis server to go down or the IO load is too high, and the efficiency is reduced. Therefore, the optimized memory allocation policy should be set to 1 (indicating that the kernel allows allocation of all physical memory, regardless of the current memory status)
4. Run redis
1. Run the service
Redis-server/etc/redis. conf
You can start the redis service in the background. After you confirm that the service is running, you can use the redis-benchmark command to test it. You can also use the redis-CLI command to perform actual operations, for example:
Redis-cli set Foo bar
OK
Redis-cli get foo
Bar
2. Close the service
Redis-cli Shutdown
If the port changes, you can specify the Port:
Redis-cli-P 6380 Shutdown
3. Save/backup
Data backup can be achieved through regular backup of this file.
Because redis writes data to the disk asynchronously, if you want to write data in the memory to the hard disk immediately, run the following command:
Redis-cli save or redis-cli-P 6380 save (specified port)
Note that the preceding deployment operations require certain permissions, such as copying and setting kernel parameters.
When executing the redis-benchmark command, the memory data is also written to the hard disk.
4. Synchronization Mechanism
The synchronization mechanism implemented by redis is relatively simple, and there is a lack of common check points and verification mechanisms for synchronization mechanisms.
During running, if the master-> slave synchronization request Forwarding is discarded, slave will not be able to restore the relevant information of the request until the slave is restarted to load data from the master. Therefore, redis is recommended to use its key/value and value to support multiple types of features to store relatively unimportant data.
5. self-starting
Download the script from here
Declaration: This script is from the network and has been modified, tested, and available.
Before using this script for management, you must configure the following kernel parameters. Otherwise, an error will be reported when the redis script restarts or stops redis, data cannot be automatically synchronized to the disk before the service is stopped:
VI/etc/sysctl. conf
VM. overcommit_memory = 1
Then the application takes effect:
Sysctl-P
Then add the service and start it automatically:
Chmod 755/etc/init. d/redis
Chkconfig -- add redis
Chkconfig -- level 345 redis on
Chkconfig -- list redis