caching in an erlang http stack · 2014. 3. 10. · memcached + erl-memcache used at spilgames for...
TRANSCRIPT
![Page 2: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/2.jpg)
Introduction About Me
• Software Architect• Passionate Erlang developer• Testing enthusiast• Love beautiful code!
2/33
![Page 3: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/3.jpg)
Introduction What I won’t talk about
Not Covered:
• Client Side Caching• Caching Proxies• CDN Acceleration
3/33
![Page 4: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/4.jpg)
Introduction About Spilgames
4/33
![Page 5: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/5.jpg)
Introduction About Spilgames
5/33
![Page 6: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/6.jpg)
Introduction About Spilgames
6/33
![Page 7: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/7.jpg)
Why Caching?
7/33
![Page 8: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/8.jpg)
Why Caching? For Performance
• To avoid disk/DB access• To avoid calculations• To avoid resource bottlenecks
8/33
![Page 9: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/9.jpg)
Why Caching? For Safety
• To reduce backend load• To mitigate outages impact
9/33
![Page 10: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/10.jpg)
Careful! Do Not Depend On Cache!
10/33
![Page 11: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/11.jpg)
Careful! Do Not Depend On Cache!
Common pitfalls
• Caching layers
• Deployments and cold start misses
• Server maintenance (on caching pools)
• Cache flushing for debugging
11/33
![Page 12: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/12.jpg)
Choosing Your Caching System Considerations
• Dataset & Entry size• Should it survive restart?
I Distribution VS replication
12/33
![Page 13: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/13.jpg)
Choosing Your Caching System Considerations
• Amount of different keys• Request & Update Frequency
I Validity & eviction strategies
13/33
![Page 14: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/14.jpg)
Choosing Your Caching System Considerations
• Caching Error ValuesI If done, how to recover quickly?I If not done, what about overload?
14/33
![Page 15: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/15.jpg)
Choosing Your Caching System Two Proposals
15/33
![Page 16: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/16.jpg)
Memcached + erl-memcache Good For
• Not reinventing the wheel• Distributed caching• Caching outside the node• System Administrators
16/33
![Page 17: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/17.jpg)
Memcached + erl-memcache How does it work?
erl-memcache
• Memcached• Pool of tcp connections• Memcached binary protocol• Detailed memory management• If local, memcached can be supervised
17/33
![Page 18: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/18.jpg)
Memcached + erl-memcache How does it work?
18/33
![Page 19: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/19.jpg)
Memcached + erl-memcache Used At Spilgames For
• Minimizing disk/DB access in Spilgames Storage Platform• https://github.com/spilgames/erl-memcache
19/33
![Page 20: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/20.jpg)
erl-cache Good For
erl-cache
• In node caching (small datasets)• Concurrency + long lived data• Per key caching strategies• Error awareness• Non intrusive memoization
20/33
![Page 21: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/21.jpg)
erl-cache How does it work?
• gen_servers with protected ETSs• Per cache server stats• Periodic eviction• Auto refresh overdue entries
21/33
![Page 22: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/22.jpg)
erl-cache How does it work?
• start_cache(Cache, DefaultOpts)• stop_cache(Cache)• get(Cache, Key, Opts)
• set(Cache, Key, Value, Opts)• evict(Cache, Key)• get_stats(Cache)
?CACHE( s1 , [ { wa i t _ fo r_ re f resh , f a l s e } ] ) .operate ( Inpu t ) −>
{ ok , Output } = extra_complex_op ( Inpu t ) ,Output .
22/33
![Page 23: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/23.jpg)
erl-cache Flexibility
Configuration per server:• validity & eviction times• refresh_callback• wait_for_refresh• sync or async set & evict• is_error_callback• error_validity
23/33
![Page 24: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/24.jpg)
erl-cache Flexibility
24/33
![Page 25: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/25.jpg)
erl-cache Used At Spilgames For
• Avoiding inline expensive calculations• Protecting the app running the bussiness logic• https://github.com/spilgames/erl-cache
25/33
![Page 26: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/26.jpg)
erl-cache Used At Spilgames For
26/33
![Page 27: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/27.jpg)
erl-cache Used At Spilgames For
27/33
![Page 28: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/28.jpg)
erl-cache Used At Spilgames For
28/33
![Page 29: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/29.jpg)
Summing Up
29/33
![Page 30: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/30.jpg)
Summing Up
30/33
![Page 31: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/31.jpg)
Summing Up What We’ve Used
• Poolboy for worker pools• Lager for logging
31/33
![Page 32: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/32.jpg)
Summing Up What We’ve Contributed
• http://github.com/spilgames/erl-memcache• http://github.com/spilgames/erl-cache• http://github.com/spilgames/erl-decorator-pt
32/33
![Page 33: Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For Minimizing disk/DB access inSpilgames Storage Platform 19/33](https://reader035.vdocument.in/reader035/viewer/2022071420/6119eb4578f96b4dad114273/html5/thumbnails/33.jpg)
Summing Up Questions?
33/33