Size RETENTION, EXPIRE and memory

This guide chooses a scheduled hook's EXPIRE, its index's RETENTION and the drain batch size, so that late events count, buckets are not lost, and reads find what they need. It then sizes the memory they take, in Redis and in featuremeshd.

Before you start, know for each hook:

  • how late your producers can send an event, and the longest outage you want to survive without losing a bucket;
  • what reads each index: serving at request time, a decision hook, or a scheduled hook;
  • the drain period, EVERY ( WITH ).

The tutorials set EXPIRE long, because their records are in the past ( Records dated in the past ). In production, EXPIRE is how late an event may be and still count, and how long an outage may last without losing a bucket.

1. Set EXPIRE

A drain runs an entry when its time T, on the queue store's clock, is at most the entry's due time plus EXPIRE, and drops it otherwise ( Queues and drains ). With a due time of 10:01:10 and EXPIRE = INTERVAL '2 MINUTE', a drain at 10:03:10 runs it, and one at 10:03:11 drops it.

Everything before the drain counts against EXPIRE: the consumer's lag, the drain period, a backlog, a drain retried after a failure. A payment that arrives late queues its bucket again with the bucket's own due time, already past: it runs at the next drain if it is within EXPIRE, and is dropped otherwise. So set EXPIRE above the sum of:

  • the lateness you accept from producers;
  • the consumer lag you recover from;
  • the longest outage of a store or of featuremeshd you ride out;
  • the drain period, EVERY, plus a drain's own duration.

Keep EVERY well below EXPIRE. A period close to EXPIRE lets entries expire before a drain runs them.

2. Size a chain of rollups

In Buckets per key , each minute run schedules its hour. A late payment then schedules its hour only through a minute run. When its minute's entry is dropped, it schedules no hour run, although an hour run scheduled by another minute still reads it from the index.

A minute run can come as late as the minute's due + EXPIRE, and it queues the hour with the hour's own due time, already past by then. For the 10:59 minute, due is 11:00:10. With a minute EXPIRE of 10 minutes, its last run can come at 11:10:10, and it queues the 10:00 hour, due at 11:05:00 (HOUR_CLOSE, 65 minutes after the hour starts). The hour's EXPIRE must cover that: above 5 minutes 10 seconds, plus the hour drain's period and lag.

In general:

  • The hour's EXPIRE is at least the minute's last due time, plus the minute's EXPIRE, minus the hour's due time, plus the hour drain's period and lag.
  • The index's RETENTION is then above 1 hour, plus 5 minutes, plus the hour's EXPIRE, plus the margin.

To skip the chain, schedule the hour straight from each payment, as Long horizons does.

3. Set each index's RETENTION by its reader

An index is trimmed on the store's clock, not on the reader's time ( Retention ). Size it for the reader that looks furthest back.

Read at request time

For a 10-minute window read at T while Redis's clock reads S, keep RETENTION above 10 minutes + (S - T), plus a margin for clock differences. A read as of an hour ago needs an hour more.

Read by a decision hook

A decision hook reads the index as of the payment's own time. When the hook runs late (consumer lag, an outage, a replay), payments older than RETENTION are trimmed as they are written. The decision then counts too few payments, and replaces the stored one. Keep the index's RETENTION above the window plus the longest lag you recover from, and raise it before a replay ( Replay a topic ).

Read by a scheduled hook

A run reads the index in the same drain, so its time is at most due + EXPIRE, plus the batch's own duration. For minute buckets with a 10-second close delay, keep RETENTION above the bucket's length, plus 10 seconds, plus EXPIRE, plus a margin for the batch and for the difference between the queue store's clock and Redis's. A shorter RETENTION makes a late run read an index already trimmed, and undercount.

In general, a run reads the index back to the start of its window, as late as its due time plus EXPIRE. Keep the index's RETENTION above the window's length, plus the delay between the window's end and its due time, plus EXPIRE, plus the time between the drain and the read, plus any clock difference between the queue's store and the index's.

4. Size a long horizon

