dynamodb管理ttl_如何使用DynamoDB TTL和Lambda安排临时任务

dynamodb管理ttl

by Yan Cui

崔燕

如何使用DynamoDB TTL和Lambda安排临时任务 (How to schedule ad-hoc tasks with DynamoDB TTL and Lambda)

CloudWatch Events let you easily create cron jobs with Lambda. However, it’s not designed for running lots of ad-hoc tasks, each to be executed once, at a specific time. The default limit on CloudWatch Events is a lowly 100 rules per region per account. It’s a soft limit, so it’s possible to request a limit increase. But the low initial limit suggests it’s not designed for use cases where you need to schedule millions of ad-hoc tasks.

通过CloudWatch Events,您可以轻松地使用Lambda创建cron作业。 但是,它并非设计用于运行特定任务,每个任务只能在一次执行一次。 CloudWatch Events的默认限制是每个帐户每个区域最低100条规则 。 这是一个软限制,因此可以请求增加限制。 但是较低的初始限制表明它不适用于需要安排数百万个临时任务的用例。

CloudWatch Events is designed for executing recurring tasks.

CloudWatch Events专为执行重复任务而设计。

问题 (The Problem)

It’s possible to do this in just about every programming language. For example, .Net has the Timer class and JavaScript has the setInterval function. But I often find myself wanting a service abstraction to work with. There are many use cases for such a service, for example:

几乎每种编程语言都可以做到这一点。 例如,.Net具有Timer类,而JavaScript具有setInterval函数。 但是我经常发现自己想要使用服务抽象。 此类服务有很多用例,例如:

  • A tournament system for games would need to execute business logic when the tournament starts and finishes.

    当比赛开始和结束时,用于游戏的比赛系统将需要执行业务逻辑。
  • An event system (think eventbrite.com or meetup.com) would need a mechanism to send out timely reminders to attendees.

    事件系统(认为eventbrite.com或meetup.com )将需要一个机制来及时提醒发送给与会者。

  • A to-do tracker (think wunderlist) would need a mechanism to send out reminders when a to-do task is due.

    待办事项跟踪程序(想想清单 )需要一种机制来在待办事项到期时发出提醒。

However, AWS does not offer a service for this type of workloads. CloudWatch Events is the closest thing, but as discussed above it’s not intended for the use cases above. You can, however, implement them using cron jobs. But such implementations have other challenges.

但是,AWS不为此类工作负载提供服务。 CloudWatch Events是最接近的事情,但是如上所述,它并不适用于上述用例。 但是,您可以使用cron作业来实现它们。 但是这样的实现还有其他挑战。

I have implemented such service abstraction a few times in my career already. I experimented with a number of different approaches:

在我的职业生涯中,我已经实现了几次这样的服务抽象。 我尝试了多种不同的方法:

  • cron job (with CloudWatch Events)

    cron作业(带有CloudWatch Events)
  • wrapping the .Net Timer class as an HTTP endpoint

    将.Net Timer类包装为HTTP终结点

  • using SQS Visibility Timeout to hide tasks until they’re due

    使用SQS可见性超时来隐藏任务,直到到期

And lately, I have seen a number of folks use DynamoDB Time-To-Live (TTL) to implement these ad-hoc tasks. In this post, we will take a look at this approach and see where it might be applicable for you.

最近,我看到许多人使用DynamoDB生存时间 (TTL)来实现这些临时任务。 在这篇文章中,我们将研究这种方法,并查看它可能适用于您的地方。

我们如何衡量方法? (How do we measure the approach?)

For this type of ad-hoc task, we normally care about:

