skip to Main Content

I am developing a rest api that serves a game. Every three minutes a job runs on server updating an important information of this game. So after this job runs, I need to invalid the cache and create a new one with recent information.

Ok, I implemented on my application MemCached, but a senior developer said that it would be very important to have other cache. He suggested to me to use Varnish, but I really don’t know if it would fit in my logic.

Do you have any suggestions of how I could achieve this?

2

Answers


  1. You can use Mcrouter a memcached protocol router to basically replicate your Memcache.
    This config can handle your request:

    {
    "pools": {
        "A": {
            "servers": [
                // First Memcache address:
                "memcache_1_ip:11211",
                // Second Memcache address:
                "memcache_2_ip:11211"
            ]
        }
    },
    "route": {
        "type": "OperationSelectorRoute",
        "operation_policies": {
            "add": "AllSyncRoute|Pool|A",
            "delete": "AllSyncRoute|Pool|A",
            "get": "LatestRoute|Pool|A",
            "set": "AllSyncRoute|Pool|A"
        }
    }
    
    Login or Signup to reply.
  2. Varnish will work just fine in your case. Of course, Memcached is used for caching transient data whereas Varnish is a full page cache, so it’s great for reducing the load on your backend application (whichever language it’s powered with, PHP or anything).

    You will not need to make any change to your application to cache things with Varnish properly (however you could go that route as well, and adjust your app to send the proper caching headers). Simply develop the VCL (Varnish Configuration Language) file with instructions on your cache policy.

    Do not use complete copy paste for VCL files you find online. Add smallest snippest as possible, understand how things work and Varnish will not dissapoint you. Important would be:

    • Ensure that your cache varies by the API token (if you use for API authentication). You will implement this in vcl_hash procedure.

    • Integrate cache clearing in your job for updating information: Varnish cache can be cleared by use of a PURGE HTTP request (again, you’d need to develop the necessary VCL code for it, inside vcl_recv procedure).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search