For a sum that reads hour buckets and the index ( Long horizons ):

  • The tolerance is the contract with the rollup. A rollup behind by more than the tolerance (an outage, a backlog) undercounts. Pick a tolerance above the delay before a bucket is first due (5 minutes past the hour, HOUR_CLOSE, in the tutorial), plus the drain's usual lag. The tutorial's 10 minutes leave 5 for the drain to pick the bucket up and write it.
  • Hours and days are UTC. Buckets per local day are 23 or 25 hours long on a daylight-saving change: Local-day buckets .
  • Coarser buckets for longer windows trade the far edge's exactness. For 90 days, read day buckets for the whole days, hour buckets for the edge days, and the index only for the hours the hour buckets do not cover yet. The index then keeps about a day plus EXPIRE instead of 90 days. The window starts at a whole hour, so its far edge, 90 days (2,160 hours) back, moves by less than an hour, and counts or misses the payments of that part of an hour. Its near edge is exact at the request. Minute buckets for the edge hour narrow the far edge to less than a minute. Only the index, kept for the whole window, makes the far edge exact to the event.
  • Shorten the index's RETENTION only once the coarser buckets exist for the days it drops. Keep it above what the buckets computed from it read: about a day plus EXPIRE, plus the margin.

5. Size drain batches

A drain batch runs up to drain_batch_max calls, 1,000 by default ( serving.hooks ). A heavy call, with many reads, a long history or a model, can make a batch last minutes. The drainer renews its lease while the batch runs ( Leases ). A batch that fails is computed again whole, and its entries wait for it.

last_drain in SHOW SERVING STATUS gives the last drain's references and duration: 3,000 references at a drain_batch_max of 1,000 ran three batches. Lower drain_batch_max when a batch takes longer than you want an entry to wait.

6. Size Redis memory for an index

An index is as durable as its Redis, which runs with eviction off ( Prepare the stores ). Its memory then has to hold every member inside RETENTION.

Memory is about events per second × RETENTION in seconds × bytes per event, plus a per-key overhead, with room for the busiest keys. Bytes per event cover the sorted set's member and its entry in <key>:ids. Measure them on your own records. Write a sample, then divide MEMORY USAGE of a key and of its :ids by the members it holds.

redis-cli MEMORY USAGE 'shop:pay:{7}'                      # then divide by: redis-cli ZCARD 'shop:pay:{7}'
redis-cli MEMORY USAGE 'shop:pay:{7}:ids'
bash

What drives the count of members:

  • An index holds every event inside RETENTION. A long horizon that reads buckets changes how many members a request fetches, not how many the index stores. In Long horizons , the oldest members a request reads are those of the partial first hour, just under seven days old. The index keeps seven days (168 hours) of events, plus a margin for the difference between Redis's clock, which trims, and the request's time (§ 3).
  • A sink keyed by event holds one key per event. A decision per payment holds one key per payment for its RETENTION. A production decision store keeps days.

A replay after a long stop writes at the store's full speed. Keep room under maxmemory for it.

7. Size featuremeshd's memory

featuremeshd's engine has one memory pool, 1,024 MiB unless FEATUREMESHD_MEMORY_LIMIT_MB (the chart's memoryLimitMb) says otherwise ( Configure featuremeshd ). Serving calls, hook batches and view runs of that process share it.

  • Hooks. A batch holds up to batch_max_records records, and a drain batch up to drain_batch_max calls, with what their sources return. Measure a hooks pod's memory while it catches up on a backlog, which fills every batch.
  • Views. Any featuremeshd with hooks enabled can run any view, so each one needs the memory of the largest view run beside its hooks. How to measure a run is in View memory .
  • The container. Keep memoryLimitMb under the container's memory limit, with room for the process itself.

Check the settings

Watch these after a deployment and after each outage ( Monitor and recover ):

  • entries_expired stays at zero. Each one is a bucket not written, or a late correction lost.
  • oldest_due_age_seconds stays well under EXPIRE. Its peak during an outage is how close you came to losing buckets.

Common failures

  • Buckets missing after an outage: the outage, plus the drain period, outlasted EXPIRE. Raise EXPIRE, and rebuild the buckets by replaying the topic from before the outage ( Start, backfill and replay ).
  • Hour buckets missing while minute buckets are there: the chained hour's EXPIRE is too short for the minute's last run (§ 2).
  • Index members or buckets missing without an error: Redis evicted them, or ran out of memory. Size it (§ 6), set noeviction, then replay the topic.
  • A backfill dropped every bucket: a production EXPIRE against records days old. Raise it for the backfill ( Backfill a hook that feeds scheduled hooks ).

Related