Vakarelov
Vakarelov

Reputation: 97

Using Hash vs Keys for storing objects in Redis

I want to store a large object in Redis by dividing it into part and storing each part separately. This will allow me in implement lazy loading of the parts. The object is divided into a root object, and leaf objects, which are connected to the root via proxy objects. When an object is needed by the root, the proxy decides if it needs to be loaded from the database.

(This is a PHP project where the objects must be loaded between calls: Request -> (Partial-)Loading of objects -> Processing -> Response to user -> update of the database)

I want to select between storing the objects (serializations) in individual keys (following some namespace convention) or in a hash table, as field/value pair.

A major constraint is that I need the objects to expire after some time, and the time must be updated after each access, but for all of them, not only the ones that are accessed. For example, root and obj1, obj5, and obj11, are loaded and modified, but all other must have expiration time updated.

The expiration updating must be handled by Redis, because the root doesn't know what is happening in the proxies and the proxies cannot talk to each-other. Basically, I need to tell Reids to reset the expiration timer for all objects that meet some conditions. A hash makes this very easy because is can be done with a single command:

EXPIRE hashtable-name 600

(Can this be done with some pattern-matching query with keys?)

However, all in all, I want to minimize the time from load to response to the user. Thus, if keys give me noticeably better load time, while putting more work during updating the database, this may be preferred.

Some operations may involve only a few loads, but some may involve lots (all parts of the object). I am not sure for which I should be optimize.

Thanks!

Upvotes: 2

Views: 676

Answers (1)

Itamar Haber
Itamar Haber

Reputation: 50102

Hash is the way to go in your use case, given the requirement to expire all concerned objects in one fell swoop. The use of a Hash instead of independent keys will not result in any meaningful performance penalty.

Upvotes: 2

Related Questions