Companionway

Meeting challenges with success

Redis Engineering Guide

Redis Engineering Guide

Architecture, Implementation, Management, and Python Integration for Quantitative Pipelines

  1. The Redis Advantage: Architecture & Performance

Redis (REmote DIctionary Server) is an open-source, in-memory data structure store designed for lightning-fast caching, messaging, and database workloads.

  • Written in C: Stripped of heavy runtime overhead, its core engine operates with maximal efficiency, delivering sub-millisecond response times and handling hundreds of thousands of operations per second.
  • Data Structures (Hashes & JSON): Beyond simple strings, Redis supports robust data structures like Hashes. A Redis Hash operates almost identically to a Python dictionary, allowing you to map multi-field records (such as option snapshots and stock summaries) directly without complex serialization.
  • Low Memory Footprint: Keeping data entirely in RAM using optimized memory allocators ensures massive datasets compress down to tiny footprints—typically consuming only a few megabytes for thousands of structured records.
  • Robustness & High Availability: Built to be a permanent infrastructure fixture with persistence options (RDB snapshots and AOF logs) and replication architectures like Sentinel to guarantee uptime.
  1. Installation on Linux and Raspberry Pi

Installing Redis on Debian/Ubuntu Linux distributions (including Raspberry Pi OS) is fast and reliable via the native package manager:


Supervisor

The Silent Watchdog: Managing Application Lifecycles with Supervisor

Building an elegant, high-performance network stack is only half the battle. Once you have engineered a custom network bond, deployed a secure VPN tunnel, and written a lean Python authentication service to guard your private subdirectories, you face a new operational challenge: reliability. If your custom Flask script encounters an unexpected edge-case exception, or if the server reboots after a power flicker, who is watching the watchman to ensure the service hooks back into the grid instantly?


Nginx Auth

Gatekeeping the Grid: Building a Secure Nginx Protected Subdirectory with HTTP Basic Auth

When hosting a custom dashboard or a set of administrative tools on a private server, you don't always need a massive, database-backed user authentication system just to keep prying eyes out of your data. If your architecture is lean, the absolute most efficient approach is to let the web server itself handle the gatekeeping.

Using Nginx’s native basic authentication modules, you can isolate a specific subdirectory under your document root (like /admin or /private) and lock it down for a single user. To keep this deployment secure and maintainable, we will manage the credentials outside of the web server's public path, treating our password file like a protected production environmental config.


Pihole

The Ultimate Network Shield: Why Every Private Network Needs a Pi-hole

Once you have built a resilient, active-failover server infrastructure and established a secure VPN tunnel to access it from anywhere, the next logical step in network engineering isn't about adding more access—it’s about taking control of the data flowing through it. Every time you load a webpage, stream a video, or open an app, your devices are bombarded with a hidden torrent of tracking pixels, analytical telemetry, and aggressive advertisements.


Pivpn

Nailing Down the Infrastructure: Building a Bulletproof PiVPN Tunnel Over a Network Bond

There is nothing quite like the peace of mind that comes from having a secure, encrypted tunnel directly into your home infrastructure. Whether you are sitting in a coffee shop or traveling miles away, setting up a virtual private network (VPN) allows you to securely "spelunk" through your private network, manage your servers, and access your local dashboards as if you were sitting right at your desk.


Active Failover Bond

High-Wire Engineering: Building an Active-Backup Network Bond with nmcli via SSH

There is a specific brand of adrenaline known only to systems administrators: modifying the primary network interface configuration of a remote server over an active SSH session. One wrong keystroke, one premature service restart, and you instantly cut your own lifeline, locking yourself out of a headless machine that might be miles away.

This was the exact challenge faced during a recent infrastructure upgrade on a Raspberry Pi server running a modern Linux distribution managed by NetworkManager. The goal: combine the physical onboard Ethernet (eth0) and the wireless network (wlan0) into a single, resilient, active-failover bonded interface (bond0) pinned to a static IP address. If the physical network cable gets yanked out, the system should instantly failover to Wi-Fi without dropping a single packet. And it all had to be executed completely headless over the network.


The Elegant Simplicity of Server Side Includes (SSI)

The Elegant Simplicity of Server Side Includes (SSI)

<p>In an era where web development seems completely dominated by massive JavaScript frameworks, heavy container runtimes, and complex backend rendering engines, we often overlook the elegant, lightweight tools already built into our infrastructure. If you are managing a local server—like a Raspberry Pi running your personal dashboard—you don’t need a massive stack just to display semi-dynamic content.</p>

<p>You just need <span class="highlight">Server Side Includes (SSI)</span>.</p>

<p>It is simple, incredibly fast, and beautifully efficient. With exactly two lines of code, you can turn a completely static HTML page into a modular, auto-updating administrative display.</p>

<h2>The Problem with Modern Bloat</h2>
<p>Imagine you have a few automated scripts running routinely on your server via cron. They dump out system status files, active task lists, or network metrics into plain text or raw HTML fragments.</p>

<p>If you want to view all of those outputs on a single, clean web page, the modern temptation is to spin up a complex web framework or write intensive client-side JavaScript to fetch and insert the data. But that adds overhead, security attack surfaces, and a lot of boilerplate code to maintain.</p>

<p>SSI bypasses all of that. It tells the web server itself to assemble the pieces of the puzzle right on the disk, a millisecond before handing the final page to your browser.</p>

<h2>The Two-Line Implementation</h2>
<p>To get this running in an environment like Nginx, the setup is almost childproof. It requires exactly two steps.</p>

<h3>Line 1: The Infrastructure Enablement</h3>
<p>First, you tell Nginx that a specific directory or site is allowed to parse files for includes. Inside your Nginx site configuration file, you drop a single directive into your location block:</p>
location / {
    ssi on; 
    try_files $uri $uri/ =404;
}
<h3>Line 2: The HTML Insertion</h3>
<p>Second, inside your HTML file where you want the external script output to appear, you drop a specialized directive disguised as a standard HTML comment. For example, to pull in an active task list snippet:</p>
<!--#include virtual="/includes/task.dat"-->
<p>That’s it.</p>

<h2>Why This is an Engineering Win</h2>
<p>When a browser requests that HTML page, Nginx intercepts the file, scans it for that specific comment token, grabs the live contents of <code>task.dat</code> straight off the disk, slips it perfectly into the layout, and ships a unified page to the client.</p>

<ul>
    <li><span class="highlight">Instant Updates:</span> If your cron job refreshes your data file every few minutes, your webpage updates instantly on the next browser re-load.</li>
    <li><span class="highlight">Decoupled Architecture:</span> Your background automation scripts don’t need to know anything about HTML layouts or CSS styles. They just output raw text or data fragments. Nginx handles the visual presentation framing.</li>
    <li><span class="highlight">Bulletproof Reliability:</span> There are no database connections to drop, no node modules to update, and no heavy memory footprints. It utilizes the highly optimized, native file-handling capabilities of your web server.</li>
</ul>

<div class="sunken">
    <p style="margin: 0; font-style: italic; text-align: center;">Sometimes, the best engineering isn't about adding more layers—it’s about leveraging the powerful, simple tools that are already hiding right under the hood.</p>
</div>

-- Built in collaboration with Gemini, an AI development partner. --


Parsing ansi Code

A function to extract ansi code ...

Dealing with strings that contain ansi codes for colorizing can be a challenge especially if you need the actual display length of the embedded strings.


Taking advantage of *args and **kwargs

Taking Advantage of *args and **kwargs

I consistently forget the option word I have included in a function and so I had to design a way to accept different keywords (option) names to trigger features with in a function.

As an example, assigning a color to a box border is very easy by just adding something like this to a function’s arguments:

def buildabox(msg, border_color="red"):
  """
  docs go here to put a box around msg
  """
  ...

But, if you accidenally call the function like this, it will fail. my_box = buildabox(“my message”, box_color=“white on back”): …


Personal Coding Workflow

Some thoughts on personal coding workflow

My coding is rudimentary at best but the lessons I have learned along the way save me from constant anguish.



Credits

cos Companionway
ale vim ALE
netlify Deploy to Netlify
vim VIM the editor
gtoolz gtoolz
gtoolz python tools Google Gemini