indexing-speed.asciidoc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. [[tune-for-indexing-speed]]
  2. == Tune for indexing speed
  3. [float]
  4. === Use bulk requests
  5. Bulk requests will yield much better performance than single-document index
  6. requests. In order to know the optimal size of a bulk request, you should run
  7. a benchmark on a single node with a single shard. First try to index 100
  8. documents at once, then 200, then 400, etc. doubling the number of documents
  9. in a bulk request in every benchmark run. When the indexing speed starts to
  10. plateau then you know you reached the optimal size of a bulk request for your
  11. data. In case of tie, it is better to err in the direction of too few rather
  12. than too many documents. Beware that too large bulk requests might put the
  13. cluster under memory pressure when many of them are sent concurrently, so
  14. it is advisable to avoid going beyond a couple tens of megabytes per request
  15. even if larger requests seem to perform better.
  16. [float]
  17. [[multiple-workers-threads]]
  18. === Use multiple workers/threads to send data to Elasticsearch
  19. A single thread sending bulk requests is unlikely to be able to max out the
  20. indexing capacity of an Elasticsearch cluster. In order to use all resources
  21. of the cluster, you should send data from multiple threads or processes. In
  22. addition to making better use of the resources of the cluster, this should
  23. help reduce the cost of each fsync.
  24. Make sure to watch for `TOO_MANY_REQUESTS (429)` response codes
  25. (`EsRejectedExecutionException` with the Java client), which is the way that
  26. Elasticsearch tells you that it cannot keep up with the current indexing rate.
  27. When it happens, you should pause indexing a bit before trying again, ideally
  28. with randomized exponential backoff.
  29. Similarly to sizing bulk requests, only testing can tell what the optimal
  30. number of workers is. This can be tested by progressively increasing the
  31. number of workers until either I/O or CPU is saturated on the cluster.
  32. [float]
  33. === Unset or increase the refresh interval
  34. The operation that consists of making changes visible to search - called a
  35. <<indices-refresh,refresh>> - is costly, and calling it often while there is
  36. ongoing indexing activity can hurt indexing speed.
  37. By default, Elasticsearch runs this operation every second, but only on
  38. indices that have received one search request or more in the last 30 seconds.
  39. This is the optimal configuration if you have no or very little search traffic
  40. (e.g. less than one search request every 5 minutes) and want to optimize for
  41. indexing speed.
  42. On the other hand, if your index experiences regular search requests, this
  43. default behavior means that Elasticsearch will refresh your index every 1
  44. second. If you can afford to increase the amount of time between when a document
  45. gets indexed and when it becomes visible, increasing the
  46. <<dynamic-index-settings,`index.refresh_interval`>> to a larger value, e.g.
  47. `30s`, might help improve indexing speed.
  48. [float]
  49. === Disable refresh and replicas for initial loads
  50. If you need to load a large amount of data at once, you should disable refresh
  51. by setting `index.refresh_interval` to `-1` and set `index.number_of_replicas`
  52. to `0`. This will temporarily put your index at risk since the loss of any shard
  53. will cause data loss, but at the same time indexing will be faster since
  54. documents will be indexed only once. Once the initial loading is finished, you
  55. can set `index.refresh_interval` and `index.number_of_replicas` back to their
  56. original values.
  57. [float]
  58. === Disable swapping
  59. You should make sure that the operating system is not swapping out the java
  60. process by <<setup-configuration-memory,disabling swapping>>.
  61. [float]
  62. === Give memory to the filesystem cache
  63. The filesystem cache will be used in order to buffer I/O operations. You should
  64. make sure to give at least half the memory of the machine running Elasticsearch
  65. to the filesystem cache.
  66. [float]
  67. === Use auto-generated ids
  68. When indexing a document that has an explicit id, Elasticsearch needs to check
  69. whether a document with the same id already exists within the same shard, which
  70. is a costly operation and gets even more costly as the index grows. By using
  71. auto-generated ids, Elasticsearch can skip this check, which makes indexing
  72. faster.
  73. [float]
  74. === Use faster hardware
  75. If indexing is I/O bound, you should investigate giving more memory to the
  76. filesystem cache (see above) or buying faster drives. In particular SSD drives
  77. are known to perform better than spinning disks. Always use local storage,
  78. remote filesystems such as `NFS` or `SMB` should be avoided. Also beware of
  79. virtualized storage such as Amazon's `Elastic Block Storage`. Virtualized
  80. storage works very well with Elasticsearch, and it is appealing since it is so
  81. fast and simple to set up, but it is also unfortunately inherently slower on an
  82. ongoing basis when compared to dedicated local storage. If you put an index on
  83. `EBS`, be sure to use provisioned IOPS otherwise operations could be quickly
  84. throttled.
  85. Stripe your index across multiple SSDs by configuring a RAID 0 array. Remember
  86. that it will increase the risk of failure since the failure of any one SSD
  87. destroys the index. However this is typically the right tradeoff to make:
  88. optimize single shards for maximum performance, and then add replicas across
  89. different nodes so there's redundancy for any node failures. You can also use
  90. <<modules-snapshots,snapshot and restore>> to backup the index for further
  91. insurance.
  92. [float]
  93. === Indexing buffer size
  94. If your node is doing only heavy indexing, be sure
  95. <<indexing-buffer,`indices.memory.index_buffer_size`>> is large enough to give
  96. at most 512 MB indexing buffer per shard doing heavy indexing (beyond that
  97. indexing performance does not typically improve). Elasticsearch takes that
  98. setting (a percentage of the java heap or an absolute byte-size), and
  99. uses it as a shared buffer across all active shards. Very active shards will
  100. naturally use this buffer more than shards that are performing lightweight
  101. indexing.
  102. The default is `10%` which is often plenty: for example, if you give the JVM
  103. 10GB of memory, it will give 1GB to the index buffer, which is enough to host
  104. two shards that are heavily indexing.
  105. [float]
  106. === Additional optimizations
  107. Many of the strategies outlined in <<tune-for-disk-usage>> also
  108. provide an improvement in the speed of indexing.