{"componentChunkName":"component---src-templates-blog-post-js","path":"/blog/2020-06-29-mq/","result":{"data":{"markdownRemark":{"id":"9e6dbd4c-b93e-56b2-9f72-14d12d36c06f","html":"<h1>Message Queue</h1>\n<h2>队列</h2>\n<h3>偏移量（Offset）</h3>\n<p>队列指针位置</p>\n<h4>消费偏移量</h4>\n<h4>生产偏移量</h4>\n<h3>队列语义</h3>\n<h4>Queue 语义</h4>\n<p>一个队列只支持一个消费者。</p>\n<h4>Topic 语义（扇出 Fanout）</h4>\n<p>一个队列支持多个消费者，且消费都各自独立消费互不相关。</p>\n<h2>Broker</h2>\n<h3>Topic</h3>\n<h4>Partition</h4>\n<h2>Producer</h2>\n<h3>同步模式</h3>\n<p>性能差，保证数据的准确性</p>\n<h3>异步模式</h3>\n<p>Producer 内部内存队列缓冲消息，Broker 定时或定量批量发送。</p>\n<p>性能高，有一定延迟，\n重启服务可能丢失消息</p>\n<h2>Consumer</h2>\n<h2>Consumer Group</h2>\n<p>不同消费者组可以消费同一个 Topic， 各自维护消费指针，相互之间没有影响。</p>\n<p>同一 Consumer 组下的消费者可以同时消费多个 Partition，但两个 Consumer 不能同时消费一个 Partition。</p>\n<h2>消息存储</h2>\n<h3>Data Node</h3>\n<p>数据分片保存</p>\n<h3>存储方式</h3>\n<h4>磁盘文件</h4>\n<h4>数据库</h4>\n<p>Data Node - DataBase</p>\n<p>Partition - Table</p>\n<h5>mysql</h5>\n<p>一个物理数据库 10 个 mysql 实例，每个实例 99 个表。</p>\n<table>\n<thead>\n<tr>\n<th>field</th>\n<th>desc</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>id</td>\n<td>自增主键</td>\n</tr>\n<tr>\n<td>biz_id</td>\n<td>业务查询主键</td>\n</tr>\n<tr>\n<td>body</td>\n<td>消息体</td>\n</tr>\n<tr>\n<td>---</td>\n<td>---</td>\n</tr>\n</tbody>\n</table>\n<h4>Metadata Model</h4>\n<h2>Broker 消费模式</h2>\n<h3>Push</h3>\n<p>Broker 端推，消费者端简单。Broker 需要维护状态。</p>\n<p>Broker 可能会被堵塞，消息丢失</p>\n<h3>pull</h3>\n<p>最佳实践</p>\n<h2>负载均衡</h2>\n<h3>Producer 和 Partition 之间</h3>\n<p>轮询</p>\n<h3>同一组 Consumer 和 Partition 之间</h3>\n<h4>动态重平衡</h4>\n<p>消费者组中消费者变化，动态分配 Partition。</p>\n<h2>消费语义</h2>\n<h3>At Least Once 至少一次交付</h3>\n<p>少部分消息可能会被消费多次，消费者端需要做幂等处理。</p>\n<h3></h3>\n<h2>消息顺序性</h2>\n<h2>Lag（Burrow）堆积</h2>\n<p>头指针和尾指针间的距离</p>\n<h2>扩展性</h2>\n<h3>动态添加分区</h3>\n<p>分区热迁移</p>\n<h2>隔离性</h2>\n<h2>通讯</h2>\n<h2>开源项目</h2>\n<h3>kafka</h3>\n<h4>服务发现</h4>\n<p>Zookeeper</p>\n<h3>PMQ 2.0</h3>\n<h4>架构</h4>\n<h5>Metadata 管理</h5>\n<h5></h5>\n<h4>服务发现</h4>\n<p>Nginx + DNS</p>\n<h4>负载均衡</h4>","frontmatter":{"date":"June 29, 2020","title":"MQ 核心概念","description":"","tags":null}}},"pageContext":{"id":"9e6dbd4c-b93e-56b2-9f72-14d12d36c06f"}}}