Getting Started with Redis

Source: Internet
Author: User
Tags syslog redis tutorial

About Redis

REmote DIctionary Server (Redis) is a Key-value storage system written by Salvatore Sanfilippo.
Redis is an open source API that is written in ANSI C, adheres to the BSD protocol, supports networks, can be persisted in memory, key-value databases, and provides multiple languages.
It is commonly referred to as a data structure server, because the value can be a string (string), a hash (MAP), a list, a collection (sets), and an ordered collection (sorted sets).
For more information, see the wiki Redis and Redis official website.

Redis Installation
    1. Please download the UNIX department directly on the website.
    2. Window 64-bit version please download at this address.
    3. After downloading, unzip or Windows install the MSI file directly, create a new two bat file in the unzip directory or install directory. is Start.bat, content is Redis-server redis.windows.conf and Clear.bat, content is Redis-cli flushdb.
    4. Double-click Open Redis-cli.exe, enter ping, get pong instructions Redis connection is OK.
Redis Common Commands

Redis commands can be used interactively in Redis-cli.exe, entering
set k 1
Return
ok
That is, Reids has stored the key "K", Value "1" data.
Refer to the following tutorials for the specific commands you use:

    • Runoob.com's Redis Tutorial
    • A simple and common command tutorial in Blog
    • Royce Wang combed the Advanced common commands tutorial
    • Redis Official Command documentation
Advanced Features 1. Security

After you have set up a client connection, you need a password before any action is specified, and an external user can make 150W access for another second.
Change Password:
-Method One: Configure with the command line: Yes config set requirepass abc . Check the password: config get requirepass authorized or auth abc
-Method Two: Modify the settings redis.conf inside the Requirepass attribute to give the password, I give here is ABC. Then if you want to use the operation can be used when the authorization sudo /opt/java/redis/bin/redis-cli -a abc or after the entry auth abc and then you can operate freely.

2. master-slave replication

Two virtual machines were prepared with IP 192.168.15.128 and 192.168.15.133, which allows multiple slave servers to have the same database copy as master server, with master-slave replication
Configuration is configured on slave above slave

```slaveof 192.168.15.128 6379masterauth abc```如果没有主从同步那么就检查一下是不是防火墙的问题,我用的是ufw,设置一下sudo ufw allow 6379就可以了,这个时候可以通过info查看具体的情况
3. Transaction processing

Redis's support for transactions is relatively straightforward, and Redis can only guarantee that commands in one client-initiated transaction can be executed consecutively, without inserting other client commands in the middle. When a client issues a multi command in a connection, the connection goes into the context of a transaction, and the subsequent command is not executed immediately, but is placed first in a queue, and Redis executes all the commands in the queue sequentially when the EXEC command is executed.
For example, one of the following:

```set age 100multiset age 10set age 20execget age --这个内容就应该是20multiset age 20set age 10execget age --这个时候的内容就成了10,充分体现了一下按照队列顺序执行的方式discard  取消所有事务,也就是事务回滚```

Optimistic lock
Watch key if the key without watch is changed then the outdate transaction cannot be executed.
However, it is important to note that there is no guarantee that the key of the operation in the queue will not change (concurrent environment), and watch can be used to monitor and roll back (optimistic concurrency) in the concurrency. Of course, Redis also offers a number of combinations of atomic commands (such as checking and adding, adding after deletion, which are particularly useful).

4. Persistence mechanism

Redis is an in-memory database that supports persistence.
-Snapshotting Snapshot mode is the default method of storage, by default written to the Dump.rdb binary file, you can configure Redis to automatically take snapshots if more than M key is modified in n seconds
-append-only file aof log mode, when using AOF, Redis appends each function to the file, and when Redis restarts, the saved write commands in the file are re-executed in memory.

5. Piping

Submit multiple commands at once (if you just make some settings, the commands do not need to rely on the result of the predecessor command, which can improve efficiency). Of course, Redis also provides many multi-commands, allowing multiple values to be added, obtained, and so on at once.

6. VMs

The operating system's pages are too large, and Redis uses 32-byte pages by default. Keep all the keys in memory, compress all the values (very powerful, save 10G of data to occupy 2G less than the storage, I suspect I was wrong) saved in swap. You can also configure the option to swap in and out using either synchronous or asynchronous mode.

Client

In the official website there is a Redis support for various languages of the client, I downloaded the client is the Java version of the Jedis. Https://github.com/xetorthio/jedis

