v0.7.0
Menu

#Memory and limits

#All data is in RAM

Every database is loaded into memory at start, and stays there. The log on disk is for recovery, not for serving reads. That makes reads fast and puts a hard ceiling on how much data one server can hold.

Measured with the release binary on 100k small documents (five fields each): about 48 MB of resident memory, around 480 bytes a document, or 62 MB with two indexes. On disk a document of that kind is about 120 bytes. Indexes cost roughly 3 to 14 MB per 100k documents depending on the values.

The aim was servers with 2 or 4 GB. With about 1.2 GB usable, that is a ceiling of roughly 2.5 million typical documents. Beyond that the design would have to change. Storing documents as bytes was tried and rejected because reads and scans got several times slower.

#The memory limit

There is always a limit. Unless FERRITE_MAX_MEMORY says otherwise, it is 70% of the memory of the machine, or of the cgroup limit when the server runs under one (a container, or a unit with MemoryMax), and never under 64 MB. It is printed at start, for example memory limit 1433 MB.

FERRITE_MAX_MEMORY meaning
unset 70% of machine or cgroup memory, at least 64 MB
a number the limit in megabytes
0 no limit
FERRITE_MAX_MEMORY=3000 ferrite_db

The limit is for the whole process, so connection buffers and big replies count, not only documents. Where /proc is not available (not Linux) there is no default limit, and setting the variable makes the server refuse to start.

#At the limit

The size of the process is sampled every 100 ms.

  • insert, update, upserts and createIndex are refused with a limit error. Deletes still work, since they are the way out.
  • A transaction commit that adds anything is refused.
  • Writes are allowed again once the process is under nine tenths of the limit.
  • The first refused write prints memory limit reached, refusing writes until memory is freed.
  • While the server is full it asks the allocator to return freed memory to the system, so deleting documents does let writes resume.

Because of the sampling, a burst of writes can pass the limit by whatever it adds in 100 ms. Leave room under what the machine has. Under systemd, keep MemoryMax above FERRITE_MAX_MEMORY, for example 1536M for a limit of 1024.

A server whose log is already bigger than the limit still opens, and takes deletes only. A transaction works on copies of the collections it uses, so it needs room for them.

#Request and reply limits

limit value
request line, or binary message 16 MiB
values in one request 1,000,000
nesting depth 100 levels
reply 64 MiB
databases 256
regex pattern 4096 bytes, compiled size 1 MiB
stage that holds documents ($sort, $group, before a $lookup) 100 MiB

A find that would produce a reply over 64 MiB stops and says so. Use limit. An aggregate reply is cut off at the same size.

A request over a limit gets an error reply, usually the code limit. A client that sends a line that is too long, or is too slow, is cut off.

#Connection limits

limit value
connections 1000
connections from one IP address 100 (IPv6 counts by its first 64 bits)
time to log in, when a token or users exist 10 seconds in all, lines of at most 4 KiB
time for a request to arrive after the last reply 60 seconds

Time limits are deadlines, not timeouts that restart with every byte, so a client sending one byte a minute is still cut off. Blank lines do not count as requests. Over the connection limit, a new connection gets one error line and is closed. A TLS client is just closed.

The first line on a connection has to start with {, or be the four bytes that start the binary wire, or the connection is closed. This stops a web page from sending HTTP to the port and having the body run as commands.

There are no environment variables for these limits.

The limits are not a hard bound on memory. The worst case is about 16 MiB times 1000 connections if every client sends a big line at once. A request line of 16 MiB can take several times its size to parse. Run behind a firewall or with TLS and logins, and leave headroom.

#Transaction, watch and aggregate limits

  • Transactions: 10,000 writes, 64 MiB of requests, a collection of at most 1,000,000 documents, 2,000,000 copied in all, 100 collections, and 30 seconds idle. See transactions.
  • Change streams: a watcher queue and the per-database history each hold 10,000 events or 64 MiB. A client that falls behind is closed. See change streams.
  • $out, $merge, $facet input and bucket stages: 100 MiB. A $facet output: 16 MiB.

#Not covered

  • Memory kept by a collection's hash map after deletes does not shrink.
  • No per-client rate limit, and no limit on how many documents one find returns other than the reply size.
  • Clients behind one proxy share its address limit.