The Architecture of Bun Concurrency and Linux Limits
Core System Limits Comparison
| Resource Type | Default Linux / systemd Limit | Production Recommended Limit | Impact of Misconfiguration |
|---|---|---|---|
| Open File Descriptors | 1024 | 1048576 | Connection drops, EMFILE errors |
| Max Queued Connections (somaxconn) | 128 | 65535 | Connection timeouts, SYN packet drops |
| Virtual Memory Map Areas | 65530 | 524288 | Out-of-memory crashes under heavy heap sizing |
| Process Thread Limits | 7500 | 65535 | Failure to spawn worker threads |
Hardening Systemd Unit Files for Production Bun Services
[Unit]
Description=High-Performance Bun HTTP Microservice
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=notify
User=www-data
Group=www-data
WorkingDirectory=/var/www/api
ExecStart=/usr/local/bin/bun run start:prod
Restart=always
RestartSec=5
# Resource Limits
LimitNOFILE=1048576
LimitNPROC=65535
LimitMEMLOCK=infinity
TasksMax=infinity
# Security Hardening & Sandboxing
ProtectSystem=strict
ProtectHome=true
NoNewPrivileges=true
PrivateTmp=true
ReadWritePaths=/var/www/api/logs /var/www/api/tmp
# Performance Tuning
CPUSchedulingPolicy=other
Nice=-5
StandardOutput=journal
StandardError=journal
SyslogIdentifier=bun-api
[Install]
WantedBy=multi-user.targetKernel-Level Sysctl Tuning for Maximum Throughput
# Increase maximum socket receive and send buffers
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
# Expand the backlog queue for incoming TCP connections
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
# Enable TCP fast open and reuse time-wait sockets safely
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 15
# Increase maximum virtual memory mappings
vm.max_map_count = 524288Handling Memory Allocations and Garbage Collection
MemoryHigh=12G
MemoryMax=14G
OOMPolicy=continue
OOMScoreAdjust=-500Diagnostic Verification and Load Testing
hey -z 30s -c 500 -H 'Connection: keep-alive' https://api.local/health