Checksum handling with prooph components

Checksum handling with prooph components

While working on this prooph components powered API, I realized it would make sense for our mobile client to know when data changed on the server-side without constantly pulling all data from the API and comparing it locally. In a "traditional" application I would have used a checksum mechanism - e.g. creating a checksum of all the data in the datastore - but with an event sourced application this is no longer needed as we can simply rely on the timestamp of the last event happened to know if things have changed.

Settings things up was rather simple. The projection set-up looks like this:

/* @var ProjectionManager $projectionManager */
/* @var SyncReadModel $readModel */
$projection = $projectionManager->createReadModelProjection(
    ->whenAny(function(array $state, Message $event) {
        $this->readModel()->stack('updateChecksumFor', $event);

The projection listens on any event and passes each and every received event to the updateChecksumFor() method of our read model. In the updateChecksumFor() method of the read model we first map the incoming event classes to a unique type, e.g. all events residing in moduleA namespace get "grouped" as type moduleA. The easiest way of doing that was a simple string search in the event class name - "not the best solution"(tm) but works for now:

$type = get_class($event);
if (stripos($type, 'moduleA')) {
    $type = 'moduleA';
} else if (stripos($type, 'moduleB')) {
    $type = 'moduleB';
} else if (stripos($type, 'moduleC')) {
    $type = 'moduleC';
} else {
    // simply ignore all other events!

We then insert the data in the PostgreSQL database by making use of PostgreSQL upsert feature:

$sql = <<<EOT
INSERT INTO "sync_checksums" ("type", "checksum") 
VALUES (:type, :checksum) 
ON CONFLICT ("type") DO 
UPDATE SET "checksum" = :checksum WHERE "sync_checksums"."type" = :type;

$statement = $this->pdo->prepare($sql);
$statement->bindValue(':type', $type);
$statement->bindValue(':checksum', $event->createdAt()->format('U'), 

The client can fetch the data via a seperate API endpoint and then figure out which API calls are needed to update the local datastore to retrieve the latest changes.

Tags: , , ,

Eintrag von Stephan Hochdörfer am 12.09.2017

comments powered by Disqus