Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Client-side caching introduction

Server-assisted, client-side caching in Redis

Client-side caching reduces network traffic between a Redis client and the server, which generally improves performance.

By default, an application server (which sits between the user app and the database) contacts the Redis database server through the client library for every read request. The diagram below shows the flow of communication from the user app, through the application server to the database and back again:

When you use client-side caching, the client library maintains a local cache of data items as it retrieves them from the database. When the same items are needed again, the client can satisfy the read requests from the cache instead of the database:

Accessing the cache is much faster than communicating with the database over the network and it reduces network traffic. Client-side caching reduces the load on the database server, so you may be able to run it using less hardware resources.

As with other forms of caching, client-side caching works well in the very common use case where a small subset of the data is accessed much more frequently than the rest of the data (according to the Pareto principle).

Updating the cache when the data changes

All caching systems must implement a scheme to update data in the cache when the corresponding data changes in the main database. Redis uses an approach called tracking.

When client-side caching is enabled, the Redis server remembers or tracks the set of keys that each client connection has previously read. This includes cases where the client reads data directly, as with the GET command, and also where the server calculates values from the stored data, as with STRLEN. When any client writes new data to a tracked key, the server sends an invalidation message to all clients that have accessed that key previously. This message warns the clients that their cached copies of the data are no longer valid and the clients will evict the stale data in response. Next time a client reads from the same key, it will access the database directly and refresh its cache with the updated data.

Note:
If any connection from a client gets disconnected (including one from a connection pool), then the client will flush all keys from the client-side cache. Caching then resumes for subsequent reads from the connections that are still active.

The sequence diagram below shows how two clients might interact as they access and update the same key:

Which client libraries support client-side caching?

The following client libraries support CSC from the stated version onwards:

Client Version
redis-py v5.1.0
Jedis v5.2.0

Which commands can cache data?

All read-only commands (with the @read ACL category) will use cached data, except for the following:

You can use the MONITOR command to check the server's behavior when you are using client-side caching. Because MONITOR only reports activity from the server, you should find the first cacheable access to a key causes a response from the server. However, subsequent accesses are satisfied by the cache, and so MONITOR should report no server activity if client-side caching is working correctly.

What data gets cached for a command?

Broadly speaking, the data from the specific response to a command invocation gets cached after it is used for the first time. Subsets of that data or values calculated from it are retrieved from the server as usual and then cached separately. For example:

Usage recommendations

Like any caching system, client-side caching has some limitations:

Below are some guidelines to help you use client-side caching efficiently, within these limitations:

RATE THIS PAGE
Back to top ↑