Elasic Cache for Redis
バージョン
$ redis-cli --version
redis-cli 5.0.7
$ redis-server -v
Redis server v=5.0.7 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=66bd629f924ac924
接続:
REDIS_URL=redis://mycloud-stage-redis-nc.sigrvp.ng.0001.apne1.cache.amazonaws.com:6379/0
$ export $(cat .env|xargs)
$ redis-cli -u $REDIS_URL PING
PONG
Python
redis-py: https://github.com/redis/redis-pydjango-redis: https://github.com/jazzband/django-redischannels_redis: https://github.com/django/channels_redis
クラスターモードへの接続
import logging
from redis.cluster import RedisCluster
ENDPOINT = "mycloud-stage-redis-test.sigrvp.clustercfg.apne1.cache.amazonaws.com"
logging.basicConfig(level=logging.INFO)
redis = RedisCluster(host=ENDPOINT, port="6379")
if redis.ping():
logging.info("Connected to Redis")
django-redis:
app/reids.py:
from django_redis.client import DefaultClient
from redis.cluster import RedisCluster
class RedisClusterClient(DefaultClient):
def connect(self, index):
url = self._server[index]
return RedisCluster.from_url(url)
app/settings.py:
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://youcloud-stage-redis-test.sigrvp.clustercfg.apne1.cache.amazonaws.com:6379/0",
"OPTIONS": {
"CLIENT_CLASS": "app.redis.RedisClusterClient",
},
}
}
セキュリティグループ設定
- TCP6379 を相手のサーバーが所属するネットワークに対して許可すること
AOF(Append-Only File)
- データの耐久性を保証 ( redis >= )
- ElastiCache for Redis でファイルのみを追加する (AOF)
- クラスターモードを無効化
- マルチ AZ 必須