locked
Role based caching question RRS feed

  • Question

  • Using Role based caching, I want to impliment a provider based update because my updates to the cache are metered.

    Anyone know of a good reference on how to do this?   I just need to get started, thanks.

    The problem is this, when the cache expires, I want the update to go through the cache so I wont be charged multiple times for the same update.

    Wednesday, February 13, 2013 7:52 PM

Answers

  • HI

    One possible cause is: Say a query to DB requires 1 minute. 2 incoming requests are received within 1 minute (to the same instance or different instances). When the second request is served, since the first request has not fully loaded the data and thus has not written data to cache, the second request tries to query database and then write to cache again. In addition, please use Put rather than Add when you try to add something to cache. Add will throw an exception if the item already exists. Put will replace the existing item. You can refer to http://msdn.microsoft.com/en-us/library/ee790944(v=azure.10).aspx for more information.


    Dino He
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Dino He Monday, February 25, 2013 8:20 AM
    • Marked as answer by danielsn Monday, February 25, 2013 1:59 PM
    Thursday, February 21, 2013 2:02 AM

All replies

  •  HI

    I'm not sure what you mean by provider based update. Do you mean you have some data cached in a dedicated role instance, and you want to update the data in the cache instead of the data source, and after a few minutes, you update the data source in a single request?

     

    First please note if the data source is Azure SQL Database or table storage, the cost related to transactions is relatively small. It is only needed to pay $0.01 for every 100,000 transactions (roughly identical to requests). If your service and the data storage server are in the same data center, it is not needed to pay for data transfer. If not, there's additional data transfer cost, but not too much. Thus, if cost is your concern, then there's no need to implement a complex solution that reduces a little cost.

     

    If you want to implement the solution, the obstacle in my mind is you won't get notification when an item is about to expire, then there's no guarantee that you won't miss any data. The safest solution is to update to both cache and local disk. Writing data to local disk is free, although this approach can reduce performance. Since data on local disk will never expire unless the instance is taken down for certain reason, you can write the data back to the data source any time you want.


    Dino He
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Dino He Friday, February 15, 2013 8:11 AM
    Thursday, February 14, 2013 10:18 AM
  • Hi Dino,

    Yes, my terms were not very good.

    Here is my question.  If I have a role based caching system setup and the cache has expired, since it is distributed, it is possible that a request will happen on server 1 and on server 2 at the same time.  So, I need a "provider" that I can use to make sure that only one of the servers ends up replenishing the cache.  Not only does it waste resources to have them both query the datasource, but one of them will error when it tries to write to the cache with a value that is already there.

    I am fairly new at caching in general as well as role based caching so if I am not making any sense, that is why(g).

    Thanks,

    Daniel

    Friday, February 15, 2013 3:21 PM
  • HI

    I’d like to point out that even in a load balanced scenario, a single request will only go to a single server. Your concern might happen if two requests contain the exact same input (for example, user calculates the data again with the same input). But in those cases, you won't get any error. As long as the cache key is the same, the second request's data will override the first one. The performance impact is minimum as writing to cache is fast.

     


    Dino He
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 18, 2013 9:14 AM
  • Thanks Dino,

    If that is the case, then I am using it wrong.

    What I am doing is to check if the result is in the cache, and if it isnt then I am querying the DB and then trying again.  When I try to then write it, it says it was indeed there. 

    I have since watched a video that says the cache value isnt removed from the cache when it expires, so maybe that is the problem.

    But basically I only update the cache if it says there is no value with that name, and yet, i get an error sometimes when I try to update.  (Though technically I dont know if it is happening on the cloud instance too, as I only trap the error when it runs in VS.)

    Thanks

    Tuesday, February 19, 2013 7:51 PM
  • HI

    One possible cause is: Say a query to DB requires 1 minute. 2 incoming requests are received within 1 minute (to the same instance or different instances). When the second request is served, since the first request has not fully loaded the data and thus has not written data to cache, the second request tries to query database and then write to cache again. In addition, please use Put rather than Add when you try to add something to cache. Add will throw an exception if the item already exists. Put will replace the existing item. You can refer to http://msdn.microsoft.com/en-us/library/ee790944(v=azure.10).aspx for more information.


    Dino He
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Dino He Monday, February 25, 2013 8:20 AM
    • Marked as answer by danielsn Monday, February 25, 2013 1:59 PM
    Thursday, February 21, 2013 2:02 AM
  • Hi Dino,

    I think this reponse was possibly meant for a different thread.

    Sunday, February 24, 2013 9:23 PM
  • HI

    Thanks for point it out, I update my answer, please fell free to give me the feedback.

    Thanks


    Dino He
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 25, 2013 8:21 AM
  • Thank you Dino He,

    It was the "put" that I didnt know about, but needed to.  This will fix the problems.

    Monday, February 25, 2013 2:00 PM