size-your-shards.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 50GB
  99. Large shards may make a cluster less likely to recover from failure. When a node
  100. fails, {es} rebalances the node's shards across the data tier's remaining nodes.
  101. Large shards can be harder to move across a network and may tax node resources.
  102. While not a hard limit, shards between 10GB and 50GB tend to work well. You may
  103. be able to use larger shards depending on your network and use case.
  104. If you use {ilm-init}, set the <<ilm-rollover,rollover action>>'s
  105. `max_primary_shard_size` threshold to `50gb` to avoid shards larger than 50GB.
  106. To see the current size of your shards, use the <<cat-shards,cat shards API>>.
  107. [source,console]
  108. ----
  109. GET _cat/shards?v=true&h=index,prirep,shard,store&s=prirep,store&bytes=gb
  110. ----
  111. // TEST[setup:my_index]
  112. The `pri.store.size` value shows the combined size of all primary shards for
  113. the index.
  114. [source,txt]
  115. ----
  116. index prirep shard store
  117. .ds-my-data-stream-2099.05.06-000001 p 0 50gb
  118. ...
  119. ----
  120. // TESTRESPONSE[non_json]
  121. // TESTRESPONSE[s/\.ds-my-data-stream-2099\.05\.06-000001/my-index-000001/]
  122. // TESTRESPONSE[s/50gb/.*/]
  123. [discrete]
  124. [[shard-count-recommendation]]
  125. ==== Aim for 20 shards or fewer per GB of heap memory
  126. The number of shards a node can hold is proportional to the node's
  127. heap memory. For example, a node with 30GB of heap memory should
  128. have at most 600 shards. The further below this limit you can keep your nodes,
  129. the better. If you find your nodes exceeding more than 20 shards per GB,
  130. consider adding another node.
  131. To check the current size of each node's heap, use the <<cat-nodes,cat nodes
  132. API>>.
  133. [source,console]
  134. ----
  135. GET _cat/nodes?v=true&h=heap.current
  136. ----
  137. // TEST[setup:my_index]
  138. You can use the <<cat-shards,cat shards API>> to check the number of shards per
  139. node.
  140. [source,console]
  141. ----
  142. GET _cat/shards?v=true
  143. ----
  144. // TEST[setup:my_index]
  145. [discrete]
  146. [[avoid-node-hotspots]]
  147. ==== Avoid node hotspots
  148. If too many shards are allocated to a specific node, the node can become a
  149. hotspot. For example, if a single node contains too many shards for an index
  150. with a high indexing volume, the node is likely to have issues.
  151. To prevent hotspots, use the
  152. <<total-shards-per-node,`index.routing.allocation.total_shards_per_node`>> index
  153. setting to explicitly limit the number of shards on a single node. You can
  154. configure `index.routing.allocation.total_shards_per_node` using the
  155. <<indices-update-settings,update index settings API>>.
  156. [source,console]
  157. --------------------------------------------------
  158. PUT my-index-000001/_settings
  159. {
  160. "index" : {
  161. "routing.allocation.total_shards_per_node" : 5
  162. }
  163. }
  164. --------------------------------------------------
  165. // TEST[setup:my_index]
  166. [discrete]
  167. [[fix-an-oversharded-cluster]]
  168. === Fix an oversharded cluster
  169. If your cluster is experiencing stability issues due to oversharded indices,
  170. you can use one or more of the following methods to fix them.
  171. [discrete]
  172. [[create-indices-that-cover-longer-time-periods]]
  173. ==== Create indices that cover longer time periods
  174. If you use {ilm-init} and your retention policy allows it, avoid using a
  175. `max_age` threshold for the rollover action. Instead, use
  176. `max_primary_shard_size` to avoid creating empty indices or many small shards.
  177. If your retention policy requires a `max_age` threshold, increase it to create
  178. indices that cover longer time intervals. For example, instead of creating daily
  179. indices, you can create indices on a weekly or monthly basis.
  180. [discrete]
  181. [[delete-empty-indices]]
  182. ==== Delete empty or unneeded indices
  183. If you're using {ilm-init} and roll over indices based on a `max_age` threshold,
  184. you can inadvertently create indices with no documents. These empty indices
  185. provide no benefit but still consume resources.
  186. You can find these empty indices using the <<cat-count,cat count API>>.
  187. [source,console]
  188. ----
  189. GET _cat/count/my-index-000001?v=true
  190. ----
  191. // TEST[setup:my_index]
  192. Once you have a list of empty indices, you can delete them using the
  193. <<indices-delete-index,delete index API>>. You can also delete any other
  194. unneeded indices.
  195. [source,console]
  196. ----
  197. DELETE my-index-*
  198. ----
  199. // TEST[setup:my_index]
  200. [discrete]
  201. [[force-merge-during-off-peak-hours]]
  202. ==== Force merge during off-peak hours
  203. If you no longer write to an index, you can use the <<indices-forcemerge,force
  204. merge API>> to <<index-modules-merge,merge>> smaller segments into larger ones.
  205. This can reduce shard overhead and improve search speeds. However, force merges
  206. are resource-intensive. If possible, run the force merge during off-peak hours.
  207. [source,console]
  208. ----
  209. POST my-index-000001/_forcemerge
  210. ----
  211. // TEST[setup:my_index]
  212. [discrete]
  213. [[shrink-existing-index-to-fewer-shards]]
  214. ==== Shrink an existing index to fewer shards
  215. If you no longer write to an index, you can use the
  216. <<indices-shrink-index,shrink index API>> to reduce its shard count.
  217. [source,console]
  218. ----
  219. POST my-index-000001/_shrink/my-shrunken-index-000001
  220. ----
  221. // TEST[s/^/PUT my-index-000001\n{"settings":{"index.number_of_shards":2,"blocks.write":true}}\n/]
  222. {ilm-init} also has a <<ilm-shrink,shrink action>> for indices in the
  223. warm phase.
  224. [discrete]
  225. [[combine-smaller-indices]]
  226. ==== Combine smaller indices
  227. You can also use the <<docs-reindex,reindex API>> to combine indices
  228. with similar mappings into a single large index. For time series data, you could
  229. reindex indices for short time periods into a new index covering a
  230. longer period. For example, you could reindex daily indices from October with a
  231. shared index pattern, such as `my-index-2099.10.11`, into a monthly
  232. `my-index-2099.10` index. After the reindex, delete the smaller indices.
  233. [source,console]
  234. ----
  235. POST _reindex
  236. {
  237. "source": {
  238. "index": "my-index-2099.10.*"
  239. },
  240. "dest": {
  241. "index": "my-index-2099.10"
  242. }
  243. }
  244. ----