skip to Main Content

Caching is a fundamental aspect of optimizing web applications for speed and efficiency. so it’s critical to decide the best method and strategy in order to use it. as far as I’m concerned both of them (serviceworker & laravel cashing) use the HTTP Caching to cache something on the localStorage.

My question is:

  • Which method uses fewer resources?
  • Which one is an effective way for caching?
  • In which scenario is it recommended to use Redis for caching?

from my perspective, the service worker is a better option when you have static content that hardly ever gets updated. but why do people use Redis and can the serviceworker be known as clientside caching and the Cacheing from Laravel as server side caching?

2

Answers


  1. Based on my experience, Service Worker uses fewer resources from the server, but Laravel Caching, on the contrary, consumes more resources because it is on the server side.

    I think using Service Worker to cache photos, CSS, and JS files and Laravel Caching to cache queries is better.

    Redis is also used to cache data that rarely changes, and instead of doing a repeated query every time, you save it inside Redis because it has a high speed.

    Login or Signup to reply.
  2. there are many different cache layers and it will depend and your use case, most of the time you can use several layers together to reduce costs and speed up application responsiveness.

    You can cache information in the Client Side, the most common strategies are:

    • Browser Cache
    • LocalStorage
    • IndexedDB
    • Service Workers
    • Session Storage
    • ….

    You can cache information in a CDN before the request lands into your servers, this special useful when your server will always answer the request with the same result, like when resolving an image or an api call for things calculated stuff like 2+2=4 you can find more information searching by,

    • CDN Static Content Caching
    • CDN Dynamic Content Caching

    You can cache information in your Server Application using In-Memory cache: this is specially useful when you have to cache information for the same Request Life Cycle.

    • This particularly changes a lot depending the platform and framework you are using, but you will use in-memory cache when for example your application has to access multiple times the same resource during the same request lifecycle.

    You can cache information in your Server / Infrastructure: This is the most used cache strategy in the server side, specially as the cache layer scales with your infrastructure as you can have multiple servers accessing the same cached resources. In PHP Specifically you these are the most common used mechanism:

    • File System
    • Memcached,
    • Redis
    • MongoDB…

    I’ll stop there as you have more than enough to start investigating.

    Answer to your questions:

    In which scenario is it recommended to use Redis for caching?

    Redis is a tool that scale well, it is generally used when you need high availability and performance in a distributed system that is deployed cross avaialability zones and regions.

    Which method uses fewer resources?

    Web Worker will use your client resources, how much resources will depend on the amount of information that you are caching and what you are doing to create and destroy the cache, your client will pay for this.

    Laravel Cache will use your infrastructure for Caching and it will depend again what strategy you use, you will pay for this.

    Comparing one vs the other does not make much sense, as explained before these cache mechanism are different and cover different use cases.

    Which one is an effective way for caching?

    Totally depends on your use case:

    • If you have things like images that you can cache in your client browser, use Browser Cache

    • If you have to resolve static content you use CDN cache

    • If you have request that are likely to produce a unique output for your client and that data is not likely to change you can cache that data in the client side

    • If you want a Event Based PWA you can certainly use a combination if IndexedDB / localStorage + Web Workers + Socket connection

    • If you have a request that is likely to produce a Unique Dynamic Server response based that is always the same when the same params are given you can use Dynamic Content Caching

    • If you have a Third Party Request that is required during a Calculation that needs to happen every time that people is calling your APIs you can use A caching strategy like Redis to capture the third party response or pre-calculated data

    • If you have to access the same resource multiple times during the same Server request life-cycle you can use In-memory cache

    • You can certainly use multiple cache layers

    Hope this helps to clarify a bit, Still consider taking on additional resources and reading more about the mentioned caching strategies.

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