amaron
amaron

Reputation: 57

Modeling data in Redis

I am building a system that keeps track of many counters in real time in Redis. Each counter is basically the impression, conversion details for ad keywords shown on a specific url.

ie. if 10 keywords are shown on a specific url, I need to update a count for each of those keywords for both impressions and conversions. And on each impression of a url, possibly a different set of 10 keywords can be shown.

ie. the basic data model I need is something like

I understand Redis doesnt have nested hashes so I cant store a 2 level hash as I have shown above.

What is the best way to solve this problem?

I thought of combining k1-impression and k1 conversion and making it one single field

ie like

But the problem is the lengths of 'k1', 'k2' etc is significant ( 120-150 bytes) and I dont want to replicate that data, if possible, to save on memory.

How would I go about solving this problem?

Any help will be appreciated.

Upvotes: 0

Views: 132

Answers (1)

Mark Tozzi
Mark Tozzi

Reputation: 10893

If your keywords are of significant enough length that you're worried about it, you should normalize them. Make a hash of keyword -> id, and a hash of id -> keyword, for encoding and decoding them. Then you can have per-url hashes of the form url => {kw_id:impressions => 1123, kw_id:conversions => 28}. This will also serve you well when you start needing to make indexes of the key words, which you will as soon as you get a requirement to show the top 10 best performing key words across all urls, for example.

Upvotes: 1

Related Questions