Home/Low-level design/In-Memory Database

In-Memory Database

TTL, conditional updates, and version history

Version selection precedes TTL visibility. Find the last processed version at or before the requested time, then test its lifetime. If that version is expired, return absence; searching backward for an older unexpired value would resurrect overwritten content.

cpp
struct Version {Int written_at;std::optional<std::string> value;std::optional<Int> expires_at;};
// A null value is a deletion marker, distinct from an empty stored string.

Equal timestamps preserve operation order, so historical lookup must prefer the last appended version at that timestamp. Invalid TTL and invalid historical bounds must be checked before accepting logical time. Valid failed conditionals do advance time, even when they add no version. A timeline vector permits binary-search lookups after defining a stable timestamp/order policy. Snapshot scans must consult the same visibility function as point reads to avoid disagreement at expiry boundaries.

C++ contract conventions: numeric values use signed Int (long long); all inputs and intermediate arithmetic are within its representable range. Opt<T> means std::optional<T> and absence is std::nullopt; Vec<T> means std::vector<T>. Invalid documented arguments throw std::invalid_argument. Inputs passed by const reference remain caller owned. Returned containers and records own their contents. Calls use the declared C++ parameter types. Implicit conversions performed by the caller are outside the runtime validation contract. Unless a lesson states otherwise, string ordering is lexicographic over Unicode code points (Text) or UTF-8 identifiers (std::string).

Put it into practice

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

Open practice
Next lessonDocument Search Engine