• Stars
    star
    591
  • Rank 75,679 (Top 2 %)
  • Language
    Python
  • License
    BSD 3-Clause "New...
  • Created almost 9 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Redis channel layer backend for Django Channels

channels_redis

Provides Django Channels channel layers that use Redis as a backing store.

There are two available implementations:

  • RedisChannelLayer is the original layer, and implements channel and group handling itself.
  • RedisPubSubChannelLayer is newer and leverages Redis Pub/Sub for message dispatch. This layer is currently at Beta status, meaning it may be subject to breaking changes whilst it matures.

Both layers support a single-server and sharded configurations.

Installation

pip install channels-redis

Note: Prior versions of this package were called asgi_redis and are still available under PyPI as that name if you need them for Channels 1.x projects. This package is for Channels 2 projects only.

Usage

Set up the channel layer in your Django settings file like so:

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("localhost", 6379)],
        },
    },
}

Or, you can use the alternate implementation which uses Redis Pub/Sub:

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.pubsub.RedisPubSubChannelLayer",
        "CONFIG": {
            "hosts": [("localhost", 6379)],
        },
    },
}

Possible options for CONFIG are listed below.

hosts

The server(s) to connect to, as either URIs, (host, port) tuples, or dicts conforming to redis Connection. Defaults to redis://localhost:6379. Pass multiple hosts to enable sharding, but note that changing the host list will lose some sharded data.

Sentinel connections require dicts conforming to:

{
    "sentinels": [
        ("localhost", 26379),
    ],
    "master_name": SENTINEL_MASTER_SET,
    **kwargs
}

note the additional master_name key specifying the Sentinel master set and any additional connection kwargs can also be passed. Plain Redis and Sentinel connections can be mixed and matched if sharding.

If your server is listening on a UNIX domain socket, you can also use that to connect: ["unix:///path/to/redis.sock"]. This should be slightly faster than a loopback TCP connection.

prefix

Prefix to add to all Redis keys. Defaults to asgi. If you're running two or more entirely separate channel layers through the same Redis instance, make sure they have different prefixes. All servers talking to the same layer should have the same prefix, though.

expiry

Message expiry in seconds. Defaults to 60. You generally shouldn't need to change this, but you may want to turn it down if you have peaky traffic you wish to drop, or up if you have peaky traffic you want to backlog until you get to it.

group_expiry

Group expiry in seconds. Defaults to 86400. Channels will be removed from the group after this amount of time; it's recommended you reduce it for a healthier system that encourages disconnections. This value should not be lower than the relevant timeouts in the interface server (e.g. the --websocket_timeout to daphne).

capacity

Default channel capacity. Defaults to 100. Once a channel is at capacity, it will refuse more messages. How this affects different parts of the system varies; a HTTP server will refuse connections, for example, while Django sending a response will just wait until there's space.

channel_capacity

Per-channel capacity configuration. This lets you tweak the channel capacity based on the channel name, and supports both globbing and regular expressions.

It should be a dict mapping channel name pattern to desired capacity; if the dict key is a string, it's intepreted as a glob, while if it's a compiled re object, it's treated as a regular expression.

This example sets http.request to 200, all http.response! channels to 10, and all websocket.send! channels to 20:

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("localhost", 6379)],
            "channel_capacity": {
                "http.request": 200,
                "http.response!*": 10,
                re.compile(r"^websocket.send\!.+"): 20,
            },
        },
    },
}

If you want to enforce a matching order, use an OrderedDict as the argument; channels will then be matched in the order the dict provides them.

symmetric_encryption_keys

Pass this to enable the optional symmetric encryption mode of the backend. To use it, make sure you have the cryptography package installed, or specify the cryptography extra when you install channels-redis:

pip install channels-redis[cryptography]

symmetric_encryption_keys should be a list of strings, with each string being an encryption key. The first key is always used for encryption; all are considered for decryption, so you can rotate keys without downtime - just add a new key at the start and move the old one down, then remove the old one after the message expiry time has passed.

Data is encrypted both on the wire and at rest in Redis, though we advise you also route your Redis connections over TLS for higher security; the Redis protocol is still unencrypted, and the channel and group key names could potentially contain metadata patterns of use to attackers.

Keys should have at least 32 bytes of entropy - they are passed through the SHA256 hash function before being used as an encryption key. Any string will work, but the shorter the string, the easier the encryption is to break.

