Skip to content
Engineering10 min read

How Redis was reporting pending messages that were already deleted from its stream

XPENDING kept counting a Redis Streams message that XTRIM had already deleted. Here's what the pending entries list actually tracks, why XAUTOCLAIM made it more confusing, and what Redis 8.2's ACKED option changes.

How Redis was reporting pending messages that were already deleted from its stream

When publishing documentation changes, there are usually many things that go in the process of doing so such as indexing the new page changes so search results can pick them up correctly, caching the changes so we don't have to fetch the documentation's contents from scratch for every visit and a lot more stuff, but let's keep it limited to both of those to keep the article more focused on the caching side of things.

We've set up a workflow to prevent any discrepancies in the caching side of things for publishing docs so whenever a change is published, we'd revalidate the caches/indexes in the background, test and verify everything is ready for use before labelling it as "Live" for the end user.

In order to make it work that way, our model kept published content separately and represented each search refresh as a Redis Streams message containing a page identifier and a revision number so an indexing worker would read the message, update search, and "ACK" it after the update succeeded. If the worker stopped before finishing, it would save its progress so another worker attempt can pick up what the previous worker has left out and complete the refresh from there without starting from scratch.

We'll be referring to this from now on as a "Checkpoint" system to keep things simpler!

Octri tracks a docs update as publication, cache revalidation, and search indexing proceed.

One interesting question we had while working on this was: what would happen to the message while that worker was unavailable? Other docs changes could be published, so we'd end up with more entries to the stream, and if another worker stored more entries to the stream, we could accidentally exceed our retention policy and trim out older entries, which could in turn, result in us accidentally dropping the previous worker's checkpoint state.

So we created a small experiment on the side using Redis (v8.2.0 with redis-cli) to create proof whether or not that edge case was possible and started 8 parallel workers on a constrained docker container and something unexpected happened...

3 out of the 8 workers stopped half-way through and stored a checkpoint in the stream, but since our retention policy was too strict, one of the workers' checkpoint states was dropped, this is all expected and part of the experiment, but what wasn't expected is that calling XPENDING incorrectly showed that there was one unfinished message and looking up its ID in the stream returned nothing at all even in the consumer's pending history. So Redis retained enough information to tell us that the message had been delivered, but the message's payload (page identifier and revision) was dropped altogether.

We found out that the behavior we noticed above was actually documented in Redis's docs, but not being aware of it really proved to be quite the head-scratcher for us (I almost went bald scratching my head looking at this to be honest). What was even more interesting is that if we were to attempt recovering that partial pending reference, we'd end up removing the pending reference potentially causing an internal crash if our code isn't extremely type-safe (fortunately for us, our code is type-safe, but that also meant that we'd discard the pending message due to its mismatching type anyways).

Eight worker stations share a Redis stream; three have stopped and an older checkpoint has been trimmed.

What Redis was actually keeping track of

The first thing we looked into was how XPENDING could keep reporting a message that XRANGE couldn't find. We assumed that if Redis knew a message was still pending, it must have been keeping the message around somewhere. That's a reasonable assumption when you're using the stream to recover work, but it turns out Redis keeps those two things separately.

Every consumer group has something called a Pending Entries List (PEL), which keeps track of which message IDs have been delivered to which consumers and haven't been acknowledged yet. The actual message contents still live in the stream. The PEL doesn't store another copy of the page identifier, revision, or checkpoint data.

By default, XTRIM uses a policy called KEEPREF. As the name suggests, it keeps the pending references even if the entries they're pointing to get trimmed. So when we removed our checkpoint's stream entry, Redis was left with a delivery record pointing to something that no longer existed. XPENDING was reading that delivery record, while XRANGE was looking for the actual stream entry.

Two Redis records show message 1-0 still in the pending list but missing from the stream.

Once we understood that, we wanted to reproduce the result without all eight workers involved. We created a fresh stream, delivered one update without acknowledging it, and then added a second update so we could trim the first one:

redis
XADD docs:updates 1-0 page /guides/authentication revision 2
XGROUP CREATE docs:updates search 0
XREADGROUP GROUP search worker-1 STREAMS docs:updates >
XADD docs:updates 2-0 page /guides/pagination revision 1
XTRIM docs:updates MAXLEN = 1

The retention limit of one entry is obviously not something we'd use for actual documentation updates. We just wanted to force the deletion with as little extra setup as possible.

After trimming, the same three commands gave us the result we'd seen earlier. XPENDING still counted one unfinished message, XRANGE returned an empty result for 1-0, and reading the consumer's pending history returned the message ID with a null payload:

text
> XPENDING docs:updates search
[1,"1-0","1-0",[["worker-1","1"]]]

> XRANGE docs:updates 1-0 1-0
[]

> XREADGROUP GROUP search worker-1 STREAMS docs:updates 0
[["docs:updates",[["1-0",null]]]]

We had been looking at the pending count as a sign that there was something to recover, which made the whole thing confusing. Redis was technically doing exactly what its documentation describes. The delivery reference survived, but the information our worker needed to resume the job didn't.

