Redis

Source: Internet
Author: User
Tags redis version keep alive install redis
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
  1. Wget http://redis.googlecode.com/files/redis-2.4.16.tar.gz
  2. 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
  1. Cd/opt/redis-2.4.16
  2. 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
  1. Mkdir/etc/redis
  2. Cp redis. conf/etc/redis

  

Then, we can start Redis directly in any path!

III. Running

Run Redis:

 

Shell code
  1. 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
  1. 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
  1. 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
  1. Vim/etc/sysctl. conf

  

AppendVm. overcommit_memory = 1
Then executeSysctl vm. overcommit_memory = 1To take effect:

 

Shell code
  1. # Sysctl vm. overcommit_memory = 1
  2. 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
  1. 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
  1. # Backup
  2. Redis-cli save
  3.   
  4. # Shut down the redis server
  5. 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
  1. Touch/etc/init. d/redis-server
  2. Chmod + x/etc/init. d/redis-server

 



Edit/Etc/init. d/redis-server, Enter the following content:

 

Shell code
  1. #! /Bin/bash
  2. #  
  3. # Redis Startup script for redis processes
  4. #  
  5. # Author: snowolf
  6. #  
  7. # Processname: redis
  8.   
  9. Redis_path = "/usr/local/bin/redis-server"
  10. Redis_conf = "/etc/redis. conf"
  11. Redis_pid = "/var/run/redis. pid"
  12.   
  13. # Source function library.
  14. ./Etc/rc. d/init. d/functions
  15.   
  16. [-X $ redis_path] | exit 0
  17.   
  18. RETVAL = 0
  19. Prog = "redis"
  20.   
  21.   
  22. # Start daemons.
  23. Start (){
  24. If [-e $ redis_pid-! -Z $ redis_pid]; then
  25. Echo $ prog "already running ...."
  26. Exit 1
  27. Fi
  28.   
  29. Echo-n $ "Starting $ prog"
  30. # Single instance for all caches
  31. $ Redis_path $ redis_conf
  32. RETVAL =$?
  33. [$ RETVAL-eq 0] & {
  34. Touch/var/lock/subsys/$ prog
  35. Success $ "$ prog"
  36.     }  
  37. Echo
  38. Return $ RETVAL
  39. }  
  40.   
  41.   
  42. # Stop daemons.
  43. Stop (){
  44. Echo-n $ "Stopping $ prog"
  45. Killproc-d 10 $ redis_path
  46. Echo
  47. [$ RETVAL = 0] & rm-f $ redis_pid/var/lock/subsys/$ prog
  48.   
  49. RETVAL =$?
  50. Return $ RETVAL
  51. }  
  52.   
  53.   
  54. # See how we were called.
  55. Case "$1" in
  56. Start)
  57. Start
  58. ;;
  59. Stop)
  60. Stop
  61. ;;
  62. Status)
  63. Status $ prog
  64. RETVAL =$?
  65. ;;
  66. Restart)
  67. Stop
  68. Start
  69. ;;
  70. Condrestart)
  71. If test "x 'pidof redis '"! = X; then
  72. Stop
  73. Start
  74. Fi
  75. ;;
  76. *)
  77. Echo $ "Usage: $0 {start | stop | status | restart | condrestart }"
  78. Exit 1
  79. Esac
  80. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.