定制 gosuperscript/zero-downtime-event-replays 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gosuperscript/zero-downtime-event-replays

Composer 安装命令:

composer require gosuperscript/zero-downtime-event-replays

包简介

Zero downtime event replay for Spatie's Laravel event sourcing package

README 文档

README

Usually you have to deal with a few problems that might cause downtime when replaying events. Your read models will be truncated at the beginning of a replay, and won't have the correct data until the replay has finished. Besides that, you'd have to wait with projecting newly recorded events until the replay has finished, to protect the replay order.

This package solves both problems, allowing a replay to happen to a copy of your read models. Once the replay is up to speed, newly recorded events will be played to both projections, so that your copy is kept up to date with the live read model. After verifying that the new replay is correct, the package enabled to promote your replay to live. Resulting in a (near) zero downtime release.

Usually running a new reply will look like this:

  1. Create a new replay, ang give it key to identify it. For example "add_extra_field_to_balance_projection". You also specify the projectors you want to play to in this step.
$manager = resolve(\Gosuperscript\ZeroDowntimeEventReplays\ReplayManager::class);
// Create a replay
$manager->createReplay('add_extra_field_to_balance_projection', [
    "App\Projectors\BalanceProjector"
]);
  1. The replay can be started. When replaying, it calls the useConnection method on the projector. So the projector knows where it should write its data to. This package comes with an EloquentZeroDowntimeProjector that gives you some magic for dealing with different connections.
$manager->startReplay('add_extra_field_to_balance_projection');
  1. Once the replay is finished, but there is still some lag to production because of newly recorded events. You can start the replay again, it will start from the latest projected event. Its always possible to monitor the state of the replay and the lag compared to production.
// get the state & progress of your replay 
$manager->getReplay('add_extra_field_to_balance_projection');

// how many events is the replay behind the event stream?
$manager->getReplayLag('add_extra_field_to_balance_projection');
  1. Once there is no lag, we can start projecting new events to replays.
$manager->startProjectingToReplay('add_extra_field_to_balance_projection');
  1. Once every thing checks out, you can promote your replay to production.
    $manager->putReplayLive('add_extra_field_to_balance_projection');
  1. Lastly you can cleanup your replay
    $manager->removeReplay('add_extra_field_to_balance_projection');

Installation

You can install the package via composer:

composer require gosuperscript/zero-downtime-event-replays

Usage

$manager = resolve(\Gosuperscript\ZeroDowntimeEventReplays\ReplayManager::class);
// Create a replay
$manager->createReplay('your_replay_key', ['projectorA', 'projectorB']);

// Start replay history
$manager->startReplay('your_replay_key');

// get the state & progress of your replay 
$manager->getReplay('your_replay_key');

// how many events is the replay behind the event stream?
$manager->getReplayLag('your_replay_key');

// once a replay is up to date with the event stream, we can project events to it when they happen 
$manager->startProjectingToReplay('your_replay_key');

// Once the replay is approved, we can promote it to production
$manager->putReplayLive('your_replay_key');

// Or we can delete the replay
$manager->removeReplay('your_replay_key');

ZeroDowntime projectors

In order to make projectors work with zero downtime replays, they have to implement the ZeroDowntimeProjector interface. This interface asks you to implement the following methods:

interface ZeroDowntimeProjector
{
    // This method lets the projector know that its replaying on a replay
    public function forReplay(): self;

    // Sets the connection to replay to, using the replay key. Each connection must be treated as a clone of the production schema.
    public function useConnection(string $connection): self;

    // Promote your connection to production
    public function promoteConnectionToProduction(): void;

    // cleanup/remove connection
    public function removeConnection();
}

Since most projections probably are replaying to eloquent, this package includes a EloquentZeroDowntimeProjector abstract class and a Projectable trait to be used on your eloquent read models.

To make your projectors work with this package:

  1. Make sure your projector extends the EloquentZeroDowntimeProjector.
  2. On all read models used by the projector, add the Projectable trait.
  3. Implement a models method on your projector, that returns all models that the projector writes to. This is used by the EloquentZeroDowntimeProjector in order to setup the right db scheme and promote the right models to production.
    public function models(): array
    {
        return [
            new BalanceProjector(),
        ];
    }
  1. Everywhere where you query or update your read model, use the forProjection method.
    // when truncating
    Balance::forProjection($this->connection)->truncate();
    
    // when querying
    Balance::forProjection($this->connection)->where('user_id', $event->user_id)->first();
    
    // when updating
    Balance::forProjection($this->connection)->where('user_id', $event->user_id)->increment('total', $event->amount);
    
    // when newing an instance
    $balance = Balance::newForProjection($this->connection, ['id' => $event->user_id, 'total' => $event->amount]);
    $balance->save();

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

gosuperscript/zero-downtime-event-replays 适用场景与选型建议

gosuperscript/zero-downtime-event-replays 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 590.8k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「event-sourcing」 「gosuperscript」 「zero-downtime-event-replays」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 gosuperscript/zero-downtime-event-replays 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gosuperscript/zero-downtime-event-replays 我们能提供哪些服务?
定制开发 / 二次开发

基于 gosuperscript/zero-downtime-event-replays 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 590.8k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 24
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04