Friday, December 29, 2017

Storing Python Object in Redis - The "Brute Force" Approach

Sometimes we have the need to store Python object (class instance) to "object storage" server for some reason and then to retrieve it later. This post explains how to that by using Redis as the "object storage" server.

DISCLAIMER:  This post assumes that the machine where the code is executed and the Redis server is in the same machine or located within a secure premises.

You can clone the code from:

The principle used by the code is simple:

  1. Serialize the object as string by using Python pickle.dumps() from the pickle module (
  2. Store the object as "string" data type in Redis ( and use the following formula for the key to address the object in Redis: "test-meta-webhook-" + object.subs_id , where subs_id acts as a unique identifier for the object.
  3. Retrieve the object string by using the same key used in 2. 
  4. Deserialize the object by using pickle.loads() from the pickle module.

The approach explained above is just for playground code because it probably doesn't scale as expected or is not well suited for latency sensitive application (the pickle.dumps() and pickle.loads() took too much time). However, for simple experimental code it's a nice to have "brute force" solution ;-). Below is sample output of the code:

Python Object Serialization/Deserialization to/from Redis

Post a Comment

No comments: