I'm building a high performance web framework using Bjoern as the WSGI server.
I'm now wondering, if you need to handle say, 200.000 req/s, how would you scale/spread Bjoern to multiple servers, or rather load-balance as it's commonly called? What would be your preferred approach?
Is there some helper/builtin in Bjoern to aid in doing that? Or should I employ a separate load balancer in Python?
For example let's take the following simple WSGI server:
import bjoern, os
urls = {'/':"hello world", '/greet':"hi user!"}
def application(environ, start_response):
response_body = urls[environ['PATH_INFO']]
status = '200 OK'
response_headers = [('Content-Type', 'text/plain'),
('Content-Length', str(len(response_body)))]
start_response(status, response_headers)
return [response_body]
bjoern.listen(app, "localhost", 8000)
bjoern.run()
To scale it to multiple processors it needs to be modified like in the following way:
import bjoern, os
# We use two workers in this case, meaning 2 processors.
NUM_WORKERS = 2
worker_pids = []
urls = {'/':"hello world", '/greet':"hi user!"}
def application(environ, start_response):
response_body = urls[environ['PATH_INFO']]
status = '200 OK'
response_headers = [('Content-Type', 'text/plain'),
('Content-Length', str(len(response_body)))]
start_response(status, response_headers)
return [response_body]
bjoern.listen(app, "localhost", 8000)
for _ in xrange(NUM_WORKERS):
pid = os.fork()
if pid > 0:
worker_pids.append(pid)
elif pid == 0:
try:
bjoern.run()
except KeyboardInterrupt:
pass
exit()
try:
for _ in xrange(NUM_WORKERS):
os.wait()
except KeyboardInterrupt:
for pid in worker_pids:
os.kill(pid, signal.SIGINT)
But what if I want to scale it to multiple servers instead (thus using their resources as well)?
Employing other web servers such as Nginx, Lighttp or Monkey-http seems overkill, especially in a context where the project's philosophy strives in keeping everything compact and without unnecessary fluff.