{"record":{"id":"3c45e88b98705774","repo":"redis/jedis","slug":"failed-to-deserialize-object","errorCode":null,"errorMessage":"Failed to deserialize object","messagePattern":"Failed to deserialize object","errorType":"exception","errorClass":"JedisCacheException","httpStatus":null,"severity":"error","filePath":"src/main/java/redis/clients/jedis/csc/CacheEntry.java","lineNumber":53,"sourceCode":"\n  private static byte[] toBytes(Object object) {\n    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();\n        ObjectOutputStream oos = new ObjectOutputStream(baos)) {\n      oos.writeObject(object);\n      oos.flush();\n      oos.close();\n      return baos.toByteArray();\n    } catch (IOException e) {\n      throw new JedisCacheException(\"Failed to serialize object\", e);\n    }\n  }\n\n  private T toObject(byte[] data) {\n    try (ByteArrayInputStream bais = new ByteArrayInputStream(data);\n        ObjectInputStream ois = new ObjectInputStream(bais)) {\n      return (T) ois.readObject();\n    } catch (IOException | ClassNotFoundException e) {\n      throw new JedisCacheException(\"Failed to deserialize object\", e);\n    }\n  }\n}\n","sourceCodeStart":35,"sourceCodeEnd":57,"githubUrl":"https://github.com/redis/jedis/blob/6dac31d4c224fb3257c216f3985340c6f500cdcb/src/main/java/redis/clients/jedis/csc/CacheEntry.java#L35-L57","documentation":"CacheEntry.toObject deserializes cached bytes with ObjectInputStream; IOException or ClassNotFoundException during readObject is wrapped in JedisCacheException('Failed to deserialize object'). This happens when bytes in the cache cannot be turned back into the value type — typically because the class changed, is missing, or the bytes were written by a different code version.","triggerScenarios":"Reading a cache value whose stored bytes were serialized with a different serialVersionUID (class evolved between writes); the deserialized class is not on the classpath (ClassNotFoundException); bytes written by another process/app with a different schema; corrupted cache bytes.","commonSituations":"Rolling deploys where old serialized entries persist in the cache while new code has modified the value class; hot reload / dev restarts reusing a serialized cache; shared cache across services with slightly different class versions.","solutions":["Fix serialVersionUID (declare and keep it stable) or clear/invalidate the cache after deploying class changes.","Ensure the value class is present in the classpath of the reading application (ClassNotFoundException case).","Wrap cache reads with a fallback that treats deserialization failure as a cache miss and reloads from Redis.","Prefer a versioned serialization format (JSON/protobuf via custom Cacheable) over Java serialization to survive schema evolution."],"exampleFix":"// before\nclass UserDto implements Serializable { String name; } // serialVersionUID auto-computed, changes on edit\n// after\nclass UserDto implements Serializable {\n  private static final long serialVersionUID = 1L;\n  String name;\n}","handlingStrategy":"fallback","validationCode":"// verify readability of previously cached entries after a deploy:\n// iterate cache entries and attempt cache.get(); treat JedisCacheException as a miss and repopulate","typeGuard":null,"tryCatchPattern":"try {\n  return cache.get(keyBytes);\n} catch (JedisCacheException e) {\n  if (e.getMessage().contains(\"Failed to deserialize\")) {\n    cache.delete(keyBytes); // stale/incompatible entry\n    return loadFromRedis(keyBytes); // fallback source of truth\n  }\n  throw e;\n}","preventionTips":["Declare and never change serialVersionUID on cached value classes","Flush/clear the cache on rolling deploys that touch cached classes","Keep the value classes on the classpath of every app sharing the cache","Prefer JSON/protobuf via a custom Cacheable for cross-version stability"],"tags":["deserialization","cache","class-not-found"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"6dac31d4c224fb3257c216f3985340c6f500cdcb","analyzedAt":"2026-09-08T04:55:01.204Z","contentChangedAt":"2026-09-08T04:55:01.204Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}