|
| 1 | +--- |
| 2 | +categories: |
| 3 | +- docs |
| 4 | +- develop |
| 5 | +- stack |
| 6 | +- oss |
| 7 | +- rs |
| 8 | +- rc |
| 9 | +- oss |
| 10 | +- kubernetes |
| 11 | +- clients |
| 12 | +description: Server-assisted, client-side caching in Redis |
| 13 | +linkTitle: Client-side caching |
| 14 | +title: Client-side caching introduction |
| 15 | +weight: 20 |
| 16 | +--- |
| 17 | + |
| 18 | +*Client-side caching* reduces network traffic between |
| 19 | +a Redis client and the server, which generally improves performance. |
| 20 | + |
| 21 | +By default, an [application server](https://en.wikipedia.org/wiki/Application_server) |
| 22 | +(which sits between the user app and the database) contacts the |
| 23 | +Redis database server through the client library for every read request. |
| 24 | +The diagram below shows the flow of communication from the user app, |
| 25 | +through the application server to the database and back again: |
| 26 | + |
| 27 | +{{< image filename="images/csc/CSCNoCache.drawio.svg" >}} |
| 28 | + |
| 29 | +When you use client-side caching, the client library |
| 30 | +maintains a local cache of data items as it retrieves them |
| 31 | +from the database. When the same items are needed again, the client |
| 32 | +can satisfy the read requests from the cache instead of the database: |
| 33 | + |
| 34 | +{{< image filename="images/csc/CSCWithCache.drawio.svg" >}} |
| 35 | + |
| 36 | +Accessing the cache is much faster than communicating with the database over the |
| 37 | +network and it reduces network traffic. Client-side caching reduces |
| 38 | +the load on the database server, so you may be able to run it using less hardware |
| 39 | +resources. |
| 40 | + |
| 41 | +As with other forms of [caching](https://en.wikipedia.org/wiki/Cache_(computing)), |
| 42 | +client-side caching works well in the very common use case where a small subset of the data |
| 43 | +is accessed much more frequently than the rest of the data (according |
| 44 | +to the [Pareto principle](https://en.wikipedia.org/wiki/Pareto_principle)). |
| 45 | + |
| 46 | +## Updating the cache when the data changes {#tracking} |
| 47 | + |
| 48 | +All caching systems must implement a scheme to update data in the cache |
| 49 | +when the corresponding data changes in the main database. Redis uses an |
| 50 | +approach called *tracking*. |
| 51 | + |
| 52 | +When client-side caching is enabled, the Redis server remembers or *tracks* the set of keys |
| 53 | +that each client connection has previously read. This includes cases where the client |
| 54 | +reads data directly, as with the [`GET`]({{< relref "/commands/get" >}}) |
| 55 | +command, and also where the server calculates values from the stored data, |
| 56 | +as with [`STRLEN`]({{< relref "/commands/strlen" >}}). When any client |
| 57 | +writes new data to a tracked key, the server sends an invalidation message |
| 58 | +to all clients that have accessed that key previously. This message warns |
| 59 | +the clients that their cached copies of the data are no longer valid and the clients |
| 60 | +will evict the stale data in response. Next time a client reads from |
| 61 | +the same key, it will access the database directly and refresh its cache |
| 62 | +with the updated data. |
| 63 | + |
| 64 | +{{< note >}}If any connection from a client gets disconnected (including |
| 65 | +one from a connection pool), then the client will flush all keys from the |
| 66 | +client-side cache. Caching then resumes for subsequent reads from the |
| 67 | +connections that are still active. |
| 68 | +{{< /note >}} |
| 69 | + |
| 70 | +The sequence diagram below shows how two clients might interact as they |
| 71 | +access and update the same key: |
| 72 | + |
| 73 | +{{< image filename="images/csc/CSCSeqDiagram.drawio.svg" >}} |
| 74 | + |
| 75 | +## Which commands can cache data? |
| 76 | + |
| 77 | +All read-only commands (with the `@read` |
| 78 | +[ACL category]({{< relref "/operate/oss_and_stack/management/security/acl" >}})) |
| 79 | +will use cached data, except for the following: |
| 80 | + |
| 81 | +- Any commands for |
| 82 | + [probabilistic data types]({{< relref "/develop/data-types/probabilistic" >}}). |
| 83 | + These types are designed to be updated frequently, which means that caching |
| 84 | + has little or no benefit. |
| 85 | +- Non-deterministic commands such as [`HGETALL`]({{< relref "/commands/hgetall" >}}), |
| 86 | + [`HSCAN`]({{< relref "/commands/hscan" >}}), |
| 87 | + and [`ZRANDMEMBER`]({{< relref "/commands/zrandmember" >}}). By design, these commands |
| 88 | + give different results each time they are called. |
| 89 | +- Redis Query Engine commands (with the `FT.*` prefix), such as |
| 90 | + [`FT.SEARCH`]({{< baseurl >}}/commands/ft.search). |
| 91 | + |
| 92 | +You can use the [`MONITOR`]({{< relref "/commands/monitor" >}}) command to |
| 93 | +check the server's behavior when you are using client-side caching. Because `MONITOR` only |
| 94 | +reports activity from the server, you should find the first cacheable |
| 95 | +access to a key causes a response from the server. However, subsequent |
| 96 | +accesses are satisfied by the cache, and so `MONITOR` should report no |
| 97 | +server activity if client-side caching is working correctly. |
| 98 | + |
| 99 | +## What data gets cached for a command? |
| 100 | + |
| 101 | +Broadly speaking, the data from the specific response to a command invocation |
| 102 | +gets cached after it is used for the first time. Subsets of that data |
| 103 | +or values calculated from it are retrieved from the server as usual and |
| 104 | +then cached separately. For example: |
| 105 | + |
| 106 | +- The whole string retrieved by [`GET`]({{< relref "/commands/get" >}}) |
| 107 | + is added to the cache. Parts of the same string retrieved by |
| 108 | + [`SUBSTR`]({{< relref "/commands/substr" >}}) are calculated on the |
| 109 | + server the first time and then cached separately from the original |
| 110 | + string. |
| 111 | +- Using [`GETBIT`]({{< relref "/commands/getbit" >}}) or |
| 112 | + [`BITFIELD`]({{< relref "/commands/bitfield" >}}) on a string |
| 113 | + caches the returned values separately from the original string. |
| 114 | +- For composite data types accessed by keys |
| 115 | + ([hash]({{< relref "/develop/data-types/hashes" >}}), |
| 116 | + [JSON]({{< relref "/develop/data-types/json" >}}), |
| 117 | + [set]({{< relref "/develop/data-types/sets" >}}), and |
| 118 | + [sorted set]({{< relref "/develop/data-types/sorted-sets" >}})), |
| 119 | + the whole object is cached separately from the individual fields. |
| 120 | + So the results of `JSON.GET mykey $` and `JSON.GET mykey $.myfield` create |
| 121 | + separate entries in the cache. |
| 122 | +- Ranges from [lists]({{< relref "/develop/data-types/lists" >}}), |
| 123 | + [streams]({{< relref "/develop/data-types/streams" >}}), |
| 124 | + and [sorted sets]({{< relref "/develop/data-types/sorted-sets" >}}) |
| 125 | + are cached separately from the object they form a part of. Likewise, |
| 126 | + subsets returned by [`SINTER`]({{< relref "/commands/sinter" >}}) and |
| 127 | + [`SDIFF`]({{< relref "/commands/sdiff" >}}) create separate cache entries. |
| 128 | +- For multi-key read commands such as [`MGET`]({{< relref "/commands/mget" >}}), |
| 129 | + the ordering of the keys is significant. For example `MGET name:1 name:2` is |
| 130 | + cached separately from `MGET name:2 name:1` because the server returns the |
| 131 | + values in the order you specify. |
| 132 | +- Boolean or numeric values calculated from data types (for example |
| 133 | + [`SISMEMBER`]({{< relref "/commands/sismember" >}})) and |
| 134 | + [`LLEN`]({{< relref "/commands/llen" >}}) are cached separately from the |
| 135 | + object they refer to. |
| 136 | + |
| 137 | +## Usage recommendations |
| 138 | + |
| 139 | +Like any caching system, client-side caching has some limitations: |
| 140 | + |
| 141 | +- The cache has only a limited amount of memory available. When the limit |
| 142 | + is reached, the client must *evict* potentially useful items from the |
| 143 | + cache to make room for new ones. |
| 144 | +- Cache misses, tracking, and invalidation messages always add a slight |
| 145 | + performance penalty. |
| 146 | + |
| 147 | +Below are some guidelines to help you use client-side caching efficiently, within these |
| 148 | +limitations: |
| 149 | + |
| 150 | +- **Use a separate connection for data that is not cache-friendly**: |
| 151 | + Caching gives the most benefit |
| 152 | + for keys that are read frequently and updated infrequently. However, you |
| 153 | + may also have data, such as counters and scoreboards, that receives frequent |
| 154 | + updates. In cases like this, the performance overhead of the invalidation |
| 155 | + messages can be greater than the savings made by caching. Avoid this problem |
| 156 | + by using a separate connection *without* client-side caching for any data that is |
| 157 | + not cache-friendly. |
| 158 | +- **Estimate how many items you can cache**: The client libraries let you |
| 159 | + specify the maximum number of items you want to hold in the cache. You |
| 160 | + can calculate an estimate for this number by dividing the |
| 161 | + maximum desired size of the |
| 162 | + cache in memory by the average size of the items you want to store |
| 163 | + (use the [`MEMORY USAGE`]({{< relref "/commands/memory-usage" >}}) |
| 164 | + command to get the memory footprint of a key). For example, if you had |
| 165 | + 10MB (or 10485760 bytes) available for the cache, and the average |
| 166 | + size of an item was 80 bytes, you could fit approximately |
| 167 | + 10485760 / 80 = 131072 items in the cache. Monitor memory usage |
| 168 | + on your server with a realistic test load to adjust your estimate |
| 169 | + up or down. |
| 170 | + |
| 171 | + ## Reference |
| 172 | + |
| 173 | + The Redis server implements extra features for client-side caching that are not used by |
| 174 | + the main Redis clients, but may be useful for custom clients and other |
| 175 | + advanced applications. See |
| 176 | + [Client-side caching reference]({{< relref "/develop/reference/client-side-caching" >}}) |
| 177 | + for a full technical guide to all the options available for client-side caching. |
0 commit comments