当前位置:网站首页>Module 8 operation

Module 8 operation

2022-06-11 19:35:00 InfoQ

Homework : Design message queue to store message data  MySQL  form
【 Job requirements 】
1.  Include table name 、 Field 、 Indexes ;2.  Use words to describe the design ideas and reasons , for example : Why design an index ?3.  One page  PPT  that will do .
【 Tips 】
1.  You need to consider one table per message queue , Or put all the messages in one table , Add a “ Queue name ” Field of .


answer :
  • Different task queues create different tables , such as xx_q, yy_q etc. . This reduces the size of each message table , Improve performance ; Reduce interference between different messages , Improve SQL Execution speed .

  • Table structure
Here we use xx_q give an example , The main fields are as follows :
CREATE TABLE `xx_q` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
 //  Since the primary key
  `status` tinyint(4) DEFAULT 0,
 //  Queue type :0: todo, 1: done
  `ps` int(11) DEFAULT 0,
 // Get the process number of the message , Equivalent to a lock  
  `ut` timestamp,
 // Time stamp
  `msg` json,
 // Message body parameters ,json Format preservation
  PRIMARY KEY (`id`),
  KEY `ut` (`ps`,`status`)
 // Traverse the index of the queue ,  Faster retrieval of messages

  • working principle :
  • Lock this work message : update xx_q set ps=connection_id() , status=1 where ps = 0 and status=0 limit 1;
  • Get message body : select id, msg from xx_q where ps = connection_id() and status=1;
  • Conduct   News consumption
  • Delete the consumed message  delete from xx_q where ps = connection_id() and status=1;

  • Clean up the messages occupied by the zombie process : Time clearing   conversation id  No longer exists ( Abnormal exit ) but status=1  And   Zombie time exceeded 30 Minutes of news
update xx_q set status=0, ps=0 where status=1 and ps not in (`connection_id of 'show processlist'`) and ut < current_timestamp - interval 30 minute;
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111931468651.html