06.Caching Techniques - JohnyzHub/jax-rs GitHub Wiki

Caching

Caching in software development is a technique that provides ability to access data at higher speed. Caching serves as an intermediary component between the primary storage appliance and the recipient hardware or software device to reduce the latency in data access.

In case of HTTP, the caching is little different as it doesn't have any intermediary storage component. It has several ways of notifying the client about the availability of updated data and thereby reduces the network traffic by avoid sending the duplicate data. But not between restful web service and the database.

JAX-RS API embraces the HTTP caching features for managing the cache of the results returned by restful web services.

Types:
The key to making caching work effectively is to use HTTP caching headers.
  • Strong caching headers: These headers specify how long a cached resource is valid.
    • Expires
    • Cache-Control max-age
  • Weak caching headers: These headers help the browser decide if it needs to fetch an item from cache by issuing a conditional GET request.
    • Last-Modified
    • ETag
Cache-Control:

This is a general header field. This is used to specify directives for caching mechanisms in both requests and responses. Cache-control gives more control over web caching. Refer this link to know more about cache-control Cache-control supports several directives. The popular one is max-age. This directive suggests (in seconds denominations) how long the cached resource is valid.


For working examples, refer
Server: MovieCachedService.listMovies()
Client: MovieCacheClient.findMovies_maxage()

last-modified:
last-modified header value in the response indicates when the entity present in the response was last modified. Client can make use of this value to make another request in combination with if-modified-since or if-unmodified-since.
if-modified-since:
The precondition validation will be passed if set with pre-modified date.
This is useful in reading(ie GET) the data only if modified.
if-unmodified-since:
The precondition validation will be passed if set with post-modified date.
This is useful in PUT/POST calls to perform the insert/update operation only if the object is not modified to avoid duplicate operation to address concurrency issues.


For working examples, refer
Server:
MovieCachedService.getMovie()
MovieCachedService.updateMovie_unmodified()
Client:
MovieCacheClient.findMovie_ifModified()
MovieCacheClient.findMovie_ifUnmodified()
MovieCacheClient.updateMovie_ifUnmodified()

EntityTag:

EntityTag is an unique opaque identifier, that represents the state of the entity returned in the response object. Typically this is hashcode of the object. This etag is useful in the scenarios where the service consumer(ie. client) is interested in the latest object not just because the object is updated(ie. last modified date is changed) but a set(one or more) of the object attributes are changed.

match:
The precondition validation will be passed if set with matching entity tag.
This is useful in PUT/POST calls to perform the insert/update operation only if the object is not modified to avoid duplicate operation to address concurrency issues.
none-match:
The precondition validation will be passed if set with non matching entity tag.
This is useful in reading the latest object, only if modified.


For working examples, refer
Server:
MovieCachedService.getActorInfo()
Client:
MovieCacheClient.findActor_etag_match()
MovieCacheClient.findActor_etag_unmatch()

Conditional data update:
To perform the conditional update on the object, a combination of last modified date and entitytag may be used with appropriate headers.

When last modified date and entity tag are used together, the results seems to be like below:

results table
etag-lmd
acronyms
description


For working examples, refer
Server:
MovieCachedService.updateMovie()
Client:
MovieCacheClient.updateMovie_etag_NM_lastModified_NM()
MovieCacheClient.updateMovie_etag_M_lastModified_NM()
MovieCacheClient.updateMovie_etag_NM_lastModified_M()
MovieCacheClient.updateMovie_etag_M_lastModified_M()

⚠️ **GitHub.com Fallback** ⚠️