[Django] #34212: Redis cache client uses a read connection for incr operation

130 views
Skip to first unread message

Django

unread,
Dec 14, 2022, 6:16:35 AM12/14/22
to django-...@googlegroups.com
#34212: Redis cache client uses a read connection for incr operation
-------------------------------------+-------------------------------------
Reporter: Ilari | Owner: nobody
Sahi |
Type: Bug | Status: new
Component: Core | Version: 4.1
(Cache system) |
Severity: Normal | Keywords: redis cache incr
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
RedisCacheClient uses a read only connection for the incr operation:
https://github.com/django/django/blob/main/django/core/cache/backends/redis.py#L133

However, it is a write operation, at least according to Amazon ElastiCache
Redis server. redis-py raises a ReadOnlyError exception:

{{{
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-
packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/usr/local/lib/python3.10/site-
packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args,
**callback_kwargs)
File "/usr/local/lib/python3.10/site-
packages/django/views/generic/base.py", line 103, in view
return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python3.10/site-
packages/django/utils/decorators.py", line 46, in _wrapper
return bound_method(*args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/ratelimit/decorators.py",
line 18, in _wrapped
ratelimited = is_ratelimited(request=request, group=group, fn=fn,
File "/usr/local/lib/python3.10/site-packages/ratelimit/core.py", line
119, in is_ratelimited
usage = get_usage(request, group, fn, key, rate, method, increment)
File "/usr/local/lib/python3.10/site-packages/ratelimit/core.py", line
205, in get_usage
count = cache.incr(cache_key)
File "/usr/local/lib/python3.10/site-
packages/django/core/cache/backends/redis.py", line 214, in incr
return self._cache.incr(key, delta)
File "/usr/local/lib/python3.10/site-
packages/django/core/cache/backends/redis.py", line 136, in incr
return client.incr(key, delta)
File "/usr/local/lib/python3.10/site-packages/redis/commands/core.py",
line 1852, in incrby
return self.execute_command("INCRBY", name, amount)
File "/usr/local/lib/python3.10/site-packages/redis/client.py", line
1258, in execute_command
return conn.retry.call_with_retry(
File "/usr/local/lib/python3.10/site-packages/redis/retry.py", line 46,
in call_with_retry
return do()
File "/usr/local/lib/python3.10/site-packages/redis/client.py", line
1259, in <lambda>
lambda: self._send_command_parse_response(
File "/usr/local/lib/python3.10/site-packages/redis/client.py", line
1235, in _send_command_parse_response
return self.parse_response(conn, command_name, **options)
File "/usr/local/lib/python3.10/site-packages/redis/client.py", line
1275, in parse_response
response = connection.read_response()
File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line
827, in read_response
raise response

Exception Type: ReadOnlyError at ...
Exception Value: You can't write against a read only replica.
}}}

In incr-function, get_client should be called with write argument as True.

--
Ticket URL: <https://code.djangoproject.com/ticket/34212>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 14, 2022, 7:24:28 AM12/14/22
to django-...@googlegroups.com
#34212: Redis cache client uses a read connection for incr operation
-------------------------------------+------------------------------------
Reporter: Ilari Sahi | Owner: nobody
Type: Bug | Status: new
Component: Core (Cache system) | Version: 4.1
Severity: Normal | Resolution:
Keywords: redis cache incr | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+------------------------------------
Changes (by Mariusz Felisiak):

* cc: Daniyal Abbasi, Nick Pope (added)
* stage: Unreviewed => Accepted


Comment:

Good catch!

--
Ticket URL: <https://code.djangoproject.com/ticket/34212#comment:1>

Django

unread,
Dec 14, 2022, 8:24:56 AM12/14/22
to django-...@googlegroups.com
#34212: Redis cache client uses a read connection for incr operation
-------------------------------------+------------------------------------
Reporter: Ilari Sahi | Owner: Leo Tom
Type: Bug | Status: assigned

Component: Core (Cache system) | Version: 4.1
Severity: Normal | Resolution:
Keywords: redis cache incr | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+------------------------------------
Changes (by Leo Tom):

* owner: nobody => Leo Tom
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/34212#comment:2>

Django

unread,
Dec 15, 2022, 11:00:22 PM12/15/22
to django-...@googlegroups.com
#34212: Redis cache client uses a read connection for incr operation
-------------------------------------+------------------------------------
Reporter: Ilari Sahi | Owner: Leo Tom
Type: Bug | Status: assigned
Component: Core (Cache system) | Version: 4.1
Severity: Normal | Resolution:
Keywords: redis cache incr | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+------------------------------------
Changes (by Leo Tom):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/34212#comment:3>

Django

unread,
Dec 16, 2022, 1:04:31 AM12/16/22
to django-...@googlegroups.com
#34212: Redis cache client uses a read connection for incr operation
-------------------------------------+------------------------------------
Reporter: Ilari Sahi | Owner: Leo Tom
Type: Bug | Status: assigned
Component: Core (Cache system) | Version: 4.1
Severity: Normal | Resolution:
Keywords: redis cache incr | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
-------------------------------------+------------------------------------
Changes (by Mariusz Felisiak):

* needs_tests: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/34212#comment:4>

Django

unread,
Dec 16, 2022, 1:14:09 PM12/16/22
to django-...@googlegroups.com
#34212: Redis cache client uses a read connection for incr operation
-------------------------------------+-------------------------------------

Reporter: Ilari Sahi | Owner: Leo Tom
Type: Bug | Status: assigned
Component: Core (Cache system) | Version: 4.1
Severity: Normal | Resolution:
Keywords: redis cache incr | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_tests: 1 => 0
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/34212#comment:5>

Django

unread,
Dec 16, 2022, 1:40:39 PM12/16/22
to django-...@googlegroups.com
#34212: Redis cache client uses a read connection for incr operation
-------------------------------------+-------------------------------------
Reporter: Ilari Sahi | Owner: Leo Tom
Type: Bug | Status: closed

Component: Core (Cache system) | Version: 4.1
Severity: Normal | Resolution: fixed

Keywords: redis cache incr | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"a1bcdc94da6d597c51b4eca0411a97a6460b482e" a1bcdc94]:
{{{
#!CommitTicketReference repository=""
revision="a1bcdc94da6d597c51b4eca0411a97a6460b482e"
Fixed #34212 -- Made RedisCacheClient.incr() use write connection.

Co-authored-by: Sin-Woo Bang <sinwo...@gmail.com>
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34212#comment:6>

Reply all
Reply to author
Forward
0 new messages