MemCachier Django Tutorial

by memcachier


MemCachier and Django on Heroku

This is an example Django 2.0 queue app (first in, first out) that uses the MemCachier add-on in Heroku. A running version of this app can be found here.

Note: this example works with Python 3 and Django 2.0. It should also work for Django 1.11. For older versions please check out our previous Django 1.6 version

Deploy to Heroku

You can deploy this app yourself to Heroku to play with.


Running Locally

Run the following commands to get started running this app locally:

$ git clone
$ cd examples-django2
$ python -m venv venv
$ source venv/bin/activate
$ pip install -r requirements.txt
$ python migrate
$ python runserver

Then visit http://localhost:8000 to play with the app.

Deploying to Heroku

Run the following commands to deploy the app to Heroku:

$ git clone
$ cd examples-django2
$ heroku create
Creating app... done, ⬢ rocky-chamber-17110 |
$ heroku addons:create heroku-postgresql:hobby-dev
$ heroku addons:create memcachier:dev
$ git push heroku master
$ heroku run python migrate
$ heroku open

Configuring MemCachier (

To configure Django to use pylibmc with SASL authentication. You'll also need to setup your environment, because pylibmc expects different environment variables than MemCachier provides. Somewhere in your file you should have the following lines:

servers = os.environ['MEMCACHIER_SERVERS']
username = os.environ['MEMCACHIER_USERNAME']
password = os.environ['MEMCACHIER_PASSWORD']

    'default': {
        # Use Django's native pylibmc backend (since Django 1.11, use
        # django-pylibmc for older versions)
        'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
        # TIMEOUT is not the connection timeout! It's the default expiration
        # timeout that should be applied to keys! Setting it to `None`
        # disables expiration.
        'TIMEOUT': None,
        'LOCATION': servers,
        'OPTIONS': {
            # Use binary memcache protocol (needed for authentication)
            'binary': True,
            'username': username,
            'password': password,
            'behaviors': {
                # Enable faster IO
                'no_block': True,
                'tcp_nodelay': True,
                # Keep connection alive
                'tcp_keepalive': True,
                # Timeout settings
                'connect_timeout': 2000, # ms
                'send_timeout': 750 * 1000, # us
                'receive_timeout': 750 * 1000, # us
                # Timeout for set/get requests (sadly timeouts don't mark a
                # server as failed, so failover only works when the connection
                # is refused)
                '_poll_timeout': 2000, # ms
                # Better failover
                'ketama': True,
                'remove_failed': 1,
                'retry_timeout': 2,
                'dead_timeout': 30,

Feel free to change the _poll_timeout setting to match your needs.

Get involved!

We are happy to receive bug reports, fixes, documentation enhancements, and other improvements.

Please report bugs via the github issue tracker.

Master git repository:

  • git clone git://


This library is BSD-licensed.