Redis common commands and advanced features

Source: Internet
Author: User
Tags configuration settings redis server

11.redis Common Commands

Keys * Returns all keys

Keys my* Fuzzy Matching

Exists key to confirm if key exists

Del key

Expire key time expires in [seconds] for an existing key setting

TTL key to view expiration time, 1 means expired

Move moves the key in the current database to a different database

Select database_name Selecting a Database

Move key database_name

Persist key cancels the expiration time, when the TTL key returns-1 does not mean the expiration

Randomkey randomly returns a key

Rename Rename key

Rename Set2 set20

Type key returns the types of key

Ping Test connection is OK

Echo Prints some content on the command line

Select database selects databases with Redis database numbers from 0 to 15

Quit/exit/ctrl+c exit

Dbsize returns the current number of databases

Info get server information and statistics

Config get key real-time upload of received requests

Config Get *

FLUSHDB Delete all keys in the currently selected database

Flushall Delete all keys in all databases


12.redis Advanced Applications

12.1 Security

Set Password:

Vi/usr/local/redis/etc/redis.conf

Requirepass Password

Connect after restarting the service

/usr/local/redis/bin/redis-cli

Auth Password

or/usr/local/redis/bin/redis-cli-a password.

12.2 Master-slave replication

Master can have multiple slave

Multiple slave can be connected to the same master, and can be connected to other slave

Master-slave replication does not block primary, and master can continue to process client requests while synchronizing data

Improve the scalability of the system

Redis Master-slave replication process:

slave Connect with master, send sync Sync command

Master initiates a background process, saves the database snapshot to a file, and the master master process starts collecting new write commands and caches

Slave Save this file to your hard disk

Configure the master-slave server:

Configuring the Slave server is simple, just add the following configuration to the slave configuration file:

slaveof 192.168.1.1 6379# Specifies the IP and port of master

masterauth Password #这是主机的密码

info to view master and slave machine information

12.3 Handling of Things

Redis 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, and when a client issues a multi command in a connection, this

The connection goes into a transaction context, and the subsequent command of the connection is not executed immediately, but is placed first in a queue, and when the EXEC command is executed, Redis sequentially executes all the commands in the queue

Multi can open transaction context

exec commands in the execution of a transaction

Discard canceling a transaction

Optimistic Locks: most are implemented based on the record mechanism of the version of the data, which is to add a version identity to the data

, in a version solution based on a database table, it is generally possible to read data by adding a version field to the database table.

Read this version number together, and then update the version number by 1. At this point, the version number of the submitted data is compared to the current version number of the corresponding record in the datasheet

If the submitted data version number is greater than the current version number of the database, it is updated, otherwise it is considered to be outdated data

Redis optimistic Lock

Get age

Watch age

Multi

The watch command monitors the given key, and the entire transaction fails if the monitored key has changed since the call to watch.

You can also call watch to monitor multiple keys several times, so you can be optimistic about the specified key, noting that watch's key is the entire

The connection is valid and the transaction is the same. If disconnected, monitoring and transactions are automatically cleared, of course the Exec,discard,unwatch command will

Clears all monitoring in the connection.

12.4 persistence mechanism

Redis is an in-memory database that supports persistence, which means that Redis often needs to synchronize in-memory data to the hard disk to ensure persistence

1.snapshotting (snapshot) is also the default mode

How to 2.append-only file (abbreviated AOF)

snapshots are the default persistence mode. In this way, the in-memory data is written to the binary file in a snapshot, and the default file name is

Dump.rdb. You can automatically do snapshot persistence through configuration settings, and we can configure Redis to be modified within n seconds if more than M key

automatically write snapshots

Save 900 1#900 seconds if more than one key is modified, snapshot save is initiated

Save 300 10#300 seconds if more than 10 keys are modified, snapshot save is initiated

Save 60 10000

Because the snapshot is done at a certain interval, if Redis is accidentally down, all changes after the last snapshot are lost

aof is more durable than snapshot, because Redis appends none of the received write commands to a file by using the Write function when AOF is used.

When Redis restarts, the contents of the entire database are rebuilt in memory by re-executing the write commands saved in the file.

of course, because the OS caches write modifications in the kernel, it may not be immediately written to the hard disk, so the persistence of the AoF method is likely to lose some of the modifications.

You can tell Redis through the configuration file that we want to force the OS to write to disk via the Fsync function

appendonly Yes #启用aof持久化方式

#appendfsync always #收到写命令就立即写入磁盘, slowest, but guaranteed full persistence

Appendfsync everysec #每秒钟写入磁盘一次, a good compromise in performance and persistence

#appedfsync No #完全依赖os, best performance, no guarantee of durability

12.5 Publish a subscription message

A publish Subscription (PUB/SUB) is a message communication pattern that is primarily designed to decouple the coupling between a message publisher and a message subscriber, and Redis acts as a pub/sub

Server, which provides the ability to route messages between subscribers and publishers, and subscribers can pass subscribe and

The Psubscribe command subscribes to Redis server for the type of message you are interested in, and Redis calls the information type a channel

(Cahnnel). When a publisher sends a specific type of information to Redis server through the Publish command, all clients subscribing to that type of information receive this message

Subscribe TV1 Monitoring

Subscribe TV1 TV2

publish TV1 zsjbroadcast Broadcast

12.6 Use of virtual memory

Redis's virtual memory is not the same as the operating system's virtual memory, but the idea and purpose are the same

is to temporarily swap infrequently accessed data from memory to disk, freeing up valuable memory space

For other data that needs to be accessed, especially in memory databases such as Redis, memory is not always sufficient, except that data can be split across multiple Redis servers

Another way to improve database capacity is to use virtual memory to swap infrequently accessed data to disk

vm-enabled yes# turn on VM functionality

Vm-swap-file/tmp/redis.swap #交换出来的value保存的文件路径

Vm-max-memory 1000000 #redis使用的最大内存上限

Vm-page-size #每个页面的大小32字节

Vm-pages 1344217728 #最多使用多少页面

Vm-max-threads 4# The number of worker threads used to perform a value object swap-in cache


This article is from the "Inverse Day" blog, please be sure to keep this source http://xxzjzsj.blog.51cto.com/3052058/1629164

Redis common commands and advanced features

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.