Due to the use of Kestrelin the previous period, at the same time to operate Memcached timely update the cache, but also operate database, persistent data. It looks like Redis can be both a Cache and a Queue! So, start studying Redistoday!
I. Brief introduction of Redis
The Redis - REmote DIctionaryServer can be directly understood as a remote dictionary service, which is based on the Key-value mode memcached+ Database persistence.
If you really want to compare Redis with memcached, refer to:
Using memcached, I was deeply impressed by the problem of object size, because SQL is not optimized for direct mapping of objects, causing the cache object to be larger than 1mb,memcached throws an exception. And the Redis default cache object 512MB, the maximum support 1GB. At least when you cache the object, you can have more space to stretch! Also, is the data type. Memcached is relatively simple, and redis can support more complex data types such as hash, SET, SortedSet, and so on.
PS:Memcached is a shardingimplemented on the server side,Redis has no corresponding implementation, it is said The 3.0 series began to support, but it seemed like 2 years ago.
Second, installation
Redis is too simple to install, so I almost "can't help". Because even the "configure" file is not required, you just need to make a "made" good.
Download the latest Redis version here, with Redis 2.4.16
Download & Unzip:
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 extracted to any directory, a make installation to get execution, configuration files.
Install (This will extract Redis to the/opt/directory):
Shell Code
- cd/opt/redis-2.4. -
- Make
After make, we get the following executable file:
- redis-server: Daemon boot program for Redis server
- redis-cli: Redis command-line Operations tool. or a plain text protocol operation via Telnet
- Redis-benchmark: Redis Performance testing tool to test the read and write performance of Redis in your system and in your configuration
The above files are located in the src directory.
I have habitually executed make install, which looks like the executable file I need to install to /usr/local/bin:
Quote # 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 is 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 up to date.
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 is 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 ' was 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 '
So I don't have to copy the files. Unexpected Harvest!
In addition, you will get 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 under any path!
third, the Operation
To run Redis:
Shell Code
- Redis-server/etc/redis/redis.conf
Citation [1958] 16:18:24 * Server started, Redis version 2.4.16
[1958] 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.overcom Mit_memory=1 ' for the take effect.
[1958] 16:18:24 * The server is now a ready-to-accept connections on port 6379
[1958] 16:18:24-0 clients connected (0 slaves), 717544 bytes in use
Iv. Testing
accessing Redis via client command REDIS-CLI
Reference # REDIS-CLI
Redis> Set name Zlex
Ok
Redis> Get Name
"Zlex"
To test the data:
Shell Code
- Redis-benchmark-l
This test will go on until you CTRL + C:
====== PING (inline) ======
10000 requests completed in 0.12 seconds
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% <= milliseconds
99.91% <= milliseconds
99.93% <= milliseconds
99.96% <= milliseconds
99.98% <= milliseconds
100.00% <= milliseconds
81300.81 Requests per second
====== PING ======
10000 requests completed in 0.12 seconds
Parallel clients
3 bytes Payload
Keep Alive:1
99.96% <= 1 milliseconds
100.00% <= 1 milliseconds
84033.61 Requests per second
^cet (keys): 26200.00
Five, close
You can also complete the Redis shutdown operation via the client command redis-cli :
Shell Code
- REDIS-CLI shutdown
References [2639] 16:35:35 # User requested shutdown ...
[2639] 16:35:35 * Saving the final RDB snapshot before exiting.
[2639] 16:36:49 * DB saved on disk
[2639] 16:36:49 # Redis is now ready to exit, Bye bye ...
Six, tuning
1./etc/sysctl.conf
When you start Redis earlier, you see the following warning:
Reference [1958] 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.overcom Mit_memory=1 ' for the take effect.
You need to modify the /etc/sysctl.conf file:
Shell Code
- Vim/etc/sysctl.conf
Append vm.overcommit_memory = 1 at end
Then execute the sysctl vm.overcommit_memory=1to make it effective:
Shell Code
- # sysctl vm.overcommit_memory=1
- Vm.overcommit_memory = 1
2./proc/sys/vm/overcommit_memory
In order to adjust the memory allocation policy, you need to configure /proc/sys/vm/overcommit_memory
- 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
The default is 0, if the memory situation is more tense, set to 1:
Shell Code
- echo 1 >/proc/sys/vm/overcommit_memory
3.redis.conf
After starting Redis in front, it's always a hassle to keep a log on the command line. Even through "&", it doesn't help to get the backstage running. This requires modifying the redis.confto run in Daemo mode!
redis.conf Parameters:
- 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 *: The frequency at which the snapshot is saved, the first * indicates how long (seconds), and the third * indicates how many times the write operation was 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)
- Slaveof <masterip> <masterport>: Master-slave configuration, configure the master IP port on the redis-slave.
For example, we can modify it to the following ways:
Reference Daemonize Yes #守护进程模式
Save #当时间间隔超过60秒, or persist when storing more than 1000 records.
MaxMemory 256MB #分配256MB内存
PS: Remember, be sure to set the maxmemmory, and the configuration size is smaller than the physical memory, leaving enough memory for the system to use.
A classmate of the company's Redis, a period of data explosion, resulting in a tight memory, swap intensified, direct downtime. This is because maxmemmoryis not set.
vii. Cluster Configuration
It is dangerous to put all the eggs in one basket. First of all, do the main preparation. Second, if you can do a consistent hash, you can play the role of load balancing.
To configure Master-slave, simply configure the master node IP Port on the Slave:
The master IP here is 192.168.133.139 port bit 6379, which configures the redis.conf:Slaveof 192.168.133.139 6379
PS: For two Redis server can exchange visits, need to comment out bind 127.0.0.1
Start Master,slave in turn:
master[7651] 19:08:07 * Server started, Redis version 2.4.16
[7651] 19:08:07 * DB loaded from disk:0 seconds
[7651] 19:08:07 * The server is now a ready-to-accept connections on port 6379
[7651] 19:08:08 * Slave ask for synchronization
[7651] 19:08:08 * Starting BGSAVE for SYNC
[7651] 19:08:08 * Background saving started by PID 7652
[7652] 19:08:08 * DB saved on disk
[7651] 19:08:08 * Background saving terminated with success
[7651] 19:08:08 * Synchronization with Slave succeeded
SLAVE[7572] 19:07:39 * Server started, Redis version 2.4.16
[7572] 19:07:39 * DB loaded from disk:0 seconds
[7572] 19:07:39 * The server is now a ready-to-accept connections on port 6379
[7572] 19:07:39 * Connecting to MASTER ...
[7572] 19:08:08 * MASTER <-> SLAVE sync Started:sync sent
[7572] 19:08:08 * Master <-> SLAVE sync:receiving bytes from master
[7572] 19:08:08 * MASTER <-> SLAVE sync:loading DB in memory
[7572] 19:08:08 * MASTER <-> SLAVE sync:finished with success
See the above log, it means that Master-slave has been connected.
Simple test, master write, slave read:
Master Write 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 read Telnet 192.168.133.140 6379
Trying 192.168.133.140 ...
Connected to 192.168.133.140.
Escape character is ' ^] '.
Get Name
$7
Snowolf
Get!
viii. master/slave backup
Execute the following command from the server:
Shell Code
- #备份
- REDIS-CLI Save
- #关闭redis服务器
- REDIS-CLI shutdown
Then, copy the Rdb file under the data directory.
Nine, System services
Used to start all services through the service, of course, this is related to my production environment deployment, usually only the permissions assigned to the Account Operation Service command for the deployment. The main purpose is to ensure system security.
Refer to the Memcached system service file previously written to transform a redis version!
Create a new file and give permissions:
Shell Code
- Touch/etc/init.d/redis-server
- chmod +x/etc/init.d/redis-server
To edit /etc/init.d/redis-server, type the following:
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/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-a!-Z $redis _pid];then
- echo $prog"already running ..."
- Exit 1
- Fi
- Echo-n $"Starting $prog"
- # 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 $redis _path
- Echo
- [$RETVAL = 0] && rm-f $redis _pid/var/lock/subsys/$prog
- Retval=$?
- Return $RETVAL
- }
- # See how we were called.
- Case "$" 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: $ {start|stop|status|restart|condrestart}"
- Exit 1
- Esac
- Exit $RETVAL
Quote # service Redis-server restart
Stopping Redis [failed]
Starting Redis [OK]
# Service Redis-server Status
Redis (PID 14965) is running ...
Very convenient!
Redis Simple Configuration