Sub topic 11
Session guarantees: read-your-writes, monotonic reads and more
This chapter assumes you know what replication and eventual consistency are, and that you can read simple Python. Session guarantees are small, cheap promises made to one client. They remove the most confusing anomalies of eventual consistency without requiring linearizability.
Why sessions matter
In an eventually consistent store, a client may talk to different replicas on different requests. A write that went to replica 1 may not have reached replica 2 yet. A session is a sequence of reads and writes by one client. A session guarantee constrains what that client sees, and says nothing about what other clients see.
The four guarantees
| Guarantee | Promise | Anomaly it prevents |
|---|---|---|
| Read-your-writes | A read sees all earlier writes from the same session. | You post a comment, reload, and it is gone. |
| Monotonic reads | Once a read returns a value, later reads return that value or a newer one. | A page shows 5 replies, a refresh shows 3. |
| Monotonic writes | A session's writes are applied in the order issued. | "Delete file" lands before "create file". |
| Writes-follow-reads | A write is ordered after the writes whose results the session has read. | A reply appears before the post it answers. |
Together these four were described by Terry et al. for the Bayou system. The causal consistency model implies all four.
How systems implement them
The usual technique is to track a version token on the client. The client remembers the highest version it has written or read. A replica serves a request only if it has caught up to that version.
class Replica:
def __init__(self):
self.version = 0
self.data = {}
def write(self, key, value):
self.version += 1
self.data[key] = value
return self.version
def read(self, key, min_version):
if self.version < min_version:
return None # too stale: caller must retry elsewhere
return self.data.get(key), self.version
class Session:
def __init__(self, replicas):
self.replicas = replicas
self.last_seen = 0 # highest version written or read
def write(self, replica, key, value):
self.last_seen = max(self.last_seen, replica.write(key, value))
def read(self, key):
for replica in self.replicas:
result = replica.read(key, self.last_seen)
if result is not None:
value, version = result
self.last_seen = max(self.last_seen, version)
return value
raise RuntimeError("no replica is fresh enough")
r1, r2 = Replica(), Replica()
s = Session([r2, r1]) # r2 is tried first but is stale
s.write(r1, "x", 1)
print(s.read("x")) # 1, because r2 is skipped as too staleThis sketch uses one counter per replica for simplicity. A real system must use a version that is comparable across replicas, such as a vector clock or a log position. Updating last_seen on reads gives monotonic reads, and updating it on writes gives read-your-writes.
Common errors
- Session lost. Guarantees hold only while the client keeps its token. If a user switches devices or clears cookies, the guarantee ends.
- Sticky routing assumed to be enough. Pinning a client to one replica works until that replica fails over to a stale one.
- Expecting cross-client guarantees. Read-your-writes does not mean your friend sees your write. Only linearizability gives that.
- Forgetting availability cost. If no replica has caught up, the read must wait or fail.
Exercise
A user updates their profile photo and immediately reloads the page. The old photo appears. A second reload shows the new photo, and a third shows the old one again. Which two session guarantees are being violated?
Show answer
The first reload violates read-your-writes, because the user's own write is not visible. The third reload violates monotonic reads, because the client saw the new photo and then went back to an older value.