对于此类临时任务,我们通常关心:

  • Precision: how close to my scheduled time is the task executed? The closer the better.

    精度 :任务在我预定的时间有多近? 越近越好。

  • Scale (number of open tasks): can the solution scale to support many open tasks, i.e. tasks that are scheduled but not yet executed?

    规模(未完成任务的数量) :解决方案是否可以扩展以支持许多未完成任务,即已计划但尚未执行的任务?

  • Scale (hotspots): can the solution scale to execute many tasks around the same time? E.g. millions of people set a timer to remind themselves to watch the Superbowl, so all the timers fire within close proximity to kickoff time.

    扩展(热点):解决方案可以扩展以在同一时间执行许多任务吗? 例如,数以百万计的人设置了一个计时器来提醒自己观看超级碗,因此所有计时器都在启动时间附近触发。

DynamoDB TTL作为调度机制 (DynamoDB TTL as a scheduling mechanism)

From a high level, this approach looks like this:

从较高的角度来看,这种方法看起来像这样:

  • A scheduled_items DynamoDB table which holds all the tasks that are scheduled for execution.

    scheduled_items DynamoDB表包含计划执行的所有任务。

  • A scheduler function that writes the scheduled task into the scheduled_items table, with the TTL set to the scheduled execution time.

    一个scheduler函数,它将调度的任务写入scheduled_items表,并且将TTL设置为调度的执行时间。

  • An execute-on-schedule function that subscribes to the DynamoDB Stream for scheduled_items and reacts to REMOVE events. These events correspond to when items have been deleted from the table.

    execute-on-schedule功能订阅到DynamoDB流为scheduled_items并响应REMOVE事件。 这些事件对应于从表中删除项目的时间。

可伸缩性(未完成任务的数量) (Scalability (number of open tasks))

Since the number of open tasks just translates to the number of items in the scheduled_items table, this approach can scale to millions of open tasks.

由于未完成任务的数量仅转换为scheduled_items表中项目的数量,因此这种方法可以扩展到数百万个未完成任务。

DynamoDB can handle large throughputs (thousands of TPS) too. So this approach can also be applied to scenarios where thousands of items are scheduled per second.

DynamoDB也可以处理大吞吐量(数千TPS)。 因此,这种方法还可以应用于每秒计划数千个项目的方案。

可扩展性(热点) (Scalability (hotspots))

When many items are deleted at the same time, they are simply queued in the DynamoDB Stream. AWS also auto scales the number of shards in the stream, so as throughput increases the number of shards would go up accordingly.

同时删除许多项目时,它们只是在DynamoDB流中排队。 AWS还自动缩放流中分片的数量,因此随着吞吐量的增加,分片的数量将相应增加。

But, events are processed in sequence. So it can take some time for your function to process the event depending on:

但是,事件是按顺序处理的。 因此,您的函数可能需要一些时间来处理事件,具体取决于:

  • its position in the stream, and

    它在信息流中的位置,以及
  • how long it takes to process each event.

    处理每个事件需要多长时间。

So, while this approach can scale to support many tasks all expiring at the same time, it cannot guarantee that tasks are executed on time.

因此,尽管这种方法可以扩展以支持许多同时到期的任务,但它不能保证任务能按时执行。

精确 (Precision)

This is a big question about this approach. According to the official documentation, expired items are deleted within 48 hours. That is a huge margin of error!

这是关于此方法的一个大问题。 根据官方文件 ,过期物品将在48小时内删除。 那是一个很大的误差范围!

As an experiment, I set up a Step Functions state machine to:

作为实验,我设置了一个“步骤功能”状态机以:

  1. add a configurable number of items to the scheduled_items table, with TTL expiring between 1 and 10 mins

    scheduled_items表中添加可配置的项目数,其中TTL在1至10分钟之间到期

  2. track the time the task is scheduled for and when it’s actually picked up by the execute-on-schedule function

    跟踪计划任务的时间以及按计划execute-on-schedule功能实际提取任务的时间

  3. wait for all the items to be deleted

    等待所有项目被删除

The state machine looks like this:

状态机如下所示:

I performed several runs of tests. The results are consistent regardless of the number of items in the table. A quick glimpse at the table tells you that, on average, a task is executed over 11 mins AFTER its scheduled time.

