Backend for online trivia game which provide users system, game match making, and gameplay. The server has been developed using Sanic and Redis and implements a unique architecture to handle large amount of online users (see System Architecture section below).
Support: Python3.6+
Example for mobile app that use this backend: Google Play / App store
Clone repository:
$ git clone https://github.com/adamcohenhillel/trivia-game-backend-sanic.git
Install packages:
pip install -r requirements.txt
python main.py
Make sure you have Redis server up and running, see settings.py to modify redis server address For production, please see Production suggestions section below
You can run a simple terminal client from /examples/basic_interact.py
To start a game match , you need to run 2 clients!
The following explanation
using the following suggestions, this backend was able to scale to more than 10,000 connected users simultaneously
In the Quick Start section above we run the backend app using the command python main.py
which using the Sanic's inbuilt webserver - and it's fine but not perfect for production. Instead, we can use an alternative ASGI webserver like Uvicorn (also Daphne and Hypercorn):
pip install uvicorn
uvicorn main:app
We can use ASGI (uvicorn) alone without traditional web server because we don't serve static files, but it can help if we had a load balancer in front of it
Only if the redis server run on the same machine as the Sanic app!
If the Redis server run on the same machine with the Sanic app, we cam modify the default settings of the redis server so our backend can go faster and be more durable for a large number of users. The default method to communicate with the redis server from our backend is by TCP socket, but there is another way - whcich is much better and much more resource efficient - using UNIX sockets. Open the redis conf file:
vim /etc/redis/redis.conf
Uncomment the lines (change unixsocketperm line from 700 to 775):
unixsocket /var/run/redis.sock
unixsocketperm 775
Restart redis server:
sudo service redis-server restart
There are few changes that you can make to your linux machine to prepare for scale. Because our app based on sockets connections - which are FILES in Linux OS, there might be a bottleneck here. So - we can increaes the limita for open files in our os (Ubuntu Server 20 LTS):
run command:
ulimit -n 1048576
open the file /etc/sysctl.conf
and add the following lines:
net.ipv4.tcp_max_syn_backlog = 1048576
net.core.somaxconn = 1048576
net.core.netdev_max_backlog = 1048576
then run the command:
sysctl --system