26
votes

I am really very confused about Memcached, Redis and Varnish. I know they are used for caching, but I don't know how much they help, and how you know which one to use.

And lastly, I would like know what you would use for a site with user-uploaded pictures and videos? If it helps I am using the Django framework.

2

2 Answers

36
votes

Varnish is the odd one out, it's a server that runs as a reverse proxy in front of the real webserver (apache, nginx, etc.) and it stores the response of the server separately and could decide to serve it for a subsequent request without passing the request to the backend (the webserver), so simply it's like HTML caching.

Memcached and redis are actually data storage servers, specifically key-value storage servers. In terms of python you could say it's one huge 'dictionary', you set values with a key and retrieve them by key. There's few differences between both of them, you can simply Google memcached vs redis.

8
votes

I'm using Django and memcached, so I can tell how to use that one.
I have some data, almost never changing, that require a small amount of time (a few seconds) to be loaded.
Caching them will turn seconds into fractions of seconds, so loading time is reduced by a factor of 10.

Using memcached is very simple:

  1. Install and run memcached for your OS.

  2. Configure Django to use it as caching mechanism.
    In settings.py or equivalent set BACKEND option:
    'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache'

  3. Store and retrieve data from memcached:

    from django.core.cache import cache
    cache.set('myData',myData)
    # ... in another place:
    myData = cache.get('myData')
    if not myData:
         # re-calculate myData in case of a 'cache miss', then re-caching it.
         # myData = <calculations>
         cache.set('myData',myData)