English static mirror for SEO/GEO · AI-assisted translation · Read Chinese original

ZhiChai Forum: A Native PHP Forum System Migrated from Go with Redis + SQLite Dual Storage

Forum topic · ✨步子哥 · 2025-10-07

Summary

ZhiChai Forum (zhichai.net) is a modern forum system rewritten in native PHP after migrating from Go. It uses a distributed architecture combining Redis as a high-speed cache with SQLite as persistent storage, plus an asynchronous write pipeline: writes go to Redis and an async queue, then a background worker (process_sqlite_queue.php) batches them into SQLite with retries (up to 3 attempts, exponential backoff 5s/10s/20s) and a dead-letter queue. The project claims roughly 90% latency reductions for registration, topic creation, and replies versus synchronous writes. It follows MVC plus service-oriented architecture (UserService, TopicService, ReplyService, PermissionService, etc.), with dependency injection based on the Dice container. A three-tier RBAC system covers users, admins, and super admins, with topic/reply moderation and user freeze/role management. The frontend uses Bootstrap 5.3.3, HTMX 1.9.12, Marked.js, DOMPurify, and supports emoji reactions, WebAuthn login, Markdown, responsive design, dark theme, and PWA features. Security includes parameterized queries, XSS/CSRF protection, and bcrypt (cost 12). The codebase is about 25,000 lines of PHP across 156 files, suited to communities of 1,000–10,000 users, deployable standalone, containerized, or distributed.

Overview

ZhiChai Forum is a modern forum system developed in native PHP (no framework), fully migrated from an earlier Go version. It uses a distributed design with Redis + SQLite dual storage, asynchronous processing, caching, and a complete user permission system.

Key points

  • Native PHP backend: no PHP framework; MVC structure (src/Models/, src/Controllers/, views/) plus service-oriented components (UserService, TopicService, ReplyService, PermissionService, CacheRefillService, EmojiService).
  • Dependency injection via the Dice DI container: constructor auto-injection, singleton services, aliases.
  • Dual storage: Redis serves sessions, topic lists, replies, emoji counters, and queues; SQLite provides durable tables (users, topics, replies, user_emojis, passkeys).
  • Async SQLite writes: writes go to Redis + an async queue and return immediately; a background worker (process_sqlite_queue.php) batch-writes SQLite (10 tasks per batch) and syncs cache. Failed tasks retry up to 3 times with exponential backoff (5s → 10s → 20s), then go to a dead-letter queue.
  • Claimed performance gains (async vs. sync): user registration ~200ms → ~20ms, new topic ~150ms → ~15ms, reply ~100ms → ~10ms (~90% improvement).
  • Three-tier RBAC (user / admin / super admin) covering content editing/deletion, topic close/hide, reply hiding, user freeze, and role changes, with protection against demoting the last super admin.
  • Frontend: Bootstrap 5.3.3, HTMX 1.9.12, Marked.js 9.1.2, DOMPurify 3.0.5; Markdown editing with live preview, nested/quoted replies, 7 emoji reactions, responsive design, dark theme, ARIA support, PWA features.
  • Auth: username+password (bcrypt, cost 12) plus WebAuthn (fingerprint/Face ID) passkeys stored in a passkeys table; secure session management.
  • Caching: multi-layer (PHP memory → Redis 1h TTL → SQLite), with proactive update, lazy invalidation, and pre-warming; admin cache status/warm/clear UI.
  • Security: parameterized SQL, output escaping + DOMPurify for XSS, CSRF tokens, audit logs.
  • Operations

  • Health check at /health, async queue status at /debug/async.
  • Queue CLI: php process_sqlite_queue.php status|dead-letter|daemon.
  • Stack: PHP 8.0+, SQLite 3.x, Redis 6.x, Composer (Dice, WebAuthn), Nginx/Apache + PHP-FPM.
  • Scale: ~25,000 lines of PHP, 156 files, 25 docs, 20+ test scripts, 30+ admin scripts.
  • Deployment: single-machine for 1k–10k user communities; supports Docker, Redis clustering, and cloud/microservice setups.
The author highlights as innovations: the Redis-queue-based async SQLite writer, the fault-tolerant dual-storage design (system continues if Redis fails), intelligent cache management, and modern PHP patterns (DI, middleware, routing) without a framework.

Tags

#php#forum#sqlite#redis#asynchronous-processing#rbac#htmx#webauthn

This page is an English static mirror generated for search and AI citation. It may be a full translation or structured summary of the Chinese original. Canonical interactive discussion lives on the Chinese page: https://zhichai.net/topic/175971514