History Tracking
Redlite provides built-in history tracking with time-travel queries, allowing you to retrieve the state of any key at a specific point in time.
Overview
Section titled âOverviewâHistory tracking is a three-tier opt-in system:
- Global - Track history for all keys in all databases
- Database - Track history for all keys in a specific database (0-15)
- Key - Track history for a specific key only
Each level can have independent retention policies:
- Unlimited - Keep all historical versions
- Time-based - Delete versions older than N milliseconds
- Count-based - Keep only the last N versions
Configuration
Section titled âConfigurationâEnable History Tracking
Section titled âEnable History TrackingâEnable at the global level (all databases):
HISTORY ENABLE GLOBAL [RETENTION {TIME ms|COUNT n}]Enable at the database level (database 0):
HISTORY ENABLE DATABASE 0 [RETENTION {TIME ms|COUNT n}]Enable for a specific key:
HISTORY ENABLE KEY mykey [RETENTION {TIME ms|COUNT n}]Default behavior: No retention specified = unlimited history.
Example - 30-day retention:
HISTORY ENABLE GLOBAL RETENTION TIME 2592000000Example - Keep last 100 versions:
HISTORY ENABLE KEY user:1000 RETENTION COUNT 100Disable History Tracking
Section titled âDisable History TrackingâHISTORY DISABLE GLOBALHISTORY DISABLE DATABASE 0HISTORY DISABLE KEY mykeyThree-Tier Priority
Section titled âThree-Tier PriorityâHistory configuration follows a priority cascade:
- Key-level (highest priority) - If enabled for a specific key, use key-level config
- Database-level (medium priority) - If key-level not configured, use database config
- Global-level (lowest priority) - If neither key nor database config, use global
This allows fine-grained control:
# Track everything for 30 days globallyHISTORY ENABLE GLOBAL RETENTION TIME 2592000000
# But keep user data for 100 versions insteadHISTORY ENABLE KEY user:1000 RETENTION COUNT 100
# Disable history for sensitive keysHISTORY DISABLE KEY password:secretQuerying History
Section titled âQuerying HistoryâHISTORY GET - Retrieve Historical Entries
Section titled âHISTORY GET - Retrieve Historical EntriesâGet all historical entries for a key:
HISTORY GET mykeyWith optional filters:
# Limit to 10 most recent entriesHISTORY GET mykey LIMIT 10
# Get entries from specific timestamp onwardsHISTORY GET mykey SINCE 1704067200000
# Get entries up to specific timestampHISTORY GET mykey UNTIL 1704153600000
# Get entries in a time rangeHISTORY GET mykey SINCE 1704067200000 UNTIL 1704153600000 LIMIT 20Response format:
[ [ "version", 1, "operation", "SET", "timestamp", 1704067200000, "type", "string", "data", "first_value" ], ...]HISTORY GETAT - Time-Travel Query
Section titled âHISTORY GETAT - Time-Travel QueryâGet the value of a key at a specific timestamp:
HISTORY GETAT mykey 1704067200000Example:
# Write some valuesSET mykey "v1" # timestamp: 1704067200000SET mykey "v2" # timestamp: 1704067260000SET mykey "v3" # timestamp: 1704067320000
# Query historical stateHISTORY GETAT mykey 1704067200000 # Returns "v1"HISTORY GETAT mykey 1704067260000 # Returns "v2"HISTORY GETAT mykey 1704067320000 # Returns "v3"Managing History
Section titled âManaging HistoryâHISTORY STATS - View Statistics
Section titled âHISTORY STATS - View StatisticsâGet statistics about history for a key:
HISTORY STATS mykeyReturns:
[ "total_entries", 42, "oldest_timestamp", 1704067200000, "newest_timestamp", 1704239200000, "storage_bytes", 15234]HISTORY LIST - Find Keys with History
Section titled âHISTORY LIST - Find Keys with HistoryâList all keys that have history tracking enabled:
HISTORY LIST
# With pattern matchingHISTORY LIST PATTERN "user:*"HISTORY CLEAR - Manual Cleanup
Section titled âHISTORY CLEAR - Manual CleanupâDelete history entries for a key:
# Delete all history for mykeyHISTORY CLEAR mykey
# Delete history before a timestampHISTORY CLEAR mykey BEFORE 1704067200000HISTORY PRUNE - Global Cleanup
Section titled âHISTORY PRUNE - Global CleanupâDelete all history entries before a timestamp across all keys:
HISTORY PRUNE BEFORE 1704067200000Storage
Section titled âStorageâReclaim storage with VACUUM:
VACUUMThis deletes expired keys and runs SQLite VACUUM to reclaim disk space.
Examples
Section titled âExamplesâAudit Trail
Section titled âAudit TrailâTrack all changes to a critical key:
# Enable unlimited history for audit trailHISTORY ENABLE KEY critical:key
# Application writesSET critical:key "initial"SET critical:key "updated"SET critical:key "final"
# Later, audit changesHISTORY GET critical:key# Shows all modifications with timestampsDebugging State Changes
Section titled âDebugging State ChangesâInvestigate what happened to a key:
# Check current stateGET mykey# "current_value"
# Travel back in timeHISTORY GETAT mykey 1704067200000# "previous_value"
# See full timelineHISTORY GET mykey# [... all versions ...]Performance Considerations
Section titled âPerformance Considerationsâ- Recording overhead: Minimal - async background write
- Query performance: Fast for recent entries (indexed by timestamp)
- Storage impact: ~2-5KB per version for typical values
- Retention: Automatically enforced, no manual intervention needed for time/count policies
Limitations
Section titled âLimitationsâ- History is per-key, not per-field (HSET changes history for entire hash)
- No history for transactions (MULTI/EXEC) - each command recorded separately
- No history for Pub/Sub messages
- History disabled in library mode by default (enable explicitly)
Related Commands
Section titled âRelated CommandsâVACUUM- Delete expired keys and reclaim disk spaceKEYINFO- Get key metadata (type, TTL, created/updated timestamps)