Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api/app/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,12 @@
"DJANGO_REDIS_LOG_IGNORED_EXCEPTIONS", True
)

# Enable reading from replicas in Redis Cluster mode.
# Distributes read traffic to replica nodes (port 6380 on ElastiCache Serverless).
REDIS_CLUSTER_READ_FROM_REPLICAS = env.bool(
"REDIS_CLUSTER_READ_FROM_REPLICAS", default=True
)

CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.locmem.LocMemCache",
Expand Down
4 changes: 4 additions & 0 deletions api/core/redis_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import threading
from copy import deepcopy

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django_redis.client.default import DefaultClient # type: ignore[import-untyped]
from django_redis.exceptions import ( # type: ignore[import-untyped]
Expand Down Expand Up @@ -125,6 +126,9 @@ def get_connection(self, connection_params: dict) -> RedisCluster: # type: igno
# Add explicit socket timeout
client_cls_kwargs["socket_timeout"] = SOCKET_TIMEOUT
client_cls_kwargs["socket_keepalive"] = True
client_cls_kwargs["read_from_replicas"] = (
settings.REDIS_CLUSTER_READ_FROM_REPLICAS
)
# ... and then build and return the client
return RedisCluster(**client_cls_kwargs) # type: ignore[abstract]
except Exception as e:
Expand Down
4 changes: 4 additions & 0 deletions api/tests/unit/core/test_redis_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django_redis.exceptions import ( # type: ignore[import-untyped]
ConnectionInterrupted,
)
from pytest_django.fixtures import SettingsWrapper
from pytest_mock import MockerFixture
from redis.exceptions import RedisClusterException

Expand Down Expand Up @@ -42,8 +43,10 @@ def test_cluster_connection_factory__connect_cache(mocker: MockerFixture): # ty

def test_cluster_connection_factory__get_connection_with_non_conflicting_params( # type: ignore[no-untyped-def]
mocker: MockerFixture,
settings: SettingsWrapper,
):
# Given
settings.REDIS_CLUSTER_READ_FROM_REPLICAS = False
mockRedisCluster = mocker.patch("core.redis_cluster.RedisCluster")
connection_factory = ClusterConnectionFactory(
options={"REDIS_CLIENT_KWARGS": {"decode_responses": False}}
Expand All @@ -60,6 +63,7 @@ def test_cluster_connection_factory__get_connection_with_non_conflicting_params(
port=6379,
socket_keepalive=True,
socket_timeout=0.2,
read_from_replicas=False,
)


Expand Down
Loading