当前位置:网站首页>分布式定时器
分布式定时器
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版本等。如下是分布式定时器管理界面:


边栏推荐
- Lyscript get previous and next instructions
- How async await implements concurrency
- consul安装与配置
- P5472 [NOI2019] 斗主地(期望、数学)
- Use Baidu PaddlePaddle easydl to complete garbage classification
- Start from scratch blazor server (2) -- consolidate databases
- Deployment and use of Minio distributed object storage
- 14. User web layer services (II)
- Redis installation
- Static proxy instance
猜你喜欢

Zhou Hongyi talks about Internet thinking: users, not customers

游戏流程与底层实现 逐步完成

China business CDP white paper | love Analysis Report

Service workers let the website dynamically load webp pictures

程序的存储态与运行态

The game process and the underlying implementation are gradually completed

Rest style

REST风格

强缓存、协商缓存具体过程

consul安装与配置
随机推荐
consul安装与配置
2022.07.10 summer training personal qualifying (V)
301. Delete invalid brackets
Client service registration of Nacos registry
Modify the running container port mapping
[diary of supplementary questions] [2022 Niuke summer multi school 2] i-let fat tension
[diary of supplementary questions] [2022 Niuke summer multi school 2] l-link with level editor I
如何让照片中的人物笑起来?HMS Core视频编辑服务一键微笑功能,让人物笑容更自然
IDEA复制模块
Hcip rip comprehensive experiment
Design process sharing of wireless anti loss alarm based on single chip microcomputer
Static proxy instance
Develop your own NPM package from 0
Character function and string function (Part 1)
php保留两位小数的几种方法介绍
社区点赞业务缓存设计优化探索
Lua middle__ index、__ Understanding of newindex, rawget and rawset
Simple selection sort and heap sort
SQL注入 Less18(头部注入+报错注入)
Skiasharp's WPF self drawn drag ball (case version)