1
0

size-your-shards.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. [[size-your-shards]]
  2. == Size your shards
  3. To protect against hardware failure and increase capacity, {es} stores copies of
  4. an index’s data across multiple shards on multiple nodes. The number and size of
  5. these shards can have a significant impact on your cluster's health. One common
  6. problem is _oversharding_, a situation in which a cluster with a large number of
  7. shards becomes unstable.
  8. [discrete]
  9. [[create-a-sharding-strategy]]
  10. === Create a sharding strategy
  11. The best way to prevent oversharding and other shard-related issues
  12. is to create a sharding strategy. A sharding strategy helps you determine and
  13. maintain the optimal number of shards for your cluster while limiting the size
  14. of those shards.
  15. Unfortunately, there is no one-size-fits-all sharding strategy. A strategy that
  16. works in one environment may not scale in another. A good sharding strategy must
  17. account for your infrastructure, use case, and performance expectations.
  18. The best way to create a sharding strategy is to benchmark your production data
  19. on production hardware using the same queries and indexing loads you'd see in
  20. production. For our recommended methodology, watch the
  21. https://www.elastic.co/elasticon/conf/2016/sf/quantitative-cluster-sizing[quantitative
  22. cluster sizing video]. As you test different shard configurations, use {kib}'s
  23. {kibana-ref}/elasticsearch-metrics.html[{es} monitoring tools] to track your
  24. cluster's stability and performance.
  25. The following sections provide some reminders and guidelines you should consider
  26. when designing your sharding strategy. If your cluster has shard-related
  27. problems, see <<fix-an-oversharded-cluster>>.
  28. [discrete]
  29. [[shard-sizing-considerations]]
  30. === Sizing considerations
  31. Keep the following things in mind when building your sharding strategy.
  32. [discrete]
  33. [[single-thread-per-shard]]
  34. ==== Searches run on a single thread per shard
  35. Most searches hit multiple shards. Each shard runs the search on a single
  36. CPU thread. While a shard can run multiple concurrent searches, searches across a
  37. large number of shards can deplete a node's <<modules-threadpool,search
  38. thread pool>>. This can result in low throughput and slow search speeds.
  39. [discrete]
  40. [[each-shard-has-overhead]]
  41. ==== Each shard has overhead
  42. Every shard uses memory and CPU resources. In most cases, a small
  43. set of large shards uses fewer resources than many small shards.
  44. Segments play a big role in a shard's resource usage. Most shards contain
  45. several segments, which store its index data. {es} keeps segment metadata in
  46. JVM heap memory so it can be quickly retrieved for searches. As a
  47. shard grows, its segments are <<index-modules-merge,merged>> into fewer, larger
  48. segments. This decreases the number of segments, which means less metadata is
  49. kept in heap memory.
  50. [discrete]
  51. [[shard-auto-balance]]
  52. ==== {es} automatically balances shards within a data tier
  53. A cluster's nodes are grouped into <<data-tiers,data tiers>>. Within each tier,
  54. {es} attempts to spread an index's shards across as many nodes as possible. When
  55. you add a new node or a node fails, {es} automatically rebalances the index's
  56. shards across the tier's remaining nodes.
  57. [discrete]
  58. [[shard-size-best-practices]]
  59. === Best practices
  60. Where applicable, use the following best practices as starting points for your
  61. sharding strategy.
  62. [discrete]
  63. [[delete-indices-not-documents]]
  64. ==== Delete indices, not documents
  65. Deleted documents aren't immediately removed from {es}'s file system.
  66. Instead, {es} marks the document as deleted on each related shard. The marked
  67. document will continue to use resources until it's removed during a periodic
  68. <<index-modules-merge,segment merge>>.
  69. When possible, delete entire indices instead. {es} can immediately remove
  70. deleted indices directly from the file system and free up resources.
  71. [discrete]
  72. [[use-ds-ilm-for-time-series]]
  73. ==== Use data streams and {ilm-init} for time series data
  74. <<data-streams,Data streams>> let you store time series data across multiple,
  75. time-based backing indices. You can use <<index-lifecycle-management,{ilm}
  76. ({ilm-init})>> to automatically manage these backing indices.
  77. [role="screenshot"]
  78. image:images/ilm/index-lifecycle-policies.png[]
  79. One advantage of this setup is
  80. <<getting-started-index-lifecycle-management,automatic rollover>>, which creates
  81. a new write index when the current one meets a defined `max_primary_shard_size`,
  82. `max_age`, `max_docs`, or `max_size` threshold. When an index is no longer
  83. needed, you can use {ilm-init} to automatically delete it and free up resources.
  84. {ilm-init} also makes it easy to change your sharding strategy over time:
  85. * *Want to decrease the shard count for new indices?* +
  86. Change the <<index-number-of-shards,`index.number_of_shards`>> setting in the
  87. data stream's <<data-streams-change-mappings-and-settings,matching index
  88. template>>.
  89. * *Want larger shards?* +
  90. Increase your {ilm-init} policy's <<ilm-rollover,rollover threshold>>.
  91. * *Need indices that span shorter intervals?* +
  92. Offset the increased shard count by deleting older indices sooner. You can do
  93. this by lowering the `min_age` threshold for your policy's
  94. <<ilm-index-lifecycle,delete phase>>.
  95. Every new backing index is an opportunity to further tune your strategy.
  96. [discrete]
  97. [[shard-size-recommendation]]
  98. ==== Aim for shard sizes between 10GB and 65GB
  99. Shards larger than 65GB may make a cluster less likely to recover from failure.
  100. When a node fails, {es} rebalances the node's shards across the data tier's
  101. remaining nodes. Larger shards can be harder to move across a network and may
  102. tax node resources.
  103. If you use {ilm-init}, set the <<ilm-rollover,rollover action>>'s
  104. `max_primary_shard_size` threshold to `65gb` to avoid larger shards.
  105. To see the current size of your shards, use the <<cat-shards,cat shards API>>.
  106. [source,console]
  107. ----
  108. GET _cat/shards?v=true&h=index,prirep,shard,store&s=prirep,store&bytes=gb
  109. ----
  110. // TEST[setup:my_index]
  111. The `pri.store.size` value shows the combined size of all primary shards for
  112. the index.
  113. [source,txt]
  114. ----
  115. index prirep shard store
  116. .ds-my-data-stream-2099.05.06-000001 p 0 65gb
  117. ...
  118. ----
  119. // TESTRESPONSE[non_json]
  120. // TESTRESPONSE[s/\.ds-my-data-stream-2099\.05\.06-000001/my-index-000001/]
  121. // TESTRESPONSE[s/65gb/.*/]
  122. [discrete]
  123. [[shard-count-recommendation]]
  124. ==== Aim for 20 shards or fewer per GB of heap memory
  125. The number of shards a node can hold is proportional to the node's
  126. heap memory. For example, a node with 30GB of heap memory should
  127. have at most 600 shards. The further below this limit you can keep your nodes,
  128. the better. If you find your nodes exceeding more than 20 shards per GB,
  129. consider adding another node.
  130. To check the current size of each node's heap, use the <<cat-nodes,cat nodes
  131. API>>.
  132. [source,console]
  133. ----
  134. GET _cat/nodes?v=true&h=heap.current
  135. ----
  136. // TEST[setup:my_index]
  137. You can use the <<cat-shards,cat shards API>> to check the number of shards per
  138. node.
  139. [source,console]
  140. ----
  141. GET _cat/shards?v=true
  142. ----
  143. // TEST[setup:my_index]
  144. [discrete]
  145. [[avoid-node-hotspots]]
  146. ==== Avoid node hotspots
  147. If too many shards are allocated to a specific node, the node can become a
  148. hotspot. For example, if a single node contains too many shards for an index
  149. with a high indexing volume, the node is likely to have issues.
  150. To prevent hotspots, use the
  151. <<total-shards-per-node,`index.routing.allocation.total_shards_per_node`>> index
  152. setting to explicitly limit the number of shards on a single node. You can
  153. configure `index.routing.allocation.total_shards_per_node` using the
  154. <<indices-update-settings,update index settings API>>.
  155. [source,console]
  156. --------------------------------------------------
  157. PUT my-index-000001/_settings
  158. {
  159. "index" : {
  160. "routing.allocation.total_shards_per_node" : 5
  161. }
  162. }
  163. --------------------------------------------------
  164. // TEST[setup:my_index]
  165. [discrete]
  166. [[fix-an-oversharded-cluster]]
  167. === Fix an oversharded cluster
  168. If your cluster is experiencing stability issues due to oversharded indices,
  169. you can use one or more of the following methods to fix them.
  170. [discrete]
  171. [[create-indices-that-cover-longer-time-periods]]
  172. ==== Create indices that cover longer time periods
  173. If you use {ilm-init} and your retention policy allows it, avoid using a
  174. `max_age` threshold for the rollover action. Instead, use
  175. `max_primary_shard_size` to avoid creating empty indices or many small shards.
  176. If your retention policy requires a `max_age` threshold, increase it to create
  177. indices that cover longer time intervals. For example, instead of creating daily
  178. indices, you can create indices on a weekly or monthly basis.
  179. [discrete]
  180. [[delete-empty-indices]]
  181. ==== Delete empty or unneeded indices
  182. If you're using {ilm-init} and roll over indices based on a `max_age` threshold,
  183. you can inadvertently create indices with no documents. These empty indices
  184. provide no benefit but still consume resources.
  185. You can find these empty indices using the <<cat-count,cat count API>>.
  186. [source,console]
  187. ----
  188. GET _cat/count/my-index-000001?v=true
  189. ----
  190. // TEST[setup:my_index]
  191. Once you have a list of empty indices, you can delete them using the
  192. <<indices-delete-index,delete index API>>. You can also delete any other
  193. unneeded indices.
  194. [source,console]
  195. ----
  196. DELETE my-index-*
  197. ----
  198. // TEST[setup:my_index]
  199. [discrete]
  200. [[force-merge-during-off-peak-hours]]
  201. ==== Force merge during off-peak hours
  202. If you no longer write to an index, you can use the <<indices-forcemerge,force
  203. merge API>> to <<index-modules-merge,merge>> smaller segments into larger ones.
  204. This can reduce shard overhead and improve search speeds. However, force merges
  205. are resource-intensive. If possible, run the force merge during off-peak hours.
  206. [source,console]
  207. ----
  208. POST my-index-000001/_forcemerge
  209. ----
  210. // TEST[setup:my_index]
  211. [discrete]
  212. [[shrink-existing-index-to-fewer-shards]]
  213. ==== Shrink an existing index to fewer shards
  214. If you no longer write to an index, you can use the
  215. <<indices-shrink-index,shrink index API>> to reduce its shard count.
  216. [source,console]
  217. ----
  218. POST my-index-000001/_shrink/my-shrunken-index-000001
  219. ----
  220. // TEST[s/^/PUT my-index-000001\n{"settings":{"index.number_of_shards":2,"blocks.write":true}}\n/]
  221. {ilm-init} also has a <<ilm-shrink,shrink action>> for indices in the
  222. warm phase.
  223. [discrete]
  224. [[combine-smaller-indices]]
  225. ==== Combine smaller indices
  226. You can also use the <<docs-reindex,reindex API>> to combine indices
  227. with similar mappings into a single large index. For time series data, you could
  228. reindex indices for short time periods into a new index covering a
  229. longer period. For example, you could reindex daily indices from October with a
  230. shared index pattern, such as `my-index-2099.10.11`, into a monthly
  231. `my-index-2099.10` index. After the reindex, delete the smaller indices.
  232. [source,console]
  233. ----
  234. POST _reindex
  235. {
  236. "source": {
  237. "index": "my-index-2099.10.*"
  238. },
  239. "dest": {
  240. "index": "my-index-2099.10"
  241. }
  242. }
  243. ----