ERR Operation against a key holding the wrong kind of value during Redis schema migration
0 reputation · 10 Jan 2024, 00:53 UTC
0 reputation · 10 Jan 2024, 00:53 UTC
Goal: Determine a reliable strategy to revert Redis data to its previous state after a failed schema change that triggers the "ERR Operation against a key holding the wrong kind of value" error.
Constraints: Redis MULTI/EXEC does not roll back; any preceding queued commands still execute on failure, and there is no built‑in snapshot/restore mechanism within a transaction. Manual backups (BGSAVE, RDB) add operational overhead and must be tested for large datasets, while idempotent migration scripts require careful design to avoid partial updates.
Questions: What are the trade‑offs between manual backup/restore and idempotent migration scripts for schema changes in Redis? How can applications detect and handle the wrong‑kind‑of‑value error to guarantee consistency without relying on transaction rollback? Is there a pattern using Redis Functions or modules that can provide atomic schema migration with automatic compensation on failure?
31275 reputation · 10 Jan 2024, 02:45 UTC
Manual backup/restore (BGSAVE/RDB or redis-cli --rdb) gives a full point‑in‑time snapshot. It is simple to reason about — restore replaces the entire dataset — but it requires downtime or a read‑replica promotion for large datasets, and the restore is all‑or‑nothing. Testing restore time on staging with production‑like data volume is essential because fork() memory pressure and disk I/O can cause latency spikes.
Idempotent migration scripts written in Lua (EVAL/EVALSHA) or as Redis Functions (Redis 7+) run atomically on the server. They can check a key’s type with TYPE, conditionally migrate, and clean up on failure. The trade‑off is development effort: each key pattern needs a tailored script, and scripts must be chunked to stay under the lua-time-limit (default 5 s). Lua scripts are atomic — if the script errors, none of its commands apply — but they do not automatically roll back commands that ran before the script in a MULTI/EXEC block.
The error surfaces at command execution time (e.g., calling HSET on a string key). Applications should catch the Redis reply error, log the key name, expected type, and actual type, then trigger a compensating action. A safe pattern:
SCAN (never KEYS) to enumerate candidate keys.TYPE.Redis Functions allow stored server‑side functions that replicate via AOF/replication. They can encode multi‑step migrations with explicit compensation logic, but there is no automatic rollback — compensation must be coded. Deploy function changes atomically with FUNCTION LOAD followed by FUNCTION FLUSH and reload. Modules like RediSearch or RedisJSON add data types but do not change the atomicity constraints; the same compensation pattern applies.
redis-cli --rdb + redis-server --loadmodule) before running live.SET key val then HSET key field val) to verify error handling and compensation path.TYPE, RENAME, HSET, then error — confirm no partial writes remain.What is the approximate number of keys and total data size you need to migrate? This determines whether a full RDB restore is feasible or if a batched Lua/Function approach is required.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.