Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For...

33
Caching in an Erlang HTTP stack Enrique Paz @quiquepaz 1/33

Transcript of Caching in an Erlang HTTP stack · 2014. 3. 10. · Memcached + erl-memcache Used At Spilgames For...

Page 1: 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

Caching in an Erlang HTTP stackEnrique Paz @quiquepaz

1/33

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Summing Up Questions?

33/33