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

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

  1. dynamodb java_使用Java扫描DynamoDB项目

    dynamodb java 在之前的文章中,我们介绍了如何查询DynamoDB数据库 查询DynamoDB第1部分 查询DynamoDB第2部分 . 除了发出查询之外,DynamoDB还提供扫描功能. ...

  2. linux每个进程的地址空间大小,别再说你不懂 Linux 内存管理了,10 张图给你安排的明明白白...

    原标题:别再说你不懂 Linux 内存管理了,10 张图给你安排的明明白白 转自:LemonCode 过去的一周有点魔幻,有印象的有三个新闻:天猫总裁绯闻事件,蘑菇街裁员,不可能打工的周某也放出来了. ...

  3. dynamodb容器使用_使用DynamoDB映射器将DynamoDB项目映射到对象

    dynamodb容器使用 以前,我们使用Java创建了DynamoDB表. 对于各种数据库,例如sql数据库或nosql,有一组工具可帮助访问,持久化和管理对象/类与基础数据库之间的数据. 例如,对于 ...

  4. dynamodb java_使用Java查询DynamoDB项

    dynamodb java 在上一篇文章中,我们继续在DynamoDB数据库上插入数据. 在本教程中,我们将对DynamoDB表发出一些基本查询. 主要规则是每个查询都必须使用哈希键. 查询的最简单形 ...

  5. dynamodb java_使用Java更新DynamoDB项

    dynamodb java 在上一篇文章中,我们继续使用Java将项目插入DynamoDB. DynamoDB还支持更新项目. 我们将使用Login表获取更新示例. 发布更新时,必须指定要更新的项目的 ...

  6. 别再说你不懂Linux内存管理了,10张图给你安排的明明白白!

    来自:后端技术学堂 过去的一周有点魔幻,有印象的有三个新闻:天猫总裁绯闻事件,蘑菇街裁员,不可能打工的周某也放出来了.三件事,两件和互联网行业相关,好像外面的世界很是精彩啊!吃瓜归吃瓜,学习还是不能落 ...

  7. 通读《技术管理实战36讲》自我介绍内容安排

    自我介绍 你好,我是小Z,一个工作在交付前线的程序员,和客户对接项目需求并协调内部同事保质保量的完成交付任务就是我的日常工作. 非常荣幸能有机会阅读到这个专栏,不得不说这个专栏给我很多的启发和指引,感 ...

  8. Amazon DynamoDB 入门2:工作原理、API和数据类型介绍

    本节主要介绍DynamoDB 基本概念.核心组件.数据结构.Api DynamoDB 基本概念 DynamoDB 是 AWS 独有的完全托管的 NoSQL Database.它的思想来源于 Amazo ...

  9. aws dynamodb_使用适用于Java 2的AWS开发工具包的AWS DynamoDB版本字段

    aws dynamodb 将任何实体上的版本属性保存到 AWS DynamoDB数据库,它只是表示已修改实体次数的数字表示. 首次创建实体时,可以将其设置为1,然后在每次更新时递增. 好处是立竿见影的 ...

最新文章

  1. mongodb的安装以及客户端
  2. ORB特征提取策略对ORB-SLAM2性能的影响
  3. 5G时代,工业互联网安全挑战远超消费互联网
  4. 自己动手实现山东大学QLSC_STU无线网络掉线后自动重连
  5. 使用 AngularJS 和 ReactJS 的经验
  6. Linux Shell常用技巧(三)
  7. SQL Server 查询数据库中所有的表名及行数
  8. 学习:深入浅出之正则表达式(转)
  9. Android应用开发-onNewIntent()
  10. undefined reference to libiconv_open'
  11. mysql 5.7 slow_mysql 5.7 解决 set global slow_query_log=on;报错
  12. 用jquery实现图片轮播
  13. sqlserve 热备用状态更新_什么是核心交换机的链路聚合、冗余、堆叠、热备份
  14. 小程序源码:全新外卖侠cps5.6全套微信小程序源码下载-多玩法安装简单
  15. oracle中一个月的最后一天,SQL和Oracle获取每周、每月、每年第一天和最后一天
  16. gnome桌面终端程序 Guake
  17. K8S-四层负载均衡-Service解读
  18. php开源小程序直播,微信小程序直播
  19. GOOGLE地图基站定位-Google Mobile Maps API
  20. 对象存储(Object-based Storage)概述

热门文章

  1. -wl是不是c语言的标识符,C语言基础知识考试
  2. Debian 安装 yum
  3. 用php生成HTML文件的类
  4. bzoj1222: [HNOI2001]产品加工
  5. 01-hibernate注解:类级别注解,@Entity,@Table,@Embeddable
  6. PullToRefreshListView中嵌套ViewPager滑动冲突的解决
  7. 【Ubuntu14】Nginx+PHP5+Mysql记录
  8. ldconfig deferred processing now taking place
  9. 如何使用python导入mat格式的数据并整理
  10. 阿里云天池 Python训练营Task4: Python数据分析:从0完成一个数据分析实战 学习笔记