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:- User updates their profile name to “Alice”
- Application writes to primary:
SET user:123:name Alice - Application immediately reads from replica:
GET user:123:name - Replica hasn’t replicated yet, returns old value: “Bob”
- 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
- Client tracks sync token: The SDK maintains a
upstashSyncTokenthat’s updated after each write - Write operations: Server includes current sync token in response headers
- Read operations: Client sends latest sync token in request headers
- Server waits: If reading from replica, server waits until replica has caught up to the sync token
- Consistent read: Client receives data reflecting all previous writes
Enabling read-your-writes
Read-your-writes is enabled by default: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 writesWhat it doesn’t guarantee
❌ Reading other clients’ writes immediatelyBest practices
-
Keep read-your-writes enabled by default
-
Persist sync tokens for cross-request consistency
-
Disable only when appropriate
-
Don’t share Redis instances across users