我进行了几次测试。 无论表中的项目数量如何,结果都是一致的。 快速浏览一下表格即可了解到,平均而言,任务在预定时间后的11分钟内执行。

I repeated the experiments in several other AWS regions:

我在其他几个AWS区域重复了实验:

I don’t know why there is such a marked difference between US-EAST-1 and the other regions. One explanation is that the TTL process requires a bit of time to kick in after a table is created. Since I was developing against the US-EAST-1 region initially, its TTL process has been “warmed” compared to the other regions.

我不知道为什么US-EAST-1与其他地区之间有如此明显的区别。 一种解释是,创建表后,TTL过程需要一点时间才能启动。 自从我最初针对US-EAST-1地区开发以来,与其他地区相比,它的TTL流程已被“温暖”。

结论 (Conclusions)

Based on the result of my experiment, it will appear that using DynamoDB TTL as a scheduling mechanism cannot guarantee a reasonable precision.

根据我的实验结果,看来使用DynamoDB TTL作为调度机制不能保证合理的精度。

On the one hand, the approach scales very well. But on the other, the scheduled tasks are executed at least several minutes behind, which renders it unsuitable for many use cases.

一方面,该方法可以很好地扩展。 但是,另一方面,计划的任务至少要延迟几分钟才能执行,这使其不适用于许多用例。

翻译自: https://www.freecodecamp.org/news/how-to-schedule-ad-hoc-tasks-with-dynamodb-ttl-and-lambda-421fa5778993/

dynamodb管理ttl

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392123.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

5g创业的构想_数据科学项目的五个具体构想

5g创业的构想Do you want to enter the data science world? Congratulations! That’s (still) the right choice.您想进入数据科学世界吗? 恭喜你! 那(仍然)是正确的选择。 The market currently gets tougher. So, you must be mentally prepared f…

Microsoft Windows Phone 7 Toolkit Silverlight SDK XNA Game Studio 4.0 开发工具套件正式版下载...

Windows Phone 7开发工具套件包括Visual Studio 2010 Express for Windows Phone、Windows Phone模拟器、Expression Blend 4 for Windows Phone、XNA Game Studio 4.0和新增加的必应地图SDK。 英文版的光盘镜像:点击下载 文档中心:Windows Phone develo…

数据挖掘—Apriori算法(Java实现)

算法描述 (1)扫描全部数据,产生候选1-项集的集合C1; (2)根据最小支持度,由候选1-项集的集合C1产生频繁1-项集的集合L1; (3)对k>1,重复执行步骤…

怎么汇报一周开发工作情况_如何在没有经验的情况下获得第一份开发人员工作

怎么汇报一周开发工作情况Whether you’ve done a coding bootcamp or taught yourself, getting your first developer job with only a few months of coding under your belt is hard.无论您是完成了编码训练营还是自学了,仅靠几个月的编码就很难拿到第一份开发人…

vue.js的认知

Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的渐进式框架。 Vue 只关注视图层, 采用自底向上增量开发的设计。 Vue 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件。 Vue 学习起来非常简单,。转载于…

c语言中的无符号字节,C语言之有符号数和无符号数

我们知道,在C语言中存在无符号数和有符号数(一些高级语言如Java里面是没有无符号数的),但是对于计算机而言,其本身并不区别有符号数和无符号数,因为在计算机里面都是0或者1,但是在我们的实际使用中有时候需要使用有符号…

8种排序算法比较

8种排序算法,各算法名称见下表或见源码。运行程序时,将需要你输入一数值,以确定对多少随机数进行排序。然后将会显示各排序算法的耗时。并且你可选择时否进行正序和反序测试。 由于水平有限,可能存在一些错误,还请各位…

两个问题,关于XP进程优化及SVSP虚拟存储平台

这两个问题让我有点头痛,是Boss这阵子布置给我的,都一段时间了,我还是没找出合适的解决方案来答复Boss.第一个问题是:查查X200或X61中的进程,看哪些是可以不要的,停掉,但又不影响用户使用。&…

