Highlights

Contents

Part II
02

Key-value stores: Redis, DynamoDB

We address a record directly when the caller knows its key. The same interface can describe a disposable cache or authoritative state with very different recovery duties. We will follow lookup, conditional update, durability, and distributed key ownership.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Access-pattern-drivendesign2Key-value stores: Redis,DynamoDB3Document stores: MongoDB4Wide-column: Cassandra,Bigtable5Graph databases and SQLvs NoSQL6Eventual consistency andquorums7Case study: a completedata model

#Section 6Redis-style key-value access

A Heron worker needs the latest score for m7. It asks for score:m7 directly instead of searching all matches. A key-value store maps an identifying key to a value. The caller usually supplies the exact key, so the interface favors direct access rather than arbitrary relationship queries.

The request travels to the owner, the owner locates the key, and the result returns. The stored value can encode a score and version. A missing key needs an explicit meaning: unknown match, expired cache entry, or incomplete rebuild. Returning “not found” for all of these can hide an infrastructure failure as a product fact.

Redis also offers structured values and operations, so a design can update a field or perform a supported atomic operation without replacing an opaque byte string. The specific command and its atomic scope matter. Redis data types documentation describes those interfaces rather than promising one universal transaction boundary.

For Heron, keep authoritative events elsewhere when Redis is a rebuildable score cache. A cache miss then triggers a bounded source lookup or reconstruction, followed by cache population. If Redis instead owns the score, state the persistence, replication, and recovery contract. The familiar key syntax does not decide those obligations.

GET score:m7 HASHES TO ONE SLOT AND RETURNS VALUE PLUS VERSIONGET score:m7 HASHES TO ONE SLOT AND RETURNS VALUE PLUS VERSIONappGET score:m7hash tableh(m7){home:2, away:1}v12if Redis is a rebuildable cache, a miss rebuilds from the event log;if it owns the score, state its persistence and recovery contract
Figure 4.6Illustrative known-key lookup returns score and version; the storage role determines recovery behavior.

#Section 7Lost updates and conditional writes

Two workers read score version 7 and compute different updates. Both replace the record, so the later replacement can erase the earlier one. A lost update occurs when a change overwrites another change because it was based on an older shared state.

A conditional write applies only if the stored record satisfies a condition. In this example, both workers request “write version 8 only if the current version is 7.” The store must check and update atomically. One succeeds; the other sees a failed condition and rereads or rejects its stale operation.

The check cannot happen as a separate ordinary read followed by an unconditional write. Another actor could update the record between those steps. The storage operation must enforce the comparison inside the same atomic boundary. Returning a condition-failed result gives the application evidence that its expected state was no longer current.

This prevents an overwrite based on stale state, but it does not decide the business policy for conflicting scorers. Heron may require one authorized scorer per match, or it may serialize accepted commands through a sequencer. Conditional updates defend a record boundary; authorization and cross-record invariants need their own explicit rules.

TWO WRITERS EXPECT VERSION 7; THE ATOMIC CHECK LETS ONLY ONE WINTWO WRITERS EXPECT VERSION 7; THE ATOMIC CHECK LETS ONLY ONE WINworker Aworker Bv7 → v8if v = 7 set v = 8if v = 7 set v = 8ABpassesfails: now 8check and write are one operation inside the storea separate read-then-write lets both see 7which scorer should win is a business rule, not a version rule
Figure 4.7Illustrative concurrent version-7 writers compete for one atomic version transition; one must retry or fail.

#Section 8Cache eviction versus authoritative loss

A score key disappears because the cache needs memory. Eviction removes a cached entry under a space policy. It is safe only when the entry can be reconstructed within the service’s recovery and load budget. The same disappearance is data loss if that entry was the only authoritative copy.

An expiry time can bound the useful lifetime of a derived score, but it does not prove the entry is current before expiry. A scorer update may arrive immediately after the cache fill. Define whether updates invalidate the key, replace it with a newer version, or let a short stale interval remain acceptable.

