Learning from the NoSQL Redis

Source: Internet
Author: User
Tags configuration settings redis server

/** * Here is my virtual machine-related startup command
    • /usr/local/redis/bin/redis-server/usr/local/redis/etc/redis.conf
    • /usr/local/redis/bin/redis-cli-a Redis
    • Vi/usr/local/redis/etc/redis.conf
* * Front, say I am writing Java code integration Redis encountered problems, has been prompted connection Refused:connect, later view redis-conf file, see there is a protected-mode, originally yes, changed to No,  Restart the service to do so. Redis installation (The following are the operations on the CentOS version) download good redis-x.x.tar.gz
Tar xzf redis-3.2.0.tar.gzcd Redis-3.2.0make
Make Gcc,yum install GCC before installing
Start
Src/redis-server
Open Client
Src/redis-cli
Test
redis> set foo barokredis> get foo "bar"
Specify the configuration file to start (I moved the profile to/usr/local/redis/etc/redis.conf)
Command:./redis-server/usr/local/redis/etc/redis.conf
(My Native Boot command/usr/local/redis/bin/redis-server/usr/local/redis/etc/redis.conf) to see if the service started: Ps-ef | grep REDIS,NETSTAT-TUNPL | grep 6379
Open Client:/USR/LOCAL/REDIS/BIN/REDIS-CLI
To let it run in the background, do not occupy the session, change the daemonize in the configuration file to Yes
--Methods See official Website documentation (notes that do a little common method)
String data type:
Setnx:nx represents not exit, if key already exists returns 0, no return 1 exists. Setnx name XGW
Setex: Specify a valid time for the key value set name 123   valid time is 10 seconds  
.............
Hashes type: more suitable for storing objects than for each field stored as a string type, storing an object in a hash type consumes less memory and makes it easier to store the entire object.
Hset User name XGW    returns 0 indicates a failure, and 1 indicates success
Hget User Name
.............
List type: Key is List
Lpush list Hello returns list length
Lpush List World
Lrange List 0-1 shows all the contents of the list
.............
Set type: It is an unordered collection of type string and set is implemented by hash table
Sadd MySet Hello  returns myset length, returns 0 when adding the same element
Smembers MySet Show all contents in set
Sorted sets
    Zadd Myset3 1 A
    (integer) 1
    127.0.0.1:6379> Zadd Myset3 2 b
    (integer) 1
    127.0.0.1:6379> Zadd Myset3 3 b
    (integer) 0
    127.0.0.1:6379> zrange Myset3 0-1 withscores
.............
The simplest data type of string
Hash data type, can be used as table, hash table, faster than string speed
List data type stack, queue
Set data type and set, intersection, difference set
Zset version of data type set, ordered collection
===================================
Keys my*    , returns all keys that satisfy the given pattern
Exists key  to confirm if key exists
Expire key  , set the expiration time of key (see how many seconds the key has expired, TTL)
Persist key, removing expiration time
Del key     , delete a key  
Move key 1 ( default has 0-15 database , default into 0 database)
Type key    , view key types (String,zset (ordered collection), List,...)
Dbsize      , current database key size
Info        , viewing redis server information
Config get  , related configuration parameters
Flushdb     , clear the key under the current database
Flushall    , clears all keys from all databases
Security:
Under redis-conf configuration requirepass password, when using the client connection, with the parameter-a password connection, otherwise you will be prompted not to have permission
Master-slave replication:
    • A master can have multiple slave
    • Multiple slave can connect to the same master, can also connect to other slave (slave can Connect) (heartbeat, after master down, a slave immediately become master)
    • Master-slave replication does not block primary, and master can continue to process client requests while synchronizing data
    • Provides scalability of the system
Master-slave replication process:
    1. Slave connect with master, send sync Sync command
    2. Master initiates a background process, saves the database snapshot to a file, and the master master process starts collecting new write commands and caches
    3. After the background has finished saving, send this file to slave
    4. Slave Save this file to your hard disk
Configuring the Master-Slave server: Configuring the slave server is simple, simply add the following configuration to the slave configuration file: slaveof 192.168.111.138 6379 #<-Specify the IP and port of master Masterauth Redis #< -This is the host's password to judge the master and slave through info to observe: Role:slave,master_link_status:up here encountered the problem: master_link_status:up, while my slave shows down, modify the Protected-mode in redis-conf to no . Transaction Management:Multi Open transaction Exec executes the command in the queue discard clears the command queue for the transaction and exits the transaction context. = Transaction rollback optimistic lock complex transaction control optimistic locks are implemented based on the record mechanism of the version of the data. The watch command monitors the given key, and when exec, if the monitored key has changed from watch, the entire transaction fails, and the industry can call watch to monitor multiple keys more than once, so that the specified key can be optimistic. Watch's key is valid for the entire connection, and the same is true for the transaction. If you understand the disconnection, both the monitoring and the transaction are automatically purged. Scene (watch key in Session1, then multi, then set key in Session2, go back to Session1 to set key, Exec, execute failed) (Redis, if there is a failure in the queue, But the success of the execution is still not rolled back, which is very different from the relational database that was previously contacted. Persistence mechanismRedis is an in-memory database that supports persistence, which means that Redis needs to synchronize memory data to the hard disk to ensure that persistent Redis supports two persistence modes:
    1. Snapshotting (snapshot), which is also the default (writes data to the binary file, the default file name Dump.rdb), can be configured to automatically do snapshot persistence by configuration settings (Redis in n seconds if more than M key is modified to automatically take a snapshot, Save 1 is currently configured by default in redis.conf save 60 10000)
    2. Append-only file (abbreviated AOF) mode (writes, changes, etc. are written to files, Redis appends each received write command to the file via the Write function, and when Redis restarts, it rebuilds the contents of the database in memory by re-executing the Write command saved in the file. The OS caches write modifications in the kernel, so it may not be written to disk immediately. This aof method may also lose some of the modifications, which can be told by the configuration file to Redis by Fsync function to force the OS to write to disk. AppendOnly to Yes
    • # Appendfsync always//command is written to disk with the worst performance but guaranteed full persistence
    • Appendfsync everysec
    • # Appendfsync no//depends on OS for best performance, but does not guarantee persistent integrity
Publish and subscribe to messagesA publish Subscription (PUB/SUB) is a message communication pattern that is primarily intended to be a coupling between the underlying message publisher and the message Subscriber. As a pub/sub server, Redis serves as a message routing feature between subscribers and publishers. Subscribers can subscribe to Redis server by using the Subscribe and Psubscribe commands for message types, and Redis makes the information type channel. 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. use of virtual memory

Swap temporarily infrequently accessed data in memory to disk, freeing up valuable memory for other data that needs to be accessed

Learning from the NoSQL 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.