Skip to main content

Overview

Read-your-writes consistency guarantees that any read operation from a client will always reflect the effects of all previous write operations from that same client. This is especially important in globally distributed databases where replication lag can cause stale reads.

The problem: eventual consistency

Upstash Redis supports global read replicas for low-latency reads worldwide. However, replication takes time:
This can lead to confusing application behavior:
  1. User updates their profile name to “Alice”
  2. Application writes to primary: SET user:123:name Alice
  3. Application immediately reads from replica: GET user:123:name
  4. Replica hasn’t replicated yet, returns old value: “Bob”
  5. User sees their old name despite just updating it

The solution: sync tokens

Upstash implements read-your-writes using sync tokens. When enabled (the default), each write response includes a sync token that represents the replication state:

How it works

  1. Client tracks sync token: The SDK maintains a upstashSyncToken that’s updated after each write
  2. Write operations: Server includes current sync token in response headers
  3. Read operations: Client sends latest sync token in request headers
  4. Server waits: If reading from replica, server waits until replica has caught up to the sync token
  5. Consistent read: Client receives data reflecting all previous writes

Enabling read-your-writes

Read-your-writes is enabled by default:
To disable:
Disabling read-your-writes can provide lower latency for read-heavy workloads where stale reads are acceptable.

Using sync tokens

Automatic sync token management

The SDK automatically manages sync tokens:

Manual sync token management

For advanced use cases, you can manually access and set sync tokens:

Cross-request consistency

In serverless environments, each request typically creates a new Redis client. To maintain consistency across requests:

Using session storage

Using cookies

Performance considerations

Latency impact

Read-your-writes can add latency to reads if replicas are behind:

When to disable

Consider disabling read-your-writes for:
  • Analytics and metrics: Stale data is acceptable
  • Caching scenarios: Eventual consistency is fine
  • Read-heavy workloads: Lower latency is more important than consistency
  • Public data: Data that doesn’t change frequently

When to keep enabled

  • User-facing data: Profile updates, settings, etc.
  • After write operations: Immediately reading what you just wrote
  • Financial operations: Ensuring consistency is critical
  • Default choice: When in doubt, keep it enabled

Consistency guarantees

What read-your-writes guarantees

✅ Your reads reflect your own writes
✅ Monotonic reads (won’t see older data after newer data)

What it doesn’t guarantee

❌ Reading other clients’ writes immediately
❌ Strong consistency across all clients

Best practices

  1. Keep read-your-writes enabled by default
  2. Persist sync tokens for cross-request consistency
  3. Disable only when appropriate
  4. Don’t share Redis instances across users

Example: user profile update