After a cache failure, every viewer may try the source at once. The key-value access that made reads cheap can conceal a much smaller source capacity. Bound miss concurrency, coalesce identical match refreshes, and protect scorer updates from the read flood. The caching unit examines those overload policies in more detail.

For an authoritative key-value service, deletion is an application operation with durability and ordering requirements. A stale retry must not recreate a deleted score accidentally. Keep tombstones or source versions as needed for the replay contract. Calling the operation an eviction does not excuse losing a fact the service promised to retain.

THE SAME KEY, TWO JOBS: A CACHE CAN FORGET, A STORE MUST NOTTHE SAME KEY, TWO JOBS: A CACHE CAN FORGET, A STORE MUST NOTderived cachescore:m7event logevicted? rebuild from sourcebound the miss loadauthoritative storescore:m7acknowledged means durabledeletion is an operation, not an eviction
Figure 4.8Illustrative cache rebuild and authoritative recovery have different failure obligations.

#Section 9Persistence and acknowledgement boundaries

A worker receives a successful write response, then the process restarts. Was the value saved? Persistence stores state so it can survive the specified loss of volatile memory. The acknowledgement boundary tells the caller which persistence work completed before success was returned.

A snapshot periodically saves a point-in-time state. An append-only log records operations for replay. Each needs a policy for writing and flushing data. A successful in-memory mutation is a different event from a durable log flush, and replication acknowledgement is a different event again.

Redis provides RDB snapshots and AOF persistence with configurable behavior. The safety and recovery tradeoffs depend on those choices, so an authoritative design must name them. Redis persistence documentation is the source for the mechanisms; this guide assigns no universal loss window to every Redis deployment.

Heron’s derived score cache can rebuild from authoritative events, so its failure can trade availability and load without losing history. An authoritative score store needs recovery tests that kill the process or host after acknowledgement and verify retained effects. Persistence is a claim about specified failures, not simply the presence of a file on disk.

WHERE THE ACK SITS DECIDES WHAT A CRASH CAN LOSEWHERE THE ACK SITS DECIDES WHAT A CRASH CAN LOSEmemory changeappend to logfsync to diskack to clientcrash after memory or log but before fsync: change may be goneack before fsync: the client was told something that may vanishtest it: kill the host after ack, check the effect survived
Figure 4.9Illustrative mutation, log append, flush, and acknowledgement are distinct states whose order must be specified.

#Section 10DynamoDB-style partitioned access

Heron wants all events for one match in sequence order. A partition key identifies a record group used for routing and placement. A sort key orders records within a chosen group when the model supports it. The pair lets a caller locate a match and scan its bounded event range.

Distribution does not remove skew. If every viewer asks for m7, that logical owner receives concentrated demand even when many quiet matches exist. A larger number of keys helps only when traffic spreads over them. Cache hot reads or distribute derived read copies when the freshness contract permits it.

DynamoDB is a managed product; Dynamo was an earlier Amazon design. They are not interchangeable names for the same API. DynamoDB tables and local secondary indexes support specified strong-read options, while global secondary index reads are eventual. DynamoDB read consistency documentation defines the product boundaries.

An illustrative hot read rate of 10,000 requests per second divided evenly across four independently usable read owners gives 2,500 per owner. That division assumes routing distributes requests and each owner has a valid copy. It does not establish how writes converge or turn a single ordered write invariant into four independent writers.

PARTITION KEY m7 PICKS THE OWNER; THE SORT KEY ORDERS EVENTS INSIDEPARTITION KEY m7 PICKS THE OWNER; THE SORT KEY ORDERS EVENTS INSIDEowner 1owner 2owner 3owner 4partition = m7seq 1seq 2seq 3seq 4seq 5sorted by sequencea popular match can overload its single owner:10,000 reads/s ÷ 4 usable read copies = 2,500 per copyread copies do not turn one ordered writer into four
Figure 4.10Illustrative partition-and-sort-key access groups one match’s event range without claiming a physical node map.