数据挖掘—朴素贝叶斯分类算法(Java实现)

算法描述 (1)扫描训练样本数据集,分别统计训练集中类别 Ci 的个数 Di 和属于类别Ci 的样本中属性Ak取值Xk为 Dik 的实例样本个数,构成统计表; (2)计算先验概率和条件概率,构成概率表…

net core 获取网站目录

AppContext.BaseDirectory 获取项目的根目录转载于:https://www.cnblogs.com/zxs-onestar/p/7147265.html

泰晤士报下载_《泰晤士报》和《星期日泰晤士报》新闻编辑室中具有指标的冒险活动-第1部分:问题

泰晤士报下载TLDR: Designing metrics that help you make better decisions is hard. In The Times and The Sunday Times newsrooms, we have spent a lot of time trying to tackle three particular problems.TLDR :设计度量标准以帮助您做出更好的决策非常困难…

速度一半永远追不上_您将永远不会知道自己应该怎么做的一半-没关系。

速度一半永远追不上by Ken Gilb肯吉尔伯(Ken Gilb) 您将永远不会知道自己应该怎么做的一半-没关系。 (You will never know half of what you think you should — and that’s ok.) Impostor syndrome is a real thing in software development. After 20 years in the indus…

c语言自学门槛,初学C语言的人最常问的几个问题

初学C语言的人最常问的几个问题C语言是一门通用计算机编程语言,应用广泛。对于新手来说学习C语言并不是那么容易,下面是C语言初学者最常问的几个问题,欢迎阅读!1.多久能学会编程?这是一个没有答案的问题。每个人投入的时间、学习效率和基础都…

背景消除的魔力

图片的功能非常强大,有一图胜千言的效果,所以在文档或演示文稿中使用图片来增加趣味性是一种很棒的想法。但问题是,图片通常会变为文字中间的独立矩形,而不是真正与内容融合在一起。您可以在图片中放置边框或效果,使其…

Puppet 之 模板和模块

1 概述模板文件是在puppet模块下面templates目录中以”.erb”结尾的文件,puppet模板主要用于文件,例如各种服务的配置文件,相同的服务,不同的配置就可以考虑使用模板文件。模块是Puppet自包含的代码和数据集合。绝大多数的清单都…

java异步io_Java中的异步IO与异步请求处理

java异步ioIn this article, I am trying to explain the difference between Async-IO and Async-Request processing in the HTTP request in the Java world.在本文中,我试图解释Java世界中HTTP请求中Async-IO和Async-Request处理之间的区别。 In the pre-Java …

异常检测机器学习_使用机器学习检测异常

异常检测机器学习什么是异常检测? (What is Anomaly Detection?) The anomaly detection problem has been a problem that has been frequently explored in the field of machine learning and has become a classic problem. Anomalies are any unusual sequenc…

数据挖掘—BP神经网络(Java实现)

public class Test {public static void main(String args[]) throws Exception {ArrayList<ArrayList<Double>> alllist new ArrayList<ArrayList<Double>>(); // 存放所有数据ArrayList<String> outlist new ArrayList<String>(); // …

c语言掌握常用函数,c语言一些常用函数.pdf

c语言一些常用函数C 语言程序设计(常用函数说明)C 语言是 1972 年由美国的 Dennis Ritchie 设计发明的,并首次在 UNIX 操作系统的 DEC PDP-11 计算机上使用。它由早期的编程语言 BCPL(Basic Combind ProgrammingLanguage)发展演变而来。在 1970 年,AT&T 贝尔实验室的 Ken T…

高阶函数 - 函数节流

/*** 函数节流 - 限制函数被频繁调用* param {Function} fn [需要执行的函数]* param {[type]} interval [限制多长的时间再重复执行fn]*/var throttle function(fn, interval) {var __self fn,timer,firstTime true;return function() {var args arguments,__me…