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


边栏推荐
- Unity 一键替换场景中的物体
- Untiy controls the playback speed of animation
- Force deduction solution summary 735 planetary collision
- Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult
- Lua对table进行深拷贝
- 2022.07.06 summer training personal qualifying (I)
- WebView详解
- Modify the running container port mapping
- Unity中使用UnityWebRequest进行网络和本地图片加载
- OsCache缓存监控刷新工具
猜你喜欢

Hcip (configuration of GRE and mGRE and OSPF related knowledge)

A new mode of one-stop fixed asset management

An idea plug-in helps you transform dto, VO, Bo, Po, do gracefully

Training mode and practice of digital applied talents in Colleges and Universities under the integration of industry and education

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

Reasons and solutions for moving the first column to the last column in El table

The principle and use of the wrap file of tolua

Redis安装

Google Earth engine (GEE) -- problems in the use of coordinate projection and reduceresolution functions in image downscaling

Application of mobile face stylization Technology
随机推荐
WebView详解
consul安装与配置
Detailed explanation of boost official website search engine project
String function (Part 2)
Reasons and solutions for moving the first column to the last column in El table
Google Earth engine (GEE) -- problems in the use of coordinate projection and reduceresolution functions in image downscaling
China business CDP white paper | love Analysis Report
Lua 中 __index、__newindex、rawget、rawset的理解
Character function and string function (Part 1)
【补题日记】[2022牛客暑期多校2]D-Link with Game Glitch
社区点赞业务缓存设计优化探索
[geek challenge 2019] babysql-1 | SQL injection
2022.07.07 summer training personal qualifying (II)
Rest style
【Try to Hack】内网基础
DNS series (III): how to avoid DNS spoofing
Interfaces and abstract classes
2022.07.12 summer training personal qualifying (VII)
Zhou Hongyi talks about Internet thinking: users, not customers
OsCache缓存监控刷新工具