Skip to content

Time to live

fdyno hides an expired item as soon as the local clock, less a skew margin, passes its TTL. A background sweeper deletes it later. Base-table reads, reads of an index that projects the TTL attribute, conditions and old images in write responses skip the expired item; other index reads return it until it is deleted, as DynamoDB does. The TTL stream record can include it.

Step What happens Reads see it Stored row A successful write that reads the row
Write (TTL = T) The item and its expiry entry t/<floor(T/60)>/<key> commit in one transaction. Pictured: TTL is on; the item is inserted with a future T below 2^40 s; no later write refreshes or replaces it; the drain comes before the threshold passes T + 157,680,000 s. yes stored
Threshold passes T Reads, conditions and write-response old images skip the item. no stored deletes it first, in the write's own transaction
Threshold reaches the next minute The entry's bucket, the minute of T, is now below the minute of the threshold, so the entry becomes eligible for the sweeper. no stored deletes it first, in the write's own transaction
Sweeper drain A later drain re-reads the item and deletes it if still expired (REMOVE if Streams on). no deleted if still expired

The expiry threshold, THRESHOLD, is the local clock minus DYNODB_TTL_SKEW_MARGIN (one minute by default). See Compatibility for how this differs from DynamoDB, which can return expired items until it deletes them.

Which items expire

itemTTLExpired decides for every read, write and drain.

Item Expires?
TTL attribute is a Number of epoch seconds and THRESHOLD - 157,680,000 <= TTL < THRESHOLD Yes
TTL more than five years (157,680,000 s) before the threshold No; it is ignored
TTL attribute missing, not a Number, or not parseable No
TTL disabled on the table No; nothing is hidden while TTL is off

Here THRESHOLD is the whole Unix second of the skew-adjusted clock; the TTL Number can be fractional. The lower bound matches the DynamoDB rule that a TTL can't be more than five years in the past. The window moves with the clock, so hiding is not permanent. An expired row that the sweeper has not deleted is hidden while the threshold is after TTL and at most TTL + 157,680,000. Once the threshold passes TTL + 157,680,000, reads return it again, with no write and no sweep. A migration or audit must not treat one absent read as proof that the row is gone.

The expiry index keeps an entry only for a TTL of at least 0 and below 2^40 seconds. UpdateTimeToLive applies at once: there is no ENABLING state and no one-hour wait between changes.

One threshold per request scope

Each scope below reads the clock once and uses that threshold for every expiry decision in it. Two items in one scope cannot disagree about the time. A new scope reads the clock again.

Operation One threshold per
GetItem Attempt (one item; a retry can read the clock again)
Query, Scan Request (one page)
BatchGetItem Request, including its fallback path and retries
PartiQL SELECT Page, including index lookups and retries
PutItem, UpdateItem, DeleteItem, PartiQL INSERT, UPDATE, DELETE Attempt
BatchWriteItem Item whose old image was read, per attempt
TransactWriteItems Attempt
TransactGetItems, ExecuteTransaction Attempt
Sweeper drain transaction Attempt: the same threshold picks eligible buckets and re-checks each item
dynodb verify table <name> --ttl-index Audit (ttl_evaluation_time)

Clock synchronization gives the clock rules.

A write to an expired item

A successful write that reads the stored row applies the threshold. If the row is expired, the write treats the item as absent for its condition, returned old image and update base. In the same transaction, it first deletes the expired row with its index entries. It emits a TTL REMOVE if Streams is enabled. Then it applies its own write (writeExpiryDelete).

With Streams enabled, a PutItem that reclaims the row emits REMOVE, then INSERT (streams). Reads alone, failed conditions and no-op write paths do not reclaim the row. GetItem never writes: an expired row stays stored until the sweeper or the next write removes it.

The expiry index

A write to an item of a TTL-enabled table maintains its expiry entry in the item's own transaction (ttl_index.go). An image has an entry key only if it has an indexable TTL.

  • Entry key changes. The write compares a live old image and the new image. It clears the old image's entry, if it had one, and sets the new image's, if it has one.
  • Same entry key. The write changes nothing in the index. It does not check that the entry exists, so it does not repair a missing entry.
  • Blind put. It sets only the new item's entry, if it has an indexable TTL. It does not know the old image, so it does not clear an old entry. The drain discards that stale entry when its bucket comes due, not at once.
  • Replacement after reclaiming an expired row. It clears the expired row's entry and sets the replacement's, if it has one.

