Redis Simple Configuration

Source: Internet
Author: User
Tags benchmark redis version keep alive redis server

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
    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 extracted to any directory, a make installation to get execution, configuration files.
Install (This will extract Redis to the/opt/directory):

Shell Code
    1. cd/opt/redis-2.4. -
    2. 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
    1. Mkdir/etc/redis
    2. CP Redis.conf/etc/redis

Then, we can start Redis directly under any path!

third, the Operation

To run Redis:

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

Append vm.overcommit_memory = 1 at end
Then execute the sysctl vm.overcommit_memory=1to make it effective:

Shell Code
    1. # sysctl vm.overcommit_memory=1
    2. 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
    1. 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
    1. #备份
    2. REDIS-CLI Save
    3. #关闭redis服务器
    4. 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
    1. Touch/etc/init.d/redis-server
    2. chmod +x/etc/init.d/redis-server



To edit /etc/init.d/redis-server, type the following:

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

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.