Q-Music
Our use case was the API
for the new Q-Music
website and the iPhone
and Android applications.
Slide 12
Slide 12 text
High volume
Slide 13
Slide 13 text
Pub-Sub
Slide 14
Slide 14 text
BG jobs with Sidekiq
Slide 15
Slide 15 text
Some do’s
Slide 16
Slide 16 text
Serialize values
Slide 17
Slide 17 text
2 class QApi::Redis
3 class << self
4 def redis
5 @redis ||= Redis.new(config)
6 end
...
181 def set(key, data)
182 redis.set key, to_json(data)
183 end
184
185 def get(key)
186 from_json redis.get(key)
187 end
188
216 private
217 def from_json(result)
218 case result
219 when Array
220 result.map { |r| from_json(r) }
221 when Hash
222 result
223 when nil
224 nil
225 else
226 MultiJson.decode(result)
227 end
228 rescue MultiJson::DecodeError
229 result
230 end
231
232 def to_json(data)
233 MultiJson.encode(data)
234 end
262 end
263 end
Slide 18
Slide 18 text
Save references
Slide 19
Slide 19 text
# Write
LPUSH plays {“id”: 42, ”title”:”99 Bottles of Rum”}
LPUSH plays {“id”: 49, ”title”:”The Drunken Sailor”}
LPUSH plays {“id”: 42, ”title”:”99 Bottles of Rum”}
# Read
LRANGE plays 0 -1
don’t do this
Slide 20
Slide 20 text
it’s fine with 100
items
Slide 21
Slide 21 text
but becomes huge
with 90 000 items
Slide 22
Slide 22 text
or 1 000 000 Justin
Bieber mentions ;-)
Slide 23
Slide 23 text
# Write
SET tracks:42 {“id”: 42, ”title”:”99 Bottles of Rum”}
SET tracks:49 {“id”: 42, ”title”:” The Drunken Sailor”}
LPUSH plays tracks:42
LPUSH plays tracks:49
LPUSH plays tracks:42
# Read
LRANGE plays 0 -1
MGET tracks:42 tracks:49
do this instead
Slide 24
Slide 24 text
or in Ruby
82 def list_from_references(list, options)
83 references = redis.lrange list,
84 options[:start], options[:stop]
85 if references.any?
86 from_json redis.mget(*references)
87 else
88 []
89 end
90 end