small bite-sized steps
• not a huge, forklift replacement
rearchitecture / reengineering project
fast
“i only block for
memcached”
scalable
many client libraries
• might be TOO many
• the hit list...
• Java ==> spymemcached
• C ==> libmemcached
• Python, Ruby, etc ==>
• libmemcached wrappers
1st school of thought
• expirations hide bugs
• you should be doing proper invalidations
• (aka, deletes)
• coherency!
school 2
• it’s 3AM and I can’t think anymore
• business guy:
• “sessions should auto-logout after 30
minutes due to bank security policy”
put sessions
in memcached?
• just a config change
• eg, Ruby on Rails
good
• can load-balance requests to any web host
• don’t touch the RDBMS on every web
request
bad
• could lose a user’s session
solution
• save sessions to memcached
• the first time, also save to RDBMS
• ideally, asynchronously
• on cache miss, restore from RDBMS
solution
• save sessions to memcached
• the first time, also save to RDBMS
• ideally, asynchronously
• on cache miss, restore from RDBMS
in the background...
• have a job querying the RDBMS
• cron job?
• the job queries for “old” looking session
records in the sessions table
• refresh old session records from
memcached
add vs replace vs set
append vs prepend
CAS
• compare - and - swap
incr and decr
• no negative numbers
queueing
• “hey, with those primitives, I could build a queue!”
don’t
• memcached is lossy
• protocol is incorrect for a queue
• instead
• gearman
• beanstalkd
• etc
cache stampedes
• gearman job-unique-id
• encode a timestamp in your values
• one app node randomly decides to
refresh slightly early
coherency
denormalization
• or copies of data
example: changing a
product price
memcached UDF’s
• another great tool in your toolbox
• on a database trigger, delete stuff from
memcached
memcached UDF’s
• works even if you do UPDATES with fancy
WHERE clauses
multigets
• they are your friend
• memcached is fast, but...
• imagine 1ms for a get request
• 200 serial gets ==> 200ms
a resultset loop
foreach product in resultset
c = memcached.get(product.category_id)
do something with c
2 loops
for product in resultset
multiget_request.append(product.category_id)
multiget_response = memcachedClient.multiget(
multiget_request)
for c in multiget_response
do something with c
memcached slabber
• allocates memory into slabs
• it might “learn” the wrong slab sizes
• watch eviction stats
0 comments
Post a comment