Redis Non-relational database (Nosql)

Source: Internet
Author: User
Tags benchmark redis server




Brief introduction:



Redis is an open-source, high-performance key-value system that can be used to cache or store data.

Redis data can be persisted and supports multiple data types: string, list, hash (hash), set (set), and ordered set (sorted set).

Can be a good complement to the relational database. It also offers a variety of client-side Python, Ruby, Erlang, and PHP that can be easily invoked.



Persistence:



Redis is a memory database (similar to memcache), excellent in data access efficiency, and because of the risk of data loss in memory, Redis provides two persistence mechanisms to write in-memory data to disk in a timely manner.



The RDB uses storage snapshots to write memory data to disk periodically, semi-persistent

AOF writes to a AOF file on disk, using a log-like Binlog, each command that changes Redis data.



Redis turns on the RDB mode by default, turning off AOF mode because AOF is more resource-intensive, but it is very safe for data, called full persistence, which mode to choose on demand.



Master-Slave function:



To prevent Redis disk corruption, resulting in data loss, REDIS provides replication capabilities to automatically synchronize data from one primary database to the database to prevent data loss.



Master-Slave principle:



1. After starting the database, send the SYNC command to the primary database
2. After the master database receives the SYNC command, it starts to save the snapshot, during which all commands issued to the primary database are cached.
3. After the snapshot is saved, the primary database sends the snapshot and cached commands to the database
4. Save the snapshot files from the main data from the database and execute the cache commands from the primary database sequentially.



During synchronization, from the database is not blocked, it defaults to using the data before the synchronization to continue to respond to commands sent by the client



: https://code.google.com/p/redis/downloads/list



I. Installation of Redis

shell> tar zxf redis-2.6.10.tar.gz
shell> cd redis-2.6.10; make; make install
Adjust Redis

shell> mkdir / usr / local / redis
shell> cd! $
shell> cp ../src/redis-2.6.10/redis.conf.
shell> cp ../src/redis-2.6.10/src/redis-cli.
shell> cp ../src/redis-2.6.10/src/redis-server.
shell> cp ../src/redis-2.6.10/src/redis-sentinel.
shell> cp ../src/redis-2.6.10/src/redis-benchmark.
shell> cp ../src/redis-2.6.10/src/redis-check-aof.
shell> cp ../src/redis-2.6.10/src/redis-check-dump.
## redis.conf main configuration file
## redis-cli command line operation tool
## redis-server launcher
## redis-sentinel cluster management tool
## redis-benchmark Performance test tool to test the read and write performance of redis under the current system configuration
## redis-check-aof update log check, --fix can repair log files
## redis-check-dump check local data files

Edit redis.conf

shell> mv redis.conf redis.conf.bak

shell> vim redis.conf

daemonize yes # start in daemon mode
pidfile /usr/local/redis/logs/redis.pid # Define the pid file storage path
port 6379 # Listening port
timeout 300 # request timeout
loglevel debug # define log level
logfile /usr/local/redis/logs/redis.log # define the log file path
databases 16 # Start the number of database instances. The default database connection is 0. You can connect to different databases by selecting N
                                           # Strategy for saving data from memory to disk:
save 900 1 # When there is 1 key data change, refresh to disk once in 900 seconds
save 300 10 # When there are 10 keys changed, refresh to disk once every 300 seconds
save 60 10000 # When 1W keys data is changed, refresh to disk once every 60 seconds
rdbcompression yes # Whether to compress data objects when dumping .rdb database
dbfilename dump.rdb # the name of the file saved by the database when dumping the database
dir / usr / local / redis / data / # path where the database is saved when dumping
# ------------------------------------------------- -------------------------------------------------- -
appendonly no # Whether to enable the log function. If you enable each operation, a log will be recorded, which is equivalent to the mysql binlog, but it will affect the efficiency.
#appendfilename appendonly.aof # AOF file name, default is ppendonly.aof
#appendfsync everysec # Rules for syncing data to disk:
                                           # 1, no Does not actively write to the disk, and depends on the system's write. Generally, it is written once in about 30 seconds.
                                           # 2, everysec Forces to write to the disk once every second. It is recommended to use it in terms of performance and durability.
                                           # 3, always force write to disk every time a write operation is received, which is the most guaranteed complete persistence, but slow.
# no-appendfsync-on-rewrite yes # When the log is rewritten, no command append operation is performed, but it is only placed in the buffer to avoid conflicts with the append of the command on the disk IO
# auto-aof-rewrite-percentage 100 # Automatically start a new log rewrite process when the current AOF file is twice the size of the AOF file obtained from the last log rewrite
# auto-aof-rewrite-min-size 64mb # The minimum value of the current AOF file to start a new log rewrite process, to avoid frequent rewrites due to the small file size when redis is started
# ------------------------------------------------- -------------------------------------------------- -
## Set whether to combine smaller packets into a single packet when sending a response to the client

Fourth, start Redis

shell> echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf

shell> sysctl vm.overcommit_memory = 1
## Adjust the kernel parameters. If redis is not adjusted, a warning message will be reported.

shell> mkdir / usr / local / redis / data # create data directory

shell> mkdir / usr / local / redis / logs # create logs directory

shell> redis-server /usr/local/redis/redis.conf # To start redis, you need to specify the configuration file path

shell> netstat -anpt | grep redis
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 1176 / redis-server
Five, test Redis

-------------------------------------------------- -------------------------------------------------- -

shell> redis-cli
redis 127.0.0.1:6379> set password 123
OK
redis 127.0.0.1:6379> get password
"123"
redis 127.0.0.1:6379> quit
## Login the local redis, write keys / value, read keys

shell> ls / usr / local / redis / data /
## Dump.rdb is not generated because the semi-persistent save rule is not triggered

shell> redis-cli shutdown
shell> ls / usr / local / redis / data /
dump.rdb
## When redis is closed, the data in memory will be saved as dump.rdb

shell> redis-server /usr/local/redis/redis.conf
shell> redis-cli
redis 127.0.0.1:6379> get password
"123"
redis 127.0.0.1:6379> quit
## redis reads data from dump.rdb file after startup, so the data still exists

-------------------------------------------------- -------------------------------------------------- -

shell> rm -rf / usr / local / redis / data / *

shell> redis-cli
redis 127.0.0.1:6379> set password abcdef
OK
redis 127.0.0.1:6379> get password
"abcdef"
redis 127.0.0.1:6379> quit

shell> kill -9 `cat / usr / local / redis / logs / redis.pid`
## When redis is not shut down normally, such as kill -9, the redis server suddenly goes down, etc.

shell> ls / usr / local / redis / data /
## Does not synchronize the data in memory to disk, which means that dump.rdb file is not generated

shell> redis-server /usr/local/redis/redis.conf

shell> redis-cli
redis 127.0.0.1:6379> get password
(nil)
redis 127.0.0.1:6379> quit
## Start redis at this time and find that the data is lost. If you can't stand the loss, you can start AOF full persistence.

-------------------------------------------------- -------------------------------------------------- -

references:

http://blog.csdn.net/qtyl1988/article/details/39553339

Redis non-relational database (Nosql)

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.