Retry failed writes
Retry a failed batch with backoff, resend what was never acknowledged, and keep every entry in the store exactly once with keyed writes.
A write to a database fails now and then, for example when a connection resets. Batch writes lets such a failure fail the journal, which loses the entries it was holding. On this page no entry is lost. The journal retries a failed batch with a growing delay. When it gives up, its supervision restarts it, and the producer which sent the entries resends what was never acknowledged. tellus delivers messages at most once and leaves acknowledgments and redelivery to protocols built from messages, as Message delivery explains. The producer and the journal form such a protocol.
Retrying raises a question: was the failed write committed? A connection can drop after the database has written the rows and before the client has learned of it. After such an ambiguous commit, a retry writes the rows a second time, unless the write is keyed. Every entry carries a sequence number, and the store keeps one row per number. A repeated write hence changes nothing.
The examples on this page come from a producer which sends 2000 numbered entries to a journal. The journal writes them in batches to a store stand-in. Writes fail in scripted ways. This page builds on Batch writes and assumes you know the getting started tutorial.
The journal retries through a timer
struct Journal {
store: Store,
backoff: Backoff,
}
impl Actor for Journal {
type Message = JournalMessage;
type Timers = Timed;
type Stash = Unstashed;
type State = Buffer;
type Error = WriteFailed;
fn init(
&self,
_: &ActorContext<Self::Message, Self::Timers>,
) -> Result<Self::State, Self::Error> {
println!("Journal started");
Ok(Buffer {
pending: Vec::new(),
in_flight: Vec::new(),
attempts: 0,
})
}
fn receive(
&self,
context: &ActorContext<Self::Message, Self::Timers>,
incoming: Incoming<Self::Message>,
mut buffer: Self::State,
) -> Result<Control<Self::State>, Self::Error> {
let Incoming::Message(message) = incoming else {
unreachable!("the journal watches no actor, hence never gets a terminated signal")
};
match message {
JournalMessage::Append {
seq,
text,
reply_to,
} => buffer.pending.push(Entry {
seq,
text,
reply_to,
}),
JournalMessage::Written(result) => match result? {
Ok(()) => {
for entry in buffer.in_flight.drain(..) {
entry.reply_to.reply(());
}
}
Err(error) if buffer.attempts < MAX_ATTEMPTS => {
let delay = self.retry_delay(buffer.attempts);
println!(
"Retrying a batch of {} entries in {} ms: {error}",
buffer.in_flight.len(),
delay.as_millis()
);
context.start_timer("retry", JournalMessage::Retry, delay);
}
Err(error) => {
println!(
"Giving up on a batch of {} entries after {} attempts: {error}",
buffer.in_flight.len(),
buffer.attempts
);
return Err(WriteFailed::GaveUp {
attempts: buffer.attempts,
error,
});
}
},
JournalMessage::Retry => buffer.write_in_flight(self, context),
}
buffer.write_if_due(self, context);
Ok(Control::Continue(buffer))
}
}
impl Journal {
fn retry_delay(&self, attempts: u32) -> Duration {
let factor = 1u32.checked_shl(attempts - 1).unwrap_or(u32::MAX);
self.backoff
.min()
.saturating_mul(factor)
.min(self.backoff.max())
}
}
enum JournalMessage {
Append {
seq: u64,
text: String,
reply_to: ReplyTo<()>,
},
Written(Result<Result<(), ConnectionReset>, JoinError>),
Retry,
}
#[derive(Debug, Error)]
enum WriteFailed {
#[error("writing a batch failed after {attempts} attempts")]
GaveUp {
attempts: u32,
#[source]
error: ConnectionReset,
},
#[error("writing a batch panicked")]
Panicked(#[from] JoinError),
}
struct Buffer {
pending: Vec<Entry>,
in_flight: Vec<Entry>,
attempts: u32,
}
impl Buffer {
fn write_if_due(&mut self, journal: &Journal, context: &ActorContext<JournalMessage, Timed>) {
if self.in_flight.is_empty()
&& !self.pending.is_empty()
&& (context.queued() == 0 || self.pending.len() >= MAX_BATCH)
{
let len = self.pending.len().min(MAX_BATCH);
self.in_flight = self.pending.drain(..len).collect();
self.attempts = 0;
self.write_in_flight(journal, context);
}
}
fn write_in_flight(
&mut self,
journal: &Journal,
context: &ActorContext<JournalMessage, Timed>,
) {
self.attempts += 1;
let batch = self
.in_flight
.iter()
.map(|entry| (entry.seq, entry.text.clone()))
.collect::<Vec<_>>();
context.pipe_to_self(
task::spawn(write(journal.store.clone(), batch)),
JournalMessage::Written,
);
}
}
struct Entry {
seq: u64,
text: String,
reply_to: ReplyTo<()>,
}The journal builds on the one of Batch writes. It writes one batch at a time with pipe_to_self, and write_if_due starts a write once the mailbox has run empty or MAX_BATCH entries, 100, are buffered. New entries wait in pending. The batch being written stays in in_flight until its write has succeeded, so a failed batch can be written again. Each Entry keeps its ReplyTo meanwhile, and the write gets a copy of the sequence numbers and texts.
An Append carries the entry's sequence number, its text and a ReplyTo for the acknowledgment. The journal is Timed, since it retries through a timer, and Retry is the timer's message. Unlike the journal of Batch writes, it has no Close message. The producer stops once every entry is acknowledged, and the journal stops with it as its child. At that point every entry is in the store, so stopping the journal loses nothing.
JournalMessage::Written(result) => match result? {
Ok(()) => {
for entry in buffer.in_flight.drain(..) {
entry.reply_to.reply(());
}
}
Err(error) if buffer.attempts < MAX_ATTEMPTS => {
let delay = self.retry_delay(buffer.attempts);
println!(
"Retrying a batch of {} entries in {} ms: {error}",
buffer.in_flight.len(),
delay.as_millis()
);
context.start_timer("retry", JournalMessage::Retry, delay);
}
Err(error) => {
println!(
"Giving up on a batch of {} entries after {} attempts: {error}",
buffer.in_flight.len(),
buffer.attempts
);
return Err(WriteFailed::GaveUp {
attempts: buffer.attempts,
error,
});
}
},A successful write acknowledges every entry of the batch through the ReplyTo of its Append. A failed write starts a timer under the key retry, and the timer's Retry writes the same batch again. The entries which arrive meanwhile wait in pending, since write_if_due starts no batch while one is in flight. So a retry never lets a later batch overtake the failed one. After MAX_ATTEMPTS failed attempts, 3 in the example, the journal gives up and fails with WriteFailed::GaveUp. A write which panicked is not retried: result? fails the journal with WriteFailed::Panicked.
The delay comes from retry_delay, which doubles it with every attempt, starting at the minimum of the journal's Backoff and capped at its maximum. main builds that Backoff from BACKOFF_MIN, 10 ms, and BACKOFF_MAX, 80 ms. So the journal waits 10 ms before the second attempt and 20 ms before the third. The producer gives the same Backoff to the journal's RestartPolicy. A restart of the journal hence waits 10 ms first and twice as long for every further consecutive restart, up to 80 ms. Backoff hands out only its bounds, so retry_delay computes the same curve for the retries itself.
Why a timer and not a sleep inside the piped future? A restart cancels an actor's timers, and termination drops them. Neither cancels a future the actor has piped. A retry which sleeps inside a piped future would hence still write after the journal has stopped. After a restart, its Written would reach a new state which never started that write.
The keyed write
#[derive(Clone, Default)]
struct Store(Arc<Mutex<Table>>);
impl Store {
fn table(&self) -> MutexGuard<'_, Table> {
self.0.lock().expect("the table lock is not poisoned")
}
}
#[derive(Default)]
struct Table {
rows: BTreeMap<u64, String>,
writes: u64,
skipped: u64,
reset_before_write: bool,
reset_after_write: bool,
give_up_failures: u32,
}
impl Table {
fn write(&mut self, batch: &[(u64, String)]) -> Result<(), ConnectionReset> {
let contains = |wanted| batch.iter().any(|(seq, _)| *seq == wanted);
if contains(RESET_BEFORE_WRITE_SEQ) && !self.reset_before_write {
self.reset_before_write = true;
return Err(ConnectionReset);
}
if contains(GIVE_UP_SEQ) && self.give_up_failures < MAX_ATTEMPTS {
self.give_up_failures += 1;
return Err(ConnectionReset);
}
let mut skipped = 0;
for (seq, text) in batch {
match self.rows.entry(*seq) {
MapEntry::Vacant(vacant) => {
vacant.insert(text.clone());
}
MapEntry::Occupied(_) => skipped += 1,
}
}
self.writes += 1;
self.skipped += skipped;
if skipped > 0 {
println!("Skipped {skipped} duplicates");
}
if contains(RESET_AFTER_WRITE_SEQ) && !self.reset_after_write {
self.reset_after_write = true;
return Err(ConnectionReset);
}
Ok(())
}
}
#[derive(Debug, Clone, Copy, Error)]
#[error("connection reset")]
struct ConnectionReset;
async fn write(store: Store, batch: Vec<(u64, String)>) -> Result<(), ConnectionReset> {
sleep(WRITE_LATENCY).await;
store.table().write(&batch)
}The store stand-in is a Table behind a lock, which main creates and the producer hands on to the journal. Its rows map a sequence number to the entry's text. A write inserts every row whose key is new and counts the rows it skips. writes counts the writes which reached the rows. Every write first sleeps for WRITE_LATENCY, 2 ms. In PostgreSQL, for example, the write is an INSERT ... ON CONFLICT DO NOTHING, and the error type is the database client's.
The failures are scripted by the entries a batch holds. The batch holding entry 301 (RESET_BEFORE_WRITE_SEQ) fails once before writing. The batch holding entry 901 (RESET_AFTER_WRITE_SEQ) writes its rows and then fails once, which is the ambiguous commit. The batch holding entry 1501 (GIVE_UP_SEQ) fails MAX_ATTEMPTS times before writing, so the journal gives up on it. Its next write, after the resend, succeeds.
The journal holds the store in its actor value, not in its state. A restart keeps the actor value, so the restarted journal writes to the same table. Whether a retry can help depends on the error. The example retries every ConnectionReset. An error which is permanent, such as a constraint violation, fails every attempt the same way. Retrying it only delays the failure, so such an error should fail the journal at once. The producer resends the same entries, so the error still stops the journal for good once its restarts are used up.
The producer keeps a window
struct Producer {
store: Store,
backoff: Backoff,
}
impl Actor for Producer {
type Message = ProducerMessage;
type Timers = Timed;
type Stash = Unstashed;
type State = Sending;
type Error = JournalStopped;
fn init(
&self,
context: &ActorContext<Self::Message, Self::Timers>,
) -> Result<Self::State, Self::Error> {
let policy = RestartPolicy::new(MAX_RESTARTS).with_backoff(self.backoff);
let config =
ActorConfig::default().with_supervision_strategy(SupervisionStrategy::Restart(policy));
let journal = context.spawn_with_config(
Journal {
store: self.store.clone(),
backoff: self.backoff,
},
config,
);
context.watch(&journal);
let mut sending = Sending {
journal,
next: 1,
open: VecDeque::new(),
};
sending.fill_window(context);
Ok(sending)
}
fn receive(
&self,
context: &ActorContext<Self::Message, Self::Timers>,
incoming: Incoming<Self::Message>,
mut sending: Self::State,
) -> Result<Control<Self::State>, Self::Error> {
match incoming {
Incoming::Message(ProducerMessage::Acked(seq)) => {
sending.acknowledge(seq, context);
sending.fill_window(context);
if sending.next > TOTAL && sending.open.is_empty() {
return Ok(Control::Stop);
}
}
Incoming::Message(ProducerMessage::AckTimeout) => sending.resend_open(context),
Incoming::Terminated(_) => {
return Err(JournalStopped {
open: sending.open.len(),
});
}
}
Ok(Control::Continue(sending))
}
}
enum ProducerMessage {
Acked(u64),
AckTimeout,
}
#[derive(Debug, Error)]
#[error("the journal stopped for good with {open} entries unacknowledged")]
struct JournalStopped {
open: usize,
}
struct Sending {
journal: ActorRef<JournalMessage>,
next: u64,
open: VecDeque<(u64, Instant)>,
}
impl Sending {
fn fill_window(&mut self, context: &ActorContext<ProducerMessage, Timed>) {
while self.open.len() < WINDOW && self.next <= TOTAL {
let seq = self.next;
self.next += 1;
self.append(seq, context);
self.open.push_back((seq, Instant::now()));
}
}
fn append(&self, seq: u64, context: &ActorContext<ProducerMessage, Timed>) {
if self.open.is_empty() {
context.start_timer("ack", ProducerMessage::AckTimeout, ACK_TIMEOUT);
}
self.journal.tell(JournalMessage::Append {
seq,
text: format!("entry {seq}"),
reply_to: context.reply_to(move |()| ProducerMessage::Acked(seq)),
});
}
fn acknowledge(&mut self, seq: u64, context: &ActorContext<ProducerMessage, Timed>) {
// A resent append is acknowledged twice!
let Some(position) = self.open.iter().position(|(open, _)| *open == seq) else {
return;
};
self.open.remove(position);
if position == 0 {
match self.open.front() {
Some((_, sent)) => {
let remaining = ACK_TIMEOUT.saturating_sub(sent.elapsed());
context.start_timer("ack", ProducerMessage::AckTimeout, remaining);
}
None => context.cancel_timer("ack"),
}
}
}
fn resend_open(&mut self, context: &ActorContext<ProducerMessage, Timed>) {
println!(
"Resending {} entries after {} ms without an ack",
self.open.len(),
ACK_TIMEOUT.as_millis()
);
let now = Instant::now();
for (seq, sent) in &mut self.open {
let seq = *seq;
self.journal.tell(JournalMessage::Append {
seq,
text: format!("entry {seq}"),
reply_to: context.reply_to(move |()| ProducerMessage::Acked(seq)),
});
*sent = now;
}
context.start_timer("ack", ProducerMessage::AckTimeout, ACK_TIMEOUT);
}
}The producer is the root of the actor system and the journal's parent. Its init spawns the journal with a Restart strategy, whose RestartPolicy allows MAX_RESTARTS consecutive restarts, 2 in the example. A journal which gave up hence starts over with an empty buffer. The producer also watches the journal, for the case that the journal stops for good (see below).
The producer's state, Sending, holds the journal's reference, the next sequence number and the open appends. Acked removes an append from the window and fills the window up again. Once all TOTAL entries, 2000, are acknowledged, the producer stops. AckTimeout resends the open appends.
fn fill_window(&mut self, context: &ActorContext<ProducerMessage, Timed>) {
while self.open.len() < WINDOW && self.next <= TOTAL {
let seq = self.next;
self.next += 1;
self.append(seq, context);
self.open.push_back((seq, Instant::now()));
}
}
fn append(&self, seq: u64, context: &ActorContext<ProducerMessage, Timed>) {
if self.open.is_empty() {
context.start_timer("ack", ProducerMessage::AckTimeout, ACK_TIMEOUT);
}
self.journal.tell(JournalMessage::Append {
seq,
text: format!("entry {seq}"),
reply_to: context.reply_to(move |()| ProducerMessage::Acked(seq)),
});
}The producer keeps at most WINDOW appends open, 256 in the example. An append is open from its send until its acknowledgment, and it remembers when it was sent. Every Append carries a ReplyTo from context.reply_to, which turns the journal's reply into an Acked message of the producer's own protocol. When the journal loses an append, its ReplyTo is dropped without a reply. Such a ReplyTo reports nothing then, so the producer watches the time instead. append starts the timer ack with ACK_TIMEOUT, 500 ms, when an append enters an empty window.
The window also bounds the journal's buffer, which Batch writes leaves unbounded. Without a resend, every entry the journal holds is an open append, so it never holds more than WINDOW entries. Every resend can add another copy of each open append, so the bound holds only while writes complete (see below).
One timer for the oldest open append
fn acknowledge(&mut self, seq: u64, context: &ActorContext<ProducerMessage, Timed>) {
// A resent append is acknowledged twice!
let Some(position) = self.open.iter().position(|(open, _)| *open == seq) else {
return;
};
self.open.remove(position);
if position == 0 {
match self.open.front() {
Some((_, sent)) => {
let remaining = ACK_TIMEOUT.saturating_sub(sent.elapsed());
context.start_timer("ack", ProducerMessage::AckTimeout, remaining);
}
None => context.cancel_timer("ack"),
}
}
}Acknowledgments need not arrive in order. A restarted journal keeps its mailbox but not its buffer. So the appends still in its mailbox are written and acknowledged before the lost ones, which were in flight or waiting in pending. The producer resends the lost ones only once their time is up. So an acknowledgment removes its append wherever it sits in the window.
One timer is enough nonetheless. The front of the window always has the earliest send time, so it is the first to time out. When the front goes, the timer restarts with the time remaining for the new front. When the last open append goes, the timer is canceled. A replaced or canceled timer is never received, even if it was due already. So once the front is acknowledged, the timeout started for it never arrives.
An append which was resent before its first acknowledgment arrived can be acknowledged twice, once per ReplyTo. The second acknowledgment finds nothing in the window and is ignored.
Resending
fn resend_open(&mut self, context: &ActorContext<ProducerMessage, Timed>) {
println!(
"Resending {} entries after {} ms without an ack",
self.open.len(),
ACK_TIMEOUT.as_millis()
);
let now = Instant::now();
for (seq, sent) in &mut self.open {
let seq = *seq;
self.journal.tell(JournalMessage::Append {
seq,
text: format!("entry {seq}"),
reply_to: context.reply_to(move |()| ProducerMessage::Acked(seq)),
});
*sent = now;
}
context.start_timer("ack", ProducerMessage::AckTimeout, ACK_TIMEOUT);
}When the timer fires, the producer resends every open append in order, each with a fresh ReplyTo. A ReplyTo replies at most once, and the first one went with the lost append. The send times start over, and the timer restarts with the full ACK_TIMEOUT.
The timeout has to be longer than the time the journal may need for a full window, with its retries and one restart. Otherwise the producer resends appends which the journal still holds, and every copy is written again only for the store to skip it. In the example a batch gets at most three attempts of 2 ms each, with 10 and 20 ms between them, about 36 ms in all. A full window of 256 appends spans three batches of 100. The appends still in the journal's mailbox when it gives up on a batch also wait for the restart, 10 ms for the first one. That comes to about 120 ms, and ACK_TIMEOUT is 500 ms.
A journal which stops for good
When the journal fails once more after MAX_RESTARTS restarts, with less than reset_after (30 s by default) of failure-free running in between, it stops for good, as Supervision and lifecycle explains. Only its watchers learn of that. Without the watch, the producer would resend its window every 500 ms forever, and every resent append would be a dead letter. So the producer watches the journal, and on the terminated signal it fails with JournalStopped. The producer is the root and runs under the default strategy, Stop. Its failure hence terminates the actor system, and main reads the store. The scripted run never takes this path: the journal gives up once, and two restarts are allowed.
What the recipe promises
Unless the journal stops for good or a write never completes, every entry reaches the store at least once. A failed write costs a retry, and a batch the journal gave up on costs a resend. Every entry is in the store exactly once, through the keyed write and not through delivery. A retry after the ambiguous commit writes rows the store already has, and so does a resend of an append the journal still held. The store skips them. The writes need not follow the order of the entries, since the appends still in a restarted journal's mailbox are written before the resent ones. The store keeps its rows by key, so their order does not depend on the order of the writes.
A write which never completes keeps its batch in flight forever. The producer then resends its window every 500 ms, and the journal buffers every copy. Only a timeout around write covers that, and database clients usually have timeouts of their own. The window lives in the producer's state, so a producer which fails loses its record of what is unacknowledged. A producer which must survive its own failure, or the end of the process, keeps that record durable, for example as an event-sourced actor.
Run it
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let store = Store::default();
let backoff = Backoff::new(BACKOFF_MIN, BACKOFF_MAX)?;
let system = ActorSystem::new(Producer {
store: store.clone(),
backoff,
});
system
.terminated()
.await
.context("awaiting actor system termination")?;
let table = store.table();
println!(
"{} rows written in {} batches, every key 1..={TOTAL} present once: {}, duplicate writes skipped: {}",
table.rows.len(),
table.writes,
table.rows.keys().copied().eq(1..=TOTAL),
table.skipped
);
Ok(())
}main starts the actor system with the producer as its root and waits until it has terminated. Then it reads the table and prints the number of rows, the writes which reached the rows, whether the keys are exactly 1 to TOTAL, and the duplicate rows skipped. From a clone of the tellus repository:
cargo run --quiet -p tellus --example how_to_retry_writesJournal started
Retrying a batch of 100 entries in 10 ms: connection reset
Retrying a batch of 100 entries in 10 ms: connection reset
Skipped 100 duplicates
Retrying a batch of 100 entries in 10 ms: connection reset
Retrying a batch of 100 entries in 20 ms: connection reset
Giving up on a batch of 100 entries after 3 attempts: connection reset
Journal started
Resending 256 entries after 500 ms without an ack
2000 rows written in 21 batches, every key 1..=2000 present once: true, duplicate writes skipped: 100The first Retrying line is for the batch holding entry 301, which failed before writing. Its retry writes the rows. The second is for the batch holding entry 901, which wrote its rows and then failed. Its retry finds all 100 keys present and skips them, which prints the only line of skipped duplicates. The batch holding entry 1501 fails three times, and the journal gives up. Its supervision restarts it after 10 ms with an empty buffer, which prints the second Journal started. The producer resends its 256 open appends once the oldest of them has waited 500 ms, and the restarted journal writes them. The last line counts 2000 rows and 21 writes which reached the rows: 20 batches of new rows and the retry which only skipped. The attempts which failed before writing are not counted.
The batch sizes depend on scheduling, as in Batch writes. A batch of fewer than 100 entries can change the sizes in the lines, the number of writes and the duplicates skipped, but not the rows or the check of the keys.