cs04iz1
cs04iz1

Reputation: 1815

Redis : Multiple keys vs bigger value

I am new to redis and i am having the following question. What will it be better for my application, to have multiple keys with smaller values, or a single key with a bigger value. As an example we could have a football league containing the teams, in each team we store the players. We could have something like

(key) premier_league  
(value) {MANU : [manu_player1,....,manu_player25], ...., MAN_CITY : [man_city_player1,....,man_city_player25]}

or something like

premier_league:MANU --> {players:[manu_player1,....,manu_player25]}
premier_league:MAN_CITY --> {players:[man_city_player1,....,man_city_player25]}

Upvotes: 5

Views: 2671

Answers (1)

Itamar Haber
Itamar Haber

Reputation: 49932

Better is a subjective quality.

In terms of memory consumption, every key in the Redis database requires some overhead so the more keys you have the more RAM that you'll be "wasting". OTOH, keeping a serialized representation in a single key has the trade off in terms of the CPU and network resources that are required to access it, especially when only a subset of the information is needed.

Note that Redis offers an interesting compromise in the form of the Hash data structure - it enables using a single key for the data while still allowing access to discrete members (fields) in it.

Upvotes: 7

Related Questions