Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Source Of Truth

A source of truth is the one place designated to hold the authoritative version of a piece of data. Every other copy of that data, whether cached, denormalized, replicated, or displayed, is derived from it and must defer to it whenever a conflict appears.

The idea shows up at every scale of a PHP application. A configuration value should live in one file or environment variable, not be duplicated across several classes that might drift apart. A database column is the source of truth for an entity’s state, while a Redis cache or a computed property is only a disposable projection of it, and must be invalidated or recomputed whenever the underlying row changes. In event-sourced systems, the event-store itself is the source of truth: the current state is never trusted directly, but rebuilt on demand by replaying events.

Without a clear source of truth, systems accumulate silent inconsistencies: a cache that outlives its data, two services that each believe their own copy is authoritative, or a generated file that is manually edited and diverges from its generator. Establishing one explicitly is what makes reconciliation and debugging possible, since there is always a reference to compare against.

<?php

    class UserRepository {
        // The database row is the source of truth for the user's state.
        public function getEmail(int $userId): string {
            if ($cached = $this->cache->get("user:$userId:email")) {
                return $cached; // a disposable projection, not the truth itself
            }

            $email = $this->pdo
                ->query("SELECT email FROM users WHERE id = $userId")
                ->fetchColumn();

            $this->cache->set("user:$userId:email", $email, ttl: 300);

            return $email;
        }
    }

?>

Documentation

See Also