daphne icon indicating copy to clipboard operation
daphne copied to clipboard

Solving - Request queue full error

Open devenbhooshan opened this issue 9 years ago • 6 comments

how can I increase the queue size of daphne server? When I am load testing my service, the Nginx server is returning

503 Service Unavailable

Request queue full.

Daphne

Where are these configurations ? I have a one nginx server running , 3 daphne instances(same host), 6 runworkers.

devenbhooshan avatar Jan 04 '17 05:01 devenbhooshan

Cpasity argument of the channel layer. See config key.

proofit404 avatar Jan 04 '17 05:01 proofit404

You can see a specific example here (it's the http.request channel you want to increase capacity of): https://github.com/django/asgi_redis/#usage

Alternatively, you should increase the number of runworkers to drain the queue faster.

andrewgodwin avatar Jan 04 '17 05:01 andrewgodwin

Thanks 👍 Increased the capacity and it worked. Would be great if we have a tool monitoring the end to end request flow.

devenbhooshan avatar Jan 04 '17 10:01 devenbhooshan

@devenbhooshan I'm curious how do you monitor you request end to end. I tried newrelic but it dosen't show the request queuing. Did you found something to monitor http requests in daphne sufficient?

t-io avatar Jun 19 '17 17:06 t-io

I'd like to insist on @t-io 's question: Is there a good monitoring tool or service where to query the status of current daphne requests?

mexomagno avatar May 08 '18 15:05 mexomagno

I have daphne behind Nginx, and what I do is I set a custom header in Nginx with the value set to $msec (time when the request hit nginx):

server {
    listen              8000;
    server_name         my.site;

    ...

    location / {
        ...
        proxy_pass http://localhost:8001; << daphne sits here
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Request-Received-At $msec; << this is the header
    }
}

Then, in my django app, I have a middleware that:

  1. Reports the time spent in queue / in django to Timings panel in browser
  2. Saves it to redis so that my monitoring tools can report it (in my case, to Cloudwatch)
def response_timing_metrics(get_response):
    """
    Middleware to track request timing metrics.

    Measures time spent in queue (nginx -> daphne -> django) and time in django.
    Stores raw queue times in Redis for later aggregation and reporting.
    """

    def middleware(request):
        if nginx_timing := request.headers.get("X-Request-Received-At"):
            queue_start = float(nginx_timing)
        else:
            queue_start = None

        start = now().timestamp()

        response = get_response(request)

        server_timings = []

        time_in_django = (now().timestamp() - start) * 1000
        server_timings.append(
            f'time_in_django;desc="Time in django";dur={time_in_django}',
        )

        if queue_start is not None:
            time_in_queue = (start - queue_start) * 1000
            server_timings.append(
                f'time_in_queue;desc="Time in queue";dur={time_in_queue}',
            )
            total_time = time_in_django + time_in_queue
            server_timings.append(
                f'total_time;desc="Total time";dur={total_time}',
            )

            raw_key = raw_time_spent_in_queue_key()
            celery_redis.lpush(raw_key, time_in_queue)
            celery_redis.expire(raw_key, 5 * 60)

        response.headers["Server-Timing"] = ",".join(server_timings)

        return response

    return middleware

ipeterov avatar Dec 04 '25 18:12 ipeterov