The API service handles all requests from the frontend client and server, requests should be dealt with quickly and background tasks should be handled in an asynchronous manner. So for any background tasks such as sending an access token, the API will create a task in a Cloud Tasks queue which in turn gets delivered to the worker service which can process the task. Normally I would use Celery with RabbitMQ to handle asynchronous tasks however, within the Google Cloud Platform, Cloud Tasks works well as an alternative and is probably less overhead.
As well as handling tasks from the API the Worker instance can run a job to retrieve cars from Tesla and update the database. This is triggered from Cloud Scheduler which can be setup like a cron job. Like Cloud Tasks, Scheduler can call secure HTTP targets using an OIDC Token.
The great thing about Cloud Run is that it can automatically scale up to multiple instances and can also scale to 0. So if no requests are coming in after sometime no instances will be running and you won’t need to pay for usage. Both the API and Worker service connect to a Cloud SQL relational database which is running PostgreSQL, service accounts can automatically handle secure access between Cloud Run and Cloud SQL.
One issue that I noticed with Cloud Run is that Python Multithreading doesn’t seem to work at the moment. Multithreading is very useful for speeding up web scraping where you are making lots of http requests, it’s something I had running locally that would fail when run on a Cloud Run instance.
Frontend
One of the main reasons for this project was to learn about Sapper - it is an application framework similar to Next.js but for the Svelte UI compiler. It works with Node.js as an express compatible middleware and does some really clever things like SSR (Server Side Rendering), Code Splitting and routing based on file structure. Writing components in Svelte is very efficient and making applications in Sapper can be quite rapid.
As well as Svelte and Sapper the frontend uses the TailwindCSS framework, this is all built with Webpack which purges any unused CSS resulting in a very lightweight application.