|
| 1 | +--- |
| 2 | +title: Caching |
| 3 | +menuTitle: Caching |
| 4 | +description: Enable and configure memcached for caching. |
| 5 | +weight: 100 |
| 6 | +keywords: |
| 7 | + - memcached |
| 8 | + - caching |
| 9 | +--- |
| 10 | + |
| 11 | +# Caching |
| 12 | + |
| 13 | +Loki supports caching of index writes and lookups, chunks and query results to |
| 14 | +speed up query performance. This sections describes the recommended Memcached |
| 15 | +configuration to enable caching for chunks and query results. The index lookup |
| 16 | +cache is configured to be in-memory by default. |
| 17 | + |
| 18 | +## Before you begin |
| 19 | + |
| 20 | +- It is recommended to deploy four dedicated Memcached clusters. |
| 21 | +- As of 2023-02-01, the `memcached:1.6.17-alpine` version of the library is recommended. |
| 22 | +- Consult the Loki ksonnet [memcached](https://github.com/grafana/loki/blob/main/production/ksonnet/loki/memcached.libsonnet) deployment and the ksonnet [memcached library](https://github.com/grafana/jsonnet-libs/tree/master/memcached). |
| 23 | + |
| 24 | +## Steps |
| 25 | + |
| 26 | +To enable and configure Memcached: |
| 27 | + |
| 28 | +1. Deploy each Memcached service with at least three replicas and configure |
| 29 | + each as follows: |
| 30 | + 1. Chunk cache |
| 31 | + ``` |
| 32 | + --memory-limit=4096 --max-item-size=2m --conn-limit=1024 |
| 33 | + ``` |
| 34 | + 1. Query result cache |
| 35 | + ``` |
| 36 | + --memory-limit=1024 --max-item-size=5m --conn-limit=1024 |
| 37 | + ``` |
| 38 | +
|
| 39 | +1. Configure Loki to use the cache. |
| 40 | + 1. If the Helm chart is used |
| 41 | +
|
| 42 | + Set `memcached.chunk_cache.host` to the Memecache address for the chunk cache, `memcached.results_cache.host` to the Memecache address for the query result cache, `memcached.chunk_cache.enabled=true` and `memcached.results_cache.enabled=true`. |
| 43 | + |
| 44 | + Ensure that the connection limit of Memcached is at least `number_of_clients * max_idle_conns`. |
| 45 | + |
| 46 | + The options `host` and `service` depend on the type of installation. For example, using the `bitname/memcached` Helm Charts with the following commands, the `service` values are always `memcached`. |
| 47 | + ``` |
| 48 | + helm upgrade --install chunk-cache -n loki bitnami/memcached -f memcached-overrides.yaml |
| 49 | + helm upgrade --install write-cache -n loki bitnami/memcached -f memcached-overrides.yaml |
| 50 | + helm upgrade --install results-cache -n loki bitnami/memcached -f memcached-overrides.yaml |
| 51 | + helm upgrade --install index-cache -n loki bitnami/memcached -f memcached-overrides.yaml |
| 52 | + ``` |
| 53 | + In this case, the Loki configuration would be |
| 54 | + ```yaml |
| 55 | + loki: |
| 56 | + memcached: |
| 57 | + chunk_cache: |
| 58 | + enabled: true |
| 59 | + host: chunk-cache-memcached.loki.svc |
| 60 | + service: memcache |
| 61 | + batch_size: 256 |
| 62 | + parallelism: 10 |
| 63 | + results_cache: |
| 64 | + enabled: true |
| 65 | + host: results-cache-memcached.loki.svc |
| 66 | + service: memcache |
| 67 | + default_validity: 12h |
| 68 | + ``` |
| 69 | + 1. If the Loki configuration is used |
| 70 | + 1. Configure the chunk cache |
| 71 | + ```yaml |
| 72 | + chunk_store_config: |
| 73 | + chunk_cache_config: |
| 74 | + memcached: |
| 75 | + batch_size: 256 |
| 76 | + parallelism: 10 |
| 77 | + memcached_client: |
| 78 | + host: <memcached host> |
| 79 | + service: <port name of memcached service> |
| 80 | + ``` |
| 81 | + 1. Configure the query result cache |
| 82 | + ```yaml |
| 83 | + query_range: |
| 84 | + cache_results: true |
| 85 | + results_cache: |
| 86 | + cache: |
| 87 | + memcached_client: |
| 88 | + consistent_hash: true |
| 89 | + host: <memcached host> |
| 90 | + service: <port name of memcached service> |
| 91 | + max_idle_conns: 16 |
| 92 | + timeout: 500ms |
| 93 | + update_interval: 1m |
| 94 | + ``` |
0 commit comments