kataras/iris · info
key not found
Error message
key not found
What it means
ErrKeyNotFound ('key not found') signals that a redis key for a session ID does not exist. Producers wrap it with fmt.Errorf to include the missing key name, so callers must check with errors.Is rather than string equality. It lets the session manager treat a missing session as an expired/invalid session instead of a hard failure.
Source
Thrown at sessions/sessiondb/redis/database.go:316
// Close terminates the redis connection.
func (db *Database) Close() error {
return closeDB(db)
}
func closeDB(db *Database) error {
return db.c.Driver.CloseConnection()
}
var (
// ErrRedisClosed an error with message 'redis: already closed'
ErrRedisClosed = errors.New("redis: already closed")
// ErrKeyNotFound a type of error of non-existing redis keys.
// The producers(the library) of this error will dynamically wrap this error(fmt.Errorf) with the key name.
// Usage:
// if err != nil && errors.Is(err, ErrKeyNotFound) {
// [...]
// }
ErrKeyNotFound = errors.New("key not found")
)
View on GitHub (pinned to 7bedaf55a0)
Solutions
- Treat it as 'session expired': regenerate a new session and re-authenticate the user
- Check errors.Is(err, redisdb.ErrKeyNotFound) before generic error handling so expiry is not logged as a failure
- Enable redis persistence (AOF/RDB) or sticky sessions if losing sessions across restarts is unacceptable
Example fix
// before
if err := db.Acquire(sid); err != nil { return err }
// after
if err := db.Acquire(sid); err != nil {
if errors.Is(err, redisdb.ErrKeyNotFound) {
sid, _ = db.Acquire("") // new session
} else { return err }
} Defensive patterns
Strategy: type-guard
Validate before calling
// cannot pre-check remote existence cheaply; optionally:
exists, _ := store.Exists(sid)
if !exists { sid = "" } // force new session Type guard
func isKeyNotFound(err error) bool { return errors.Is(err, redisdb.ErrKeyNotFound) } Try / catch
if err := db.Acquire(sid); err != nil {
if errors.Is(err, redisdb.ErrKeyNotFound) {
sid, err = db.Acquire("") // regenerate session
}
return sid, err
} Prevention
- Always branch with errors.Is(err, ErrKeyNotFound) — the message includes the key name so string matching breaks
- Design expiry as a normal flow: recreate sessions on miss
- Enable redis persistence or use session replication to reduce misses
When it happens
Trigger: Database.Acquire called with a session ID that no longer exists in redis (expired TTL, flushed DB, or never-created ID); Decode called with data/key absent from the store.
Common situations: Users returning after redis TTL expiry or a redis restart (no persistence); a load balancer routing to a different instance whose redis lacks the session; clients replaying stale session cookies after a cache flush.
Related errors
- redis: already closed
- unknown value type of %T
- not implemented yet
- session not found
- directoryPath is empty
AI-assisted analysis of kataras/iris@7bedaf55a0 (2026-08-30).
Data as JSON: /api/errors/3e574ca89b922ad4.
Report an issue: GitHub.