How to Build and Deploy a Serverless Game with DataStax Astra DB, JAMStack, Stargate, and Netlify
Tags: productivity, webdev, javascript, tutorial
Published: 2022-07-12 17:48:58

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s22ue6k10t0dyunkpgan.jpeg)


_In one of our many free tutorials on [DataStax Developers YouTube channel](https://www.youtube.com/c/DataStaxDevs/videos), we walked you through [how to build and deploy a serverless game](https://www.youtube.com/watch?v=mL5cWQnKJkc) — BattleStax — an online party game that you can enjoy with your friends._

BattleStax is implemented as a JAMStack app that uses [Stargate](https://stargate.io/), Netlify, [DataStax Astra DB](https://astra.dev/3yOixH7), and [GitHub](https://github.com/DataStax-Academy/battlestax) to demonstrate how to build and deploy an application using modern, scalable architectures. In this post, we’ll break down the video to help you quickly create your own BattleStax game using React and Redux — implemented with a CI/CD pipeline, global content delivery network (CDN), and [Apache Cassandra®.](https://cassandra.apache.org/)

The technologies we will take you through are:



* **React** for the frontend
* **Redux** for state management
* **Stargate** and **Astra DB** for the backend
* **Netlify** for deployment

Even if you’ve never heard of JAMStack or worked with JavaScript, we have everything [preloaded for you in the cloud](https://github.com/DataStax-Academy/battlestax) so you can follow along with the exercises, information, and solutions provided. We hope that by the end of the tutorial, you will have new knowledge on how to work with JAMStack.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wt27qhys1wcod4mkp4oq.png)
Figure 1: A screenshot of the global scale, hit party game you will get to deploy publicly.


# What is JAMStack?

JAMstack is the new standard architecture for the web, and JAM stands for **J**avaScript **A**PI **M**arkup. It takes all the modern technologies we have and puts them together into a single platform to deploy an application from start to production. Because it has full continuous integration, it wins in **security, scalability, performance, maintainability, and portability.**

Using Git workflows and modern build tools, pre-rendered content is served to a CDN and made dynamic through APIs and serverless functions. Technologies in the stack include JavaScript frameworks, Static Site Generators, Headless CMSs, and CDNs.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sskb31slfywjvqs31ez8.png)

Figure 2: What is JAMStack, [www.JamStack.org](http://www.jamstack.org/)?

JAMstack uses static pages to deliver content rather than rendering content at runtime. Traditional servers create a website, load it up on the server, and render it in real-time. In the case of JAMStack, you actually pre-render all of the pages, serving up static content. But it still offers dynamic, serverless functions, by clicking up to backend microservices. Microservice architecture enables the rapid, frequent and reliable delivery of large, complex applications.

Essentially, we’re pre-building when we build and pre-rendering all of these pages, and pushing them to various devices in the CDN. If you’re not familiar with CDN, one way to understand it is that it takes your content and distributes it across edge servers automatically as part of their system.

So instead of having a single web server where your site is hosted, and users connecting to those servers to get to your site, your content has been spread around the globe on this network, making delivery instantaneous.

If you want to understand more, check out our [JAMStack guide on GitHub](https://github.com/DataStax-Academy/battlestax/blob/master/README_JAM.md).


# Why are we using Apache Cassandra?

Cassandra is a distributed, NoSQL database that is used by the vast majority of Fortune 100 companies. Facebook, Instagram, and Netflix all use Apache Cassandra. If you go on your phone and open an app, chances are, you’re already using it.

Apache Cassandra helps companies process large volumes of fast-moving data in a reliable, scalable way. Known for its performance at scale, Cassandra is regarded as the Porsche Lamborghini of the developer world, but can be finicky if you don’t know how to run it properly. Cassandra is essentially indefinitely scalable. There is no master node and Cassandra is a peer-to-peer system. You can actually have two-thirds of your cluster down and still be up and available to facilitate operations and write and read data.

[DataStax Astra DB](https://docs.datastax.com/en/astra/docs/), built on the best distribution of Cassandra, provides the ability to develop and deploy data-driven applications with a cloud-native service, without the hassles of database and infrastructure administration. By automating tuning and configuration, DataStax Astra radically simplifies database operations.

Astra DB is basically Cassandra in the cloud. What is really helpful is that, from the development standpoint, even using just the free tier, you can load it up, hook up your Cassandra database with Astra and Netlify, and make your app work. And if you decide to go open source and run your own Cassandra, you can do that too with the same driver and code.


# Tutorial Overview

You will find step-by-step instructions with screenshots all on [GitHub](https://github.com/DataStax-Academy/battlestax). We’ve pre-baked the IDE and configurations and dependencies so you don’t have to download anything and you can complete the workshop in the cloud. Just launch GitPod right out of your code and jump into it.

Begin by following the instructions in our [BattleStax repo on GitHub](https://github.com/DataStax-Academy/battlestax) to setup and deploy your first app, create your BattleStax repository and set up your Netlify account.

Here is an overview of the four labs we will walk you through on GitHub.



* Bootstrapping: [Setup and deploy your first app](https://github.com/DataStax-Academy/battlestax/blob/master/README_step00.md)
* Lab 1 — [Introduction to Netlify \
Expose your “hello world” API](https://github.com/DataStax-Academy/battlestax/blob/master/README_Netlify.md)
* Lab 2 — [What are DataStax Astra and Stargate?\
Implementing a Serverless Data API](https://github.com/DataStax-Academy/battlestax/blob/master/README_Astra_Stargate.md)
* Lab 3 — [What are Redux and React? \
Create client state with Redux](https://github.com/DataStax-Academy/battlestax#:~:text=What%20are%20DataStax%20Astra%20and%20Stargate)
* Lab 4 — [Bind Redux to the User Interface](https://github.com/DataStax-Academy/battlestax/blob/master/README_step04.md)

Ready? Let’s go!


# Lab 1: Intro to Netlify + Expose your “hello world” AP

Let’s talk about [Netlify](https://github.com/DataStax-Academy/battlestax/blob/master/README_Netlify.md) and what it can do for you (or, feel free to jump ahead to [exposing your “hello world” API](https://github.com/DataStax-Academy/battlestax/blob/master/README_step01.md))

You may already have used Netify’s Build, Package, deploy and host features to[ set up and deploy your empty application](https://github.com/DataStax-Academy/battlestax/blob/master/README_step00.md). Netlify can also configure your builds and manage atomic deploys.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p1fek9v27h12g5d36p92.png)

Figure 3: Netlify can configure builds and deploy sites.

Netlify lets you deploy serverless Lambda functions without an AWS account, and with function management handled directly within Netlify. This means you can create serverless functions in your application that you can access seamlessly in your local environment, or via a global CDN (once deployed). You can do this without an actual server to deploy code to.

Remember, we’ve created static pages, and we’re going to generate those static pages. That’s where these Netlify functions come in, creating serverless functions.

If you want to dig in and learn more, click here for detailed documentation: [what can Netlify do for you](https://github.com/DataStax-Academy/battlestax/blob/master/README_Netlify.md).

Let’s get coding. You have two options:



* Option A: Cloud-based GitPod **(recommended)**
* Option B: Local development environment

We recommend [Option A](https://github.com/DataStax-Academy/battlestax/blob/master/README_step01.md). Gitpod is a cloud based IDE based on Eclipse Theia very similar to VSCode. You need to authenticate with your GitHub account, and GitPod will initialize your workspace, building the solution. Click here to [launch Gitpod](https://gitpod.io/from-referrer/) to initialize your environment.

Alternatively, you can use your laptop, which is [explained in the documentation](https://github.com/DataStax-Academy/battlestax/blob/master/README_step01.md).

Lab 1 will cover how to:



1. [Set up your environment](https://github.com/DataStax-Academy/battlestax/blob/master/README_step01.md#1-setup-your-environment)
2. [Make a serverless endpoint using Netlify functions](https://github.com/DataStax-Academy/battlestax/blob/master/README_step01.md#2-make-a-serverless-endpoint-using-netlify-functions)
3. [Merge back to master](https://github.com/DataStax-Academy/battlestax/blob/master/README_step01.md#3-merge-back-to-master)
4. [Check your deployment in Netlify](https://github.com/DataStax-Academy/battlestax/blob/master/README_step01.md#4-check-your-deployment-in-netlify)


# Lab 2: DataStax Astra DB and Stargate + Implementing a Serverless Data API

We’ve covered Astra DB briefly in the section above and now we’ll introduce [Stargate](https://stargate.io/), an open-source data gateway to abstract Cassandra-specific concepts from app developers and support different API options. It is deployed between client applications and a database to provide an abstraction layer that can be used to shape your data access to fit your application’s needs.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ya0pkr6b351bj3ljgoo4.png)

Figure 4: Stargate is a framework used to customize all aspects of data access.

Let’s implement a CRUD API in Astra DB. In [Lab 2 of the BattleStax tutorial](https://github.com/DataStax-Academy/battlestax/blob/master/README_step02.md), we will:



* Create an Astra DB database to store game documents
* Create test cases to check that our API call is working correctly
* Build the API call to Astra DB to create a game document, based on the requirements from our test

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/efprt22dk315ugwwztct.png)

Figure 5: With Document API, save and search schemaless JSON documents in Cassandra


# Lab 3: Intro to Redux and React + Creating a client state with Redux

[React](https://javascript.plainenglish.io/the-only-introduction-to-redux-and-react-redux-youll-ever-need-8ce5da9e53c6) is a fast, component-based, front-end JavaScript library. React typically runs in your browser and renders single-page web application user interfaces. [Redux](https://javascript.plainenglish.io/the-only-introduction-to-redux-and-react-redux-youll-ever-need-8ce5da9e53c6) is a JavaScript library that is used mostly for application state management. Redux maintains the state of an entire application in a single immutable state tree (object), which can’t be changed directly. When something changes, a new object is created (using actions and reducers).

React and Redux work together by letting you build components that react to changes of the application state. Components affected by a state change are re-rendered with the new data. Components also dispatch actions, for example when a button is clicked.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/falp9f262d1gpzykflbs.png)

Figure 6: How React and Redux works together.

Learn more by reading our [Redux and React documentation here](https://github.com/DataStax-Academy/battlestax/blob/master/README_Redux_React.md).

In [Lab 3](https://github.com/DataStax-Academy/battlestax/blob/master/README_step03.md), we will:



* Build out the game slice boilerplate code by implementing one reducer, exporting an action and a selector
* Run tests to try out the functionality of our game slice

We will cover how to:



1. [Build a gameSlice](https://github.com/DataStax-Academy/battlestax/blob/master/README_step03.md#1-building-a-gameslice)
2. [Generate an action and a selector](https://github.com/DataStax-Academy/battlestax/blob/master/README_step03.md#2-generate-an-action-and-a-selector)
3. [Create an Async Action](https://github.com/DataStax-Academy/battlestax/blob/master/README_step03.md#3-create-an-async-action)
4. [Running TDD tests](https://github.com/DataStax-Academy/battlestax/blob/master/README_step03.md#4-running-tdd-tests)


# Lab 4: Bind Redux to the User Interface

In this last and final step, we’ve already built a UI with React, but now we need to connect it to our game state.

In [Lab 4](https://github.com/DataStax-Academy/battlestax/blob/master/README_step04.md) we will:



* Build out the NewGame.js component by connecting it to redux.
* Build a test to try out the functionality of NewGame.js

We will cover how to:



1. [Import our Redux artifacts](https://github.com/DataStax-Academy/battlestax/blob/master/README_step04.md#1-import-our-redux-artifacts)
2. [Use hooks to connect our component to our Redux Store](https://github.com/DataStax-Academy/battlestax/blob/master/README_step04.md#2-use-hooks-to-connect-our-compoonent-to-our-redux-store)
3. [Updating the UI](https://github.com/DataStax-Academy/battlestax/blob/master/README_step04.md#3-updating-the-ui)
4. [Running TDD tests](https://github.com/DataStax-Academy/battlestax/blob/master/README_step04.md#4-running-tdd-tests)
5. [Merge back to master](https://github.com/DataStax-Academy/battlestax/blob/master/README_step04.md#5-merge-back-to-master)
6. [Verify your deployment in Netlify](https://github.com/DataStax-Academy/battlestax/blob/master/README_step04.md#6-verify-your-deployment-in-netlify)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a1q4k81fdupqipm87zzv.png)

Figure 7: Hooks in React to extract state information from Redux store.


# Summary

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jp95oovmia3kyeav4w36.png)

Figure 8: BattleStax final display.

If you’ve followed all the steps in the GitHub tutorial, by this point, you would have:



* Deployed an application through a **CI/CD** pipeline with **GitHub Actions**
* Learned how to use **serverless** functions that are globally available via **Netli**fy
* Used these backed by the top NoSQL distributed database Apache Cassandra on **DataStax Astra DB**

And it was possible to do all this without touching a server, deploying back-end code, or needing to talk to your IT friends. Enjoy creating your next viral JAMStack app!

_Explore more tutorials on our [DataStax Developers YouTube channel](https://www.youtube.com/c/DataStaxDevs/videos) and [subscribe to our event alert](https://www.datastax.com/workshops) to get notified about new developer workshops. For exclusive posts on all things data: Cassandra, streaming, Kubernetes, and more; follow [DataStax on Medium](https://datastax.medium.com/)._


# Resources



1. [YouTube: How to Build & Deploy a Serverless Game](https://www.youtube.com/watch?v=mL5cWQnKJkc)
2. [GitHub Tutorial: JamStack + React Serverless Game Workshop](https://github.com/DataStax-Academy/battlestax)
3. [Super Secret Full Game Option](https://github.com/DataStax-Examples/battlestax/wiki/LAB8_Verify-and-Deploy-in-Netlify)
4. [Join our Discord: Fellowship of the (Cassandra) Rings](https://discord.com/invite/pPjPcZN)
5. [Astra DB — Managed Apache Cassandra as a Service](https://astra.dev/3yOixH7)
6. [DataStax Community Platform](https://community.datastax.com/index.html)
7. [Stargate APIs | GraphQL, REST, Document](https://stargate.io/)
8. [DataStax Academy](https://auth.cloud.datastax.com/auth/realms/CloudUsers/protocol/saml/clients/absorb)
9. [DataStax Certifications](https://www.datastax.com/dev/certifications)
10. [DataStax Workshops](https://www.datastax.com/workshops)