Why the recovery attempt made things even more confusing

We then tried recovering the missing message with XAUTOCLAIM, which is what we'd normally use when another worker needs to take over messages left behind by one that stopped. The command can scan pending entries and claim ones that have been idle long enough.

In the smaller test, we used an idle threshold of zero to make the message immediately eligible:

text
> XAUTOCLAIM docs:updates search worker-2 0 0-0 COUNT 10
["0-0",[],["1-0"]]

At first glance, it looks like Redis found the message ID for us, but the second array is empty. That's where any successfully claimed messages would have been returned. The third array contains IDs whose entries were already gone, so Redis removed their pending references while scanning.

This has been part of XAUTOCLAIM since Redis 7.0, and it makes sense when you think about it. There's no point in transferring ownership of a message that doesn't exist anymore. The trouble is that our application could end up seeing the pending count go down and assume the missing work had been recovered or finished.

We checked XPENDING once more and got zero pending entries. But our Docs refresh hadn't happened, and the checkpoint was still missing. The type checks kept us from trying to process null as though it contained a valid page and revision, which is great, but rejecting the value wasn't going to bring the documentation's cache or search index up to date either.

That was probably the more important issue for us. We couldn't rely on the number of pending messages alone to decide whether a documentation change was ready to be marked as Live. We needed to know whether the work had actually been completed.

XAUTOCLAIM returns no claimed payloads and removes the ID of a missing stream entry.

Redis 8.2 had an option we hadn't accounted for

Since we were already testing with Redis 8.2, we went back to the trimming options to see whether we could prevent unfinished checkpoints from being deleted in the first place. That's where ACKED came in.

The default KEEPREF policy trims entries and leaves any pending references behind. With ACKED, Redis only trims entries once every consumer group has read and acknowledged them. So we repeated the two-message experiment on a fresh stream, this time using:

redis
XTRIM docs:updates:acked MAXLEN = 1 ACKED

Before the first message was acknowledged, the command returned 0: nothing was removed. We could still read the original message, including its page and revision. After acknowledging it, the same trim could remove that older entry.

That's the behavior we'd been expecting when we first designed the checkpoint system. If a worker hasn't finished the refresh, its message should still be available for another attempt instead of getting deleted simply because a newer documentation update arrived.

There is a tradeoff, though. We had asked Redis to keep one entry, but ACKED kept two until the first one was acknowledged. If a worker stays down or a consumer group stops making progress, those unacknowledged entries can accumulate. So using ACKED doesn't mean we can stop thinking about retention; it means we also have to watch for consumer groups that are falling behind and decide how much backlog we can afford to retain.

There's also a DELREF option, but it wouldn't have helped us here. It deletes the pending reference along with the message, so we'd avoid the confusing XPENDING result while still losing the checkpoint itself. For our use case, that would only make the failure harder to notice. Redis explains the differences between all three policies in the XTRIM documentation.

KEEPREF removes an unacknowledged message while ACKED retains it until acknowledgment.

What this meant for our Docs publishing workflow

Protecting unfinished messages solves the immediate trimming problem, but we still had to consider what would happen if a checkpoint was lost for some other reason. An indexing worker could fail at an awkward point, a message could have been trimmed before we changed the policy, or a bad deployment could leave an old consumer group around longer than expected.

Fortunately, we already keep the published documentation separately from the background messages. That gives us a way to tell whether the caches and search index have actually caught up without trusting the state of the stream alone.

For example, if /guides/authentication has revision 2 published but search is still serving revision 1, we'd know something went wrong with the refresh. We could schedule another attempt using the published page instead of trying to reconstruct everything from a message that no longer exists. The same applies to a cached copy of the page that's still on the older revision.

The important detail is that this check needs to work even when the original stream message is gone. If all we have left is a Redis message ID, there's no way to know which page it belonged to unless that mapping exists somewhere else. Having the published page and its current revision available means we can check what needs updating directly. We'd also need to handle deleted or unpublished pages, so old content doesn't get left behind in search.

There was one other case we didn't want to miss. Say revision 2 finishes indexing, then a worker that was delayed on revision 1 wakes up and tries to write its older result. Without checking the revision, the late worker could put search back on the previous version. So the indexing step needs to avoid replacing a newer revision with an older one, even if the older job eventually completes successfully.

The next test for this workflow is straightforward: publish an update, interrupt the worker before it acknowledges the message, let retention run, then bring recovery back up. We should be able to find the new content through search and load the current cached page before the update is labelled Live. Whether Redis reports zero pending messages is useful to know, but it isn't enough to tell us those checks have passed.

Octri compares revision 2 of a published page with revision 1 in its cache and search index.

We started this experiment because we were worried that trimming might remove a checkpoint. It did, exactly as we expected. The part that caught us off guard was how much information Redis could still report about the deleted message, and how easy it would have been to mistake that information for something we could actually recover.

Now we know what XPENDING is telling us, and why the answer can still be perfectly valid even after the underlying message has disappeared.

Octri