Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance Issue: Redundant has Call in get Method Slowing Down Redis #16705

Open
kunalray1993 opened this issue Feb 28, 2025 · 2 comments
Open

Comments

@kunalray1993
Copy link

if (true !== this->has(key)) {

Description

After upgrading Phalcon from 4.0.6 to 5.8.0 and PHP from 7.2 to 8.3, we noticed a performance issue related to the get method when using Redis as a cache backend. The method first calls has (which internally executes an EXISTS command) before calling get, effectively doubling the number of Redis operations. Under high traffic, this redundancy negatively impacts Redis performance.

Expected Behavior

The get method should retrieve the value directly without first checking for existence, unless absolutely necessary. The default value handling should be done in a more optimized manner.

Current Behavior

  1. The get method first calls has -> Redis executes EXISTS key.
  2. Then, get is called -> Redis executes GET key.
  3. This results in two commands for every single retrieval operation, reducing efficiency under load.

Steps to Reproduce

  1. Upgrade Phalcon from 4.0.6 to 5.8.0.
  2. Use Redis as a cache backend.
  3. Call $cache->get('some_key', 'default_value') in a high-traffic scenario.
  4. Monitor Redis and observe the EXISTS call happening before GET.

Proposed Solution

Modify the get method to avoid calling has. Instead, rely on the GET result directly:

  • If GET returns null (or false), use the default value.
  • This would eliminate the extra EXISTS call and improve Redis performance.

Environment

  • Phalcon Version: 5.8.0
  • PHP Version: 8.3
  • Redis Version: 7.0.15
  • Operating System: Ubuntu 22.04

Image
Image

Would it be possible to optimize the get method to avoid this redundancy? Looking forward to your feedback.

@kunalray1993 kunalray1993 changed the title Performance Issue: Redundant has Call in get Method Slowing Down Redis Performance Issue: Redundant has Call in get Method Slowing Down Redis Feb 28, 2025
@noone-silent
Copy link
Contributor

I noticed that too. We should remove all additional checks for methods that using has. It is the users responsibility to check if the key exists and act accordingly in their code. This change does not only affect redis, it affects all providers.

@francoisgrogor
Copy link

Upvote this. This needs to be addressed please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants