Image: Czapp Árpád
Build scalably with GCP Datastore
Tags: google cloud platform, web dev, gcloud
July 06, 2021
Google Cloud Platform honestly has too many features for me to keep up with. This feature called Datastore is quite useful for storage in creating highly scalable apps.
What is it?
It's a NoSQL-style database. It automatically handles things like sharding and replication and so it's highly available, durable and will automatically scale to handle the real-time load of an app. It offers a few ways of doing business like SQL-like querying, ACID (atomic, consistent, isolated, durable) transactions and indexes.
Note: Datastore is already on the way out! Soon all data stores will be automatically upgraded to Firestore, a faster more consistent solution.
How to use it: A simple node.js example
Clone the example app and 'cd' into it
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples
cd nodejs-docs-samples/appengine/datastore
Add the '@google-cloud/datastore' dependency to the 'package.json'
"dependencies":
"@google-cloud/datastore": "^6.0.0",
"express": "^4.16.4"
Get a grip on the App code
- This sample app code will log, retrieve, and display visitor IP addresses.
- Each log entry is a 2-field class that's given the type
visit
. - Log entries are saved to Datastore using the
save
command. - The 10 most recent visits are retrieved in descending order using the Dataset
runQuery
command.
Deploy the App
- I needed to copy the
app.yaml
files from thehello-world
standard example into this project first... 🤷♂️ (no idea why its not included here by default) - In GCP console, add a project to use.
- set the current active project to use the new project:
gcloud config set project [project_id]
- run
gcloud app deploy
Browse your new app: 'gcloud app browse'
Inspect the Datastore via the console
- Navigate to your project in GCP.
- Note the latest build status by going to the Cloud Build section of GCP.
- Check out the latest Datastore entries added:
Conclusion
Datastore is a great way to get started with a scalable database. It's easy to use and has a lot of features. It's also on the way out and will no longer be available for new projects after 2023. If you are already using it, you can continue to use it until Google decides to deprecate it. For new projects, you'll want to use Firestore instead.