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

Caching Strategies for an Erlang Based Web Stack

Jan 15, 2015

Download

Technology

enriquepazperez

Summary of the different caching techniques in a high traffic web architecture, including pros, cons and use cases for each solution. Representative issues spotted in some of the caching approaches will be shared as well.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Caching Strategies for an Erlang Based Web Stack

Caching in an Erlang HTTP stackEnrique Paz @quiquepaz

1/33

Page 2: Caching Strategies for an Erlang Based Web Stack

Introduction About Me

• Software Architect• Passionate Erlang developer• Testing enthusiast• Love beautiful code!

2/33

Page 3: Caching Strategies for an Erlang Based Web Stack

Introduction What I won’t talk about

Not Covered:

• Client Side Caching• Caching Proxies• CDN Acceleration

3/33

Page 4: Caching Strategies for an Erlang Based Web Stack

Introduction About Spilgames

4/33

Page 5: Caching Strategies for an Erlang Based Web Stack

Introduction About Spilgames

5/33

Page 6: Caching Strategies for an Erlang Based Web Stack

Introduction About Spilgames

6/33

Page 7: Caching Strategies for an Erlang Based Web Stack

Why Caching?

7/33

Page 8: Caching Strategies for an Erlang Based Web Stack

Why Caching? For Performance

• To avoid disk/DB access• To avoid calculations• To avoid resource bottlenecks

8/33

Page 9: Caching Strategies for an Erlang Based Web Stack

Why Caching? For Safety

• To reduce backend load• To mitigate outages impact

9/33

Page 10: Caching Strategies for an Erlang Based Web Stack

Careful! Do Not Depend On Cache!

10/33

Page 11: Caching Strategies for an Erlang Based Web Stack

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 Strategies for an Erlang Based Web Stack

Choosing Your Caching System Considerations

• Dataset & Entry size• Should it survive restart?

I Distribution VS replication

12/33

Page 13: Caching Strategies for an Erlang Based Web Stack

Choosing Your Caching System Considerations

• Amount of different keys• Request & Update Frequency

I Validity & eviction strategies

13/33

Page 14: Caching Strategies for an Erlang Based Web Stack

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 Strategies for an Erlang Based Web Stack

Choosing Your Caching System Two Proposals

15/33

Page 16: Caching Strategies for an Erlang Based Web Stack

Memcached + erl-memcache Good For

• Not reinventing the wheel• Distributed caching• Caching outside the node• System Administrators

16/33

Page 17: Caching Strategies for an Erlang Based Web Stack

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 Strategies for an Erlang Based Web Stack

Memcached + erl-memcache How does it work?

18/33

Page 19: Caching Strategies for an Erlang Based Web Stack

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 Strategies for an Erlang Based Web Stack

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 Strategies for an Erlang Based Web Stack

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 Strategies for an Erlang Based Web Stack

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 Strategies for an Erlang Based Web Stack

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 Strategies for an Erlang Based Web Stack

erl-cache Flexibility

24/33

Page 25: Caching Strategies for an Erlang Based Web Stack

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 Strategies for an Erlang Based Web Stack

erl-cache Used At Spilgames For

26/33

Page 27: Caching Strategies for an Erlang Based Web Stack

erl-cache Used At Spilgames For

27/33

Page 28: Caching Strategies for an Erlang Based Web Stack

erl-cache Used At Spilgames For

28/33

Page 29: Caching Strategies for an Erlang Based Web Stack

Summing Up

29/33

Page 30: Caching Strategies for an Erlang Based Web Stack

Summing Up

30/33

Page 31: Caching Strategies for an Erlang Based Web Stack

Summing Up What We’ve Used

• Poolboy for worker pools• Lager for logging

31/33

Page 32: Caching Strategies for an Erlang Based Web Stack

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 Strategies for an Erlang Based Web Stack

Summing Up Questions?

33/33