If you're using Django, you may also wish to set this to your site's SECRET_KEY setting via the CHANNEL_LAYERS setting:

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": ["redis://:[email protected]:6379/0"],
            "symmetric_encryption_keys": [SECRET_KEY],
        },
    },
}

on_disconnect / on_reconnect

The PubSub layer, which maintains long-running connections to Redis, can drop messages in the event of a network partition. To handle such situations the PubSub layer accepts optional arguments which will notify consumers of Redis disconnect/reconnect events. A common use-case is for consumers to ensure that they perform a full state re-sync to ensure that no messages have been missed.

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.pubsub.RedisPubSubChannelLayer",
        "CONFIG": {
            "hosts": [...],
            "on_disconnect": "redis.disconnect",
        },
    },
}

And then in your channels consumer, you can implement the handler:

async def redis_disconnect(self, *args):
    # Handle disconnect

Dependencies

Redis server >= 5.0 is required for channels-redis. Python 3.7 or higher is required.

Used commands

Your Redis server must support the following commands:

  • RedisChannelLayer uses BZPOPMIN, DEL, EVAL, EXPIRE, KEYS, PIPELINE, ZADD, ZCOUNT, ZPOPMIN, ZRANGE, ZREM, ZREMRANGEBYSCORE
  • RedisPubSubChannelLayer uses PUBLISH, SUBSCRIBE, UNSUBSCRIBE

Local Development

You can run the necessary Redis instances in Docker with the following commands:

$ docker network create redis-network
$ docker run --rm \
    --network=redis-network \
    --name=redis-server \
    -p 6379:6379 \
    redis
$ docker run --rm \
    --network redis-network \
    --name redis-sentinel \
    -e REDIS_MASTER_HOST=redis-server \
    -e REDIS_MASTER_SET=sentinel \
    -e REDIS_SENTINEL_QUORUM=1 \
    -p 26379:26379 \
    bitnami/redis-sentinel

Contributing

Please refer to the main Channels contributing docs. That also contains advice on how to set up the development environment and run the tests.

Maintenance and Security

To report security issues, please contact [email protected]. For GPG signatures and more security process information, see https://docs.djangoproject.com/en/dev/internals/security/.

To report bugs or request new features, please open a new GitHub issue.

This repository is part of the Channels project. For the shepherd and maintenance team, please see the main Channels readme.

More Repositories

1

django

The Web framework for perfectionists with deadlines.
Python
78,641
star
2

channels

Developer-friendly asynchrony for Django
Python
6,086
star
3

daphne

Django Channels HTTP/WebSocket server
Python
2,377
star
4

djangoproject.com

Source code to djangoproject.com
PostScript
1,866
star
5

asgiref

ASGI specification and utilities
Python
1,453
star
6

django-localflavor

Country-specific Django helpers, formerly of contrib fame
Python
820
star
7

django-contrib-comments

Python
612
star
8

deps

Django Enhancement Proposals
442
star
9

djangosnippets.org

The code that powers djangosnippets.org, it allows users to post and share useful "snippets" of code.
Python
415
star
10

djangobench

Harness and benchmarks for evaluating Django's performance over time
Python
209
star
11

django-box

VM to run the Django test suite. ARCHIVED Please use https://github.com/django/django-docker-box
Python
67
star
12

django-docs-translations

Translations of the Django documentation. Questions and discussions happen on https://forum.djangoproject.com/c/internals/i18n/14. The previously used group on Google Groups is no longer being used. If you are looking for old topics, you may be able to find them at https://groups.google.com/forum/#!forum/django-i18n.
Makefile
63
star
13

code.djangoproject.com

Configuration for Django's Trac instance (code.djangoproject.com)
Python
47
star
14

code-of-conduct

Internal documentation of the DSF Code of Conduct committee
45
star
15

django-asv

Benchmarks for Django using asv
Python
40
star
16

asgi_ipc

IPC-based ASGI channel layer
Python
37
star
17

dsf-working-groups

Working group mechanism for the DSF
25
star
18

ticketbot

Django's IRC ticketbot. Linkifies tickets and changesets. Hangs out in #django and #django-dev.
Python
18
star
19

.github

Org-level integration with GitHub features
10
star
20

django-fuzzers

Python
4
star