Tic-tac-toe Alternator – Lab

11 Min to complete

Tic-tac-toe Alternator – Lab

In this lesson, you’ll deploy an example application, a simple Tic-tac-toe multi-user game server game, which uses ScyllaDB as a data storage backend.

The lab is based on the Amazon DynamoDB Tic-tac-toe example app project.

You’ll start ScyllaDB by running a single instance using Docker. Please ensure that your environment meets the following prerequisites:

  1. Docker for Linux, Mac, or Windows. Please note that running ScyllaDB in Docker is only recommended to evaluate and try ScyllaDB. For best performance, a regular OS install is recommended.
  2. 3GB of RAM or greater for Docker.
  3. Python
  4. Pip: Package Installer for Python

If you are new to ScyllaDB, it’s recommended to start with the ScyllaDB Essentials course.

Project Alternator is an Amazon DynamoDB-compatible API that allows any application written for Amazon DynamoDB to be run, unmodified, against ScyllaDB. You can deploy ScyllaDB wherever you want: on-premises or on any public cloud. ScyllaDB provides lower latencies and solves DynamoDB’s high operational costs. This lesson provides an overview of the project.

Setting up the Environment and Starting a ScyllaDB Cluster 

Note: You can skip this step if you have already completed the Project Alternator Basics lab.

Before starting the cluster, make sure the aio-max-nr value is high enough (1048576 or more). 

This parameter determines the maximum number of allowable Asynchronous non-blocking I/O (AIO) concurrent requests by the Linux Kernel, and it helps ScyllaDB perform in a heavy I/O workload environment.

Check the value: 

cat /proc/sys/fs/aio-max-nr

If it needs to be changed:

echo "fs.aio-max-nr = 1048576" >> /etc/sysctl.conf
sysctl -p /etc/sysctl.conf

Start a one-node cluster with Alternator enabled

docker run  --name some-scylla   --hostname some-scylla -p 8000:8000  -d scylladb/scylla:5.2.0    --smp 1 --memory=750M --overprovisioned 1 --alternator-port=8000 --alternator-write-isolation=always

Wait a few seconds and make sure the cluster is up and running:

docker exec -it some-scylla nodetool status

If you don’t already have pip installed, go ahead and install it.

Once it’s installed and up to date, if you don’t already have Flask and Boto installed, install it by running:

pip install Flask
pip install boto3

Run the Tic-tac-toe Application

After all the required packages are installed, download the Tic-tac-toe application:

git clone https://github.com/scylladb/scylla-code-samples/
cd scylla-code-samples/alternator/tictactoe_example_app/

Make sure you have all the required dependencies installed:

pip3 install -r /path/to/requirements.txt

Start the application:

wget https://raw.githubusercontent.com/scylladb/alternator-load-balancing/master/python/boto3_alternator.py
python3 application.py --mode local --serverPort 5000 --port 8000

Open your browser and browse to http://localhost:5000/

Note: Change localhost to the actual public IP if you’re not deploying locally.

Play the Game

Once the browser shows the start page, enter a name, say “user1”, and Log in. This example application does not perform any user authentication. The user ID is only used to identify players.

If this is your first time playing the game, a page appears requesting you to create the required table (Games) in ScyllaDB. Click CREATE TABLE.

 

In a different browser (or in Incognito mode), browse to http://localhost:5000/ and log in with a different user, say “user2”. This allows you to play against yourself.

Back in the first browser, click Create, enter “user2” as the opponent, and click Create Game!

Refresh the second browser and accept the pending invitation:

Start playing:

Under the Hood

To see what happens in the ScyllaDB data storage, use the AWS CLI. If you don’t have it installed, start by installing it following the instructions based on your operating system.

If you have never used the AWS CLI in the past, you need to configure it before you can run it. Any credentials will work as you’re running it locally:

aws configure

Check which tables were created:

aws dynamodb list-tables --endpoint-url http://localhost:8000

You can see that a Games table was created. Read the data in it:

aws dynamodb scan --table-name Games --endpoint-url http://localhost:8000

This table holds information about the game being played. As the game moves forward, the application stores an attribute for each move played. The name of the attribute is the position on the table.

Summary

In this lab, you saw how easy it is to run a DynamoDB application as is, with ScyllaDB as the underlying database.

To learn more about Alternator and using ScyllaDB as an alternative to DynamoDB, check out this blog post and the documentation.

You can learn more about the code used in this application on the AWS example documentation.

fa-angle-up