I. About redis
Redis is an advanced key-value database. It is similar to memcached, but data can be persistent and supports a wide range of data types. It keeps key-value databases simple and fast, while absorbing the advantages of some relational databases. So that it is located between the relational database and the key-value database. Redis not only saves strings data, but also stores lists (ordered) and sets (unordered) data. It also supports advanced functions such as sorting, this ensures the atomicity of operations when implementing functions such as incr and setnx. In addition, it also supports master-slave replication and other functions. Redis can be viewed as a data structure server.
All redis data is stored in the memory and then asynchronously stored to the disk (this is called the "semi-persistent mode "); you can also write every data change to an append only file (AOF) (this is called "Full persistence mode ").
Official Website:
Http://redis.io/
Ii. Install redis
Redis code is written in ANSI-C and can be installed and run on all POSIX systems (such as Linux, * BSD, Mac OS X, Solaris, etc. In addition, redis does not rely on any non-standard library, nor does it need to add compilation parameters. Make is the only thing we need to compile and install redis.
1. download the latest redis version 2.2.12.
CD/usr/local/src
Wget http://redis.googlecode.com/files/redis-2.2.12.tar.gz
2. Compile and install redis
Tar zxvf redis-2.2.12.tar.gz
CD redis-2.2.12
Make
After the make command is executed, four executable files are generated in/usr/local/bin, which areRedis-Server,Redis-cli,Redis-Benchmark,Redis-statAnd their functions are as follows:
Redis-Server: Daemon Startup Program of the redis Server
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-stat: Redis status detection tool, which can detect the current redis status parameters and delay status
3. Create a redis directory (recommended, not required)
Run the following command to create a directory and copy the file to the directory:
Sudo-S
Mkdir-P/usr/local/redis/bin
Mkdir-P/usr/local/redis/etc
Mkdir-P/usr/local/redis/var
CP redis-server redis-cli redis-benchmark redis-STAT/usr/local/redis/bin/
CP redis. CONF/usr/local/redis/etc/
Create a redis directory to better manage redis-related resources in a unified manner. You can also use
Make install
Installed in the default system directory
3. Configure redis
After redis is successfully installed, run the redis-server command to run redis. In this case, it runs according to the default configuration (the default configuration is not the background operation ). If we want redis to run as required, we need to modify the configuration file. The redis configuration file is the redis In the second CP operation above. CONF file, which is copied to the/usr/local/redis/etc/directory. Modify it to configure the redis server.
1. redis. conf configuration parameters:
# If no is run as a daemon, the current running status will be continuously printed.
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/usr/local/redis/var/
########### 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 logs/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 small and the value is large, Vm usage will be better, because the memory saved is relatively large.
** When your key is not hour, you can consider using some very methods to convert a large key into a large value. For example, you can consider changing the key, value is combined 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 recommended that the number of threads accessing the swap file not 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
/Usr/local/redis/bin/redis-server/usr/local/redis/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:
/Usr/local/redis/bin/redis-cli set Foo bar
OK
/Usr/local/redis/bin/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 saveOrRedis-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.