TOC:
mesh image router
I wrote mesh image router five years ago to be the backend of a photo sharing app. The CDN mesh could be deployed on any high storage capacity, high network throughput servers as a self-routing ad-hoc content-addressed data fetching. I refreshed it with more docs and features a bit this year.
architecture
mesh image router (mir) uses a storage and location aware self-routing hierarchy. You could even place a βrealβ CDN in front of mir but mir is designed to route and manage petabytes of storage on the backend itself (though it expects to haβ¦
TOC:
mesh image router
I wrote mesh image router five years ago to be the backend of a photo sharing app. The CDN mesh could be deployed on any high storage capacity, high network throughput servers as a self-routing ad-hoc content-addressed data fetching. I refreshed it with more docs and features a bit this year.
architecture
mesh image router (mir) uses a storage and location aware self-routing hierarchy. You could even place a βrealβ CDN in front of mir but mir is designed to route and manage petabytes of storage on the backend itself (though it expects to have an ultimate fallback origin server for all content, so the image router tiny cdn does expire unused (or less-requested) content over time).
The mesh image router operates hierarchically where you can provision:
- more expensive fast edge nodes for the βhottest objectsβ (big ram + nvme + 400+ Gbps networking)
- but if content isnβt found at the βhot and fastβ layer, it falls back to the next mid layer (larger multi-terabyte SSD servers, 200+ Gbps networking)
- but if content isnβt found at the next mid layer, it falls back to the bulk-before-last-resort layer (multi-petabyte HDD storage, 100+ Gbps networking)
- but if content isnβt found in the bulk-before-last-resort, it gets fetched from the cold storage (B2/S3 etc).
When images are found on a lower level when requested, they get fetched into the higher level as replicated (and LRU-managed) content for faster future serving, also since this is an image router it is aware of image sizes and can provision/scale/deduplicate images based on size classes as well (i.e. detecting user intent for the quality of an image to return and figuring out the best βavailable right nowβ capacity to serve the request even if the data isnβt existing yet; i.e. web request for a 320x240 thumbnail, but you donβt have the original image, but you do have a 1024x768 already downscaled image, so you re-downscale the 1024x768 to the new thumbnail request size instead of "pulling the original, resize original (could be 15+ MB) down to new thumbnail), etc).
basically, all serving follows a path of:
- Content Routing: Content IDs are hashed to determine the owning peer
- Local Serving: If content exists locally on the owner, serve directly
- Peer Streaming: If content is on a peer, stream via control protocol (without copying data)
- Upstream Fetch: If not cached, fetch from upstream level
- Origin Fetch: If not in any cache, fetch from S3 origin
- On-demand Resize: Generate smaller sizes from larger cached versions
One interesting feature I found when building the system is pypy allows this system to serve over 1 million updates per second while just regular cpython canβt push this architecture more than about 300k requests per second. So, I make sure to leave everything βpypy compliantβ and not include any extra unnecessary features or modules which are unsupported by pypy.
protocol
mir implements a custom binary protocol tagged with activities of:
Private Control Protocol
Binary TCP protocol with 5-byte length prefix:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Frame Structure β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β [5 bytes: length] [1 byte: command] [N bytes: data]β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Commands:
| Byte | Command | Payload |
|---|---|---|
# | Heartbeat | HEARTBEAT literal |
^ | Ping | PING literal |
* | Pong | PONG literal (response) |
+ | Request | File path (UTF-8) |
= | Save | 3-byte JSON len + JSON + binary |
@ | Record | JSON hit record |
- | Warm | File path for pre-caching |
& | Rebalance | Same format as Save |
Save/Rebalance Payload Structure:
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β [3 bytes: JSON length] [JSON metadata] [binary content] β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
JSON metadata: {"relname": "relative/path/to/file.ext"}
Content Request Flow
1. Client Request
GET /puddle/{webId}/{size}/{filename.ext}
β
βΌ
2. Path Resolution
βββββββββββββββββββββββββββββββββββ
β Check webPathCache β
β β If cached, use cached path β
β β If False, return 404 β
β β If None, resolve via API β
βββββββββββββββββββββββββββββββββββ
β
βΌ
3. Content ID Mapping
βββββββββββββββββββββββββββββββββββ
β webID β contentId via API β
β contentId β filesystem path β
βββββββββββββββββββββββββββββββββββ
β
βΌ
4. Peer Routing
βββββββββββββββββββββββββββββββββββ
β Hash contentId to peer β
β β If self: continue β
β β If other: stream from peer β
βββββββββββββββββββββββββββββββββββ
β
βΌ
5. Local Cache Check
βββββββββββββββββββββββββββββββββββ
β File exists on disk? β
β β Yes: serve + LRU increment β
β β No: try generation/fetch β
βββββββββββββββββββββββββββββββββββ
β
βΌ
6. Image Generation
βββββββββββββββββββββββββββββββββββ
β Higher-res version exists? β
β β Yes: request resize β
β β No: fetch from upstream β
βββββββββββββββββββββββββββββββββββ
β
βΌ
7. Upstream Fetch
βββββββββββββββββββββββββββββββββββ
β Level 1+ cache β Control fetch β
β Origin level β S3 GET + resize β
βββββββββββββββββββββββββββββββββββ
β
βΌ
8. Response
FileResponse with max-cache headers
Upload Flow
1. POST /upload-propagate
multipart/form-data: meta (JSON) + afile (binary)
β
βΌ
2. Authentication
βββββββββββββββββββββββββββββββββββ
β Validate upload key from meta β
β β Invalid: 401 + 3s delay β
βββββββββββββββββββββββββββββββββββ
β
βΌ
3. Local Storage
βββββββββββββββββββββββββββββββββββ
β Stream to temp file β
β Atomic rename to target path β
β Skip if file already exists β
βββββββββββββββββββββββββββββββββββ
β
βΌ
4. Upstream Propagation
βββββββββββββββββββββββββββββββββββ
β Push to upstream ControlNode β
β via = protocol command β
βββββββββββββββββββββββββββββββββββ
Multi-Process Architecture
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Main Process β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β LRUManager ββ
β β (multiprocessing.Manager) ββ
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β β
β βββββββββββββββββββΌββββββββββββββββββ β
β βΌ βΌ βΌ β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β Worker 0 β β Worker 1 β β Worker N β β
β β MeshConfig β β MeshConfig β β MeshConfig β β
β β Public HTTP β β Public HTTP β β Public HTTP β β
β β Private TCP β β Private TCP β β Private TCP β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Startup Sequence
- Parse command line for server name
- Find server position in mesh config
- Create LRUManager (shared across workers)
- Initialize shared LRU with disk capacity
- Fork worker processes (one per CPU)
- Each worker:
- Creates MeshConfig instance
- Starts public HTTP server
- Starts private TCP server
- Connects to peer control nodes
- Runs async event loop
Unix Socket Naming
Public servers on Unix sockets use process-indexed paths:
{base_path}.{process_idx}.sock
Example: /tmp/lru-00.0.sock, /tmp/lru-00.1.sock, β¦
mesh topology definitions
since we have a service mesh, we have to figure out how to connect everything together.
itβs just one big-ass config file:
TOML Topology Format
# config/topology.prod.toml
[meta]
name = "production"
description = "Production CDN mesh for example.com"
version = "1.0"
[s3]
host = "my-bucket.s3.us-west-000.backblazeb2.com"
[services]
content_resolution_api = "http://api.internal:9999/papi/pt"
thumbnail_endpoint = "http://thumbs.internal:6661/job.image.thumbnails.Thumbnails"
# =============================================================================
# Level 0: Edge Cache Nodes (closest to users)
# =============================================================================
# These nodes receive client requests and form a peer mesh.
# Content is distributed across peers using consistent hashing.
[[levels]]
[[levels.servers]]
name = "edge-us-east-1"
cache_dir = "/var/cache/mir/edge-us-east-1"
# Where clients are redirected for this server's content
[levels.servers.redirect_target]
host = "edge1.cdn.example.com"
port = 443
# Unix socket for local nginx proxy (high performance)
[[levels.servers.public]]
type = "unix"
path = "/run/mir/edge1.sock"
workers = "cpus" # One worker per CPU core
# Direct HTTP access (for health checks, debugging)
[[levels.servers.public]]
type = "net"
host = "0.0.0.0"
port = 8080
# Private control channel for peer communication
[[levels.servers.private]]
host = "edge1.internal.example.com"
port = 5556
[[levels.servers]]
name = "edge-us-west-1"
cache_dir = "/var/cache/mir/edge-us-west-1"
[levels.servers.redirect_target]
host = "edge2.cdn.example.com"
port = 443
[[levels.servers.public]]
type = "unix"
path = "/run/mir/edge2.sock"
workers = "cpus"
[[levels.servers.public]]
type = "net"
host = "0.0.0.0"
port = 8081
[[levels.servers.private]]
host = "edge2.internal.example.com"
port = 5557
[[levels.servers]]
name = "edge-eu-west-1"
cache_dir = "/var/cache/mir/edge-eu-west-1"
[levels.servers.redirect_target]
host = "edge3.cdn.example.com"
port = 443
[[levels.servers.public]]
type = "net"
host = "0.0.0.0"
port = 8082
[[levels.servers.private]]
host = "edge3.internal.example.com"
port = 5558
# =============================================================================
# Level 1: Regional Cache
# =============================================================================
# Aggregates cache misses from edge nodes in a region.
# No public interface - only accessed by edge nodes.
[[levels]]
[[levels.servers]]
name = "regional-us"
cache_dir = "/var/cache/mir/regional-us"
[[levels.servers.private]]
host = "regional-us.internal.example.com"
port = 6666
[[levels.servers]]
name = "regional-eu"
cache_dir = "/var/cache/mir/regional-eu"
[[levels.servers.private]]
host = "regional-eu.internal.example.com"
port = 6667
# =============================================================================
# Level 2: Origin (S3-compatible storage)
# =============================================================================
# The ultimate source of truth. Uses the S3 host defined in [s3] section.
[[levels]]
[[levels.servers]]
name = "origin"
is_s3_origin = true
Section Reference
[meta] - Topology Metadata
| Field | Type | Required | Description |
|---|---|---|---|
name | string | Yes | Unique name for this topology |
description | string | No | Human-readable description |
version | string | No | Version identifier (default: β1.0β) |
[s3] - Origin Storage
| Field | Type | Required | Description |
|---|---|---|---|
host | string | Yes | S3-compatible endpoint hostname |
Note: S3 credentials are stored in secrets, not here.
[services] - External Services
| Field | Type | Required | Description |
|---|---|---|---|
content_resolution_api | string | No | API for web ID β content ID resolution |
thumbnail_endpoint | string | No | Service for on-demand image resizing |
[[levels]] - Cache Hierarchy Tiers
Levels are defined as an array. Level 0 is closest to users, higher levels are closer to origin.
[[levels.servers]] - Server Definitions
| Field | Type | Required | Description |
|---|---|---|---|
name | string | Yes | Unique server identifier |
cache_dir | string | No | Local filesystem path for cache storage |
is_s3_origin | boolean | No | True if this is an S3 origin server |
[levels.servers.redirect_target] - Client Redirect
| Field | Type | Required | Description |
|---|---|---|---|
host | string | Yes | Public hostname for redirects |
port | integer | Yes | Port (typically 443 for HTTPS) |
[[levels.servers.public]] - Public Interfaces
Unix socket interface:
| Field | Type | Required | Description |
|---|---|---|---|
type | string | Yes | Must be "unix" |
path | string | Yes | Socket file path |
workers | string/int | No | "cpus" or specific count (default: "cpus") |
Network interface:
| Field | Type | Required | Description |
|---|---|---|---|
type | string | Yes | Must be "net" |
host | string | Yes | Bind address (e.g., "0.0.0.0") |
port | integer | Yes | Listen port (1-65535) |
[[levels.servers.private]] - Peer Control Channel
| Field | Type | Required | Description |
|---|---|---|---|
host | string | Yes | Internal hostname/IP |
port | integer | Yes | Control port (1-65535) |
Topology Dataclasses
hotspot avoidance
we implemnet a βRequest Flow with Replicationβ allowing content to be replicated and detected and served from βnot its primary shardβ as well.
Client requests contentId="viral123" at Non-Owner Peer 2
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 1. Path Resolution (webPathCache lookup) β
β PublicWebPath β ContentId + extension β
β Track: path_cache_hit or path_cache_miss β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 2. Local Cache Check (CRITICAL - checks replicas!) β
β if os.path.isfile(fsPath): β
β β File exists (owner OR replica) β
β Track: lru_hit β
β record_access(contentId) β hot tracking β
β return FileResponse β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β (if not cached locally)
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 3. Determine Owner via Consistent Hash β
β owner = hash_at_peer_level(contentId) β
β = MD5(contentId) % num_peers β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
ββ If owner == self:
β β
β βΌ
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β Owner Path: Try generate β fetch upstream β
β β Track: lru_miss (if fetching) β
β β record_access(contentId) β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
ββ If owner != self:
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 4. Stream from Owner Peer β
β Track: cross_peer_stream β
β record_access(contentId) β may trigger replicationβ
β return StreamResponse β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Replication Lifecycle
Time: T0 (First Request)
ββ Non-owner Peer 2 receives request for "viral123"
ββ Not in local cache
ββ Streams from Owner (Peer 1)
ββ record_access("viral123")
ββ webPathCache["_ac_viral123"] = 1
Time: T+10s (Second Request)
ββ Another request at Peer 2
ββ Still streams from owner
ββ webPathCache["_ac_viral123"] = 2
Time: T+20s (Third Request)
ββ Another request at Peer 2
ββ webPathCache["_ac_viral123"] = 3
ββ Promoted to HotItemTracker (thread-safe)
ββ access_count = 3
ββ last_access = T+20
ββ decay_score = 3.0
Time: T+60s (Background Sync Task Runs)
ββ Calculate adaptive threshold
β ββ If 60K req/min: threshold β 297
β ββ If 100 req/min: threshold β 5
β
ββ Update decay scores for all tracked items
β ββ "viral123": score = 3 Γ e^(-0.693Γ40/300) = 2.7
β
ββ Find items with score > threshold
β ββ If threshold=5: "viral123" score too low
β ββ Need more accesses or lower traffic
β
ββ For hot items: send WARM to neighbors
Time: T+5min (Score=100, viral content!)
ββ decay_score > threshold
ββ Calculate targets: ring neighbors of owner
β ββ Owner=Peer 1, neighbors=[2,3]
β
ββ Send WARM commands
β ββ Peer 1 β Peer 2: WARM /vi/ra/l1/md/viral123.jpg
β ββ Peer 1 β Peer 3: WARM /vi/ra/l1/md/viral123.jpg
β
ββ Peer 2 receives WARM
β ββ Fetches from Peer 1
β ββ Caches locally
β ββ Sends ACK
β
ββ Mark as replicated in bloom filter
ββ Future requests at Peer 2: served from local cache!
all tests pass
stats
claude prompt
the current claude code allows you to format your own in-cli prompt with details somewhat like a normal shell prompt.
i combined all the details i could fetch and added these live-updating details to the claude bottom toolbar area:
the prompt shows you a live view of:
-
current model choice (a bit of a lie, see rant about claude codeβs shitty βshared global config for concurrent sessionsβ management below)
-
unique input tokens this session (not counting duplicate sends or cache prefixes)
-
output tokens (generate by the model) this sessions
-
context remaining with used tokens vs available tokens
-
available tokens is automatically reduced by the 77,000 token buffer remaining claude code uses as their βcompaction triggerβ
-
lines of code added and removed this session
-
total time this session has been active
-
estimate API cost if you were using a cash based plan (subscription plan still shows cost, but obviously you arenβt dynamically charged per-usage)
Just add this to your ~/.claude/settings.json:
where statusline.py is this thing (not worth a repo really):
#!/usr/bin/env python3
"""
Enhanced Claude Code status line.
Layout: prompt (left-aligned) | metadata (right-aligned)
"""
import json
import os
import re
import sys
# Constants
AUTOCOMPACT_BUFFER = 77_000 # Reserved for compaction, adjust if needed
TERM_WIDTH = 140 # Assumed terminal width (can't detect live width in subprocess)
# ANSI colors
C_RESET = "\033[0m"
C_RED = "\033[31m"
C_GREEN = "\033[32m"
C_YELLOW = "\033[33m"
C_BLUE = "\033[34m"
C_MAGENTA = "\033[35m"
C_CYAN = "\033[36m"
C_DIM = "\033[2m"
def strip_ansi(text):
"""Remove ANSI escape codes to get visible length."""
return re.sub(r"\033\[[0-9;]*m", "", text)
def visible_len(text):
"""Get visible length of text (excluding ANSI codes)."""
return len(strip_ansi(text))
def fmt_k(n):
"""Format number as Xk or X.Xk"""
if n >= 1000:
return f"{n / 1000:.0f}k" if n >= 10000 else f"{n / 1000:.1f}k"
return str(n)
def fmt_duration(ms):
"""Format milliseconds as human-readable duration."""
secs = ms / 1000
if secs < 60:
return f"{secs:.0f}s"
mins = int(secs // 60)
secs = int(secs % 60)
if mins < 60:
return f"{mins}m{secs:02d}s"
hours = int(mins // 60)
mins = int(mins % 60)
return f"{hours}h{mins:02d}m"
def main():
try:
data = json.load(sys.stdin)
except json.JSONDecodeError:
print("[?] error")
return
# Debug: save last input for checking formatting offline or in testing
if False:
try:
with open(
os.path.expanduser("~/.claude/statusline-lastinput.json"), "w"
) as f:
json.dump(data, f, indent=2)
except Exception:
pass
# === Extract data ===
# Model
model = data.get("model", {}).get("display_name", "?")
# Cumulative session tokens
ctx = data.get("context_window", {})
total_in = ctx.get("total_input_tokens", 0)
total_out = ctx.get("total_output_tokens", 0)
ctx_size = ctx.get("context_window_size", 200_000)
# Usable context (excluding autocompact buffer)
usable_ctx = ctx_size - AUTOCOMPACT_BUFFER
# Current context from transcript
current_ctx = 0
transcript_path = data.get("transcript_path")
if transcript_path and os.path.exists(transcript_path):
try:
# Read only the last ~32KB of transcript (much faster for long sessions)
with open(transcript_path, "rb") as f:
f.seek(0, 2) # End of file
size = f.tell()
read_size = min(size, 32768)
f.seek(max(0, size - read_size))
tail = f.read().decode("utf-8", errors="ignore")
# Parse lines from the end
for line in reversed(tail.splitlines()):
try:
msg = json.loads(line)
usage = msg.get("message", {}).get("usage")
if usage:
current_ctx = (
usage.get("input_tokens", 0)
+ usage.get("cache_creation_input_tokens", 0)
+ usage.get("cache_read_input_tokens", 0)
)
break
except (json.JSONDecodeError, KeyError, TypeError):
continue
except Exception:
pass
# Fallback to cumulative
if current_ctx == 0:
current_ctx = total_in + total_out
approx = "~"
else:
approx = ""
# Context percentage of usable space
pct = int((current_ctx / usable_ctx) * 100) if usable_ctx > 0 else 0
# Color for context percentage
if pct < 50:
pct_color = C_GREEN
elif pct < 80:
pct_color = C_YELLOW
else:
pct_color = C_RED
# Cost and stats
cost_data = data.get("cost", {})
cost = cost_data.get("total_cost_usd", 0)
lines_added = cost_data.get("total_lines_added", 0)
lines_removed = cost_data.get("total_lines_removed", 0)
api_duration_ms = cost_data.get("total_api_duration_ms", 0)
# Prompt: user@host:path
user = os.environ.get("USER", "?")
host = os.uname().nodename.split(".")[0]
cwd = os.getcwd()
home = os.path.expanduser("~")
if cwd.startswith(home):
cwd = "~" + cwd[len(home) :]
# === Build output ===
# Left side: prompt
left = f"{C_RED}{user}{C_RESET}@{C_MAGENTA}{host}{C_RESET}:{C_BLUE}{cwd}{C_RESET}"
# Right side: metadata
# Format lines changed: +added-removed
lines_str = f"{C_GREEN}+{lines_added:,}{C_RESET};{C_RED}-{lines_removed:,}{C_RESET}"
right_parts = [
f"{C_CYAN}{model}{C_RESET}",
f"in:{C_YELLOW}{fmt_k(total_in)}{C_RESET} out:{C_YELLOW}{fmt_k(total_out)}{C_RESET}",
f"ctx:{pct_color}{approx}{pct}%{C_RESET} {C_DIM}({fmt_k(current_ctx)}/{fmt_k(usable_ctx)}){C_RESET}",
lines_str,
f"{C_DIM}{fmt_duration(api_duration_ms)}{C_RESET} {C_MAGENTA}${cost:,.2f}{C_RESET}",
]
right = " | ".join(right_parts)
# Calculate padding to right-align metadata
left_len = visible_len(left)
right_len = visible_len(right)
padding = TERM_WIDTH - left_len - right_len
# Ensure at least 2 spaces between left and right
if padding < 2:
padding = 2
output = f"{left}{' ' * padding}{right}"
print(output, end="")
if __name__ == "__main__":
main()
note though: the prompt canβt be 100% accurate because, as we covered previously, anthropic is oddly incompetent when it comes to system and utility and infrastructure design. For example, here, you can run multiple claude commands, but they all appear to share the same global ~/.claude/settings.json which gets updated live as you run things, so if you have two terminals running claude with different /model settings, the last one to change /model overwrites your global βmodelβ setting in the config file? wtf? (this is also why you can see things like βyou have used -700k tokens!β in the UI when it gets confused about your current model, or when it aggressively compacts your 1 million model session at 150k tokens when it gets confused due to using one global state for indepedent concurrent processes always conflicting.) we see all over the claude code ecosystem they combine (and confuse) config files with metrics files where they dump live self-rewriting metrics into your static config files which is justβ¦ utter bullshit if youβve ever designed systems before? you donβt add dynamically updating/changing/automated-writing stats into static user config profiles? wtf are they even doing over there? must be some meta-ethics brain where you just make everything self-rewriting json so all state is always outdated and corrupt and nobody can save or restore their own config files without breaking things because all files βSelf-rewrite real time statsβ inside of them. again, we say: wtf?