Friday, October 10, 2014

Writing asynchronous nginx plugins with python and tornado for 1000 qps

TL;DR by returning X-Accel-Redirects and using multiple tornado processes you can use python as an async plugin language for nginx.

I've been using an architecture at work for building video streaming servers that I've not seen anybody else describe. I'm combining the ease of writing, reading and testing python with the performance of nginx. I've been pretty happy with the maintainability of the result. It's been taken from separate cache and origin to cache+origin and from Smooth Streaming to HLS; and I can hit 1000 qps and 8 Gbps of video with it (with pretty beefy hardware though).

See

http://linuxfiddle.net/f/5f96743c-3d62-40f7-89ef-f64460e904c1

This setup wouldn't serve 1000 qps, missing are:

  • You'd want to use more tornado front-end processes so that python isn't the bottleneck
  • If there is any cache of any kind (e.g., vfs) in the backend, you want to use content-aware backend selection
  • You'd cache the mysql results rather than getting them out of the db every time
  • In the front-end, you'd want to merge all simultaneous requests for the same URL into one
  • Nginx should cache the service responses

3 comments:

Courtney Pearson said...
This comment has been removed by the author.
pg said...

The architecture described here is an interesting approach to combining Python’s maintainability with nginx’s ability to handle high-throughput streaming workloads. Using X-Accel-Redirects and multiple Tornado processes allows the Python layer to handle application logic asynchronously while nginx remains responsible for efficiently serving the resulting content. The practical focus on reaching 1000 QPS and 8 Gbps also makes the performance considerations particularly clear.

The optimization points are especially useful because they show that high throughput depends on the overall architecture rather than simply choosing a fast language or framework. Techniques such as increasing Tornado front-end processes, caching database results, merging simultaneous requests, and allowing nginx to cache service responses all address different bottlenecks. These concepts provide useful context for developers building scalable applications while strengthening their understanding through a Python Online Course.

pg said...

The discussion also demonstrates how asynchronous programming can be applied to a demanding real-world workload instead of being treated only as a theoretical programming concept. Understanding how Python applications interact with web servers, databases, caching layers, and backend selection can help developers make better architectural decisions when working with high-concurrency systems. A strong foundation in these areas can be further developed through Python Online Training.