检查点与日志修剪
检查点(Checkpointing)是将所有挂起的更新从易失性内存刷新到非易失性数据存储的过程。此操作对于限制恢复过程中需要重放的事务数量至关重要,特别是可以最大限度地减少数据库异常关闭或崩溃后恢复所需的时间。
无论是否存在检查点,数据库操作都是安全的,因为任何尚未确认其修改已持久化到存储的事务,都会在下次数据库启动时进行重放。然而,这种保证的前提是这些事务所包含的更改集合必须可用,这些更改被维护在 事务日志 中。
如果检查点执行频率过低,维护大量的未应用事务列表会导致事务日志堆积,因为它们对于恢复是必不可少的。检查点操作会在事务日志中包含一个特殊的 Checkpointing 条目,标记发生检查点的最后一个事务。此条目旨在识别不再需要的事务日志,因为它们包含的所有事务都已安全地存储在存储文件中。
清除不再需要用于恢复的事务日志的过程称为 清理(pruning)。清理依赖于检查点。检查点决定了哪些日志可以被清理,并决定了清理的发生,因为如果没有检查点,意味着可供清理的事务日志文件集合不会发生变化。因此,每当执行检查点操作时,就会触发清理操作。
|
有关 Neo4j 4.4 中检查点和日志清理的信息,请分别参考 配置设置 → 检查点设置、性能 → 检查点 IOPS 限制 和 事务日志 → 日志清理。 |
配置检查点策略
检查点策略(作为日志清理的驱动事件)由 db.checkpoint 配置。根据您的需求,检查点可以按周期运行(默认方式)、在事务日志写入一定数据量后运行,或连续运行。
| 策略 | 描述 |
|---|---|
|
默认 此策略每 10 分钟检查一次是否有挂起的更改需要刷新,如果有,则执行检查点并随后触发日志清理。周期性策略由 |
|
当事务日志的大小达到 |
|
企业版 此策略忽略 |
|
企业版 此策略每 10 秒检查一次是否有足够的日志容量可供清理,如果有,则触发检查点并随后清理日志。默认情况下,该容量设置为 256MiB,但可以使用 |
配置检查点间隔
如果您观察到事务日志文件数量多于预期,这可能是因为检查点执行不够频繁,或者执行时间过长。这是一种临时状态,预期日志文件数量与实际数量之间的差距会在下一次成功执行检查点后消除。检查点之间的间隔可以使用以下配置:
| 检查点配置 | 默认值 | 描述 |
|---|---|---|
|
配置检查点之间的时间间隔。 |
|
|
配置检查点之间的事务间隔。 |
控制事务日志清理
事务日志清理是指安全且自动地删除陈旧、不必要的事务日志文件。文件要被删除需要满足两个条件:
-
文件必须已经被轮转(rotated)。
-
在更靠后的日志文件中至少必须发生过一次检查点。
事务日志清理配置主要涉及指定应保留的事务日志数量。保留超过恢复所需的最低限度的日志的主要原因,源于集群部署和在线备份的需求。由于数据库更新是通过事务日志在集群成员和备份客户端之间进行通信的,保留超过最低限度的日志可以仅传输增量更改(以事务形式),而无需传输整个存储文件,从而大幅节省时间和网络带宽。
清理操作后保留的事务日志数量由设置 db.tx_log.rotation.retention_policy 控制。
db.tx_log.rotation.retention_policy 的默认值已从 2 days 更改为 2 days 2G,这意味着 Neo4j 会保留逻辑日志,其中包含两天内提交的任何事务,且位于指定的 2G 日志空间内。有关详细信息,请参阅 配置事务日志保留策略。
拥有最少量的事务日志数据可以加快检查点进程。要配置检查点进程允许使用的每秒 IO 次数,请设置配置参数 db.checkpoint.iops.limit。
|
禁用 IOPS 限制可能会导致事务处理速度略有下降。有关更多信息,请参阅 检查点 IOPS 限制 和 事务日志设置。 |
此外,从 2025.07 版本开始,您还可以使用 db.checkpoint.throughput.limit 按每秒字节数定义检查点速度。与 IOPS 限制相比,吞吐量限制对刷新速度的控制更严格,检查点进程会更多地让步以保持在配置的吞吐量范围内。
从 2025.07 版本开始,检查点日志消息中还包含平均刷新速度
Checkpoint triggered by "Call to db.checkpoint() procedure" @ txId: 92, append index: 92 checkpoint completed in 7s 464ms. Checkpoint flushed 251909 pages (9% of total available pages), in 249641 IOs. Checkpoint performed with IO limit: 600 IOPS, paused in total 70 times(6026 millis). Average checkpoint flush speed: 281.1MiB/s.
Checkpoint triggered by "Call to db.checkpoint() procedure" @ txId: 88, append index: 88 checkpoint completed in 39s 457ms. Checkpoint flushed 314688 pages (12% of total available pages), in 311753 IOs. Checkpoint performed with IO limit: 64.00MiB/s, paused in total 77 times(38085 millis). Average checkpoint flush speed: 63.04MiB/s.
检查点日志记录和指标
以下详细说明了检查点事件发生时预期出现在 logs\debug.log 中的消息
-
基于
db.checkpoint.interval.time的检查点2023-05-28 12:55:05.174+0000 INFO [o.n.k.i.t.l.c.CheckPointerImpl] Checkpoint triggered by "Scheduled checkpoint for time threshold" @ txId: 49 checkpoint started... 2023-05-28 12:55:05.253+0000 INFO [o.n.k.i.t.l.c.CheckPointerImpl] Checkpoint triggered by "Scheduled checkpoint for time threshold" @ txId: 49 checkpoint completed in 79ms. Checkpoint flushed 74 pages (7% of total available pages), in 58 IOs. Checkpoint performed with IO limit: 789 IOPS, paused in total 0 times(0 millis). Average checkpoint flush speed: 592.0KiB/s.
-
基于
db.checkpoint.interval.tx的检查点2023-05-28 13:08:51.603+0000 INFO [o.n.k.i.t.l.c.CheckPointerImpl] Checkpoint triggered by "Scheduled checkpoint for tx count threshold" @ txId: 118 checkpoint started... 2023-05-28 13:08:51.669+0000 INFO [o.n.k.i.t.l.c.CheckPointerImpl] Checkpoint triggered by "Scheduled checkpoint for tx count threshold" @ txId: 118 checkpoint completed in 66ms. Checkpoint flushed 74 pages (7% of total available pages), in 58 IOs. Checkpoint performed with IO limit: 789 IOPS, paused in total 0 times(0 millis). Average checkpoint flush speed: 592.0KiB/s.
-
当
db.checkpoint=continuous时的检查点2023-05-28 13:17:21.927+0000 INFO [o.n.k.i.t.l.c.CheckPointerImpl] Checkpoint triggered by "Scheduled checkpoint for continuous threshold" @ txId: 171 checkpoint started... 2023-05-28 13:17:21.941+0000 INFO [o.n.k.i.t.l.c.CheckPointerImpl] Checkpoint triggered by "Scheduled checkpoint for continuous threshold" @ txId: 171 checkpoint completed in 13ms. Checkpoint flushed 74 pages (7% of total available pages), in 58 IOs. Checkpoint performed with IO limit: 789 IOPS, paused in total 0 times(0 millis). Average checkpoint flush speed: 592.0KiB/s.
-
作为数据库关闭结果的检查点
2023-05-28 12:35:56.272+0000 INFO [o.n.k.i.t.l.c.CheckPointerImpl] Checkpoint triggered by "Database shutdown" @ txId: 47 checkpoint started... 2023-05-28 12:35:56.306+0000 INFO [o.n.k.i.t.l.c.CheckPointerImpl] Checkpoint triggered by "Database shutdown" @ txId: 47 checkpoint completed in 34ms. Checkpoint flushed 74 pages (7% of total available pages), in 58 IOs. Checkpoint performed with IO limit: 789 IOPS, paused in total 0 times(0 millis). Average checkpoint flush speed: 592.0KiB/s.
-
作为
CALL db.checkpoint()结果的检查点2023-05-28 12:31:56.463+0000 INFO [o.n.k.i.t.l.c.CheckPointerImpl] Checkpoint triggered by "Call to db.checkpoint() procedure" @ txId: 47 checkpoint started... 2023-05-28 12:31:56.490+0000 INFO [o.n.k.i.t.l.c.CheckPointerImpl] Checkpoint triggered by "Call to db.checkpoint() procedure" @ txId: 47 checkpoint completed in 27ms. Checkpoint flushed 74 pages (7% of total available pages), in 58 IOs. Checkpoint performed with IO limit: 789 IOPS, paused in total 0 times(0 millis). Average checkpoint flush speed: 592.0KiB/s.
-
作为备份运行结果的检查点
2023-05-28 12:33:30.489+0000 INFO [o.n.k.i.t.l.c.CheckPointerImpl] Checkpoint triggered by "Full backup" @ txId: 47 checkpoint started... 2023-05-28 12:33:30.509+0000 INFO [o.n.k.i.t.l.c.CheckPointerImpl] Checkpoint triggered by "Full backup" @ txId: 47 checkpoint completed in 20ms. Checkpoint flushed 74 pages (7% of total available pages), in 58 IOs. Checkpoint performed with IO limit: 789 IOPS, paused in total 0 times(0 millis). Average checkpoint flush speed: 592.0KiB/s.
检查点指标 也是可用的,并详细记录在 metrics/ 目录下的文件中
neo4j.check_point.duration.csv neo4j.check_point.total_time.csv neo4j.check_point.events.csv