Skip to content

Deploys and schema changes

During rolling deployments, old and new versions of your application share the same cache simultaneously. paceapi prevents cross-version deserialization errors and stale logic without flushing the cache on deploy.

Release isolation

Pass a build identifier (Git SHA, image tag, or version) as release=:

# The client is yours: create it as you would for anything else, and close it when
# your app shuts down. Nothing connects until the first request.
redis = from_url("redis://localhost:6379")
# The release is whatever names this build of your app: here, a variable your deploy sets.
cache = Cache(RedisStore(redis), release=os.environ.get("RELEASE"), store_timeout=0.25)
  • Isolated reads during rollouts: Each release reads only entries written by that release; old entries expire via TTL.
  • No deploy flush step: Nothing to delete or scan during deployment, and no risk of old workers repopulating flushed keys with stale formats.
  • Shared invalidations: invalidate(), set(), and delete() still apply across all releases.

Trade-off: each release starts with a cold cache for non-persistent namespaces.

Persistent namespaces across deploys

Set persistent=True for data independent of application code (reference tables, exchange rates, external responses) so deploys avoid cold starts:

# What a user is shown is worked out by this release's code, and is kept for this
# release: the one before it and the one after it each keep their own.
users = cache.namespace("user", ttl=300)
# A country's name is the same whichever release reads it, so it is kept across them.
countries = cache.namespace("country", ttl=86400, persistent=True)

Automatic model schema fingerprinting

Typed values include a fingerprint of their Pydantic JSON schema. Adding, removing, or changing a model field automatically changes the fingerprint, so old entries safely miss and recompute without manual cache clears. (Untyped JSON values have no schema fingerprint. Neither do responses cached by @ns.response, which are stored as bytes: use release= or schema= for them.)

Pydantic version upgrades

Because different Pydantic versions can emit slightly different JSON schemas for the same model, mixed-version fleets during a Pydantic upgrade will treat each other's entries as misses (safe, but temporarily lower hit rate).

Semantic changes (schema=)

When the meaning of a cached value changes without altering its JSON shape (for example, switching prices from dollars to cents, or changing sort order) or when changing pickled classes under PickleSerializer, bump the namespace's schema= version:

# A total used to be kept in dollars and is now kept in cents. Both are whole
# numbers, so nothing about a value kept last week says which it is. Raising
# the number says so: what was kept under the old one is a miss from now on.
orders = cache.namespace("order", ttl=86400, persistent=True, schema=2)

Pass the same schema= everywhere you obtain that namespace handle (define the namespace in one module and import it).

During a rolling deploy that does not change release=, workers on the old and the new schema= write to the same keys. Each reads the other's entry as a miss and overwrites it, so the hit rate stays low until the rollout ends. A new release= keeps a separate entry for each release instead: prefer it for a change that ships with a deploy. A persistent=True namespace is shared across releases, so release= does not separate its entries: bump its schema= when the meaning of its values changes.

When to use invalidate() vs. purge()

Neither is needed for deploys: use release= and schema= for code/format changes, invalidate() for bad-data incidents, and purge() to reclaim memory early (see Invalidation).