magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

cache:id_tags:* reverse-index keys accumulate indefinitely when using Redis/Valkey cache backend

Open CptCharlesG opened this issue 4 months ago • 9 comments

Preconditions and environment

  • Magento Open Source 2.4.9
  • Valkey cache backend (Redis)
  • Cache backend:
    • DB0: default cache
    • DB1: full page cache
    • DB2: sessions

Environment used for testing:

  • Valkey 8.1.1 (redis_version: 7.2.4)
  • phpredis 6.3.0

Steps to reproduce

  1. Enable Redis/Valkey cache:
    • DB0: default cache
    • DB1: full page cache
    • DB2: sessions
  2. Run a large site in production for months.

Expected result

When cache entries are invalidated by tags, all associated metadata should eventually be removed, including:

cache object cache:tags:* membership cache:id_tags:* reverse index

No unbounded growth of reverse-index keys should occur during normal production use.

Actual result

cache:id_tags:* keys appear to accumulate indefinitely during long-running production usage.

This results in:

steadily increasing Redis/Valkey memory usage slower cache operations large metadata structures despite a relatively small active cache cache databases growing to several gigabytes over time

Additional information

Problem

After normal Magento operation (product saves, indexing, cache invalidation, cron, frontend traffic), the number of cache:id_tags:* keys continuously grows.

Over time this results in:

  • millions of cache:id_tags:* keys
  • hundreds of MB consumed by tag metadata
  • significantly slower cache invalidation
  • increased Redis/Valkey memory usage
  • much slower bin/magento cache:flush

Example after several months of production, right after bin/magento cache:flush:

DB0 keys: ≈ 2,720,000 cache:id_tags:* 2,622,778 keys

cache:tags:f73_BLOCK_HTML MEMORY USAGE ≈ 370 MB

cache:tags:f73_CAT_P MEMORY USAGE ≈ 370 MB

Valkey memory: ≈ 2.7 GB

Investigation

After flushing only cache databases:

valkey-cli -n 0 FLUSHDB valkey-cli -n 1 FLUSHDB

(Session database was intentionally preserved.)

The cache immediately returned to:

DB0: 916 keys DB1: 45 keys DB2: 460 session keys

Valkey memory dropped dramatically:

Before: used_memory ≈ 2.7 GB After: used_memory ≈ 30 MB

Magento continued working normally.

Only a few requests later:

cache:id_tags:* = 809

which is expected.

This suggests the cache can be rebuilt from a very small baseline, while the long-term metadata growth is what eventually consumes the majority of Redis memory.

Observations

For sampled keys such as:

cache:id_tags:f73_BLOCK_...

the corresponding cache object no longer exists.

The cache item itself has been removed, while the reverse-index entry still exists.

Source code observations

Symfony::remove() performs:

$this->adapter->onRemove($cleanId);
$cache->deleteItem($cleanId);

which correctly removes reverse indices.

However bulk invalidation paths eventually execute: $this->adapter->deleteByIds($ids);

It is not obvious that deleteByIds() removes the corresponding cache:id_tags:* entries.

During cache:flush, Redis MONITOR showed numerous operations such as: SREM cache:all_ids ... but no visible deletion of cache:id_tags:*.

Suggested investigation

Please verify that all bulk cache invalidation paths (especially deleteByIds()) correctly remove the associated cache:id_tags:* reverse-index entries (created by RedisTagAdapter::onSave()), as they appear to accumulate indefinitely during normal operation.

Particular attention may be warranted for the interaction between:

Symfony::clean*() RedisTagAdapter::deleteByIds() RedisTagAdapter::onRemove()

to ensure reverse-index cleanup occurs for all invalidation paths.

If this behavior is intentional, clarification on the expected lifecycle of cache:id_tags:* keys would also be appreciated, as they appear to accumulate significantly over time in production environments.

Release note

No response

Triage and priority

  • [ ] Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • [ ] Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • [x] Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • [ ] Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • [ ] Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

CptCharlesG avatar Jul 16 '26 16:07 CptCharlesG

Hi @CptCharlesG. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce.


Join Magento Community Engineering Slack and ask your questions in #github channel. :warning: According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting. :clock10: You can find the schedule on the Magento Community Calendar page. :telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

m2-assistant[bot] avatar Jul 16 '26 16:07 m2-assistant[bot]

:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-17704 is successfully created for this GitHub issue.

github-jira-sync-bot avatar Jul 16 '26 16:07 github-jira-sync-bot

Hi @engcom-Hotel. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

  • [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
  • [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
  • [ ] 3. Add Area: XXXXX label to the ticket, indicating the functional areas it may be related to.
  • [ ] 4. Verify that the issue is reproducible on 2.4-develop branch
    Details- If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!
  • [ ] 5. Add label Issue: Confirmed once verification is complete.
  • [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.

m2-assistant[bot] avatar Jul 17 '26 09:07 m2-assistant[bot]

Hello @CptCharlesG,

Thank you for the report and collaboration!

We have tried to reproduce this issue in the latest development branch ie. 2.4-develop and the issue is reproducible for us. Hence we are confirming this issue.

Thank you for bringing this issue to our attention. If you have a potential solution, we encourage you to submit a pull request. Your contributions significantly enhance Magento Open Source for everyone. Should you need any assistance, please don't hesitate to ask!

engcom-Hotel avatar Jul 17 '26 09:07 engcom-Hotel

:x: Cannot export the issue. This GitHub issue is already linked to Jira issue(s): https://jira.corp.adobe.com/browse/AC-17704

github-jira-sync-bot avatar Jul 17 '26 09:07 github-jira-sync-bot

See https://github.com/mage-os/mageos-magento2/pull/298 for how MageOS fixes it, seems to work pretty well after some quick testing.

hostep avatar Aug 10 '26 19:08 hostep

I have the feeling we now got an official fix for this in https://github.com/magento/magento2/commit/3a6b9667113729b21b48623687fc01b3f2d8a026 (which needs https://github.com/magento/magento2/commit/327d5bfa12dabec7b3a68789cada433e23e36970 as a prerequisite, as it uses the toIdsArray method introduced here)

@CptCharlesG: not sure if you are able to test this? We on our end also started slowly testing this fix today for all the strange cache behaviors we noticed since Magento 2.4.9

hostep avatar Aug 28 '26 15:08 hostep

@CptCharlesG An official fix landed upstream in 3a6b9667113 (AC-17650), which depends on 327d5bfa12d (ACP2E-4989). The Redis tag adapter was reworked so cache:id_tags entries are pruned instead of growing indefinitely.

cc @hostep

lbajsarowicz avatar Sep 05 '26 01:09 lbajsarowicz

Thank you for your valuable contribution. Adobe Commerce Engineering team has reviewed the issue and confirmed that the fix has been delivered in the mainline codebase through 3a6b966 (AC-17650), which depends on 327d5bf (ACP2E-4989). The fix reworks the Redis tag adapter to ensure that cache:id_tags:* entries are properly cleaned up and do not grow indefinitely. The fix is planned to be backported to the 2.4.9 release line and is expected to be included in the upcoming 2.4.9-p1 release, currently targeted for January 2027. Thank you again for bringing this issue to our attention.

engcom-Charlie avatar Oct 01 '26 13:10 engcom-Charlie