A blind put and a replacement can recreate a missing entry even in the same bucket.

To recreate missing entries, disable and re-enable TTL with UpdateTimeToLive on the same attribute. This restarts backfill, so the sweeper can find stranded rows. Expect a table scan. If expired rows must stay hidden, block application reads before disabling TTL and keep them blocked until TTL is enabled again.

An entry is the key <table>/t/<bucket>/<partition key>[/<sort key>] with an empty value, where bucket = floor(TTL seconds / 60).

The sweeper reads these entries in bucket order instead of scanning the table. The index is advisory, and only one direction is safe:

Entry Effect
Stale: the item was deleted, refreshed or written blind without its old image Harmless. The drain re-reads the item, deletes it only if it is still expired, and clears the entry.
Missing The row can stay stored if no other entry reaches it: it is hidden for five years after its TTL, then visible again (see which items expire). A stale entry for the same key in another bucket can still reach it, because the drain tests the item's current TTL, not the bucket. A TTL-enabled write that moves the entry adds the one for its new image; a write that compares a live old image and the new image with the same entry key does not repair a missing one.

Rows written before TTL was enabled have no entries. UpdateTimeToLive enabling TTL resets the table's backfill state (<table>/ts) in the same transaction. The coordinator then walks the table in pages and adds the missing entries. The pass is complete once a full wrap from the table start finds nothing to add. A contended page gets up to four attempts, then waits for the next pass. The 1,000-row page target halves under contention, down to one row.

The backfill gets at most half of a pass and tries one page per table. The next pass starts after the last table whose page it attempted. A throttled attempt does not count. So tables that sort first cannot use the whole budget in every pass.

This position is in memory in each process. A new process starts at the first table. A process that becomes coordinator again resumes after its own last table.

The drain gets the tables in name order and rotates them by its own position per range (see Table order in the sweeper).

A one-row page can fail on all four attempts. Usually clients are writing that row continuously; a recovery or a metadata or state change also retries an attempt. When all four fail, the coordinator moves the cursor past the row without reading it again (fdyno_ttl_backfill_rows_skipped_total), so the rows after it get their entries. The move is refused if the cursor changed in between, or if the table was deleted and created again under the same name.

While a row is skipped in each wrap, the backfill never completes. In that case:

  • Each pass still costs at most one page of up to four attempts per table.
  • The TTL sweeper pass counts as incomplete, so it can escalate to default priority.
  • dynodb verify table --ttl-index reports missing entries as ttl_due_pending_backfill, not ttl_due_missing_entry.

After the writes stop, a later wrap that reads every row without adding an entry completes. The durable key inventory lists both keys.

The sweeper

Every process runs the sweeper every DYNODB_TTL_SWEEP_INTERVAL (one minute by default). A pass has half the interval. It ends earlier when no eligible entries remain. It normally runs at FoundationDB batch priority, so cluster load, not a fixed count, sets its rate. An incomplete-pass bound can raise the priority (worker rules) (ttl_ranges.go).

Lease Work
ttl-sweeper, one process Backfill pass and registry pruning; the range plan per table; the backlog age gauge
ttl-sweeper/range-NN, any process Take a free range lease, drain that range of every table, release it, take the next free range
Part Rule
Tables Read from the TTL catalog registry; a table that is gone or has TTL off is pruned.
Ranges Up to 16 per table. Bounds come from FoundationDB's split points of the table's items, so ranges hold similar key shares. A table estimated below 4 MiB is one range.
Range plans Advisory. Eligible for recomputation at 10 minutes old; at most 32 per pass. A malformed or unknown plan reads as one range.
Concurrency Up to four ranges at a time per process; each range drain holds its own lease.
Table order A range drain gives each table one transaction per round. Each process remembers, per range, the last table whose drain batch it attempted, or whose bucket lookup failed or found no eligible bucket, and starts its next pass with the table after it. So a pass that the deadline ends in its first round does not leave the same tables out every time. A successful bucket lookup with no delete reservation does not advance the saved position. The next pass starts at the first current table after the saved name, wrapping at the end. Acquiring the range lease alone does not move the position. The position is in memory and per process; a restarted process starts at the first table. This is no fairness bound across processes that compete for ranges or restart.
Drain transaction Reads up to 500 index entries and stops once it has reclaimed about 1 MiB of item bytes; the last item can cross that target. Only buckets whose minute has closed by the threshold are eligible.
Delete Re-reads the item; if still expired, deletes the base row and its index entries, and writes a stream REMOVE (Streams on) and an undo record (PITR on). Clears each processed entry.
Failing row If a drain transaction fails with an error of one row (reading or deleting it), not of FoundationDB, the lease or the table, the drain halves its batch until that row fails alone. It then leaves that row and its entry in place and reads past it for the rest of the pass, at most 16 rows per table and range per pass. Nothing is cleared, and the position is not stored: a skipped entry stays eligible, and a later pass that reaches it tries it again. The deadline, a peer holding the range or the 16-skip limit can keep a pass from reaching it. fdyno_ttl_drain_entries_skipped_total{table} counts skips.
GSI log behind A delete writes a GSI change record, so when the table's GSI log is more than 5 s behind, the drain gets the GSI backlog throttle. No entry is at fault: the drain leaves that table's range for a later pass, with no skip and no worker failure. The pass still counts as complete, because default priority cannot clear a GSI backlog; fdyno_ttl_oldest_eligible_age_seconds still shows the table's wait.

Ranges only spread the work. Safety never depends on them: two processes with different plans cost conflicts or a later pass, never a wrong delete. Worker limits and how throughput scales are in background worker ceilings; measured reclaim rates on EC2 are in Performance.

The skew margin

Every TTL decision uses one clock: the process's wall clock minus DYNODB_TTL_SKEW_MARGIN (ttlNow in clock.go). Reads, conditions, write-response old images, the drain and restore verification all use it. The margin moves expiry later; it never moves it earlier.

Worked example. The inputs: a TTL of 12:00:30 UTC (a whole second), the default margin of 60 s, and four processes whose clocks differ from true time.

Process clock Hides the item from (true time) Early?
Exact 12:01:31 No: 61 s late
45 s ahead 12:00:46 No: 16 s late
90 s ahead 12:00:01 Yes: 29 s early
20 s behind 12:01:51 No: 81 s late

The rule behind each row: the threshold is the whole Unix second of the clock minus the margin. An item is expired when its TTL is below the threshold (itemTTLExpired). So the exact clock hides the item once its threshold reaches 12:00:31, at 12:01:31. A process ahead by a hides it a earlier in true time.

What the example shows:

  • Within the margin, never early. A clock ahead by less than the margin hides the item after its TTL. A clock ahead by more than the margin can hide it, and the drain can delete it, before its TTL.
  • Late by the margin plus the lag. Even an exact clock shows the item for the margin after its TTL. It adds up to 1 s, because the threshold is a whole second and the comparison is strict. A clock behind adds its lag. DynamoDB also can return an expired item until it deletes it.
  • Processes disagree. Each process uses its own clock. Take a client that reads at 12:01:00. Through the 45 s-ahead process it gets no item. Through the exact process at the same moment, it gets the item.
  • Deletion comes later. The expiry entry sits in the bucket of the TTL's minute (12:00). A drain takes a bucket only when it is below the minute of the threshold. So with the exact clock, the entry becomes eligible at 12:02:00 (ttl_ranges.go). The drain uses the same threshold sample to select the bucket and to test the current item in that transaction. A backward clock step between those checks cannot make the drain keep a still-matching, recent item while it clears that item's entry. See the bounded drain model.

Setting it. The default is 1m. In production mode, a value below 1m refuses to start. Outside production, any value of 0 or more is accepted. A negative or invalid value logs a warning and keeps 1m (config_production.go, config.go). Set it above the largest clock difference between any process and true time. A larger margin keeps expired items visible longer. Stream and PITR trimming use their own margin, DYNODB_CDC_TRIM_SKEW_MARGIN.

Clock steps

TTL decisions use each process's own clock. A clock ahead by more than the margin can hide or delete items early; a clock behind keeps them visible longer. A backward step after a hide can make the item visible again until the sweeper deletes it. Keep clocks slewed, not stepped, within the margin. Clock synchronization has the exact rules.

