5

A usual deployment have looked in past and present like the following to me:

+------------------+     +---------+ tcp +-------+ tcp
| PSGI Application |----o| Starman |---->| nginx |<----(internet)
+------------------+     +---------+     +-------+

In fact I do have two fully fledged web servers in between the internet and the actual web application.

Since nginx has uWSGI directly build in and uWSGI supports the PSGI protocol, which is a fork of WSGI, I would love to use a PSGI-broker (only PSGI no HTTP) instead of a full fledged web server (Starman).

Is there an PSGI-only-broker solution available?

burnersk
  • 3,320
  • 4
  • 33
  • 56
  • I think you should be able to run http://mojolicious.org/perldoc/Mojo/Server/PSGI behind nginx using psgi. – mpapec Feb 08 '18 at 17:36

1 Answers1

6

The PSGI 'protocol' (like WSGI) is essentially a calling convention for a subroutine. A request comes into the application as a subroutine call with a hash as an argument. The application responds through the subroutine's return value: an arrayref containing HTTP status code, HTTP headers and body. There's more to it than that, but those are the essentials.

What this means is that a process can only implement PSGI if the process contains a Perl interpreter. To achieve this, the process might be implemented in Perl or it might be implemented in a language like C that can load the libperl.so shared library. Similarly a process can only implement WSGI if it contains a Python interpreter.

Your block diagram contains three parts, but in reality the PSGI application is inside the Starman process. So there are really only two parts (although both parts are multiprocess containers).

You say that "nginx has uWSGI directly build in". This does not mean that a WGSI application runs inside the Nginx process. It means that the WSGI application runs in a separate uwsgi process and Nginx communicates with that process over a TCP socket using the uWSGI protocol. This is essentially the same model as Nginx with Starman behind it, but with the distinction that the socket connection to Starman will use the HTTP protocol:

.----------------------.          .-----------.
|       Starman        |          |   Nginx   |
|                      |   HTTP   |           |   HTTP
| .------------------. |<---------|           |<-------(internet)
| | PSGI Application | |          |           |
| '------------------' |          |           |
'----------------------'          '-----------'

The HTTP protocol does have higher overheads than the uWSGI protocol so you could get better performance by running an application server that speaks the WSGI socket protocol and can load libperl.so to implement the PSGI interface. uWSGI can do that:

.----------------------.          .----------.
|        uWSGI         |          |  Nginx   |
|                      |   WSGI   |          |   HTTP
| .------------------. |<---------|          |<-------(internet)
| | PSGI Application | |          |          |
| '------------------' |          |          |
'----------------------'          '----------'
Aristotle Pagaltzis
  • 112,955
  • 23
  • 98
  • 97
Grant McLean
  • 6,898
  • 1
  • 21
  • 37
  • Can you provide any supporting documentation or benchmarks for the performance gain you describe in the last paragraph? I'm curious. – simbabque Feb 09 '18 at 10:12
  • It's difficult to imagine a non-trivial application where the difference would matter - usually the bottleneck would be in the DB or application code. However my comment was based on a blog post I read a while back about benchmarking a trivial PSGI app. It might have been [this one](https://bitrate.epipe.com/starman-vs-uwsgi-psgi-server-performance-comparison_104) – Grant McLean Feb 10 '18 at 17:44
  • More important than speed is the fact that uWSGI consumes much less memory and is more stable. – Aristotle Pagaltzis Jun 05 '21 at 06:01
  • That blog post link won’t load any more but [there’s a copy in the Wayback Machine](https://web.archive.org/web/2012/http://bitrate.epipe.com/starman-vs-uwsgi-psgi-server-performance-comparison_104). Turns out it’s from 2012… quite a while ago. Then again Starman hasn’t changed much over time. However, uWSGI has had busy periods, and while I don’t expect it to have markedly different performance, it may well have gotten slimmer or fatter. – Aristotle Pagaltzis Jun 05 '21 at 06:04