|
| 1 | ++++ |
| 2 | +title = 'Loki with Extstore' |
| 3 | +date = 2024-09-04T14:58:37-07:00 |
| 4 | ++++ |
| 5 | + |
| 6 | +## Granfa Loki with Extstore |
| 7 | + |
| 8 | +You may have seen this excellent blog post: https://grafana.com/blog/2023/08/23/how-we-scaled-grafana-cloud-logs-memcached-cluster-to-50tb-and-improved-reliability/ |
| 9 | + |
| 10 | +... and are now attempting to make use of this knowledge, but something isn't |
| 11 | +working quite right. This document will give you a quick start in tuning Loki |
| 12 | +and Extstore to work well together. |
| 13 | + |
| 14 | +## TLDR |
| 15 | + |
| 16 | +We assume your Loki chunk storage size is 1.5mb |
| 17 | + |
| 18 | +For memcached, add at least the following tuning options: |
| 19 | + |
| 20 | +`-I 2m -o ext_wbuf_size=32,ext_threads=10,ext_max_sleep=10000,slab_automove_freeratio=0.10,ext_recache_rate=0` |
| 21 | + |
| 22 | +IE, your full start line may look like: |
| 23 | + |
| 24 | +`memcached -m 6000 -I 2m -o ext_path=/disk/extstore:500G,ext_wbuf_size=32,ext_threads=10,ext_max_sleep=10000,slab_automove_freeratio=0.10,ext_recache_rate=0` |
| 25 | + |
| 26 | +*Please set -m and ext_path appropraitely for your system*. Leave some RAM for |
| 27 | +your system to breathe and a little disk space overhead. |
| 28 | + |
| 29 | +Please use version 1.6.21 or newer as it improves the extstore write speed and |
| 30 | +fixes some related bugs. |
| 31 | + |
| 32 | +In your loki configuration: |
| 33 | + |
| 34 | +``` |
| 35 | +chunk_store_config: |
| 36 | + chunk_cache_config: |
| 37 | + memcached: |
| 38 | + batch_size: 3 |
| 39 | + parallelism: 2 |
| 40 | + memcached_client: |
| 41 | + addresses: 127.0.0.1:11211 |
| 42 | + timeout: 60s |
| 43 | + background: |
| 44 | + writeback_goroutines: 1 |
| 45 | + writeback_buffer: 1000 |
| 46 | + writeback_size_limit: 500MB |
| 47 | +``` |
| 48 | + |
| 49 | +NOTE: `batch_size` can be set to 2x the number of memcached servers you have. |
| 50 | +So if you have 3 servers, 6 should work. Keep `parallelism` as low as |
| 51 | +possible, but increase this value if you are not maxing out network usage on |
| 52 | +memcached. |
| 53 | + |
| 54 | +Loki's default configuration is very aggressive, which is normally fine for |
| 55 | +memory backed memcached. However extstore needs a little more time to fetch or |
| 56 | +write to disk. |
| 57 | + |
| 58 | +Finally, please check that your memcached instances and loki instances aren't |
| 59 | +swapping (out of RAM) or out of CPU, as this can make query times longer and |
| 60 | +cause timeouts. |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +### Why we have to tune these settings |
| 65 | + |
| 66 | +Loki's defaults assume both A) A RAM backed cluster, and B) potentially a |
| 67 | +_large_ cluster made of tens to dozens to hundreds of cache nodes. Many users |
| 68 | +are trying a low number of memcached nodes with extstore (1-3). |
| 69 | + |
| 70 | +When Loki fetches keys from a _pool_ of memcached servers, it will fetch a |
| 71 | +`batch_size` of keys to the _entire pool_ all at once. If you have a |
| 72 | +`batch_size` of 500 and 40 memcached servers, _each memcached_ will receive |
| 73 | +12-14 keys at the same time, as the batch is split across them. |
| 74 | + |
| 75 | +If you are fetching `500` keys against `1` server, that is a much larger batch |
| 76 | +of keys hitting a single server. There are other issues with this but this |
| 77 | +document will not discuss them for now. |
| 78 | + |
| 79 | +### Memcached tuning discussion |
| 80 | + |
| 81 | +The defaults for extstore are fairly conservative. Most of the performance |
| 82 | +improvement you will see is from raising `ext_threads`, which allows it to |
| 83 | +fully utilize an SSD. |
| 84 | + |
| 85 | +If you have a particularly fast SSD, the thread count can be further raised to |
| 86 | +20 or 30. |
| 87 | + |
| 88 | +The number of memcached worker threads is specified with `-t` and defaults to |
| 89 | +`4`, do _not_ set this higher than the number of CPU's your server has. |
| 90 | + |
| 91 | +The rest of the tunings help provide minor speedups. |
| 92 | + |
| 93 | +### Loki tuning discussion |
| 94 | + |
| 95 | +Above we discuss the `batch_size` problem. You can also see `timeout` is set |
| 96 | +very high. We run into issues for a few reasons: |
| 97 | + |
| 98 | +- Loki's memcache client timeout is measuring the amount of time to _fetch and |
| 99 | + read and process the entire batch of keys from each host_. |
| 100 | + |
| 101 | +If you are fetching 2 keys from one host (3MB of data), the 100ms default might seem okay. However, retrieving 500 1.5 megabyte keys over the network from SSD on one host might take quite a while. If you have fewer memcached hosts, or your Loki server does not have a lot of CPU to process results quickly, this timeout will need to be set very high. |
| 102 | + |
| 103 | +We will update this document if this changes. |
| 104 | + |
| 105 | +- `writeback_goroutines: 1` |
| 106 | + |
| 107 | +This defaults to 10. Loki will aggressively write all of the data it fetches |
| 108 | +from a backing store _back to memcached_ as each query runs. Memcached keeps |
| 109 | +some memory reserved as a buffer to give it time to flush data to disk. If it |
| 110 | +cannot write to disk fast enough, you will see the `evictions` counter |
| 111 | +increase. |
| 112 | + |
| 113 | +There are not a lot of good options at the moment, but setting this value to |
| 114 | +`1` will help minimize the impact. If you still have trouble with `evictions` |
| 115 | +you may need to scale up to a faster memcached instance or add more instances. |
| 116 | + |
| 117 | +We will update this document if anything changes. |
0 commit comments