Coupling Between Redis and MySQL and efficient migration from MySQL to Redis using Pipelines

Source: Internet
Author: User

Talking about the coupling between Redis and MySQL and the efficient migration from MySQL to Redis using pipelines (I) coupling between Redis and MySQL in the early stage of the business architecture, we should "look at the pot in a bowl ", don't let MySQL have a dream, but Redis does not care. After all, some relational structures are not suitable for running in Redis, "working with both men and women". We recommend that MySQL and Redis be connected. generally, it is used for selection in different scenarios, instead of performance selection, only when both are available, comprehensive performance, hardware costs, O & M costs, and other options such as web games enable Redis + MySQL: in the game, friend relationships, rankings, counters, queues, and cache are suitable for implementation through Redis. Another example is Sina Weibo's architecture, such as user relationship: in MySQL, it is stored in a row like <fans, followers>. In Redis, you can save it as a set, or the general process such as zset is that the basic structure copied from MySQL to Redis should be: 1. weibo --> enter the Message Queue --> Save to MySQL --> copy to Redis 2. query --> query cache --> query Redis --> query MySQL (ii) Fast MySQL migration → Redis ① the table to be exported by MySQL David _lin [plain] mysql> desc David _lin; + --------- + ------------- + ------ + ----- + --------- + ------- + | Field | Type | Null | Key | Default | Extra | + --------- + ----------- + ------ + ----- + --------- + ------- + | id | int (11) | NO | PRI | NULL | myname | varchar (25) | NO | UNI | NULL | mymoney | int (11) | NO | 0 | + --------- + ------------- + ------ + ----- + --------- + ------- + mysql> select * from David _lin; + ---- + -------- + --------- + | id | myname | mymoney | + ---- + -------- + --------- + | 1 | david | 100000 | 2 | rocky | 200000 | + ---- + -------- + --------- + ② write the Redis Command executed in each row of the export script as follows: HSET David _lin [myname] [mymone Y] [plain] [root @ odd ~] # Cat mysql_to_redis. SQL SELECT CONCAT ("* 4 \ r \ n", '$', LENGTH (redis_cmd), '\ r \ n', redis_cmd,' \ r \ n ', '$', LENGTH (redis_key), '\ r \ n', redis_key,' \ r \ n', '$', LENGTH (hkey ), '\ r \ n', hkey,' \ r \ n', '$', LENGTH (hval), '\ r \ n', hval,' \ R ') FROM (SELECT 'hset' AS redis_cmd, 'David' AS redis_key, myname AS hkey, mymoney AS hval FROM david _lin) AS t ③ start to import [plain] [root @ odd ~] # Mysql-uroot-poracle test -- skip-column-names -- raw <mysql_to_redis. SQL | redis-cli -- pipe All data transferred. waiting for the last reply... last reply replicated ed from server. errors: 0, replies: 0 ④ Query [plain] Redis 127.0.0.1: 6379> hgetall david 1) "david" 2) "100000" 3) "rocky" 4) in redis) "200000" here is only a demo, a small amount of data, but, look at this result, some similar row-to-column conversion, column operations, there are wood :) By David Lin Good Lucky

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.