CouchDB delegates computation of design documents functions
to external query servers. The external query server is a special OS
process which communicates with CouchDB over standard input/output using a
very simple line-based protocol with JSON messages.

An external query server may be defined with environment variables following
this pattern:

COUCHDB_QUERY_SERVER_LANGUAGE="PATH ARGS"

Where:

LANGUAGE: is a programming language which code this query server may
execute. For instance, there are PYTHON, RUBY, CLOJURE and other
query servers in the wild. This value in lowercase is also used for ddoc
field language to determine which query server processes the functions.

Note, that you may set up multiple query servers for the same programming
language, but you have to name them differently (like PYTHONDEV etc.).

PATH: is a system path to the executable binary program that runs the
query server.

ARGS: optionally, you may specify additional command line arguments
for the executable PATH.

The default query server is written in JavaScript,
running via Mozilla SpiderMonkey. It requires no special environment
settings to enable, but is the equivalent of these two variables:

Hard limit on the number of OS processes usable by Query
Servers. The default value is 100:

[query_server_config]os_process_limit=100

Setting os_process_limit too low can result in starvation of
Query Servers, and manifest in os_process_timeout errors,
while setting it too high can potentially use too many system
resources. Production settings are typically 10-20 times the
default value.

Soft limit on the number of OS processes usable by Query
Servers. The default value is 100:

[query_server_config]os_process_soft_limit=100

Idle OS processes are closed until the total reaches the soft
limit.

For example, if the hard limit is 200 and the soft limit is
100, the total number of OS processes will never exceed 200,
and CouchDB will close all idle OS processes until it reaches
100, at which point it will leave the rest intact, even if
some are idle.

Due to security restrictions, the Erlang query server is disabled by
default.

Unlike the JavaScript query server, the Erlang one does not runs in a
sandbox mode. This means that Erlang code has full access to your OS,
file system and network, which may lead to security issues. While Erlang
functions are faster than JavaScript ones, you need to be careful
about running them, especially if they were written by someone else.

CouchDB has a native Erlang query server, allowing you to write your
map/reduce functions in Erlang.

First, you’ll need to edit your local.ini to include a
[native_query_servers] section:

[native_query_servers]enable_erlang_query_server=true

To see these changes you will also need to restart the server.

Let’s try an example of map/reduce functions which count the total
documents at each number of revisions (there are x many documents at
version “1”, and y documents at “2”… etc). Add a few documents to the
database, then enter the following functions as a view: