| 1 |
Memory and buffers |
| 2 |
================== |
| 3 |
|
| 4 |
.. currentmodule:: websockets |
| 5 |
|
| 6 |
In most cases, memory usage of a WebSocket server is proportional to the |
| 7 |
number of open connections. When a server handles thousands of connections, |
| 8 |
memory usage can become a bottleneck. |
| 9 |
|
| 10 |
Memory usage of a single connection is the sum of: |
| 11 |
|
| 12 |
1. the baseline amount of memory that websockets uses for each connection; |
| 13 |
2. the amount of memory needed by your application code; |
| 14 |
3. the amount of data held in buffers. |
| 15 |
|
| 16 |
Connection |
| 17 |
---------- |
| 18 |
|
| 19 |
Compression settings are the primary factor affecting how much memory each |
| 20 |
connection uses. |
| 21 |
|
| 22 |
The :mod:`asyncio` implementation with default settings uses 64 KiB of memory |
| 23 |
for each connection. |
| 24 |
|
| 25 |
You can reduce memory usage to 14 KiB per connection if you disable compression |
| 26 |
entirely. |
| 27 |
|
| 28 |
Refer to the :doc:`topic guide on compression <../topics/compression>` to |
| 29 |
learn more about tuning compression settings. |
| 30 |
|
| 31 |
Application |
| 32 |
----------- |
| 33 |
|
| 34 |
Your application will allocate memory for its data structures. Memory usage |
| 35 |
depends on your use case and your implementation. |
| 36 |
|
| 37 |
Make sure that you don't keep references to data that you don't need anymore |
| 38 |
because this prevents garbage collection. |
| 39 |
|
| 40 |
Buffers |
| 41 |
------- |
| 42 |
|
| 43 |
Typical WebSocket applications exchange small messages at a rate that doesn't |
| 44 |
saturate the CPU or the network. Buffers are almost always empty. This is the |
| 45 |
optimal situation. Buffers absorb bursts of incoming or outgoing messages |
| 46 |
without having to pause reading or writing. |
| 47 |
|
| 48 |
If the application receives messages faster than it can process them, receive |
| 49 |
buffers will fill up when. If the application sends messages faster than the |
| 50 |
network can transmit them, send buffers will fill up. |
| 51 |
|
| 52 |
When buffers are almost always full, not only does the additional memory usage |
| 53 |
fail to bring any benefit, but latency degrades as well. This problem is called |
| 54 |
bufferbloat_. If it cannot be resolved by adding capacity, typically because the |
| 55 |
system is bottlenecked by its output and constantly regulated by |
| 56 |
:ref:`backpressure <backpressure>`, then buffers should be kept small to ensure |
| 57 |
that backpressure kicks in quickly. |
| 58 |
|
| 59 |
.. _bufferbloat: https://en.wikipedia.org/wiki/Bufferbloat |
| 60 |
|
| 61 |
To sum up, buffers should be sized to absorb bursts of messages. Making them |
| 62 |
larger than necessary often causes more harm than good. |
| 63 |
|
| 64 |
There are three levels of buffering in an application built with websockets. |
| 65 |
|
| 66 |
TCP buffers |
| 67 |
........... |
| 68 |
|
| 69 |
The operating system allocates buffers for each TCP connection. The receive |
| 70 |
buffer stores data received from the network until the application reads it. |
| 71 |
The send buffer stores data written by the application until it's sent to |
| 72 |
the network and acknowledged by the recipient. |
| 73 |
|
| 74 |
Modern operating systems adjust the size of TCP buffers automatically to match |
| 75 |
network conditions. Overall, you shouldn't worry about TCP buffers. Just be |
| 76 |
aware that they exist. |
| 77 |
|
| 78 |
In very high throughput scenarios, TCP buffers may grow to several megabytes |
| 79 |
to store the data in flight. Then, they can make up the bulk of the memory |
| 80 |
usage of a connection. |
| 81 |
|
| 82 |
I/O library buffers |
| 83 |
................... |
| 84 |
|
| 85 |
I/O libraries like :mod:`asyncio` may provide read and write buffers to reduce |
| 86 |
the frequency of system calls or the need to pause reading or writing. |
| 87 |
|
| 88 |
You should keep these buffers small. Increasing them can help with spiky |
| 89 |
workloads but it can also backfire because it delays backpressure. |
| 90 |
|
| 91 |
* In the :mod:`asyncio` implementation, there is no library-level read buffer |
| 92 |
for plain TCP connections. There is a 256 KiB read buffer for TLS connections, |
| 93 |
plus 32KiB on Windows with the default event loop. |
| 94 |
|
| 95 |
There is a write buffer. The ``write_limit`` argument of |
| 96 |
:func:`~asyncio.client.connect` and :func:`~asyncio.server.serve` controls its |
| 97 |
size. When the write buffer grows above the high-water mark, |
| 98 |
:meth:`~asyncio.connection.Connection.send` waits until it drains under the |
| 99 |
low-water mark to return. This creates backpressure on coroutines that send |
| 100 |
messages. |
| 101 |
|
| 102 |
* In the :mod:`threading` implementation, there are no library-level buffers. |
| 103 |
All I/O operations are performed directly on the :class:`~socket.socket`. |
| 104 |
|
| 105 |
* In the legacy :mod:`asyncio` implementation, there is a library-level read |
| 106 |
buffer. The ``read_limit`` argument of :func:`~legacy.client.connect` and |
| 107 |
:func:`~legacy.server.serve` controls its size. When the read buffer grows |
| 108 |
above the high-water mark, the connection stops reading from the network until |
| 109 |
it drains under the low-water mark. This creates backpressure on the TCP |
| 110 |
connection. |
| 111 |
|
| 112 |
In addition, there is a 256 KiB read buffer for TLS connections, plus 32KiB on |
| 113 |
Windows. |
| 114 |
|
| 115 |
There is a write buffer. It as controlled by ``write_limit``. It behaves like |
| 116 |
the :mod:`asyncio` implementation described above. |
| 117 |
|
| 118 |
websockets' buffers |
| 119 |
................... |
| 120 |
|
| 121 |
Incoming messages are queued in a buffer after they have been received from the |
| 122 |
network and parsed. A larger buffer may help a slow applications handle bursts |
| 123 |
of messages while remaining responsive to control frames. |
| 124 |
|
| 125 |
The memory footprint of this buffer is bounded by the product of ``max_size``, |
| 126 |
which controls the size of items in the queue, and ``max_queue``, which controls |
| 127 |
the number of items. |
| 128 |
|
| 129 |
The ``max_size`` argument of :func:`~asyncio.client.connect` and |
| 130 |
:func:`~asyncio.server.serve` defaults to 1 MiB. Most applications never receive |
| 131 |
such large messages. Configuring a smaller value puts a tighter boundary on |
| 132 |
memory usage. This can make your application more resilient to denial of service |
| 133 |
attacks. |
| 134 |
|
| 135 |
The behavior of the ``max_queue`` argument of :func:`~asyncio.client.connect` |
| 136 |
and :func:`~asyncio.server.serve` varies across implementations. |
| 137 |
|
| 138 |
* In the :mod:`asyncio` and :mod:`threading` implementations, ``max_queue`` is |
| 139 |
the high-water mark of a queue of incoming frames. It defaults to 16 frames. |
| 140 |
If the queue grows larger, the connection stops reading from the network until |
| 141 |
the application consumes messages and the queue goes below the low-water mark. |
| 142 |
This creates backpressure on the TCP connection. |
| 143 |
|
| 144 |
Each item in the queue is a frame. A frame can be a message or a message |
| 145 |
fragment. Either way, it must be smaller than ``max_size``, the maximum size |
| 146 |
of a message. The queue may use up to ``max_size * max_queue`` bytes of |
| 147 |
memory. By default, this is 16 MiB. |
| 148 |
|
| 149 |
* In the legacy :mod:`asyncio` implementation, ``max_queue`` is the maximum |
| 150 |
size of a queue of incoming messages. It defaults to 32 messages. If the queue |
| 151 |
fills up, the connection stops reading from the library-level read buffer |
| 152 |
described above. If that buffer fills up as well, it will create backpressure |
| 153 |
on the TCP connection. |
| 154 |
|
| 155 |
Text messages are decoded before they're added to the queue. Since Python can |
| 156 |
use up to 4 bytes of memory per character, the queue may use up to ``4 * |
| 157 |
max_size * max_queue`` bytes of memory. By default, this is 128 MiB. |