Course › Day 6 · Scale › Scene 36 of 49 · 40 min
Booking saved, the partner never heard about it
Writing the message in the same transaction, a relay job, at-least-once delivery, idempotent consumers, event versions
On this page
#partnersThursday 16:10
Mikkel · Hygge Homes (integration)Booking 9134 (created 15:02) is in your app but never reached our system. Our cleaners and key handover weren’t scheduled. Are there others?
Karim · developerRedis failed over at 15:02:10, while Jonas restarted the data Redis with its new settings (scene 35’s follow-up). For about 40 seconds every queue dispatch failed. Six bookings were committed in that window, and their partner messages were never queued.
Mona · supportTwo of those six guests also saw an error page, although their booking went through.
Last week’s feature
Agencies like Hygge Homes run their own systems for keys and staff, and asked to be told about every booking change. An account can now set a webhook URL; Nordic Stays posts every booking change to it, signed with the account’s secret.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('accounts', function (Blueprint $table) {
$table->string('webhook_url')->nullable(); // the partner's system; null: nobody to tell
$table->text('webhook_secret')->nullable(); // encrypted: it signs every message
});
}
};
13 unchanged lines
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory;use Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\Relations\BelongsToMany;use Illuminate\Database\Eloquent\Relations\HasMany;use Illuminate\Database\Eloquent\Relations\HasManyThrough; class Account extends Model{ use HasFactory; protected $fillable = ['name']; protected $hidden = ['webhook_secret']; protected function casts(): array { return ['webhook_secret' => 'encrypted']; } public function users(): BelongsToMany { return $this->belongsToMany(User::class, 'memberships')26 unchanged lines
->using(Membership::class) ->withPivot('role') ->withTimestamps(); } public function properties(): HasMany { return $this->hasMany(Property::class); } public function channelConnections(): HasMany { return $this->hasMany(ChannelConnection::class); } /** Bookings of all this account's properties: bookings → properties.account_id */ public function bookings(): HasManyThrough { return $this->hasManyThrough(Booking::class, Property::class); } public function messageTemplates(): HasMany { return $this->hasMany(MessageTemplate::class); }}
The broken code
Every booking change already goes through CalendarSync, so that is where last week’s code tells the partner:
<?php
namespace App\Jobs;
use App\Models\Booking;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Http;
class NotifyPartner implements ShouldQueue
{
use Queueable;
public int $tries = 5;
public function __construct(public Booking $booking)
{
}
public function handle(): void
{
$account = $this->booking->property->account;
$body = json_encode(['type' => 'booking.updated', 'booking' => ['id' => $this->booking->id, 'status' => $this->booking->status->value]], JSON_THROW_ON_ERROR);
Http::timeout(5)
->withHeaders(['X-NordicStays-Signature' => hash_hmac('sha256', $body, $account->webhook_secret)])
->withBody($body, 'application/json')
->post($account->webhook_url)
->throw();
}
}
public function bookingChanged(Booking $booking): void
{
// … clashes, cleaning, channel push, screens …
if ($booking->property->account->webhook_url !== null) {
NotifyPartner::dispatch($booking)->afterCommit(); // the bug: pushed to Redis after the commit, as a second step
}
}
afterCommit() was the right instinct: never tell a partner about a booking that might still roll back. But it makes two steps that can’t succeed or fail together. 1. MySQL commits the booking. 2. Laravel pushes the job to Redis. If step 2 fails (Redis failing over, the network, a deploy killing the process between the two), the booking exists and the message doesn’t, and nothing will ever retry it: there is no job to retry.
The exception from step 2 also reached the guest as an error page, for a booking that had in fact been made (part 3).
How the fix works, in one picture
Before
The message was sent after the commit; when that failed, it was gone.
After
The message is written with the booking, in the same transaction; a relay delivers it, at least once.
The fix, part 1: the message is a row, saved with the booking
If the message is written to MySQL, in the same transaction as the booking change, there is only one step left: the commit. Both are saved, or neither is. Delivering it is a separate job that can fail and try again as often as it needs, because the row stays until it is delivered.
That table is the outbox: messages waiting to go out.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('outbox_messages', function (Blueprint $table) {
$table->id(); // the delivery order
$table->uuid('uuid')->unique(); // the id the partner sees, and uses to ignore repeats
$table->foreignId('account_id')->constrained();
$table->string('type'); // booking.updated
$table->unsignedSmallInteger('version'); // the payload's shape: 1
$table->json('payload');
$table->unsignedSmallInteger('attempts')->default(0);
$table->text('last_error')->nullable();
$table->timestamp('published_at')->nullable(); // null: still to deliver
$table->timestamps();
$table->index(['published_at', 'id']); // "the oldest undelivered" without a full scan
});
}
};
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\MassPrunable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Str;
class OutboxMessage extends Model
{
use MassPrunable;
protected function casts(): array
{
return ['payload' => 'array', 'published_at' => 'datetime'];
}
/**
* Write a message for the account's partner, inside the caller's transaction.
*
* @param array<string, mixed> $payload
*/
public static function record(Account $account, string $type, int $version, array $payload): self
{
return self::forceCreate([
'uuid' => (string) Str::uuid(),
'account_id' => $account->id,
'type' => $type,
'version' => $version,
'payload' => $payload,
]);
}
public function account(): BelongsTo
{
return $this->belongsTo(Account::class);
}
/** Delivered messages older than a week: "php artisan model:prune" deletes them in batches. */
public function prunable(): Builder
{
return static::query()->whereNotNull('published_at')->where('published_at', '<', now()->subWeek());
}
}
The coordinator records the message instead of queuing it
Every caller of CalendarSync already runs it inside its own transaction (CreateBooking, BookingConfirmation, CancelBooking, ChangeBookingDates, MoveBooking, ImportChannelReservation, ExpireHolds), so the row commits with the change, or rolls back with it.
6 unchanged lines
<?php namespace App\Channels; use App\Enums\BookingStatus;use App\Enums\MembershipRole;use App\Events\CalendarChanged;use App\Jobs\PushAvailability;use App\Models\Booking;use App\Models\OutboxMessage;use App\Models\Overbooking;use App\Models\Property;use App\Notifications\OverbookingDetected;use App\Operations\PlanTurnover;use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\Notification; final class CalendarSync11 unchanged lines
{ public function __construct(private PlanTurnover $turnover) { } /** A booking was created, confirmed, imported, cancelled, expired, moved to other dates or to this property. */ public function bookingChanged(Booking $booking): void { if (in_array($booking->status, BookingStatus::blocking(), true)) { $this->recordClashes($booking); } $this->turnover->forBooking($booking); // the cleaning follows the booking PushAvailability::forProperty($booking->property); // every channel learns the new state (scene 17) $this->pushChannels($booking->property); // every channel learns the new state (scene 17) $this->tellScreens($booking->property); // and every open calendar $this->tellPartner($booking); // and the account's own system, through the outbox } /** A booking moved away from this property: its nights are free here now. */ public function bookingLeft(Property $property): void { PushAvailability::forProperty($property); $this->pushChannels($property); $this->tellScreens($property); } /** An iCal sync changed the blocks of a property (scene 12). */ public function blocksChanged(Property $property): void { PushAvailability::forProperty($property); $this->pushChannels($property); $this->tellScreens($property); } /** The host changed a rule the channels enforce for us, like the minimum stay. */ public function stayRulesChanged(Property $property): void { PushAvailability::forProperty($property); } $this->pushChannels($property); } /** Written with the change, so a push lost after the commit is found again (part 3). */ private function pushChannels(Property $property): void { $property->channelListings()->update(['push_due_at' => now()]); $this->afterCommit(fn () => PushAvailability::forProperty($property)); } private function tellScreens(Property $property): void { CalendarChanged::dispatch($property->account_id, $property->id); // queued, and only after the commit } $this->afterCommit(fn () => CalendarChanged::dispatch($property->account_id, $property->id)); // a reload after reconnect heals a lost one } /** Hints for other systems: a failed dispatch is reported, and never turns a saved booking into an error page. */ private function afterCommit(callable $dispatch): void { DB::afterCommit(fn () => rescue($dispatch, report: true)); } /** The booking as it is now, not what changed: a partner that missed one message still ends up right. */ private function tellPartner(Booking $booking): void { $account = $booking->property->account; if ($account->webhook_url === null) { return; } OutboxMessage::record($account, 'booking.updated', version: 1, payload: [ 'id' => $booking->id, 'property_id' => $booking->property_id, 'status' => $booking->status->value, 'check_in' => $booking->stay->checkIn->toDateString(), 'check_out' => $booking->stay->checkOut->toDateString(), 'guests' => $booking->guests, 'guest_name' => $booking->guest_name, ]); } private function recordClashes(Booking $booking): void { $clashes = $booking->property->bookings()11 unchanged lines
->overlapping($booking->stay) ->whereKeyNot($booking->id) ->where('id', '<', $booking->id) // earlier bookings only: each clash is recorded once, on the later one ->get(); foreach ($clashes as $earlier) { $overbooking = Overbooking::createOrFirst([ 'property_id' => $booking->property_id, 'booking_id' => $booking->id, 'conflicting_booking_id' => $earlier->id, ]); if ($overbooking->wasRecentlyCreated) { $owners = $booking->property->account->users()->wherePivot('role', MembershipRole::Owner->value)->get(); Notification::send($owners, (new OverbookingDetected($overbooking))->afterCommit()); $this->afterCommit(fn () => Notification::send($owners, new OverbookingDetected($overbooking))); // the clash itself is a row: the dashboard shows it either way } } }}
The old job goes:
rm app/Jobs/NotifyPartner.php
A partner’s message has no next time, so it goes through the outbox. The channel pushes and screen updates are different: each one sends the whole state, so a later one replaces a lost one. Part 3 makes sure there is a later one.
The fix, part 2: a relay delivers what the outbox holds
Once a minute, the relay reads the oldest undelivered messages, posts each to its partner, and marks it delivered only after the partner answered 2xx. A message that fails stays undelivered and is tried again next minute.
Two promises come with that, and the partner documentation states them: at least once (a crash between the partner’s 200 and our update sends it again, so partners ignore an X-NordicStays-Event-Id they have already handled), and in order per account (when one message to Hygge Homes fails, their later messages wait, so a cancellation never arrives before the booking it cancels).
<?php
namespace App\Console\Commands;
use App\Models\OutboxMessage;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use Throwable;
class RelayOutbox extends Command
{
protected $signature = 'outbox:relay';
protected $description = 'Deliver undelivered outbox messages to partners, oldest first';
public function handle(): int
{
$blocked = []; // accounts whose earlier message just failed
OutboxMessage::query()
->whereNull('published_at')
->orderBy('id')
->with('account')
->limit(500) // the rest next minute: a run never takes too long
->get()
->each(function (OutboxMessage $message) use (&$blocked): void {
if (isset($blocked[$message->account_id])) {
return; // keep this account's messages in order
}
try {
$this->deliver($message);
$message->forceFill(['published_at' => now(), 'last_error' => null])->save();
} catch (Throwable $e) {
$blocked[$message->account_id] = true;
$message->forceFill(['attempts' => $message->attempts + 1, 'last_error' => mb_substr($e->getMessage(), 0, 1000)])->save();
report($e); // visible in the error tracker, never silent
}
});
return self::SUCCESS;
}
private function deliver(OutboxMessage $message): void
{
$account = $message->account;
if ($account->webhook_url === null) {
return; // the partner was removed since: nothing to send
}
$body = json_encode([
'id' => $message->uuid,
'type' => $message->type,
'version' => $message->version,
'created_at' => $message->created_at->toIso8601String(),
'data' => $message->payload,
], JSON_THROW_ON_ERROR);
Http::timeout(5)
->withHeaders([
'X-NordicStays-Event-Id' => $message->uuid,
'X-NordicStays-Signature' => hash_hmac('sha256', $body, $account->webhook_secret), // scene 8, the other way round
])
->withBody($body, 'application/json')
->post($account->webhook_url)
->throw(); // a 4xx or 5xx is a failure: try again next minute
}
}
version is the shape of data. When a field changes meaning, the next shape is version: 2, sent to partners who have said they read it, while the others keep getting version 1. Partners never have to guess what a message means.
15 unchanged lines
<?php use App\Jobs\SyncIcalFeed;use App\Models\IcalFeed;use Illuminate\Support\Facades\Schedule; // The server runs the scheduler once a minute: * * * * * php artisan schedule:run// Locally: php artisan schedule:work Schedule::command('bookings:expire-holds')->everyMinute()->withoutOverlapping();Schedule::command('payments:reconcile-refunds')->dailyAt('03:10')->withoutOverlapping(); Schedule::call(function (): void { IcalFeed::query()->lazyById()->each(fn (IcalFeed $feed) => SyncIcalFeed::dispatch($feed));})->everyFifteenMinutes()->name('sync-ical-feeds')->withoutOverlapping();Schedule::command('channels:poll')->everyTenMinutes()->withoutOverlapping();Schedule::command('messages:send-due')->everyMinute()->withoutOverlapping()->onOneServer();Schedule::command('bookings:revert-unpaid-changes')->everyMinute()->withoutOverlapping();Schedule::command('outbox:relay')->everyMinute()->withoutOverlapping()->onOneServer(); // one relay at a time keeps the orderSchedule::command('model:prune')->dailyAt('04:20'); // delivered outbox messages, after a week
The six lost messages
The relay can only deliver what was written. For the six bookings from 15:02, a developer writes their messages once, from tinker, and the next relay run delivers them:
php artisan tinker
> App\Models\Booking::whereKey([9134, 9135, 9137, 9138, 9140, 9141])->with('property.account')->get()
->each(fn ($booking) => App\Models\OutboxMessage::record($booking->property->account, 'booking.updated', 1, [
'id' => $booking->id, 'property_id' => $booking->property_id, 'status' => $booking->status->value,
'check_in' => $booking->check_in->toDateString(), 'check_out' => $booking->check_out->toDateString(),
'guests' => $booking->guests, 'guest_name' => $booking->guest_name,
]));
The fix, part 3: the side effects that aren’t messages
Two of the six guests saw an error page. After the commit, CalendarSync dispatched a channel push and a screen update, Redis refused both, and the exception reached the guest. Neither is worth an error page: the booking is saved, and both have another way to catch up.
So each one runs after the commit inside rescue(): a failure is reported (the on-call phone rings), never thrown at the guest. A screen catches up on its own when it reconnects (scene 32). A channel needs more: if no other change comes, nothing would push that property again, and Booking.com would keep selling the nights. So the change also writes, in the same transaction, that the listing owes a push. A scheduled task finds the listings whose push never ran:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('channel_listings', function (Blueprint $table) {
$table->timestamp('push_due_at')->nullable(); // the last change the channel must hear about
$table->index('push_due_at'); // the task below reads it every minute
});
}
};
A push now records the moment it started as last_pushed_at: a change made while it was running is newer than that, so it is pushed again.
58 unchanged lines
<?php namespace App\Jobs; use App\Channels\AvailabilityRuns;use App\Channels\ChannelManager;use App\Channels\ChannelRateLimited;use App\Models\ChannelListing;use App\Models\Property;use DateTimeInterface;use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;use Illuminate\Contracts\Queue\ShouldQueue;use Illuminate\Foundation\Queue\Queueable;use Illuminate\Queue\Middleware\WithoutOverlapping;use Throwable; class PushAvailability implements ShouldQueue, ShouldBeUniqueUntilProcessing{ use Queueable; public const DAYS = 365; public int $uniqueFor = 900; // a stuck lock frees itself after 15 minutes public function __construct(public ChannelListing $listing) { $this->onQueue('channels'); // never in front of guest emails again } /** Dispatch one push per listing of the property, after the current transaction commits. */ public static function forProperty(Property $property): void { foreach ($property->channelListings as $listing) { self::dispatch($listing)->afterCommit(); } } public function uniqueId(): string { return (string) $this->listing->id; // one waiting push per listing } /** @return list<object> */ public function middleware(): array { return [(new WithoutOverlapping("push-listing-{$this->listing->id}"))->releaseAfter(30)->expireAfter(600)]; } public function retryUntil(): DateTimeInterface { return now()->addHours(2); } /** @return list<int> */ public function backoff(): array { return [30, 120, 300, 900]; } public function handle(ChannelManager $channels, AvailabilityRuns $runs): void { $started = now(); // what this push can vouch for: changes until now $api = $channels->for($this->listing->channel)->api(); try { $api->updateCalendar($this->listing, $runs->for($this->listing->property, now()->startOfDay(), self::DAYS)); // each run with its own minimum } catch (ChannelRateLimited $e) { $this->release($e->retryAfter); // scene 16 return; } $this->listing->forceFill(['last_pushed_at' => now(), 'last_push_error' => null])->save(); $this->listing->forceFill(['last_pushed_at' => $started, 'last_push_error' => null])->save(); } public function failed(Throwable $exception): void { $this->listing->forceFill(['last_push_error' => $exception->getMessage()])->save(); }}
<?php use App\Jobs\PushAvailability;use App\Jobs\SyncIcalFeed;use App\Models\ChannelListing;use App\Models\IcalFeed;use Illuminate\Database\Eloquent\Builder;use Illuminate\Support\Facades\Schedule; // The server runs the scheduler once a minute: * * * * * php artisan schedule:run8 unchanged lines
// Locally: php artisan schedule:work Schedule::command('bookings:expire-holds')->everyMinute()->withoutOverlapping();Schedule::command('payments:reconcile-refunds')->dailyAt('03:10')->withoutOverlapping(); Schedule::call(function (): void { IcalFeed::query()->lazyById()->each(fn (IcalFeed $feed) => SyncIcalFeed::dispatch($feed));})->everyFifteenMinutes()->name('sync-ical-feeds')->withoutOverlapping();Schedule::command('channels:poll')->everyTenMinutes()->withoutOverlapping();Schedule::command('messages:send-due')->everyMinute()->withoutOverlapping()->onOneServer();Schedule::command('bookings:revert-unpaid-changes')->everyMinute()->withoutOverlapping();Schedule::call(function (): void { ChannelListing::query() ->where('push_due_at', '<', now()->subMinutes(2)) // a push should have started by now ->where(fn (Builder $q) => $q->whereNull('last_pushed_at')->orWhereColumn('last_pushed_at', '<', 'push_due_at')) ->lazyById() ->each(fn (ChannelListing $listing) => PushAvailability::dispatch($listing)); // unique per listing: never a second one waiting})->everyMinute()->name('push-missed-calendars')->withoutOverlapping()->onOneServer();Schedule::command('outbox:relay')->everyMinute()->withoutOverlapping()->onOneServer(); // one relay at a time keeps the orderSchedule::command('model:prune')->dailyAt('04:20'); // delivered outbox messages, after a week
CalendarSync above already calls pushChannels(). At 15:02 this would have meant six bookings saved, six guests sent to their confirmation page, six reported errors, and every channel pushed by about 15:05.
You just wrote the Transactional Outbox pattern
Transactional OutboxSave a message for another system in the same database transaction as the change it describes, and let a separate relay deliver it. The change and its message can no longer be saved one without the other.
- The outbox turns "commit, then publish" (two systems, no shared transaction) into "commit" (one system), plus a delivery that can be retried for as long as it takes.
- It promises at least once, never exactly once. Exactly-once needs the receiver’s help: the event id in every message lets the partner skip what it has already handled. That receiver is an idempotent consumer: our own webhook inbox from scene 8 is one, for Stripe.
- The outbox and scene 8’s inbox are mirror images: the inbox stores what others send us before we act on it; the outbox stores what we must send before anyone acts on it.
When not to use it
- Messages that heal themselves, like a full calendar push the next change sends again anyway:
afterCommit()is enough. - When losing a message costs nothing, like a "someone is typing" signal.
With SQS or Kafka, the outbox stays
A bigger platform might publish booking events to Amazon SQS or Kafka instead of posting webhooks, and let many services read them. The relay then publishes to the broker instead of posting to a URL. Nothing else changes: the broker is a second system, and committing to MySQL and publishing to Kafka can still succeed one without the other. What changes is on the reading side: Kafka keeps messages for days and lets every consumer read at its own pace, and each consumer still has to ignore repeats by event id.
Laravel’s ->afterCommit() and ShouldDispatchAfterCommit only decide when a dispatch happens, never whether it survives: the queue push is still a separate step after the commit. That is the gap the outbox closes.
TestsThe message is saved with the booking, or not at all; delivered, retried, signed, and kept in order
<?php
use App\Channels\CalendarSync;
use App\Enums\Channel;
use App\Jobs\PushAvailability;
use App\Models\Account;
use App\Models\Booking;
use App\Models\ChannelListing;
use App\Models\OutboxMessage;
use App\Models\Property;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Queue;
beforeEach(function () {
$this->account = Account::factory()->create();
$this->account->forceFill(['webhook_url' => 'https://partner.test/hooks', 'webhook_secret' => 'partner-secret'])->save();
$this->booking = Booking::factory()->for(Property::factory()->for($this->account))->forStay('2027-07-10', 3)->create();
});
it('saves the message in the same transaction as the change, or not at all', function () {
try {
DB::transaction(function (): void {
app(CalendarSync::class)->bookingChanged($this->booking);
throw new RuntimeException('the change fails');
});
} catch (RuntimeException) {
}
expect(OutboxMessage::count())->toBe(0); // rolled back with the change
DB::transaction(fn () => app(CalendarSync::class)->bookingChanged($this->booking));
expect(OutboxMessage::sole()->payload['id'])->toBe($this->booking->id); // committed with it: nothing can lose it now
});
it('marks a message delivered only after the partner accepted it', function () {
app(CalendarSync::class)->bookingChanged($this->booking);
Http::fake(['partner.test/*' => Http::sequence()->push('Unavailable', 503)->push('', 204)]);
$this->artisan('outbox:relay')->assertSuccessful();
expect(OutboxMessage::sole())->published_at->toBeNull()->attempts->toBe(1); // kept for the next run
$this->artisan('outbox:relay')->assertSuccessful();
expect(OutboxMessage::sole()->published_at)->not->toBeNull();
});
it('signs the message and sends the id the partner uses to ignore repeats', function () {
app(CalendarSync::class)->bookingChanged($this->booking);
Http::fake(['partner.test/*' => Http::response('', 204)]);
$this->artisan('outbox:relay');
$message = OutboxMessage::sole();
Http::assertSent(fn (Request $request): bool => $request->header('X-NordicStays-Event-Id')[0] === $message->uuid
&& $request->header('X-NordicStays-Signature')[0] === hash_hmac('sha256', $request->body(), 'partner-secret')
&& $request['version'] === 1);
});
it('holds an account’s later messages while an earlier one is failing', function () {
app(CalendarSync::class)->bookingChanged($this->booking);
$this->booking->forceFill(['status' => 'cancelled'])->save();
app(CalendarSync::class)->bookingChanged($this->booking);
Http::fake(['partner.test/*' => Http::response('Down', 500)]);
$this->artisan('outbox:relay');
Http::assertSentCount(1); // the cancellation waits for the booking
});
it('keeps a channel push owed when Redis refuses it, and pushes it from the schedule', function () {
Queue::fake();
$listing = ChannelListing::forceCreate(['property_id' => $this->booking->property_id, 'channel' => Channel::BookingCom, 'external_listing_id' => '1290031']);
$this->travelTo('2027-06-01 15:02');
DB::transaction(fn () => app(CalendarSync::class)->bookingChanged($this->booking));
expect($listing->fresh()->push_due_at->equalTo(now()))->toBeTrue(); // written with the change
$this->travelTo('2027-06-01 15:05');
Queue::fake(); // the push at 15:02 never reached Redis
$this->artisan('schedule:test', ['--name' => 'push-missed-calendars'])->assertSuccessful();
Queue::assertPushed(PushAvailability::class, fn (PushAvailability $job): bool => $job->listing->is($listing));
});
it('does not push again a listing that was pushed after its last change', function () {
Queue::fake();
$listing = ChannelListing::forceCreate(['property_id' => $this->booking->property_id, 'channel' => Channel::BookingCom, 'external_listing_id' => '1290031']);
$listing->forceFill(['push_due_at' => now()->subMinutes(10), 'last_pushed_at' => now()->subMinutes(9)])->save();
$this->artisan('schedule:test', ['--name' => 'push-missed-calendars'])->assertSuccessful();
Queue::assertNotPushed(PushAvailability::class);
});
The first test is the incident: whatever happens after the commit, the message is already in MySQL, so a Redis failover can no longer lose it.
Remember
- A message that must not be lost is written in the same transaction as the change, then relayed.
- Delivery is at least once: consumers ignore repeats by id.
Partners hear about every booking now. Next, the mobile app’s Book button times out, the app tries again, and a guest is booked twice.