redis-depth study (three) ----- affairs, master-slave replication, jedis

reids Affairs

concept

A plurality of commands can be executed, is essentially a collection of commands. All commands are a sequence of transaction, sequentially serializing execution command can not be inserted into the other, allowed stopper

effect

A queue, a disposable, sequential, exclusive execute a series of commands

Common Commands

Normal operation of its services:

 

Abandon the transaction:

 

 In fact, the transaction is part of the support for redis:

 

 Such as incr k1 though is wrong (like java runtime exception), but several other results can still operate successfully.

watch monitor

Pessimistic locking / optimistic locking / CAS (Check And Set) 

1, pessimistic locking

Pessimistic locking (Pessimistic Lock), by definition, is very pessimistic, pick up data every time think others will modify, so every time she took the data will be locked, so people want to take this data will block until it takes to lock. Prior to traditional relational database inside to use a lot of this locking mechanism, such as row locks, table locks, etc., read lock, write lock, you are doing the first lock operation

2, optimistic locking

Optimistic locking (Optimistic Lock), by definition, is very optimistic, pick up data every time that others are not modified, so it will not be locked, but when the update will determine what others during this time did not go to update this data, you can use the mechanisms of the version number. Optimistic locking is suitable for the types of applications to read, so you can improve throughput.

Optimistic locking strategy: Submit version must be greater than the current record version in order to perform the update

3, CAS

CAS mechanism which uses three basic operand: memory address V, the expected value of the old A, to modify the new value of B.

Updating a variable, only when the actual value of the expected value of the variable A and V which memory addresses are the same, only the memory address corresponding to modify the value V B.

4, watch Summary

a, Watch command, similar optimistic locking, the transaction commits, if the value of the Key has been changed other end customers, such a list has been another client push / pop too, and the whole transaction queue will not be executed

b, by monitoring more than Keys WATCH command before executing a transaction, if there is any value in Key has changed after WATCH, EXEC command to execute the transaction will be abandoned, while the return Nullmulti-bulk reply to inform the caller transaction execution failure

Stage 3

ON: to start a transaction MULTI

Into the team: the team into multiple commands into transactions, received these orders will not be executed immediately, but placed in the queue waiting to be executed inside a transaction

Execute: EXEC command triggered by the transaction

3 Features

Separate isolated Operation: All commands are serialized transaction performed sequentially. During execution of a transaction, the command will not be sent to other client requests interrupted.

No concept of isolation levels: will not be executed before the actual command queue did not submit, because before the transaction commits any instruction will not be actually executed, the query does not exist in the "things to be seen in a transaction update in foreign affairs inquiry can not see "the people are extremely headache

It does not guarantee atomicity: redis same transaction if there is a command fails, the subsequent commands will still be executed, there is no rollback

Redis publish-subscribe 

concept

A messaging mode of communication between processes: a sender (Pub) sends a message, the subscriber (Sub) receives the message.

 

 

 

 

 

command

 

 

----- master copy from the Redis replication (Master / Slave)

concept

Jargon: what we call the master-slave replication, host data is updated according to configuration and policy, automatically synchronized to the standby machine master / slaver mechanisms, Master in order to write the main, Slave read-mostly

1, a main two servants ----- generals died, waiting for soldiers to send new superior general

 

 2. Passing the torch

A Slave can be a slave of the Master, Slave can also receive other slaves connection and synchronization request, then the slave as a master in the next chain, can effectively reduce the writing pressure of the master.

Turn midway change: Data will be cleared before re-establish copies to date.

slaveof new main library IP port the new primary database

 

 3, become masters

Database synchronization so that the current is stopped, to turn the master database with other databases. ----- General dead, soldiers replace him

SLAVEOF no one

effect

1, separate read and write

2, disaster recovery

Configuration

1, with the (library) unworthy master (library)

2, the configuration from the library: slaveof main library IP host port library

And after each master disconnect, we need to reconnect, unless you enter redis.conf configuration file. redis info replication can view the current service information, master or slave

 

 

Representatives master is 192.168.101.3, port 6379

Replication Works

1, after the slave server starts successfully connected to the master sends a sync command

2, Master ordered to start the process of archiving the background while collecting all data received to modify the set command, after the background process is finished, master transfers the entire data file to the slave, to complete a full synchronization

3, the entire amount of copy: The slave database service after receiving the data file, save it and load it into memory.

4, incremental replication: Master will continue to collect all the new changes in order to pass command slave, synchronization is complete

5, but as long as the connection is re-master, a full synchronization (total amount of replication) will be performed automatically

Sentinel mode

concept

To become masters of the automatic version, the background can monitor whether the host fault, if fault according to the number of votes from the library automatically converted to a master database. A group of sentinel can simultaneously monitor multiple Master

Configuration

New sentinel.conf redis files in the directory name must not be wrong

 sentinel monitor monitored database name (from their own name) 127.0.0.1 6379 1

The last number above 1 indicates the host let hang salve vote to see who takes over as host, after the number of votes becomes the master

start up

 

Summary: The general is dead, soldiers vote, the votes of elected qualified generals; the generals at this time revived, and came back to become a brother. 

Shortcoming

Since all write operations are the first to operate on the Master, and then synchronize updates to the Slave, so there is a delay from Master to Slave synchronous machine, when the system is busy, delay problem will become more severe, increasing the number of Slave machine also make this problem worse.

jedis

Required jar package

commons-pool-1.6.jar、jedis-2.1.0.jar

Test connectivity

public  class  Demo01 {
     public  static  void  main (String [] args) {
         // connect local service Redis 
        Jedis jedis =  new new  Jedis ( "127.0.0.1", 6379 );
         // check the service is running, shot pong represents the OK 
        the System. Out.println ( "the OK Connection IS ==========>:" + jedis.ping ()); 
    } 
}

Common api

package com.atguigu.redis.test;
import java.util.*;
import redis.clients.jedis.Jedis;

public class Test02 {
    public static void main(String[] args) {
        Jedis jedis new Jedis("127.0.0.1",6379);
        
        //key
        Set<String> keys = jedis.keys("*");
        for(Iterator iterator = keys.iterator(); iterator.hasNext();) {
            String key = (String) iterator.next();
            System.out.println(key);
         }
         System.out.println("jedis.exists====>"+jedis.exists("k2"));
         System.out.println(jedis.ttl("k1"));

         //String
         //jedis.append("k1","myreids");
         System.out.println(jedis.get("k1"));
         jedis.set("k4","k4_redis");
         System.out.println("----------------------------------------");
         jedis.mset("str1","v1","str2","v2","str3","v3");
         System.out.println(jedis.mget("str1","str2","str3"));
     
        //list
         System.out.println("----------------------------------------");
         //jedis.lpush("mylist","v1","v2","v3","v4","v5");
         List<String> list = jedis.lrange("mylist",0,-1);
         for(String element : list) {
            System.out.println(element);
        }
     
        //set
         jedis.sadd("orders","jd001");
         jedis.sadd("orders","jd002");
         jedis.sadd("orders","jd003");
         Set<String> set1 = jedis.smembers("orders");
         for(Iterator iterator = set1.iterator(); iterator.hasNext();) {
            String string = (String) iterator.next();
            System.out.println(string);
        }
         jedis.srem("orders","jd002");
         System.out.println(jedis.smembers("orders").size());
         
        //hash
         jedis.hset("hash1","userName","lisi");
         System.out.println(jedis.hget("hash1","userName"));
         Map<String,String> map = new HashMap<String,String>();
         map.put("telphone","13811814763");
         map.put("address","atguigu");
         map.put("email","[email protected]");
         jedis.hmset("hash2",map);
         List<String> result = jedis.hmget("hash2", "telphone","email");
         for(String element : result) {
            System.out.println(element);
         }
     
        //zset
         jedis.zadd("zset01",60d,"v1");
         jedis.zadd("zset01",70d,"v2");
         jedis.zadd("zset01",80d,"v3");
         jedis.zadd("zset01",90d,"v4");
     
         Set<String> s1 = jedis.zrange("zset01",0,-1);
         for(Iterator iterator = s1.iterator(); iterator.hasNext();) {
            String string = (String) iterator.next();
            System.out.println(string);
        }     
    }
}

Transaction commits

 

Master-slave replication 

public static void main(String[] args) throws InterruptedException 
  {
     Jedis jedis_M new Jedis("127.0.0.1",6379);
     Jedis jedis_S new Jedis("127.0.0.1",6380);
     
     jedis_S.slaveof("127.0.0.1",6379);
     
     jedis_M.set("k6","v6");
     Thread.sleep(500);
     System.out.println(jedis_S.get("k6"));
  }

jedispool

Package  com.atguigu.redis.test; 


Import  redis.clients.jedis.Jedis;
 Import  redis.clients.jedis.JedisPool;
 Import  redis.clients.jedis.JedisPoolConfig; 


public  class  JedisPoolUtil { 
  
 Private  static  volatile  JedisPool jedisPool =  null ; // modified volatile variable will not be thread-local cache, read and write the variables are directly manipulated shared memory. 
Private  JedisPoolUtil () {} public static  JedisPool getJedisPoolInstance () 
 { IF ( null  ==  jedisPool) 
    { the synchronized  (JedisPoolUtil. class )  
  
  
   
     
       
      {
          if(null == jedisPool)
         {
           JedisPoolConfig poolConfig new JedisPoolConfig();
           poolConfig.setMaxActive(1000);
           poolConfig.setMaxIdle(32);
           poolConfig.setMaxWait(100*1000);
           poolConfig.setTestOnBorrow(true);
            
            jedisPool new JedisPool(poolConfig,"127.0.0.1");
         }
      }
    }
     return jedisPool;
 }
  
  public static void release(JedisPool jedisPool,Jedis jedis)
 {
     if(null != jedis)
    {
      jedisPool.returnResourceObject(jedis);
    }
 }
}
 
package com.atguigu.redis.test;


import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;


public class Test01 {
  public static void main(String[] args) {
     JedisPool jedisPool = JedisPoolUtil.getJedisPoolInstance();
     Jedis jedis null;
     
     try 
     {
       jedis = jedisPool.getResource();
       jedis.set("k18","v183");
       
     } catch (Exception e) {
       e.printStackTrace();
     }finally{
       JedisPoolUtil.release(jedisPool, jedis);
     }
  }
}
 

Configuration Summary:

JedisPool configuration parameters mostly by a corresponding entry to JedisPoolConfig assignment.

for maxActive : controlling a number of pool allocation jedis examples, be obtained by pool.getResource (); if assigned -1 indicates not limited; for maxActive jedis example if one has been assigned a pool, the pool state at this time is exhausted .
maxIdle : controlling a number of pool up to IDLE state jedis instance (idle); a
whenExhaustedAction : when the operation shown examples jedis pool are allocated in the finished, to be taken pool; three default.
   WHEN_EXHAUSTED_FAIL -> jedis said in no instance, direct throw NoSuchElementException;
   WHEN_EXHAUSTED_BLOCK -> said blocking live, or throw JedisConnectionException reached maxWait;
   WHEN_EXHAUSTED_GROW -> indicates a new jedis instance, it said maxActive set useless ;
maxWait : indicates when borrow a jedis example, the maximum waiting time, if the wait time is exceeded, directly cast JedisConnectionException;
testOnBorrow : obtaining a jedis instance when checking whether connection availability (of ping ()); if true, is obtained examples of jedis are available;

testOnReturn: When a return to the example jedis pool, to check whether the connection availability (of ping ());

testWhileIdle: if true, indicates that there is a thread idle object evitor idle object is scanned, if validate fails, this object will be drop off in the pool; this one makes sense only when timeBetweenEvictionRunsMillis greater than 0;

timeBetweenEvictionRunsMillis: represents the number of milliseconds between idle object to evitor scans of sleep;

numTestsPerEvictionRun : represents the maximum number of objects idle object evitor each scan;

minEvictableIdleTimeMillis : represents an object at least stay in the shortest possible time idle state, can be scanned and then deported idle object evitor; this one only timeBetweenEvictionRunsMillis greater than 0 is meaningful;

softMinEvictableIdleTimeMillis : In minEvictableIdleTimeMillis basis, adding at least minIdle objects already in the pool inside. If -1, evicted will not expel any objects based on idle time. If minEvictableIdleTimeMillis> 0, then this setting is meaningless, and only makes sense if timeBetweenEvictionRunsMillis greater than 0;

LIFO : When borrowObject return the object, is the use of DEFAULT_LIFO (last in first out, i.e., similar to queue cache of the most frequently used), if the False, the FIFO queue represents;

================================================== ================================================== ==============
wherein JedisPoolConfig default setting for some parameters are as follows:
testWhileIdle to true =
minEvictableIdleTimeMills = 60000
timeBetweenEvictionRunsMillis = 30000
numTestsPerEvictionRun = -1

Guess you like

Origin www.cnblogs.com/alimayun/p/11575372.html