Redis Distributed Solution-coids

Source: Internet
Author: User

CODIS is a distributed redis solution. For upper-layer applications, there is no obvious difference between connecting to codisproxy and connecting to the native redis server (list of unsupported commands ), upper-layer applications can be used like single-host redis. CODIS processes request forwarding and data migration without stopping services, the client is transparent, and you can simply think that the backend connection is a redis service with infinite memory.

 

CODIS consists of four parts:

  • CODIS proxy (CODIS-proxy)The redis protocol is stateless, so many nodes can be deployed.
  • CODIS Manager (CODIS-config)It is a CODIS management tool, including adding/deleting redis nodes, adding/deleting proxy nodes, initiating data migration, and other operations. It comes with httpserver and supports management configuration in the management background.
  • CODIS redis (CODIS-server)It is a redis Branch maintained by CODIS. It is based on the 2.8.21 branch and has added slot support and atomic data migration commands. CODIS-proxy and CODIS-config can run normally only when they interact with redis of this version.
  • ZookeeperUsed to store CODIS cluster metadata and maintain CODIS cluster nodes.
Advantages and disadvantages of CODIS

-Advantages

? Transparent to the client. The interaction with CODIS is the same as that with redis itself.

? Supports online data migration and the migration process is transparent to clients.

? Simple management and monitoring interfaces

? Supports high availability, whether it is redis data storage or proxy nodes

? Automatic balanced data allocation

? Supports up to 1024 redis instances with massive storage capacity

? High Performance

-Disadvantages
? If you use your own redis branch, cannot it be the same as the original redis?

? If there is only one CODIS proxy, redis's performance will be reduced by about 20%.

? Some commands are not supported, such as transaction command Muti

? Domestic open-source products with relatively weak activity

CODIS Architecture

CODIS performance (proxy + two redis nodes



First, install the go Environment

wget http://storage.googleapis.com/golang/go1.4.1.linux-amd64.tar.gz

Extract

tar -zxvf go1.4.1.linux-amd64.tar.gz 

Download zookeeper

wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz

Extract

vi ~/.bash_profile

Configure the go environment variables and ZK home

vi ~/.bash_profile


PATH=$PATH:$HOME/binexport PATHJAVA_HOME=/java/jdk1.7.0_76PATH=$JAVA_HOME/bin:$PATHCLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jarexport JAVA_HOMEexport CLASSPATHZOOKEEPER_HOME=/java/zookeeper-3.4.6GOROOT=/java/goexport GOPATH=/java/codisPATH=$PATH:$GOROOT/bin:$GOPATH/bin:$ZOOKEEPER_HOME/binexport PATH

Make environment variables take effect

source ~/.bash_profile

Create the CODIS directory

mkdir codis

Download CODIS code

go get -u -d github.com/CodisLabs/codis

Go to coids and execute make compilation code.



After compilation, three executable files are generated under the bin directory.


Modify the config. ini file

vi config.ini



Go to the zk directory and start zk.

cd /java/zookeeper-3.4.6/bin./zkServer.sh start

Run the JPS command to check whether ZK is started.


CODIS Startup Procedure

./codis-config -c ../config.ini dashboard

You can also add & to indicate that the background is started.


You can view the data in zk.


Then initialize Slots

./codis-config -c ../config.ini slot init

The slot information is stored in zk.



You can start a CODIS-Server

Copy A redis configuration file and start



./codis-server  ./redis.6379.conf

 

 

Then add a group server

Each ServiceGroup serves as a redis server group. Only one master is allowed and multiple slave instances are allowed. The Group ID can only be an integer greater than 1.





Added


Allocate slot


Start CODIS-proxy

./codis-proxy -c ../config.ini -L ./proxy.log --cpu=1 --addr=192.168.247.103:19000 --http-addr=192.168.247.103:11000

? ADDR is the IP address and port bound to the proxy.

? -- The CPU is the number of CPU cores used by the proxy, which is related to the virtual machine configuration. It is generally the same as the number of cores.

? HTTP-ADDR address used for testing
? -L log file of the specified agent


When the instance is started, the default value is offline. When the instance is in this status, it cannot provide external services.

Change proxy status to online

Access Port 11000


View the proxy ID and modify the CODIS proxy status.

./codis-config -c ../config.ini proxy online proxy_1


You can also view it on the dashboard


Connect to proxy

./redis-cli -p 19000 -h 192.168.247.103


Connect to CODIS through a Java client

Pom. xml

<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.8.0</version><type>jar</type><scope>compile</scope></dependency><dependency><groupId>io.codis.jodis</groupId><artifactId>jodis</artifactId><version>0.3.1</version></dependency><dependency><groupId>com.wandoulabs.jodis</groupId><artifactId>jodis</artifactId><version>0.2.2</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><type>jar</type><scope>test</scope></dependency>


Java code:

JedisResourcePool jedisPool = RoundRobinJedisPool.create()        .curatorClient("192.168.247.103:2181", 30000).zkProxyDir("/zk/codis/db_test/proxy").build();try (Jedis jedis = jedisPool.getResource()) {    jedis.set("foo", "bar");    String value = jedis.get("foo");    System.out.println(value);}

Delete the key you just set


Run the program to view the results


CODIS high-availability CODIS-ha

CODIS-HA must be separately installed with CODIS-ha

go get github.com/ngaut/codis-ha

Go to the CODIS-ha directory

Run the build command

go build


Start CODIS-ha

nohup ./codis-ha --codis-config=192.168.247.103:18087 --productName=test

-CODIS-config: Specifies the configuration service address.
-Productname: Product Name
-Nohup: redirects log output to the nohup file.

 

Two CODIS are started in total.


Kill one of the CODIS

kill -9 16914

Nohup. Out output log


Refresh the dashboard page


The status of the original master is changed to offine, And the slave is upgraded to master.

Then restart 6379


Log Information


The original 6379 is changed to slave.


Although it can be highly available, it can be automatically switched, but in actual application scenarios, you should pay attention to the application scenarios.

Because redis's master-slave replication is asynchronous, there is no way to ensure data integrity in asynchronous mode, and there is no final guarantee of data.

If the concurrency is high, the number of operations per second is high, and the execution is fast. If the master node is down, the slave may not be able to keep up. As a result, the slave may discard some data and the data will be inconsistent.

 

In general, we use redis to store data. In this case, we will re-store the data in redis when it cannot be obtained in redis, therefore, you generally do not have to worry about data inconsistency caused by master-slave replication.

CODIS author Huang Dongxu describes the pitfalls of distributed redis architecture design and development.

Redis Distributed Solution-coids

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.