Java usage Examples :

//连接本地的 Redis 服务new Jedis("localhost");System.out.println("Connection to server sucessfully");//jedis.auth("abc");//授权密码,没有设置可以注释掉jedis.set("foo""bar"value = jedis.get("foo");System.out.println("value:"+value);
Detailed configuration file

Directly execute Redis-server can start the Redis service, the default listening port is 6379, and then the client can connect to the server, to perform operations.
Redis provides a redis.conf configuration file that contains examples and functional descriptions for each of the Redis parameters:

    • Daemonize: Default value No, which is used to customize whether the Redis service is running in daemon mode.
    • Pidfile: Default value/var/run/redis.pid, specifies the Redis service's process number file path, which needs to be configured when running in daemon mode;
    • Port: Default value 6379, specifies the port of the Redis service; bind: Bind IP, default is all network device of the machine;
    • Timeout: The client disconnects after n seconds; loglevel: Sets the log level for the server, with the following options:
    • Debug: Record details for development or debugging; verbose: Provide a lot of useful information, but not as detailed as debug, the default is this option;
    • Notice: Moderate reminders, more for the product environment; Warning: Displays only important warning messages;
    • LogFile: Specifies the output path of the log, the default value stdout, the output to the screen, the daemon mode output to/dev/null, if you want to output the log to Syslog, you can start syslog-enabled Yes, the default option value is No.
    • Databases: Specifies the number of databases, which defaults to 16, and the database used by default is DB 0.

The following are snapshot-related settings:

    • Save: Specifies how long to refresh the snapshot to disk, this option has two attribute values and only fires when two attribute values are met; You can set multiple levels, such as the default parameter file:
    • Save 900 1: Every 900 seconds (15 minutes) at least once the key value change is triggered;
    • Save 300 10: Every 300 seconds (5 minutes) at least 10 times the key value change is triggered;
    • Save 60 10000: Every 60 seconds at least 10,000 times the key value change is triggered;
    • Rdbcompression: The default is yes, when the dump database using LZF compressed string object, if the CPU resource is more tense, can be set to No, choose not to compress;
    • Dbfilename: The default value dump.rdb,dump to the file name in the filesystem;
    • Dir: Default value./, that is, the current directory, dump out of the data file storage path;

The following are replication-related settings , which are not enabled by default, so the following table parameters are commented on in the default parameter file:

# slaveof: Specify the primary IP and port, Used to create a mirroring service;  # Masterauth: If Master is configured with a password, the settings are also required here;  Slave-serve-stale-data: The default value is yes. When slave loses a connection to the master side, or if replication is still in process, then slave will have the following two performance: when this parameter value is yes, slave continues to respond to client requests, although the data is not synchronized or even has no data (appearing in the case of initial synchronization), when the value of this parameter is no, Slave will return error messages for  "SYNC with Master in Progreee" ; # Repl-ping-slave-period: The default value of 10, specifies the period of slave to ping Master periodically;  # repl-timeout: Default 60 , specifying the time-out. Note This parameter includes the time to bulk transfer data and ping the response.  the following security-related settings # requirepass: Specify a password that the client needs to connect via a password to successfully connect;  # rename-command: Redefine the command, such as renaming the Config command to a very complex name:  Rename-command  CONFIG  B840fc02d524045429941cc15f59e41cb7be6c52;rename-command CONFIG  : Cancel this command;  

The following are settings for resource throttling:

# maxclients: Specifies the maximum number of concurrent connections for a client, by default there is no limit until Redis is unable to create a new process, and setting the parameter value to 0 also means no limit, and if the parameter specifies a value, Redis closes all new connections when the concurrent connection reaches the specified value and returns ' Max number of clients reached ' error message;# maxmemory: Set Redis maximum available memory. When the maximum memory is reached, Redis attempts to delete the key values according to the set recycle policy. If the key value cannot be deleted, or if the retention policy is set to not clear, Redis returns an error message to the memory-issuing request. This parameter is useful when Redis is cached as a first-level LRU. # Maxmemory-policy: Default value Volatile-lru, specifying the Purge policy, is available in the following ways:Remove Volatile-lru theKey withAn expireSetUsing an LRU ALGORITHMALLKEYS-LRU, remove any key accordingly to  theLRU algorithmvolatile-random, remove a random key withAn expireSetAllkeys->random, remove a random key, any keyvolatile-ttl, remove theKey with  theNearest expire Time(minor TTL) noeviction, don ' t expire atAll, justreturnAnError  on WriteOperations# Maxmemory-samples: The default value of 3,LRU and the minimum TTL policy is not a rigorous strategy, but an approximate estimate, so you can choose to sample the values for inspection. 

