Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Rapid Application Prototyping with Docker Compose

Notes 2
version: "3.8"
services:
  cache_store:
    image: redis:7-alpine
  request_tracker:
    build: ./request_counter
    ports:
      - "3000:3000"

Starting with Docker 1.10, network overlay functionality enables cross-host service scaling, while inter-service links only worked on single hosts in earlier versions. The docker compose scale command allows you to provision additional compute capacity on demand. Full syntax references for docker compose are available in the official Docker documentation.

Practical Use Case: Raspberry Pi Stock Alert System

The Raspberry Pi Zero v1.3 is a compact single-board computer with a 1GHz CPU and 512MB of RAM, capable of runing full Linux distributions, Docker, Node.js, Ruby and other common open source tools. Its $5 price point leads to frequent stockouts across retail channels.

Existing Implementation Limitations

One public stock tracking site uses web scraping to check availability across 4 to 5 leading discount electronics retailers. Its architecture includes:

  • Static HTML frontend
  • Client-side XMLHttpRequest calls to a /public/api/ endpoint
  • Backend logic that sends HTTP requests to each retailer and parses returned markup for stock status

Each request to the /public/api/ endpoint took approximately 3 seconds to complete, and load testing with Apache Bench (ab) showed the site only handled 0.25 requests per second.

Optimized Architecture

Retailers did not block scraping activity from the original tracking site, so we built an improved version from scratch with caching and decoupled layers to boost throughput. Redis is an ideal fit for this use case: it supports auto-expiring key-value pairs for lightweight caching, and pub/sub functionality to pass messages between Node.js processes.

Node.js runs on a single event loop, so CPU-intensive workloads like HTML or JSON parsing can introduce performance bottlenecks. We mitigated this by using separate worker processes connected to the web layer via Redis message channels:

  • API Layer
    • Returns 200 OK if a valid stock entry for the requested retailer exists in Redis (cache hit)
    • Returns 202 Accepted if no matching key is found (cache miss), and queues a scrape request via Redis pub/sub
    • Response latency is extremely low as the layer only perofrms simple Redis read operations
  • Scraper Worker
    • Sends HTTP requests to retailer storefronts
    • Runs site-specific parsing logic to extract stock status
    • Writes results to Redis with a 60-second TTL for automatic cache invalidation
    • Uses a Redis lock key to avoid sending excessive duplicate requests to the same retailer
version: "3.8"
services:
  api_gateway:
    build: ./api_layer
    ports:
      - "3000:3000"
  scraper_worker:
    build: ./scraper_service
  persistent_cache:
    image: redis:7-alpine

Once local validation passes, deployment to a cloud Ubuntu 22.04 instance takes under 5 minutes. After authenticating to the host, clone the project repository and run docker compose up -d to launch the full stack. The entire prototype deployment process requires only these two commands:

$ git clone https://github.com/example/pi-zero-stock-monitor
$ docker compose up -d

To apply updates to the deployed stack, pull the latest code changes and run docker compose up -d --build to rebuild updated service images and restart only modified containers.

A live demo of the stack deployed via docker compose is available at stockalert.example.io.

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.