A brief talk on the coupling between Redis and MySQL and the efficient migration of MySQL to Redis using pipelines

Source: Internet
Author: User
Tags message queue

http://blog.csdn.net/dba_waterbin/article/details/8996872

㈠redis and MySQL coupling


Early in the business architecture, we should "eat in the bowl and look at the pot", don't let MySQL have a dream, and Redis unintentionally
After all, some relational structure is not suitable for redis run, "Male and female collocation, work not tired", recommended to let MySQL and Redis knot

Secondly, these 2 people, generally in different scenarios do choose, but not in the performance of the choice,
Only in the case of 2, comprehensive performance, hardware cost, operation and maintenance costs, and other options
For example, Web game enable Redis+mysql:
In-game: Buddy relationships, leaderboards, counters, queues, caches are all well suited for Redis

Another example is the architecture of Sina Weibo, such as user-focused relationships:

In MySQL is a < fan, the person of concern > such a row is stored. And in Redis you can save as a set, or zset, etc.

The general process is copied from MySQL to Redis.
The basic structure should be:
1. Tweet--> enter Message Queue--> Deposit mysql--> Copy to Redis
2. Query--> Query Cache--> Query Redis--> query MySQL



㈡ Fast Migration Mysql→→redis



①mysql the table to be exported David_lin

[Plain]View Plaincopyprint?
  1. mysql> desc David_lin;
  2. +---------+-------------+------+-----+---------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +---------+-------------+------+-----+---------+-------+
  5. | ID | Int (11) | NO | PRI |       NULL | |
  6. | MyName | varchar (25) | NO | UNI |       NULL | |
  7. | Mymoney | Int (11) |     NO | |       0 | |
  8. +---------+-------------+------+-----+---------+-------+
  9. Mysql> select * from David_lin;
  10. +----+--------+---------+
  11. | ID | MyName | Mymoney |
  12. +----+--------+---------+
  13. | 1 |  David | 100000 |
  14. | 2 |  Rocky | 200000 |
  15. +----+--------+---------+

② writing an export script

The Redis commands executed in each row of data are as follows:
Hset David_lin [myname] [Mymoney]

[Plain]View Plaincopyprint?
  1. [email protected] ~]# cat Mysql_to_redis.sql
  2. SELECT CONCAT (
  3. "*4\r\n",
  4. ' $ ', LENGTH (redis_cmd), ' \ r \ n ',
  5. Redis_cmd, ' \ r \ n ',
  6. ' $ ', LENGTH (redis_key), ' \ r \ n ',
  7. Redis_key, ' \ r \ n ',
  8. ' $ ', LENGTH (hkey), ' \ r \ n ',
  9. HKEY, ' \ r \ n ',
  10. ' $ ', LENGTH (hval), ' \ r \ n ',
  11. Hval, ' \ R '
  12. )
  13. From (
  14. SELECT
  15. ' Hset ' as Redis_cmd,
  16. ' David ' as Redis_key,
  17. MyName as HKEY,
  18. Mymoney as Hval
  19. From David_lin
  20. ) as T



③ Start Import

[Plain]View Plaincopyprint?
    1. [Email protected] ~]# mysql-uroot-poracle test--skip-column-names--raw < Mysql_to_redis.sql | REDIS-CLI--pipe
    2. All data transferred. Waiting for the last reply ...
    3. Last reply received from server.
    4. errors:0, replies:0



④ in Redis query

[Plain]View Plaincopyprint?
    1. Redis 127.0.0.1:6379> Hgetall David
    2. 1) "David"
    3. 2) "100000"
    4. 3) "Rocky"
    5. 4) "200000"



Here is only a demo, a small amount of data, but, look at the results, some similar rows to row ha, column operations, there are wood:)

A brief talk on the coupling between Redis and MySQL and the efficient migration of MySQL to Redis using pipelines

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.