The following is the setting for Append only mode , where Redis asynchronously dumps data to disk by default, which in extreme cases may result in the loss of some data (such as a sudden server outage), if the data is important and does not want to be lost, you can enable direct write mode, In this mode, Redis synchronizes all the received writes to the Appendonly.aof file, which rebuilds all the data in memory when the Redis service starts. Note that this pattern has a very large impact on performance.

appendonly:默认值no,指定是否启用直写模式;# appendfilename:直写模式的默认文件名appendonly.aof;appendfsync:调用fsync()方式让操作系统写数据到磁盘上,数据同步方式,有下列几种模式:always:每次都调用,比如安全,但速度最慢;everysec:每秒同步,这也是默认方式;no:不调用fsync,由操作系统决定何时同步,比如快的模式;no-appendfsync-on-rewrite:默认值no。当AOF fsync策略设置为always或everysec,后台保存进程会执行大量的I/O操作。某些linux配置下redis可能会阻塞过多的fsync()调用。auto-aof-rewrite-percentage:默认值100auto-aof-rewrite-min-size:默认值64mb

The following is a slow log-related setting that records queries that have an execution time exceeding the threshold. The execution time does not include the time taken by I/O operations or sending data to the client, but rather the time it takes to actually execute the command (that is, when the thread blocks cannot accept other requests):

    • Slowlog-log-slower-than: Default value of 10000, in microseconds, defined as slow execution of the threshold;
    • Slowlog-max-len: The default value is 1024, the maximum data for the slow log. Note that this consumes the content resource, and if you want to empty it you can execute the slowlog reset command;

The following are virtual memory-related settings, virtual within the 2.4 version of the obsolete, it is not mentioned herevm-enabled no vm-swap-file /tmp/redis.swap vm-max-memory 0 vm-page-size 32 vm-pages 134217728 vm-max-threads 4

The following are the settings related to Advanced configuration:

    • Hash-max-zipmap-entries: The default value of 512, when the number of elements of a map reaches the maximum, but the maximum element length does not reach the set threshold, its hash encoding takes a special way (more efficient use of memory). This parameter is used in combination with the following parameters to set these two thresholds. Set the number of elements;
    • Hash-max-zipmap-value: The default value of 64, sets the maximum length of the value of the element in the map;
    • List-max-ziplist-entries: Default value 512, similar to hash, the list array that satisfies the condition also takes a special way to save space.
    • List-max-ziplist-value: Default Value 64
    • Set-max-intset-entries: The default value of 512, when the data in a set type is a numeric type, and the number of integral elements in set does not exceed the specified value, a special encoding is used.
    • Zset-max-ziplist-entries: Default value of 128, similar to hash and list.
    • Zset-max-ziplist-value: Default Value 64
    • Activerehashing: The default is yes to control whether the hash is automatically rebuilt. Active rehashing uses 1 microseconds of CPU time per 100 microseconds to reorganize the Redis hash table. Rebuilding is a lazy way, the more you write a hash table, the more steps you need to perform rehashing, and the rehashing operation will execute if the server is currently idle. If the requirement for real-time is high and it is difficult to accept the 2 microsecond latency that Redis occasionally has, you can set activerehashing to No, otherwise the recommended setting is yes to save memory space.

Create a conf file (Of course, you can also directly use the Redis redis.conf) and set the parameters according to the actual situation, and then start the Redis service, specify the configuration file, for example:

# more Redis.confdaemonizeYesPidfile/ data/software/redis/redis.pidPort 6379logfile/ data/software/redis/redis.logdatabases  -Save  the 1Save  - TenSave  - 10000rdbcompressionYesDbfilenameDump.rdbdir/ data/software/redis/# redis-server/data/software/redis/redis.conf
Reference documents
    1. Redis Official Documentation Http://redis.io/documentation
    2. Sun Island Main Blog http://blog.csdn.net/ithomer/article/details/9213185
    3. Royce Wang Blog http://blog.csdn.net/wf1982/article/details/7209083
    4. Runoob Tutorial Http://www.runoob.com/redis/redis-tutorial.html
    5. Baidu Library Introduction Detailed http://wenku.baidu.com/view/bc9f266448d7c1c708a145fb.html

Getting Started with 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.