How joyieldinc/predixy works
Project Type and Purpose
• Predixy is an open-source, high-performance, C++-based proxy server for Redis Sentinel and Redis Cluster deployments.
• It sits between Redis clients and backend Redis instances, providing connection multiplexing, load balancing, command routing, authentication, stats, and latency monitoring.
File Structure Analysis
• Top-level:
• conf/ — configuration files (predixy.conf references auth.conf, cluster.conf, sentinel.conf, etc.)
• doc/ — markdown docs and benchmarks
• src/ — core C++ implementation
• test/ — Python tests
• Makefile, _config.yml (for GitHub pages)
• src/:
• main.cpp — entry point
• Conf(.cpp/.h), ConfParser — load and parse config files
• AcceptConnection/Socket, ListenSocket — frontend acceptor
• ConnectConnection/Pool, ConnectSocket — backend connector pools
• Multiplexors (Epoll, Kqueue, Poll) — event-loop/Reactor pattern
• Command, RequestParser, ResponseParser — protocol parsing
• Proxy, Handler — request routing and response dispatch
• ClusterServerPool, SentinelServerPool, StandaloneServerPool — backend server management
• Distribution, Crc16, DC — sharding/load balancing logic (cluster slot, multi-DC)
• Auth — ACL enforcement
• LatencyMonitor, Stats — metrics collection
• Logger, LogFileSink — logging
Key Architectural Patterns and Principles
• Reactor pattern (I/O multiplexing per thread)
• Thread-per-core or configurable multi-threading (MT=true/false)
• Connection pooling for backend servers
• Pluggable dispatch for Redis Cluster vs Sentinel vs standalone
• Non-blocking I/O, event-driven design
• Modular separation of concerns: config, parsing, networking, routing, monitoring
Main Components
A. Predixy Core
1. Configuration Manager
• Loads predixy.conf and subsidiary files (auth.conf, cluster.conf, etc.)
2. Frontend Listener
• ListenSocket → AcceptConnection → assigns client sockets to Worker Threads
3. Worker Threads (one or more)
• Each with a Multiplexor (epoll/poll/kqueue)
• Handles ClientConnection lifecycle: parse requests, enforce Auth, route via Proxy
4. Proxy & Routing Layer
• CommandParser/RequestParser → selects appropriate ServerPool
• Distribution/Crc16 (for cluster slot) or DC-based routing
5. Backend Connector Pools
• ConnectConnectionPool manages persistent connections to each Redis node
• Separate pools for cluster nodes, sentinel masters/slaves, standalone instances
6. Response Handling
• ResponseParser → write back to client connections
7. Monitoring & Logging
• LatencyMonitor tracks per-command latency
• Stats exposes INFO interface
• Logger asynchronously writes logs, rotates by time/size
B. External Entities
• Redis Cluster nodes
• Redis Sentinel masters/slaves
• Redis standalone group(s)
• Clients (redis-cli, applications)
Data Flow and Interactions
1. Startup
• Main reads configuration → initializes ServerPools and WorkerThreads
2. Client Request
• Client → ListenSocket → AcceptConnection → WorkerThread.Multiplexor
• Parse request → Auth check → Proxy
3. Routing
• Proxy uses Distribution or Sentinel logic to pick backend(s)
• Acquire ConnectConnection from ConnectConnectionPool
• Send raw Redis protocol to backend
4. Backend Response
• Backend → ConnectSocket → WorkerThread.Multiplexor
• ResponseParser → Proxy/Handler → write to ClientConnection
• Update LatencyMonitor, Stats
5. Metrics
• Clients issue INFO command → Stats subsystem returns predixy metrics