2. Databases
WE LOVE THEM
...
Except when we don’t
2
3. When Databases Rule
• Many access patterns on the same set of
data
• Transactions (both monetary and isolated
units of work)
• Don’t know what the end state access
patterns will be
• Always :)
3
4. When Databases Suck
• Lots of concurrent users
• ORMs
• Big Data Sets
• Small Pockets of VERY hot data
4
5. How Caching Works
• External vs. built-in caching
• MySQL Query Cache
• InnoDB Buffer Pool
• Rails SOMETHING
5
14. Stale Data
• Must maintain consistency between the
database and the cache from within the
application
• Extremely difficult to validate correctness
14
15. Cache Warm-Up
• All attempts to read from the cache are
CACHE MISSES, which require three
operations.
• This can result in a significant degradation
of response time.
• Usually accompanied by a Thundering Herd
15
16. Use Cases
• Sessions • Tag Clouds
• Popular Items • Auto-suggest lists
• Full Page Cache • Relationships
• Profile • User Information
Information
• Online Users
• User Preferences • Statistics
16
17. Memcached
Memcached is an in-memory key-
value store for small chunks of
arbitrary data (strings, objects) from
results of database calls, API calls,
or page rendering.
17
18. Redis
Redis is an open source, advanced
key-value store. It is often referred
to as a “data structure server” since
keys can contain strings, hashes,
lists, sets and sorted sets.
18
20. Consistent Hashing
• Each Key deterministically goes to a
particular server. Think (KEY % SERVERS)
20
21. Memcached
• Dead Simple & Battle Tested
• Fast
• Non-Blocking get()/set()
• Multi-Threaded
• Consistent Hashing
21
22. Memcached Example
employee_id = 1234
employee_json = {
name => ‘Ryan Lowe’,
title => ‘Production Engineer’ }
set(employee_id, employee_json)
get(employee_id) [Returns employee_json]
22
23. But I don’t want all the
data
• What if I just want the name?
• 64 Bytes for the object vs. 10 for just the
name :-(
• 6x network traffic
• More work for the application
• Fatter applications
23
24. Redis
• Advanced Data Types
• Replication
• Persistence
• Usually Fast
• Very Cool Atomic Operations
24
25. Redis: The Bad
• Single-Threaded
• Limited client support for consistent
hashing
• Significant overhead for persistence (do be
discussed later)
• Not widely deployed (compared to
Memcached)
25
34. Instrumentation
• Redis: info
• Memcached: stats
• Both give system information, connections,
hits, misses, etc.
• Graphite most of the metrics!!!
34