Cloud Resume Challenge – Visitor Counter Part 1, Database

In the earlier parts of the challenge, I’d been working through the steps sequentially (for the most part!) Now we come to building the visitor counter, I feel like I need to approach things a little differently. The brief is to use Javascript make an API POST request from API Gateway, which activates a Cloud Run service, to update a value in a database, which then returns the current visitor count for display on the resume page.

As the JS depends on the API Gateway, which depends on the web service, which depends on the database, I feel like the database is the right place to start. I’ll then build backwards towards the JS code.

GCP offers a great selection of database options, and, as I literally just need to store and increment a single value, pretty much any will do. There’s the core tabular SQL option, Cloud SQL, and its globally-focussed and massively scalable counterpart Cloud Spanner. There’s also BigQuery, with its advanced analytics and machine learning capabilities. Clearly Spanner and BigQuery would be overkill here, and I’ve used mySQL in the past, and would rather try something new.

NoSQL refers to any database model that doesn’t store data in relational tables. Rather than having a rigid, pre-designed schema, NoSQL storage models use methods such as key/value pairs, or documents (similar to JSON objects.) This allows for a storage model that is highly flexible (i.e. new data types can be added or omitted without impacting the wider collection) and highly scalable, since the database can be easily spread and extended across multiple machines.

The GCP NoSQL offerings are Bigtable and Firestore. Bigtable is designed to handle massive workloads so, again, bit too much for what we’re doing here. To confuse things further(!) Firestore is available in both “native” and “Datastore” modes. Native mode allows for bi-directional syncing of data (i.e. changes to the database can be reflected on the page without reloading) amongst other things that make it a great choice for mobile and web apps. We don’t need any of that, so we’ll keep it simple and use Datastore.

I’ve started a fresh project for my visitor counter API. Navigating to Firestore in the GCP console and selecting the Datastore option for this project, sets that option permanently:

Datastore uses “documents” to store data, which are collections of nested key/value pairs. Here, I’m using the default namespace to create an entity with the kind “analytics” which has a named key of “visitors” which, in turn, has a property of “count” which has an integer value which I’ll be honest and initially set to zero:

This means that when I query my database, I’ll receive a result that looks like this:

[<Entity('analytics', 'visitors' ) {'count': 0}>]

Which is all I need! Next I’ll use Python to code the API itself and then containerise it for deployment on Cloud Run…