Skip to content

Last value pattern: keep only the newest update

Room availability changes many times a minute and only the latest matters: a conflating consumer group gets the newest message of each partition.

Updated View as Markdown

The availability of a hotel changes many times a minute. A screen that shows it needs only the current value, and handling every old update on the way there is wasted work.

A consumer group with .conflation() gets only the newest message of each partition, and its cursor moves past the older ones.

Three availability updates for hotel-7: 12 rooms, 11 rooms, 9 rooms. The group screen, with conflation, gets only the newest, 9 rooms, and its cursor moves past the other two. Another group without conflation still gets all three.Three availability updates for hotel-7: 12 rooms, 11 rooms, 9 rooms. The group screen, with conflation, gets only the newest, 9 rooms, and its cursor moves past the other two. Another group without conflation still gets all three.
Conflation: the newest value of each partition, for this group only.
// The availability of a hotel changes often; the screen needs only the last value.
for (const rooms of [12, 11, 9]) {
  await queen.queue('availability').partition('hotel-7')
    .push([{ data: { hotel: 7, rooms } }])
}

const [latest] = await queen.queue('availability')
  .group('screen')
  .subscriptionMode('all')
  .conflation()                       // the newest message of each hotel, the rest skipped
  .wait(false)
  .pop()
console.log(latest.data)              // { hotel: 7, rooms: 9 }
await queen.ack(latest, 'completed', { group: 'screen' })

Three updates were pushed (12, 11 and 9 rooms). The screen got 9, and the older two were never delivered to it.

Why
for a value that changes, only the last one matters. Conflation lets a slow consumer catch up in one step instead of replaying every update. Other groups of the same queue still get every message.

One thing to know: use it only where an old message has no meaning of its own. For events that each count, such as payments, every message must be handled.

Built from: Pop and consume.

Navigation

Type to search…

↑↓ navigate↵ selectEsc close