Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Caching in Codeigniter (PHP)

  • 17-07-2014 10:50am
    #1
    Registered Users, Registered Users 2 Posts: 279 ✭✭


    I use Codeigniter and am about to incorporate caching.

    I have to add caching as the request to my service backend takes approximately 5 seconds.

    Timeline

    1 Page request comes in

    2 No cache exists

    3 Calls backend services for data (takes 5 seconds)

    4 Once data is received, data is saved in cache

    5 Data is returned to user



    The issue I have is where the backend services call takes 5+ seconds. What I want to know is what happens during those 5 seconds if another request comes in looking for the same data.

    1 Page request 1 comes in

    2 No cache exists

    3 Calls backend services for data (takes 5 seconds)

    4 Page request 2 comes in looking for same information

    5 No Cache exists

    6 Calls backend services for data for request 2 (takes 5 seconds)


    7 Once data for request 1 is received, data is saved in cache

    8 Data is returned to user (request 1)

    9 Once data for request 2 is received, data is saved in cache (overwriting request 1 cache)

    10 Data us returned to user (request 2)


    Could I end up in a situation where I could have multiple calls to the backend services happening at the same time when looking for the same information?


Comments

  • Registered Users, Registered Users 2 Posts: 279 ✭✭stunmer


    From my experimenting with Codeigniter's standard output caching it looks like the above situation does exist. Even though you are caching, there can be multiple calls to the backend services if the first call hasn't returned a response and stored it in cache.

    Negative:

    If a user is unlucky enough to load a page with where cache has expired they will have to wait for the backend services to complete before they can see the page. Any users who also hit the page during the time it takes for the backend service to return the original users request will also have to wait the backend service load time (in my case approx 5 seconds). This is negative user experience.

    This can put high pressure on backend services with sudden high loads if the service is busy.



    The only solution I can see is to create a cron to update a local static file. Codeigniter will then read this static file. There should be no need for caching by Codeigniter.

    Any thoughts?


  • Registered Users, Registered Users 2 Posts: 241 ✭✭fcrossen


    stunmer wrote: »
    I have to add caching as the request to my service backend takes approximately 5 seconds.

    That does sound excessive... have you tried to address it?

    To avoid concurrent requests you might want to look at implementing your own caching algorithm. I had a similar issue a few years ago with a small web app that was polling multiple remote servers. I used a cron job to grab results and store them in a local text file. There was very little involved in the parsing of it, so further caching was not needed.


  • Registered Users, Registered Users 2 Posts: 6,286 ✭✭✭Talisman


    5 seconds is crazy wait time. You could use New Relic to monitor the performance of the application and identify the bottlenecks.

    Also if you are creating a cache use Memcached as it can be integrated into MySQL to improve the database performance. See Architecture of InnoDB and memcached Integration.


  • Registered Users, Registered Users 2 Posts: 279 ✭✭stunmer


    Thanks for the responses.

    The five seconds are taken up with calls to the Facebook Graph API just retrieving likes / posts information. Many of the calls are custom date ranges so I can't cut down the overall number of calls.
    fcrossen wrote: »
    I used a cron job to grab results and store them in a local text file.

    I am very tempted to do this. The only negative is that when I spin up another server / environment setting up the crons manually will be another task to complete. Also if there is no traffic to the site, the crons will still be running using up system's resources.


  • Registered Users, Registered Users 2 Posts: 6,286 ✭✭✭Talisman


    stunmer wrote: »
    The only negative is that when I spin up another server / environment setting up the crons manually will be another task to complete. Also if there is no traffic to the site, the crons will still be running using up system's resources.
    All the more reason to use Memcached - It's a key-value store, so you can use your query as a the key and store the corresponding data response from FB as the value.

    If you set it up on its own server, then your local server instances can all share the same cache and will cut out the round trips to Facebook if the requested object is already available.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 11,989 ✭✭✭✭Giblet


    Can you not do this async? Either through Ajax or some other means (a service which populates a local store).
    Also look into cache key locking, depending on how you expire the cache, you can mark the key as locked while you retrieve the data, and anyone hitting the lock hits old cached data until the lock is removed.


Advertisement