I. Brief introduction to Redis
Redis--REMoteDICtionarySErver can be directly understood as a remote dictionary service, that is, Memcached + Database Persistence based on the Key-Value mode.
If you really want to compare Redis with Memcached, refer:
I was deeply touched by the problem of Object Size when using Memcached. Because SQL does not optimize the direct ing of objects, the cache Object is larger than 1 MB, and Memcached throws an exception. Redis caches 512 MB by default, and supports a maximum of 1 GB. At least when caching objects, you can have a larger scaling space! In addition, it is a data type. Memcached is relatively simple, while Redis can support more complex data types, such as HASH, SET, SortedSet, and so on.
PS:MemcachedYesServerEnd-to-end implementationSharding,RedisThere is no corresponding implementation. It is said that3.0The series began to support, but this seems to have been around for two years.
II. Installation
It was too simple to install Redis, making me almost "unable to start ". Because the "configure" file is not required, you only need to make it.
Download the latest version of Redis here. Redis 2.4.16 is used here.
Download & decompress:
Shell code
- Wget http://redis.googlecode.com/files/redis-2.4.16.tar.gz
- Tar zxvf redis-2.4.16.tar.gz
Redis can be decompressed to any directory, and a make installation can obtain the execution and configuration files.
Install redis (decompress redis to the/opt/directory ):
Shell code
- Cd/opt/redis-2.4.16
- Make
After make, we will get the following executable files:
- Redis-server: Daemon startup program of the Redis server
- Redis-cli: Redis command line operation tool. Or use telnet for plain text protocol operations.
- Redis-benchmark: Redis performance testing tool to test the read/write performance of Redis in your system and your configuration
The preceding file is located inSrcDirectory.
I habitually execute make install. It seems that the executable file I need is installed/Usr/local/bin:
Reference # make install
Cd src & make install
Make [1]: Entering directory '/opt/software/redis-2.4.16/src'
MAKE hiredis
Make [2]: Entering directory '/opt/software/redis-2.4.16/deps/hiredis'
Make [2]: Nothing to be done for 'static '.
Make [2]: Leaving directory '/opt/software/redis-2.4.16/deps/hiredis'
MAKE linenoise
Make [2]: Entering directory '/opt/software/redis-2.4.16/deps/linenoise'
Make [2]: "linenoise_example" is the latest.
Make [2]: Leaving directory '/opt/software/redis-2.4.16/deps/linenoise'
MAKE hiredis
Make [2]: Entering directory '/opt/software/redis-2.4.16/deps/hiredis'
Make [2]: Nothing to be done for 'static '.
Make [2]: Leaving directory '/opt/software/redis-2.4.16/deps/hiredis'
LINK redis-benchmark
LINK redis-cli
Hint: To run 'Make test' is a good idea ;)
Mkdir-p/usr/local/bin
Cp-pf redis-server/usr/local/bin
Cp-pf redis-benchmark/usr/local/bin
Cp-pf redis-cli/usr/local/bin
Cp-pf redis-check-dump/usr/local/bin
Cp-pf redis-check-aof/usr/local/bin
Make [1]: Leaving directory '/opt/software/redis-2.4.16/src'
In this way, you do not need to copy files. Unexpected gains!
In addition, a default configuration file --Redis. conf.
It is best to copy it to a fixed Directory, for example:/etc/redis/directory!
Shell code
- Mkdir/etc/redis
- Cp redis. conf/etc/redis
Then, we can start Redis directly in any path!
III. Running
Run Redis:
Shell code
- Redis-server/etc/redis. conf
Reference [1958] 13 Aug 16:18:24 * Server started, Redis version 2.4.16
[1, 1958] 13 Aug 16:18:24 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. to fix this issue add'vm. overcommit_memory = 1' to/etc/sysctl. conf and then reboot or run the command 'sysctl vm. overcommit_memory = 1' for this to take effect.
[1958] 13 Aug 16:18:24 * The server is now ready to accept connections on port 6379
[1958] 13 Aug 16:18:24-0 clients connected (0 slaves), 717544 bytes in use
IV. Test
Use client commandsRedis-cliAccess Redis
Reference # redis-cli
Redis> set name zlex
OK
Redis> get name
"Zlex"
Perform data testing:
Shell code
- Redis-benchmark-l
This test will continue until you Ctrl + C:
===== PING (inline) ======
10000 requests completed in 0.12 seconds
50 parallel clients
3 bytes payload
Keep alive: 1
99.31% <= 1 milliseconds
99.53% <= 2 milliseconds
99.64% <= 3 milliseconds
99.70% <= 4 milliseconds
99.74% <= 5 milliseconds
99.78% <= 6 milliseconds
99.82% <= 7 milliseconds
99.84% <= 8 milliseconds
99.86% <= 9 milliseconds
99.89% <= 10 milliseconds
99.91% <= 11 milliseconds
99.93% <= 12 milliseconds
99.96% <= 13 milliseconds
99.98% <= 14 milliseconds
100.00% <= 15 milliseconds
81300.81 requests per second
===== PING ======
10000 requests completed in 0.12 seconds
50 parallel clients
3 bytes payload
Keep alive: 1
99.96% <= 1 milliseconds
100.00% <= 1 milliseconds
84033.61 requests per second
^ CET (10 keys): 26200.00
5. Disable
You can also use client commandsRedis-cliCompleteRedisClose operation:
Shell code
- Redis-cli shutdown
Reference [2639] 13 Aug 16:35:35 # User requested shutdown...
[2639] 13 Aug 16:35:35 * Saving the final RDB snapshot before exiting.
[2639] 13 Aug 16:36:49 * DB saved on disk
[2639] 13 Aug 16:36:49 # Redis is now ready to exit, bye...
VI. Optimization
1./Etc/sysctl. conf
When starting Redis, the following warning is displayed:
Reference [1958] 13 Aug 16:18:24 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. to fix this issue add'vm. overcommit_memory = 1' to/etc/sysctl. conf and then reboot or run the command 'sysctl vm. overcommit_memory = 1' for this to take effect.
Need to modify/Etc/sysctl. confFile:
Shell code
- Vim/etc/sysctl. conf
AppendVm. overcommit_memory = 1
Then executeSysctl vm. overcommit_memory = 1To take effect:
Shell code
- # Sysctl vm. overcommit_memory = 1
- Vm. overcommit_memory = 1
2./Proc/sys/vm/overcommit_memory
To adjust the memory allocation policy, you must configure/Proc/sys/vm/overcommit_memory
- 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.
The default value is 0. If the memory is insufficient, set it to 1:
Shell code
- Echo 1>/proc/sys/vm/overcommit_memory
3.Redis. conf
After starting Redis, it is always troublesome to skip various logs in the command line. Even if you use "&" to get its background running, it will not help. This requires modification.Redis. conf, Run in Daemo mode!
Redis. confParameters:
- Daemonize: whether to run in daemon mode
- Pidfile: pid file location
- Port: The port number of the listener.
- Timeout: Request timeout
- Loglevel: log information level
- Logfile: location of the log file
- Databases: number of databases enabled
- Save **: the snapshot retention frequency. The first * indicates the time (in seconds) and the third * indicates the number of write operations performed. Snapshots are automatically saved when a certain number of write operations are performed within a certain period of time. You can set multiple conditions.
- Rdbcompression: whether to use compression
- Dbfilename: data snapshot file name (only file name, excluding directory)
- Dir: Directory for storing data snapshots (this is the directory)
- Appendonly: whether to enable appendonlylog. If it is enabled, a log is recorded for each write operation, which improves data risk resistance but affects efficiency.
- Appendfsync: how to synchronize appendonlylog to the disk (three options are force-call fsync for each write, enable fsync once per second, and do not call fsync to wait for the system to synchronize itself)
- Slaveof <masterip> <masterport>: master-slave configuration. Configure the master ip port on redis-slave.
For example, we can modify it to the following method:
Reference daemonize yes # daemon mode
Save 60 1000 # persistence when the interval exceeds 60 seconds or when more than 1000 records are stored.
Maxmemory 256 mb # allocate mb of memory
PS: Be sure to setMaxmemmoryAnd the configuration size must be smaller than the physical memory, leaving enough memory for the system to use.
During a certain period of time, a company employee's Redis data soared, resulting in a tight memory, increased SWAP, and direct downtime. It is because no settings are set.Maxmemmory.
VII. Cluster configuration
Putting all the eggs in one basket is dangerous. First, master and backup are required. Second, if consistent hash can be performed, it can play a role in load balancing.
To configure Master-Slave, you only need to configure the Master node IP Port on Slave:
The Master IP address is 192.168.133.139 Port 6379. Configure redis. conf: slaveof 192.168.133.139 6379.
PS: to allow mutual access between two Redis servers, comment outBind 127.0.0.1
Start Master in sequence, Slave:
Master [7651] 17 Aug 19:08:07 * Server started, Redis version 2.4.16
[7651] 17 Aug 19:08:07 * DB loaded from disk: 0 seconds
[7651] 17 Aug 19:08:07 * The server is now ready to accept connections on port 6379
[7651] 17 Aug 19:08:08 * Slave ask for synchronization
[7651] 17 Aug 19:08:08 * Starting BGSAVE for SYNC
[7651] 17 Aug 19:08:08 * Background saving started by pid 7652
[7652] 17 Aug 19:08:08 * DB saved on disk
[7651] 17 Aug 19:08:08 * Background saving terminated with success
[7651] 17 Aug 19:08:08 * Synchronization with slave succeeded
Slave [7572] 17 Aug 19:07:39 * Server started, Redis version 2.4.16
[7572] 17 Aug 19:07:39 * DB loaded from disk: 0 seconds
[7572] 17 Aug 19:07:39 * The server is now ready to accept connections on port 6379
[7572] 17 Aug 19:07:39 * Connecting to MASTER...
[7572] 17 Aug 19:08:08 * MASTER <-> SLAVE sync started: SYNC sent
[7572] 17 Aug 19:08:08 * MASTER <-> SLAVE sync: Flushing 10 bytes from master
[7572] 17 Aug 19:08:08 * MASTER <-> SLAVE sync: Loading DB in memory
[7572] 17 Aug 19:08:08 * MASTER <-> SLAVE sync: Finished with success
When you see the preceding logs, the Master-Slave is connected.
Simple test, Master write, Slave read:
The Master writes telnet 192.168.133.139 6379.
Trying 192.168.133.139...
Connected to 192.168.133.139.
Escape character is '^]'.
Set name snowolf
+ OK
Slave reads telnet 192.168.133.140 6379.
Trying 192.168.133.140...
Connected to 192.168.133.140.
Escape character is '^]'.
Get name
$7
Snowolf
Done!
8. Master-slave backup
Run the following commands on the slave server:
Shell code
- # Backup
- Redis-cli save
-
- # Shut down the redis server
- Redis-cli shutdown
Then, copy the rdb file in the data directory.
IX. System Services
I am used to starting all services through the service. Of course, this is related to my production environment deployment. Generally, I only assign permissions to the account used for deployment to operate service commands. It is mainly used to ensure system security.
Refer to the previously written Memcached system service file to rebuild a Redis version!
Create a file and grant the following permissions:
Shell code
- Touch/etc/init. d/redis-server
- Chmod + x/etc/init. d/redis-server
Edit/Etc/init. d/redis-server, Enter the following content:
Shell code
- #! /Bin/bash
- #
- # Redis Startup script for redis processes
- #
- # Author: snowolf
- #
- # Processname: redis
-
- Redis_path = "/usr/local/bin/redis-server"
- Redis_conf = "/etc/redis. conf"
- Redis_pid = "/var/run/redis. pid"
-
- # Source function library.
- ./Etc/rc. d/init. d/functions
-
- [-X $ redis_path] | exit 0
-
- RETVAL = 0
- Prog = "redis"
-
-
- # Start daemons.
- Start (){
- If [-e $ redis_pid-! -Z $ redis_pid]; then
- Echo $ prog "already running ...."
- Exit 1
- Fi
-
- Echo-n $ "Starting $ prog"
- # Single instance for all caches
- $ Redis_path $ redis_conf
- RETVAL =$?
- [$ RETVAL-eq 0] & {
- Touch/var/lock/subsys/$ prog
- Success $ "$ prog"
- }
- Echo
- Return $ RETVAL
- }
-
-
- # Stop daemons.
- Stop (){
- Echo-n $ "Stopping $ prog"
- Killproc-d 10 $ redis_path
- Echo
- [$ RETVAL = 0] & rm-f $ redis_pid/var/lock/subsys/$ prog
-
- RETVAL =$?
- Return $ RETVAL
- }
-
-
- # See how we were called.
- Case "$1" in
- Start)
- Start
- ;;
- Stop)
- Stop
- ;;
- Status)
- Status $ prog
- RETVAL =$?
- ;;
- Restart)
- Stop
- Start
- ;;
- Condrestart)
- If test "x 'pidof redis '"! = X; then
- Stop
- Start
- Fi
- ;;
- *)
- Echo $ "Usage: $0 {start | stop | status | restart | condrestart }"
- Exit 1
- Esac
- Exit $ RETVAL
Reference # service redis-server restart
Stopping redis [failed]
Starting redis [OK]
# Service redis-server status
Redis (pid 14965) is running...
Very convenient!
It is not yet complete.
Redis