Skip to content

cache_key already exists errors in PostgreSQL #255

@FlipperPA

Description

@FlipperPA

We're having an issues using Django's database cache backend with PostgreSQL and django-ratelimit. We're wondering if this rings a bell with anyone.

Here's a sample of the errors we're seeing in PostgreSQL:

--
Jan 13 13:23:52 myco-pg1-w postgres[276652]: [16-1] 61e06d19.438ac (myco_web_user@myco_web) ERROR:  duplicate key value violates unique constraint "django_db_cache_pkey"
Jan 13 13:23:52 myco-pg1-w postgres[276652]: [16-2] 61e06d19.438ac (myco_web_user@myco_web) DETAIL:  Key (cache_key)=(:1:rl:28c8c88c214fb338eda51b4670e2ecc3) already exists.
Jan 13 13:23:52 myco-pg1-w postgres[276652]: [16-3] 61e06d19.438ac (myco_web_user@myco_web) STATEMENT:  INSERT INTO "django_db_cache" ("cache_key", "value", "expires") VALUES (':1:rl:28c8c88c214fb338eda51b4670e2ecc3', 'gAVLAS4=', '2022-01-13T19:23:57'::timestamp)
--
Jan 13 13:24:08 myco-pg1-w postgres[242597]: [222-1] 61e06bfe.3b3a5 (myco_web_user@myco_web) ERROR:  duplicate key value violates unique constraint "django_db_cache_pkey"
Jan 13 13:24:08 myco-pg1-w postgres[242597]: [222-2] 61e06bfe.3b3a5 (myco_web_user@myco_web) DETAIL:  Key (cache_key)=(:1:rl:cf8c7c89bd2905e99bf28775266466b1) already exists.
Jan 13 13:24:08 myco-pg1-w postgres[242597]: [222-3] 61e06bfe.3b3a5 (myco_web_user@myco_web) STATEMENT:  INSERT INTO "django_db_cache" ("cache_key", "value", "expires") VALUES (':1:rl:cf8c7c89bd2905e99bf28775266466b1', 'gAVLAS4=', '2022-01-13T19:24:13'::timestamp)
--
Jan 13 13:24:20 myco-pg1-w postgres[17644]: [15-1] 61e06e37.44ec (myco_web_user@myco_web) ERROR:  duplicate key value violates unique constraint "django_db_cache_pkey"
Jan 13 13:24:20 myco-pg1-w postgres[17644]: [15-2] 61e06e37.44ec (myco_web_user@myco_web) DETAIL:  Key (cache_key)=(:1:rl:aefc785514b81360eb2a7347251def1a) already exists.
Jan 13 13:24:20 myco-pg1-w postgres[17644]: [15-3] 61e06e37.44ec (myco_web_user@myco_web) STATEMENT:  INSERT INTO "django_db_cache" ("cache_key", "value", "expires") VALUES (':1:rl:aefc785514b81360eb2a7347251def1a', 'gAVLAS4=', '2022-01-13T19:24:25'::timestamp)
--
Jan 13 13:24:44 myco-pg1-w postgres[11512]: [15-1] 61e06dc2.2cf8 (myco_web_user@myco_web) ERROR:  duplicate key value violates unique constraint "django_db_cache_pkey"
Jan 13 13:24:44 myco-pg1-w postgres[11512]: [15-2] 61e06dc2.2cf8 (myco_web_user@myco_web) DETAIL:  Key (cache_key)=(:1:rl:c68e29bf5507ec60e8f7b9dd9cc2f87f) already exists.
Jan 13 13:24:44 myco-pg1-w postgres[11512]: [15-3] 61e06dc2.2cf8 (myco_web_user@myco_web) STATEMENT:  INSERT INTO "django_db_cache" ("cache_key", "value", "expires") VALUES (':1:rl:c68e29bf5507ec60e8f7b9dd9cc2f87f', 'gAVLAS4=', '2022-01-13T19:24:49'::timestamp)

We noticed that the offending cache keys are generated by django-ratelimit because of the :1:rl: prefix in the cache_key.

When we de-pickle the values, they're single integers, like 1, 2, 3, etc. I haven't taken a deep dive into the code yet. Django's cache DB backend code runs everything in a try / except block, which would explain why we are not seeing errors from Django itself, and only in the database. The relevant block seems to be here: https://github.com/django/django/blob/main/django/core/cache/backends/db.py#L135-L190

But it also seems that the if / elif / else block, following the SELECT, should prevent a key conflict. Does this ring a bell for anyone as an issue? This would be difficult to do a simple repro for, but we can try, if necessary.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions