size-your-shards.asciidoc 10 KB

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