Check the index

dynodb verify table <name> --ttl-index reads the table and its expiry index in pages with one evaluation time; it is a rolling audit, not one storage snapshot. It reports stale, invalid and missing entries. It also reports two counts:

  • ttl_due_missing_entry: a due row without its entry after backfill completed;
  • ttl_due_pending_backfill: the same while backfill is still open.

A stale entry in another bucket can still let the sweeper reach a row counted as missing. See Verify stored data for the exact meaning, read cost and flags.

While the sweeper is behind

An expired row stays stored until the sweeper or a write removes it. Physical deletion has no deadline. Until then, each surface treats the row as follows:

Surface An expired row that is still stored
GetItem, BatchGetItem, TransactGetItems Absent.
Query, Scan and a PartiQL SELECT on the table Absent. It does not count toward Limit, Count, ScannedCount (Query and Scan; PartiQL reports neither) or consumed capacity. It does count toward the page's read budget, so a backlog of expired rows can produce empty pages with a LastEvaluatedKey (PartiQL: NextToken). Follow it.
Query, Scan or a PartiQL SELECT on a GSI or LSI that projects the TTL attribute (ALL, or INCLUDE with it) Absent, judged on the index entry's own TTL value. The index entry stays until the row is deleted.
Query, Scan or a PartiQL SELECT on a KEYS_ONLY index, or an INCLUDE index without the TTL attribute Returned until the sweeper or a write deletes the row, as in DynamoDB, which returns expired items that are not yet deleted. The read never reads the base item to check expiry. Project the TTL attribute into an index that must hide expired items. An LSI read that fetches attributes from the base item (a Query or Scan with Select ALL_ATTRIBUTES, or a SELECT of an unprojected attribute) reads the base row, so it skips the expired item as a base read does.
Conditions, ReturnValues old images, update base The item is absent. An UpdateItem that sets a future TTL on an expired item starts from an empty item with its key; it does not keep the old attributes.
Streams No record yet. The TTL REMOVE appears when the sweeper or a later write deletes the row.
DescribeTable ItemCount, TableSizeBytes Count the row.
Backups and full exports Copy the row. A restored table applies the TTL setting again, so the row is hidden there too, but stored.
FoundationDB storage The row, its index entries and its expiry-index entry.

This differs from DynamoDB. DynamoDB still returns expired items that TTL has not yet deleted in reads. It lets an update give such an item a future TTL, so that TTL no longer deletes it. In fdyno, the read hides the item, and the update replaces it.

See ttl.go (itemTTLExpired, writeExpiryDelete), query_ops.go (the Scan and Query accept checks), export_cut.go, backup_generations.go and table_size_stats.go.

Watch it

Signal Meaning
fdyno_ttl_oldest_eligible_age_seconds Threshold minus the start of the oldest eligible bucket across tables. Not the delay of any one item.
fdyno_ttl_oldest_eligible_valid 1 when every TTL table's oldest bucket was checked.
fdyno_ttl_items_expired_total Items this process deleted.
fdyno_ttl_tick_budget_exhausted_total Passes that used their whole time budget.

The full list is under TTL sweeper. If the backlog grows, follow a worker falls behind.

How we know

Check What it shows
TTL scene in deterministic simulation Two processes with skewed clocks, writes with past, near, future, absent and non-numeric TTLs, backfill and multi-range plans. After every step: no early delete, no early hide, no resurrection; at the end, every expiring item is gone. The ttl-margin and ttl-clock negative controls must fail.
-ttl-clock-step scene A backward step during a drain: the committed attempt deletes the due row and keeps the future one indexed; a retry samples the new clock. The ttl-clock-reread control strands a row, and the audit and the disable/re-enable repair find and remove it.
-ttl-refresh scene in the Go simulation A blind refresh leaves two entries for one row; the expiry emits exactly one REMOVE and one undo per deleted row.
Upstream suites TTL cases from Alternator, ExtendDB and Nubo.

Not guaranteed

  • Physical deletion has no deadline. An expired item can stay stored, hidden, while the sweeper is behind.
  • Visibility is only as exact as the clocks. Within the margin, no item expires early; an item can stay visible for the margin plus the clock lag.