View Categories

Mailborder Redis Daemon

4 min read

Mailborder Redis Daemon (mb-redis) #

The Mailborder Redis Daemon (mb-redis) is a critical service within the Mailborder Email Gateway, designed to streamline the logging of Postfix delivery statuses into Redis. This daemon not only ensures fast and efficient message tracking but also plays a pivotal role in aligning delivery statuses with the Mailborder Vita service for comprehensive reporting.


Overview of Postfix and Redis Integration #

Postfix interacts with mb-redis to log email delivery events in Redis. This integration enables real-time status tracking and efficient retrieval of message information without the overhead of parsing log files.

  1. Postfix Queue ID Logging:

    • When an email is injected into Postfix, the xfn_send_message function captures both:
      • Mailborder Message ID ($msg_id)
      • Postfix Queue ID (from the SMTP response: queued as <Postfix ID>)
    • Both IDs are stored in Redis to facilitate tracking throughout the email’s lifecycle.
  2. Redis Key Structure:

    • Mailborder to Postfix Mapping:
      • Key: mailborder:{msg_id}
      • Value: Hash containing mailborder_id, postfix_id, status, and timestamp
    • Postfix to Mailborder Mapping (for reverse lookups):
      • Key: postfix:{queue_id}
      • Value: Hash containing mailborder_id, postfix_id, status, and timestamp
    • Delivery Status Tracking:
      • Key: mbid_status:{msg_id}
      • Value: Final delivery status (sent, bounced, deferred, etc.)

How Mailborder Vita Uses Redis #

The Mailborder Vita service leverages Redis to correlate Mailborder and Postfix queue IDs, enabling precise tracking of email delivery statuses:

  1. ID Correlation:

    • Master Server: Queries Redis for the Mailborder ID to retrieve the corresponding Postfix ID (mailborder:{msg_id}), then uses the Postfix ID (postfix:{queue_id}) to fetch the delivery status before logging to MariaDB.
    • Child Server: Follows the same process but sends the final data to the master server via API for centralized logging.
  2. Delivery Status Mapping:

    • Postfix status strings (e.g., sent, bounced) are converted into numeric codes for standardized logging across Mailborder services.
  3. Offline Queue Handling:

    • If Redis is unavailable, mb-redis queues data in /var/tmp/mailborder/redis_offline_queue.json. Once Redis connectivity is restored, the queued data is automatically processed and synced.

Redis for Performance Optimization #

Redis is used extensively across Mailborder services to cache configuration data, reduce redundant file reads, and improve overall system responsiveness:

  1. Configuration Caching:

    • Configuration files (e.g., /etc/mailborder/conf.d/redis.conf) are cached in Redis with modification time checks to avoid unnecessary reloads. This reduces I/O operations and speeds up configuration retrieval.
  2. Persistent Connections:

    • All Mailborder services, including mb-redis, use persistent Redis connections to minimize overhead from repeated connection establishment, ensuring fast data writes and lookups.
  3. Memory Management:

    • Redis is configured with a maximum of 512MB memory limit for Mailborder-related data. This ensures that even under heavy email loads, the system remains responsive. Keys are set to expire after 24 hours to prevent stale data accumulation. Note that 512MB would only be reached under extreme load. A normal memory load would be closer to 100MB or less under normal operations. 

System Requirements and Configuration #

  • PHP Version: PHP 7.x or higher (with pcntl, posix, and redis extensions).
  • Redis Configuration: Redis must be installed and configured to allow up to 512MB of memory for Mailborder. The configuration can be adjusted in /etc/mailborder/conf.d/redis.conf.
  • Socket Path: The daemon listens on /var/run/mailborder/mb-redis.sock, and Postfix logs data to this socket for efficient inter-process communication.

Service Management #

mb-redis is managed via systemd using the mb-redis.service unit file. Here are common management commands:

# Start the service
sudo systemctl start mb-redis

# Stop the service
sudo systemctl stop mb-redis

# Check the service status
sudo systemctl status mb-redis

# Enable the service at boot
sudo systemctl enable mb-redis

Logging and Error Handling #

  1. Log Files:

    • Standard Logs: /var/log/mailborder/mb-redis.log
    • Error Logs: /var/log/mailborder/mb-redis-error.log
  2. Syslog Integration:

    • The daemon logs critical events to syslog under the mail facility, ensuring centralized system logging.
  3. Error Recovery:

    • Redis disconnection during runtime triggers automatic reconnection attempts with exponential backoff. If Redis remains unavailable, data is queued for later processing.

Troubleshooting #

  • Redis Connection Errors:

    • Check the Redis service status:
      sudo systemctl status redis-server
      
    • Review error logs at /var/log/mailborder/mb-redis-error.log.
  • Socket Permission Issues:

    • Ensure the UNIX socket at /var/run/mailborder/mb-redis.sock is accessible by both postfix and mailborder users.
  • Offline Queue Not Clearing:

    • Manually review and clear /var/tmp/mailborder/redis_offline_queue.json if the file persists after Redis comes back online.