Home/Low-level design/In-Memory Database

In-Memory Database

TTL, conditional updates, and version history

A field's history is an ordered timeline, not a stack of values to fall back through. The latest version at a historical instant owns that instant even if its lifetime has expired. An expired replacement never resurrects an older indefinite value.

go
writtenAt, ttl := 4, 3
expiresAt := writtenAt + ttl
fmt.Println(7 < expiresAt) // false: expiry boundary is absent

Logical time is accepted by reads and unsuccessful conditionals as well as writes. Validate TTL and lookback bounds before accepting time, or an invalid operation can wrongly prevent a later legitimate call. Equal timestamps need processing order to break ties. A deletion marker preserves history while making current reads absent. Copy scan results so historical state remains protected.

Go models this service with a struct and pointer-receiver methods. Constructor functions return a pointer; validation failures return errors rather than panicking. Scalars in snapshot structs are copied by value. Maps, slices, and optional pointers still require detached copies when crossing the service boundary. Go maps do not have key iteration order; use ordered slices for ranked results. There are no threads, persistence, wall-clock reads, or external side effects in this exercise.

Put it into practice

Implement the exercise in a full-screen workspace, then run its tests.

Open practice
Next lessonDocument Search Engine