v1.0.0

Normal-Mode Read

Last updated


This page explains which tiers Get checks in normal mode, when it treats Redis as failed, and how it degrades within the same call.

Flow


graph TB
    Start[Get key] --> R1{Memory hit?}
    R1 -->|Hit, valid| Hit[Return Data, write back to Redis in background]
    R1 -->|Hit, expired| Exp[Delete memory and file, return Not found]
    R1 -->|Miss| R2[redis.Get up to MaxRetry times]
    R2 -->|OK and parses as Cache| Store[Store in memory and return]
    R2 -->|All failed| Switch[Switch to fallback mode]
    Switch --> Local[Continue on the fallback read path]
  1. graph TB
  2. Start[Get key] --> R1{Memory hit?}
  3. R1 -->|Hit, valid| Hit[Return Data, write back to Redis in background]
  4. R1 -->|Hit, expired| Exp[Delete memory and file, return Not found]
  5. R1 -->|Miss| R2[redis.Get up to MaxRetry times]
  6. R2 -->|OK and parses as Cache| Store[Store in memory and return]
  7. R2 -->|All failed| Switch[Switch to fallback mode]
  8. Switch --> Local[Continue on the fallback read path]

Steps


StepConditionBehaviorSource
1Memory hit, not expiredReturn immediately and write back to Redis in a goroutine (Read-Repair)get.go:25-39
2Memory hit, expiredDelete the memory entry and the local JSON file, return Not foundget.go:29-34
3Memory missRun GET against Redis up to MaxRetry times (default 3); on a parseable result, store in memory and returnget.go:41-53
4Retries exhaustedTake the write lock, switch to fallback mode (starting the health check), release the lock, then call the Fallback-Mode Readget.go:55-60

Memory First


In normal mode the memory cache takes precedence over Redis. For any key this instance has written or read, Get returns the in-memory value without asking Redis again, so a key changed or deleted externally in Redis still reads as the old value here until it expires.

Retry Behavior


ItemBehavior
IntervalNo wait between attempts
Per-attempt timeoutgo-redis client defaults (the instance only sets Addr / Password / DB)
Counted as failureConnection errors, redis.Nil (key missing), a value that does not parse as the Cache structure

Which Values Read Back from Redis


Step 3 parses the Redis value into the Cache structure (key / data / type / timestamp / ttl) with json.Unmarshal, but Set writes only the JSON of Data itself to Redis (strings have their quotes trimmed). Observed results:
ScenarioResult
Set then Get on the same instanceMemory hit, returns normally
Another instance (or after a restart) calls Get for a string, number, or array valueParse fails → switch to fallback → no local file → Not found
Another instance (or after a restart) calls Get for a map or struct value (stored as a JSON object)The object parses as Cache without error; unknown fields are ignored, so Get returns nil, nil and stores that empty entry (no TTL) in memory
Get for a missing keyredis.Nil → switch to fallback → Not found

The parse-failure and missing-key cases take the instance out of normal mode until the health check's next successful ping completes recovery. In the JSON-object case the instance stays in normal mode, but the next Get for that key hits the empty memory entry and Read-Repair writes null over the Redis value.

Example


  1. value, err := rf.Get("user:1")
  2. if err != nil {
  3. // key missing, expired, or the local file failed to parse
  4. log.Println(err)
  5. return
  6. }
  7. fmt.Println(value)

Get returns only two error messages, Not found and Failed to parse; it never returns a Redis connection error. A nil value with a nil error is possible in the JSON-object case above.
中文