A good production ready service should have functionality for various cases:
Overload
Service should respond with HTTP 429 codes to some requests while still being able to handle the rest
Debugging of a running service
inspect logs
get more logs from the suspiciously behaving service and then turn the logging level back
profile memory usage
see requests in flight
Experiments
Should be a way to turn on/off arbitrary functionality without restarting the service
Metrics and Logs
Functional testing
This tutorial shows a configuration of a typical production ready service. For information about service interactions with other utilities and services in container see Deploy Environment Specific Configurations.
Static configs tend to become quite big, so it is a good idea to move changing parts of it into variables. To do that, declare a config_vars field in the static config and point it to a file with variables.
A file with config variables could look like this.
Now in static config you could use $variable-name to refer to a variable, *#fallback fields are used if there is no variable with such name in the config variables file:
A good practice is to have at least 3 different task processors:
task_processors:fs-task-processor:# for blocking operationsthread_name:fs-workerworker_threads:$fs_worker_threadsworker_threads#fallback:2main-task-processor:# for nonblocking operationsthread_name:main-workerworker_threads:$main_worker_threadsworker_threads#fallback:6monitor-task-processor:# for monitoringthread_name:mon-workerworker_threads:$monitor_worker_threadsworker_threads#fallback:1event_thread_pool:# ev pools to deal with OS eventsthreads:$event_threadsthreads#fallback:2
Moving blocking operations into a separate task processor improves responsiveness and CPU usage of your service. Monitor task processor helps to get statistics and diagnostics from server under heavy load or from a server with a deadlocked threads in the main task processor.
Warning
This setup is for an abstract service on an abstract 8 core machine. Benchmark your service on your hardware and hand-tune the thread numbers to get optimal performance.
server:listener:# If your service is behind nginx or some other local proxy, it is# efficient to accepts incoming requests from unix-socket## unix-socket: /var/run/production_service/service.socketport:$server-portport#fallback:8085connection:in_buffer_size:32768requests_queue_size_threshold:100task_processor:main-task-processorlistener-monitor:# Listen on localhost:8085 for developer/utility requestsport:$monitor-server-portport#fallback:8086connection:in_buffer_size:32768requests_queue_size_threshold:100task_processor:monitor-task-processorlogger_access:''logger_access_tskv:''max_response_size_in_flight:1000000000server-name:$server-name
In this example we have two listeners. it is done to separate clients and utility/diagnostic handlers to listen on different ports or even interfaces.
All those handlers live on a separate components.server.listener-monitor address, so you have to request them using the listener-monitor credentials:
$ curl http://localhost:8085/service/log-level/
{"init-log-level":"info","current-log-level":"info"}
$ curl -X PUT 'http://localhost:8085/service/log-level/warning'
{"init-log-level":"info","current-log-level":"warning"}
Ping
This is a server::handlers::Ping handle that returns 200 if the service is OK, 500 otherwise. Useful for balancers, that would stop sending traffic to the server if it responds with codes other than 200.
Note that the ping handler lives on the task processor of all the other handlers. Smart balancers may measure response times and send less traffic to the heavy loaded services.
Service starts with dynamic config values from dynamic-config.fs-cache-path file or from dynamic-config-client-updater.fallback-path file. Service updates dynamic values from a configs service.
congestion_control::Component limits the active requests count. In case of overload it responds with HTTP 429 codes to some requests, allowing your service to properly process handle the rest.
With such setup you could poll the metrics from handler server::handlers::ServerMonitor that we've configured in previous section. However a much more mature approach is to write a component that pushes the metrics directly into the remote metrics aggregation service or to write a handle that provides the metrics in the native aggregation service format.
Secdist - secrets distributor
Storing sensitive data aside from the configs is a good practice that allows you to set different access rights for the two files.
server::handlers::TestsControl is a handle that allows controlling the service from test environments. That handle is used by the testsuite from functional tests to mock time, invalidate caches, testpoints and many other things. This component should be disabled in production environments.
components::TestsuiteSupport is a lightweight storage to keep minor testsuite data. This component is required by many high-level components and it is safe to use this component in production environments.
This sample requires configs service, so we build and start one from our previous tutorials.
mkdir build_release
cd build_release
cmake -DCMAKE_BUILD_TYPE=Release ..
make userver-samples-config_service
./samples/userver-samples-config_service &
make userver-samples-production_service
python3 ../samples/tests/prepare_production_configs.py
./samples/userver-samples-production_service --config /tmp/userver/production_service/static_config.yaml
Functional testing
Functional tests are used to make sure that the service is working fine and implements the required functionality. A recommended practice is to build the service in Debug and Release modes and tests both of them, then deploy the Release build to the production, disabling all the tests related handlers.
Debug builds of the userver provide numerous assertions that validate the framework usage and help to detect bugs at early stages.
Typical functional tests for a service consist of a conftest.py file with mocks+configs for the sereffectivelyvice and a bunch of test_*.py files with actual tests. Such approach allows to reuse mocks and configurations in different tests.