当前位置:网站首页>分布式定时器
分布式定时器
2022-07-28 11:26:00 【InfoQ】
1 定时器应用场景
2
分布式定时器具备的功能
- 数据可靠性: 定时器事件需要持久化,不能丢失.
3 业界实现方案
- 由于redis的zset天然具备排序功能,本身就是一个定时器的数据结构。直接采用zset,并配合分布式锁来轮询zset。该架构比较简单,但是缺点是面对海量定时器超时场景,并发的上限只能在单机,不能水平扩展,容易造成定时器事件的积压,实时性不高。
- 类似一些分布式消息队列,自带延时队列的功能。但是缺乏删除和修改定时器的能力.
4 企点分布式定时器设计
4.1 定时器数据结构:


- 时间指针: 对于每个定时器的时间指针,采用string结构。key是每个定时器租户的id,value指向当前时间轮的轮盘位置。
- 时间轮:时间轮上的每一个刻度上保存着这一秒所有的定时器事件,如何让这个列表能分段读取,是选择数据结构的关键。我们选择了redis的zset,每一秒刻度指向一个redis zset结构,redis zset存储这一秒定时事件ID的列表。zset支持通过zrange取一个范围,每个范围可以变成一个可以并发执行的任务。
- 用户数据: 使用string结构,key是定时器ID,value是业务数据。在定时器到期之后,会将这个业务数据带给业务.
4.2 架构

1. 接入层:
- 接入层主要负责定时器协议解析,提供增加、删除、更新定时器等基本操作。
2. 调度器:
功能
实现细节
- 任务分配

- 任务数据结构

- 高可用

3.worker执行器
功能
实现细节

- 流程:
- 实时性:
- kafka提供commit机制,可以保证数据在未commit时,在client重连时会从未commit位置继续消费,借此机制,定时器client可以确保消息至少被消费一次。
- 因为定时器任务重试,重复生产等原因会有小概率导致定时事件在client端被重复消费到,timer client基于带过期时间LRU缓存对timerID进行去重,尽量确保事件不重。
- Redis的任务认领通过SPOP,确保一个taskID只会被一个worker进程拉取从而减少少竞争。
- 在极端情况下可能发生,一个taskID被scheduler重复分配,为确保任务被worker进程独占,基于redis单线程特性,借助LUA脚本可以满足redis的事务,worker使用LUA脚本在更新任务状态时,将任务状态表中workerID与本进程进行绑定认领,其它进程无法再认领此任务,确保竞争发生后任务不会重复执行。
- 管理中心:定时器管理模块主要负责对集群和appid的管理。集群的的名字服务名称、k8s地址、配置中心地址等都被登记在此。不同的业务,通过申请不同的appid来做隔离。每个appid对应不同的kafka的topic、kafka集群地址、kafka版本等。如下是分布式定时器管理界面:


边栏推荐
- 【补题日记】[2022牛客暑期多校2]D-Link with Game Glitch
- P5472 [noi2019] douzhudi (expectation, Mathematics)
- 瑞吉外卖——Day01
- Hcip day 6 (OSPF related knowledge)
- Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult
- Latex矩阵简单使用
- php保留两位小数的几种方法介绍
- Globalthis is not defined solution
- 玩转诗词-领略古诗文之美
- PHP获取本周所有日期或者最近七天所有日期
猜你喜欢

Design process sharing of wireless anti loss alarm based on single chip microcomputer

Idea replication module

Direct insert sort and Hill sort

Unity遇坑记之 ab包卸载失败

Specific process of strong cache and negotiation cache

15、用户web层服务(三)

Application of mobile face stylization Technology

Redis安装

IDEA复制模块

boost官网搜索引擎项目详解
随机推荐
Force buckle 7_ 1672. Total assets of the richest customers
WebView details
Direct insert sort and Hill sort
Multithreading and high concurrency (III) -- source code analysis AQS principle
Final modifier attribute
Notes on using objectanimator
Lua middle__ index、__ Understanding of newindex, rawget and rawset
一些知识概念
Traversal and copy of files in jar package
Anonymous subclass objects of abstract classes
Reasons and solutions for moving the first column to the last column in El table
Detailed explanation of boost official website search engine project
Deployment and use of Minio distributed object storage
Interpretable ml of Li Hongyi's machine learning model
PHP获取本周所有日期或者最近七天所有日期
[diary of supplementary questions] [2022 Niuke summer multi school 2] D-Link with game glitch
SQL注入 Less24(二次注入)
Modify the running container port mapping
Unity one key replacement of objects in the scene
REST风格