Responsive android application with client side cache
HTTP Caching
Caching is a technique to store resources and serve it back whenever same request if made afterwards. Instead of making trip to server, it helps us get resource from the cache. This means better user experience as client can get cache copy instantly, thus leading to decrement in bandwidth consumption and latency for both server and client. Properly used caching technique benefits in performance optimisation for both the client and server.
There are two different types of caches namely private cache and shared cache. We will focus on private cache here.
Private Cache
Private cache is for single user. User agent conforming to HTTP caching specification, browser for instance, would store the response locally and any subsequent requests with same parameter are served locally. We can utilize various http cache headers depending on our application and make our application responsive. In addition, server will also benefit by having to server less request from client. It will have tremendous impact on user experience
Cache Implementation
User agent following HTTP RFC specification are thus able to cache request. Majority of the networking library used in android and IOS along with the browser conforms to this standard. So while designing our API's if we can also focus on how we can utilize different cache headers, which is not that difficult, we can build robust and responsive application.
There are various cache-control directive to define our caching policies. Please follow the links for detail description, https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching and https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
Advantages
There are resources that remains same forever, while other may have some expiration time. In our currently implementation we are treating all resource expiry time as immediate. Furthermore, all the request made by client has to take a trip to server consuming unnecessary bandwidth. Properly used cache policy will provide better end user experience, optimum bandwidth utilization for server and minimum data uses for client, win win situation for all.
Disadvantages
Although caching as profound impact on overall app optimization, careful investigation needs to be made while designing APIs. Caching require storage, although might not be significant, they will consume some storage space in client side, however. Moreover, if there are resources that are updated frequently, improper use of caching policy might impede user for getting fresh data as cache will serve the stale data until expiry time reaches. Regardless, there are techniques to overcome such issues but it’s arduous and warrants more efforts.
Conclusion
Caching not only provides greater user experience by saving user data and battery consumption (mobile), it also help save server bandwidth. No more unnecessary trip to server, lower server latency will be beneficial for all, however, precaution needs to be made before implementing them. If configured properly, caching will helps us in optimizing both mobile and web apps, thus increase user retention and growing our user base by providing wonderful user experience.
Nice article dai. I had implemented http cache on Android using Retrofit. I wonder how that compares to local database cache. What advantages has local db cache over http cache, if it is better?
Nice one