from Hacker News

Ask HN: System Design – Fastest webserver to serve data in memory

by pastaking on 4/3/16, 3:43 PM with 1 comments

### The Task

Imagine we're making a webservice called "Primes". It has 1 endpoint, something like this:

    from flask import Flask
    import random  

    app = Flask('primes')
    
    @app.route("/rand_prime")
    def rand_prime(n):
        return random.sample(primes, 1)
    
    if __name__ == "__main__":
        global primes
        # some function to load primes from a file into a set
        primes = load_primes()
        app.run()
We want the fastest webservice to serve this endpoint that handles as much QPS as possible.

### Constraints

- We have to load all the primes __into memory__, there's no database at all. - We cannot use memcache, redis, etc. - We have to use Python.

### Current Solution

The code above just serves the primes via the default webserver that comes bundled with Flask. Some ideas to improve upon this:

- Use a better webserver (eg. Tornado) - Proxy several instances of this behind an Nginx server

Can we make this even faster? We really don't need complicated routing or security... Is it possible to do better by writing a custom webserver?