├── LICENSE └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Assaf Morami 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Linux tuning for better CouchDB performance 2 | 3 | ## Filesystem tuning 4 | 5 | ### ext4 6 | 7 | #### Mount options (or via /etc/fstab) 8 | 9 | `errors=remount-ro,noatime,nouser_xattr,barrier=0` 10 | 11 | #### Journal 12 | 13 | (Replace `sdXY` with your partition name) 14 | 15 | ##### Check if exists 16 | 17 | `sudo tune2fs -l /dev/sdXY | fgrep has_journal` 18 | 19 | ##### Turn Off/On 20 | 21 | Unmount filesystem (If root filesystem then mount read-only) and then: 22 | `tune2fs -O ^has_journal /dev/sdXY` 23 | 24 | ### xfs 25 | 26 | #### Mount options (or via /etc/fstab) 27 | 28 | `noatime,nodiratime,logbufs=8,logbsize=256k,nobarrier` 29 | 30 | ## /etc/rc.local 31 | 32 | (Replace `sdX` with your device name) 33 | 34 | ```bash 35 | #### 36 | ## IO Scheduler 37 | #### 38 | 39 | # First, set an appropriate IO scheduler for file servers. 40 | # deadline - For spinning disks 41 | # noop - For VMs and SSDs 42 | echo noop > /sys/block/sdX/queue/scheduler 43 | 44 | # Now give the IO scheduler more flexibility by increasing the number of schedulable requests: 45 | echo 4096 > /sys/block/sdX/queue/nr_requests 46 | 47 | # To improve throughput for sequential reads, increase the maximum amount of read-ahead data. 48 | # The actual amount of read-ahead is adaptive, 49 | # so using a high value here won't harm performance for small random access. 50 | echo 4096 > /sys/block/sdX/queue/read_ahead_kb 51 | 52 | #### 53 | ## Virtual memory settings 54 | #### 55 | 56 | # To avoid long IO stalls (latencies) for write cache flushing 57 | # in a production environment with very different workloads, 58 | # you will typically want to limit the kernel dirty (write) cache size: 59 | echo 5 > /proc/sys/vm/dirty_background_ratio 60 | echo 10 > /proc/sys/vm/dirty_ratio 61 | 62 | # Assigning slightly higher priority to inode caching helps 63 | # to avoid disk seeks for inode loading: 64 | echo 50 > /proc/sys/vm/vfs_cache_pressure 65 | 66 | # Decrease swappiness to prevent swapping as much as possible 67 | echo 1 > /proc/sys/vm/swappiness 68 | 69 | # Buffering of file system data requires frequent memory allocation. 70 | # Raising the amount of reserved kernel memory will enable faster and more reliable 71 | # memory allocation in critical situations. 72 | # Raise the corresponding value to 64MB if you have less than 8GB of memory, 73 | # otherwise raise it to at least 256MB: 74 | echo 262144 > /proc/sys/vm/min_free_kbytes 75 | 76 | # It is recommended to have transparent huge pages disabled: 77 | echo madvise > /sys/kernel/mm/transparent_hugepage/enabled 78 | 79 | #### 80 | ## Process scheduler 81 | #### 82 | 83 | # There's a kernel parameter that determines how long a migrated process has to be running 84 | # before the kernel will consider migrating it again to another core. 85 | # The sysctl name is sched_migration_cost_ns, default value 50000 (that's ns so 0.5 ms). 86 | # Forking servers, like PostgreSQL or Apache, scale to much higher levels of concurrent 87 | # connections if this is made larger, by at least an order of magnitude: 88 | echo 5000000 > /proc/sys/kernel/sched_migration_cost_ns 89 | 90 | # Another parameter that can dramatically impact forking servers is sched_autogroup_enabled. 91 | # This setting groups tasks by TTY, to improve perceived responsiveness on an interactive system. 92 | # On a server with a long running forking daemon, this will tend to keep child processes from 93 | # migrating away as soon as they should. 94 | # It can be disabled like so: 95 | echo 0 > /proc/sys/kernel/sched_autogroup_enabled 96 | 97 | #### 98 | ## CPU 99 | #### 100 | 101 | # Set the scaling governor to performance. This keeps the CPU at maximum frequency 102 | echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor 103 | ``` 104 | 105 | ### Apply the changes 106 | 107 | `sudo /etc/rc.local` or `reboot` 108 | 109 | ## ionice 110 | 111 | :warning: Using `ionice` is effective if and only if IO scheduler uses an algorithm that takes 112 | priorities into account. If you have followed this guide so far, using `ionice` will have no effect 113 | since you have set IO Scheduler to `deadline` or `noop` which doesn't use priorities. 114 | Look for `cfq` for a scheduler that works with priorities. 115 | 116 | Giving CouchDB IO priority with `ionice`: `sudo ionice -p $(pidof beam.smp) -c 1 -n 0`. 117 | This can also be done in a [`systemd` unit](https://gist.github.com/SinanGabel/eac83a2f9d0ac64e2c9d4bd936be9313/3d302ee7b2667b70c8372e4f6ce4891811f2fb94#file-couchdb-2-0-install-L116): 118 | 119 | ``` 120 | IOSchedulingClass=1 121 | IOSchedulingPriority=0 122 | ``` 123 | 124 | ## Sources: 125 | 126 | - https://www.beegfs.com/wiki/StorageServerTuning 127 | - https://tweaked.io/guide/kernel/ 128 | - https://developer.couchbase.com/documentation/server/current/install/install-swap-space.html 129 | - http://www.tutorialspoint.com/unix_commands/ionice.htm 130 | - https://www.freedesktop.org/software/systemd/man/systemd.exec.html 131 | - https://blog.nelhage.com/post/transparent-hugepages/ 132 | - https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt 133 | - https://blog.codeship.com/linux-io-scheduler-tuning/ 134 | - https://askubuntu.com/a/784504 135 | --------------------------------------------------------------------------------