Skip to content

[Technical Debt]: Fix stale cache data in appconfig #3693

Closed

Description

@objecttothis

Currently, when the an app_config key/value pair is updated or inserted into the database, the cash gets invalidated then updated. There are two problems:
1- Calling the update_settings() function in the save() function causes a lot of calls to invalidating and updating the cache. Perhaps there is a better place to call this.
2- calling update_settings() invalidates and updates the cache, so any future instances of config('OSPOS') will be current, but existing instances of the config are stale until they are regenerated. This could cause data to be updated in the database and cache but not in the in-memory values.

For 1, We could call update_settings() in the batch_save() function after the foreach loop. This will cut down on all but three calls to save() directly. Then we could call update_settings() after those three calls. The potential risk is that if we add more calls to save() directly and forget to call update_settings() we will end up with stale data in the cache.

As for 2, we need to create a new instance of config('OSPOS') after update_settings is run. The problem with this method is that it's decentralized. Perhaps the best option here is to create a wrapper function and pass the config() instance by reference, then call both the save() function, update_settings() and refresh the config instance together. We may need two functions or to just call batch_save or save from the wrapper based on whether the passed parameters are a single row or multiple.

Activity

  1. added this to the 3.4.0 milestone on Feb 27, 2023
  2. jekkos commented on Sep 24, 2023

    @jekkos
    Member

    The crazy thing is that the CI4 cache is by default file backed, so instead of reading the values from the db, we read them from a file, which might not be all that fast in the end? I would only consider something like this if the query is very expensive, but here it should not be the case, it's a very simple table with not a lot of data.

    There is no easy in memory solution unless if we enable redis or memcached. But doing this only for this purpose feels a bit overkill. I would just forget about the cache and delegate the load call to the AppConfig model from the OSPOS wrapper instead; If we decide that this is not fast enough we can still consider an in memory solution afterwards. We could load the whole table at once as we used to do in CI3. I think there it used to be request scoped as well, we never had any performance issues on that front, so should be fine.

  3. objecttothis commented on Sep 24, 2023

    @objecttothis
    MemberAuthor

    Yes, the default is file, but I believe I changed that to be session based since we use sessions in CI3

  4. objecttothis commented on Sep 24, 2023

    @objecttothis
    MemberAuthor

    Also, we are loading the table all at once currently (if the cache is not populated), storing it in memory cache (after pulling it) and in the global array which all gets repeated if a user makes a change to one of the elements of the array. As I said in the other PR, I don't think we should get rid of it just for the sake of getting rid of it (since it's working... I think), but only if it serves a purpose, such as making the code empty value resilient without having to add in checks for empty values.

  5. jekkos commented on Sep 24, 2023

    @jekkos
    Member

    Yes, the default is file, but I believe I changed that to be session based since we use sessions in CI3

    I do believe the appconfig table was fully reloaded in memory at the start of every request in CI3.. I don''t think it was linked to the session at that point. (https://github.com/opensourcepos/opensourcepos/blob/master/application/hooks/load_config.php) . This hook is called pre controller constructor.

    That has changed now as we keep the table contents as a json array in a cache that is backed by a file, with ttl of 5 mins. I'm ok to keep it if it works and if we don't need to do any cache eviction once we update the config and refresh it from the database. I would certainly keep the class but then change the implementation a bit so that it can deal with array dereference and missing values directly.

    btw @objecttothis do you live in Nagorno Karabach currently? Was just reading the news on the situation in Azerbeidzjan. Hope you're all fine.

  6. objecttothis commented on Sep 25, 2023

    @objecttothis
    MemberAuthor

    @owlbrudder let's move the conversation here, rather than keeping it going in a merged PR.

    Can you clarify for me what you mean by the standard CI4 way of working?

    https://codeigniter4.github.io/userguide/general/configuration.html#getting-a-config-property
    CI4 has a means of accessing built-in configuration files and allowing the user to specify and modify custom configuration files. I had considered the idea of completely scrapping the CI3 implementation, establishing a custom config file with an associative array and populating that from the database, but decided to try to make something similar to our current implementation working since the CI4 conversion was immense as it was.

  7. objecttothis commented on Sep 25, 2023

    @objecttothis
    MemberAuthor

    btw @objecttothis do you live in Nagorno Karabach currently? Was just reading the news on the situation in Azerbeidzjan. Hope you're all fine.
    I'm far enough away to not be in any danger. It seems as if the fighting has stopped anyway.

    That has changed now as we keep the table contents as a json array in a cache that is backed by a file, with ttl of 5 mins. I'm ok to keep it if it works and if we don't need to do any cache eviction once we update the config and refresh it from the database. I would certainly keep the class but then change the implementation a bit so that it can deal with array dereference and missing values directly.
    Beyond the hope of performance increase, I can't remember now what was driving me to store the array in cache. I can't remember if not doing so was affecting accessibility or not. Feel free to tweak the current implementation as you see fit.

    My suggestion is that we use the current implementation if it doesn't mean peppering the code with a bunch of checks and then after CI4 has been released, we consider simplifying it in a separate PR. Thoughts?

  8. owlbrudder commented on Sep 25, 2023

    @owlbrudder
    Contributor

    @owlbrudder let's move the conversation here, rather than keeping it going in a merged PR.

    Can you clarify for me what you mean by the standard CI4 way of working?

    https://codeigniter4.github.io/userguide/general/configuration.html#getting-a-config-property CI4 has a means of accessing built-in configuration files and allowing the user to specify and modify custom configuration files. I had considered the idea of completely scrapping the CI3 implementation, establishing a custom config file with an associative array and populating that from the database, but decided to try to make something similar to our current implementation working since the CI4 conversion was immense as it was.

    Thank you for the clarification. I agree about the scale of the conversion.

  9. owlbrudder commented on Sep 25, 2023

    @owlbrudder
    Contributor

    btw @objecttothis do you live in Nagorno Karabach currently? Was just reading the news on the situation in Azerbeidzjan. Hope you're all fine.
    I'm far enough away to not be in any danger. It seems as if the fighting has stopped anyway.

    That has changed now as we keep the table contents as a json array in a cache that is backed by a file, with ttl of 5 mins. I'm ok to keep it if it works and if we don't need to do any cache eviction once we update the config and refresh it from the database. I would certainly keep the class but then change the implementation a bit so that it can deal with array dereference and missing values directly.
    Beyond the hope of performance increase, I can't remember now what was driving me to store the array in cache. I can't remember if not doing so was affecting accessibility or not. Feel free to tweak the current implementation as you see fit.

    My suggestion is that we use the current implementation if it doesn't mean peppering the code with a bunch of checks and then after CI4 has been released, we consider simplifying it in a separate PR. Thoughts?

    I expect 99.9% of the config[] problems will go away if we accept the proposal to write a new migration script to populate the database with all keys we dereference later. This avoids wrapping every dereference in a protective layer. Far better to focus on getting what we have working reliably than to rewrite at this point. Let rewrites become later point releases, but make the existing system work first.

  10. objecttothis commented on Sep 25, 2023

    @objecttothis
    MemberAuthor

    I expect 99.9% of the config[] problems will go away if we accept the proposal to write a new migration script to populate the database with all keys we dereference later. This avoids wrapping every dereference in a protective layer. Far better to focus on getting what we have working reliably than to rewrite at this point. Let rewrites become later point releases, but make the existing system work first.

    Sounds good to me. If @jekkos isn't opposed, I think you can proceed. There should be an existing migration for ci4. I'd put those changes in that migration so that we don't end up with 100's of migrations.

  11. owlbrudder commented on Sep 25, 2023

    @owlbrudder
    Contributor

    I expect 99.9% of the config[] problems will go away if we accept the proposal to write a new migration script to populate the database with all keys we dereference later. This avoids wrapping every dereference in a protective layer. Far better to focus on getting what we have working reliably than to rewrite at this point. Let rewrites become later point releases, but make the existing system work first.

    Sounds good to me. If @jekkos isn't opposed, I think you can proceed. There should be an existing migration for ci4. I'd put those changes in that migration so that we don't end up with 100's of migrations.

    @jekkos has already pointed me to the migration script for the existing database population, so I expect I can extend that very quickly. I will get onto it tomorrow.

  12. owlbrudder commented on Oct 5, 2023

    @owlbrudder
    Contributor

    There are cases in which just having the array key in existence is not enough to avoid problems. The migration script is now creating a record for 'mailchimp_api_key' but the value is the empty string. In Controllers/Config.php, for example, we need to also test if the value of 'mailchimp_api_key' is not empty before passing it to Services::encrypter->decrypt() because that crashes if not passed a valid string. This would be overcome if we implement an array dereferencing mechanism which tests for both existence and content. In the meantime, I have to submit a revision to Controllers/Config.php to implement this test. Previously it crashed because the key did not exist; now it crashes because the value is empty. It is reasonable to assume other cases where we create an app-config table record with empty data will raise similar problems.

  13. added
    CodeIgniter4
    on Dec 12, 2023
  14. modified the milestones: 3.4.0, 3.5.0 on Nov 4, 2024
  15. changed the title [-]CI4: Stale data in appconfig update of cache[/-] [+][Technical Debt]: Fix stale cache data in appconfig[/+] on Apr 1, 2026
  16. jekkos commented on Oct 2, 2026

    @jekkos
    Member

    @CodeRabbit can be closed?

  17. coderabbitai commented on Oct 2, 2026

    @coderabbitai
    Contributor
    🧩 Analysis chain

    🏁 Script executed:

    #!/bin/bash
    set -euo pipefail
    gh issue view 3693 --repo opensourcepos/opensourcepos --json title,body,state,comments,url
    fd -i '^(Config|AppConfig|OSPOS|Osp os|load_config|Cache)\.php$'
    rg -n -C3 --glob '*.php' 'mailchimp_api_key|class OSPOS|class OspOs|class AppConfig'

    Length of output: 64433


    🏁 Script executed:

    #!/bin/bash
    set -euo pipefail
    git branch --show-current
    git rev-parse HEAD
    ast-grep outline app/Config/OSPOS.php
    ast-grep outline app/Models/Appconfig.php
    ast-grep outline app/Events/Load_config.php
    ast-grep outline app/Controllers/Config.php
    cat -n app/Config/OSPOS.php
    cat -n app/Models/Appconfig.php
    cat -n app/Events/Load_config.php
    sed -n '250,290p' app/Controllers/Config.php
    rg -n -C3 --glob '*.php' --glob '!app/Language/**' 'update_settings\s*\(|settings\s*=&|config\s*=&|config\(.OSPOS.\)|appconfig->save\s*\(' app tests

    Length of output: 19549


    🏁 Script executed:

    #!/bin/bash
    set -euo pipefail
    sed -n '29,115p' app/Controllers/Config.php | cat -n
    rg -n -C2 --glob '*.php' '\$this->config\s*=\s*config\(OSPOS::class\)->settings' app

    Length of output: 9853


    @jekkos Not yet. The current code still has both problems described in #3693:

    • Repeated cache refreshes: Appconfig::batch_save() calls save() for each key. Each successful save deletes the settings cache and reloads the full table. See app/Models/Appconfig.php, Line 86 and Line 102.
    • Stale settings copies: Controllers and libraries copy config(OSPOS::class)->settings into their own arrays. Updating the shared configuration does not refresh those copies. For example, see app/Controllers/Config.php, Line 63.

    The Mailchimp empty-value problem discussed later is handled in the current code. That does not resolve the original cache problems.

    I recommend keeping this issue open until batch saves refresh settings once after a successful transaction, and same-request consumers can read the updated settings.

    You are interacting with an AI system.

  18. objecttothis commented on Oct 5, 2026

    @objecttothis
    MemberAuthor

    @coderabbitai the Mailchimp related problems will go away when #4407 is merged. The rest of the issue still needs to be resolved.

  19. coderabbitai commented on Oct 5, 2026

    @coderabbitai
    Contributor

    @objecttothis Thanks for the clarification. #4407 will address the Mailchimp problems. #3693 should remain open for the remaining work:

    • Refresh the settings cache once after a successful batch-save transaction.
    • Ensure same-request consumers read updated settings rather than stale copies.

    ✏️ Learnings added
    Learnt from: objecttothis
    URL: https://github.com/opensourcepos/opensourcepos/issues/3693
    
    Timestamp: 2026-10-05T06:17:35.130Z
    Learning: In opensourcepos/opensourcepos, the maintainer states that #4407 will address the Mailchimp-related configuration problems discussed in #3693. Issue #3693 still covers the unresolved repeated settings-cache refreshes in Appconfig::batch_save() and stale settings copies held by same-request consumers. Do not treat the Mailchimp fix alone as resolution of #3693.
    

    Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

    You are interacting with an AI system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    CodeIgniter4enhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions