Sphere
Log in Join
☆ 5

redis WRONGTYPE error after deploying a schema change

Redis 7.2. After a deploy we started seeing:

WRONGTYPE Operation against a key holding the wrong kind of value

from HGET user:42 profile. We changed that key from a Hash to a JSON String in the new code, but old keys are still Hashes until they're rewritten. What's the cleanest way to roll this out without breaking reads? Happy to share more detail if it helps.

⚑ Report

4 replies

☆ 0
B byte ·2026-10-19#1

Use a versioned key so old and new formats live separately: write the new value to user:42:v2 and read with a fallback, or migrate keys on read. Don't reuse the same key name for two types. You can check the type with TYPE user:42 to confirm which format you're looking at.

⚑ Report
☆ 0
S stack55 ·2026-10-22#2

Versioned keys make sense. Is there a way to atomically migrate on read so we don't keep the fallback forever? I don't want dual-read code permanently. I confirmed with TYPE that some of the old keys are still Hashes while new ones are Strings. Versioned keys avoid the mixed-type problem entirely, which is the part I should have designed for from the start.

⚑ Report
☆ 0
2 2am ·2026-10-23#3

Yes: on a cache miss or WRONGTYPE, fetch from the source of truth, write the new-format key with a TTL, and delete the old key. Use a Lua script or a SET NX lock if multiple instances might migrate the same key. Then schedule a one-time SCAN to convert or expire remaining old keys.

⚑ Report
☆ 0
N nbf ·2026-10-25#4

Went with v2 keys plus read-through migration and a background SCAN to clean up old ones. WRONGTYPE errors stopped immediately and the fallback code will be removable once the scan completes. Reusing a key across types was the real mistake.

⚑ Report
Log in to reply.