Performance tuning and resource optimization
Practical server optimizations for Bicrypto v7 and v6 — reverse-proxy micro-caching for Next.js SSR, Cloudflare rules, ScyllaDB memory capping, MariaDB and Redis tuning, NVMe swap, and kernel sysctl limits.
Bicrypto v7 moves API routing, WebSockets, trading engines, and cron jobs into
native Rust binaries (~473 MiB RSS combined across all 10 processes), which
leaves ScyllaDB, MariaDB, Next.js SSR runtime (next-server), and
local next build spikes as the main resource consumers on a production
server.
This guide documents every server-side tuning step you control on your VPS or dedicated host so you can handle more traffic on smaller hardware. See Server requirements for base hardware tiers.
What to tune first (ranked by impact)
| Priority | Target | Default Cost | Expected Savings / Gain |
|---|---|---|---|
| 1 | ScyllaDB memory & CPU caps (Ecosystem / Futures only) | ~5.0 GB RSS, 24 threads uncapped | Frees 3–4 GB RAM on 8–16 GB VPSes |
| 2 | Nginx / Apache SSR micro-caching + /_next/static headers |
Next.js SSR ~64–68 RPS, heap climbs to ~700 MB under burst load | Offloads anonymous landing/market/blog SSR pages to thousands of RPS and cuts frontend heap spikes |
| 3 | NVMe swap + NEXT_BUILD_CPUS |
Local next build spikes ~3–12 GB during live updates |
Prevents OOM kills during live in-site .next-staging builds |
| 4 | MariaDB InnoDB buffer pool | Untuned defaults or unbounded host cache (~1.3 GB+) | Matches buffer pool safely to 4 GB / 8 GB / 16 GB+ VPSes |
| 5 | Redis maxmemory policy |
Unbounded memory | Caps stale cache/session growth while protecting locks and auth state |
| 6 | Linux LimitNOFILE, TCP sysctls & allocator env |
Default 1,024 file descriptors; glibc heap fragmentation | Handles high concurrent WebSockets and keeps multi-MB exchange caches bounded |
1. Reverse-proxy micro-caching for Next.js SSR (Nginx & Apache)
In live benchmarks on a 6-core server (AMD Ryzen 5 3600), the Rust edge serves
13,000–37,500+ RPS cleanly, while Next.js server-side rendering on /en tops
out near 64–68 RPS because every uncached hit executes React SSR on Node.js.
Adding a short 15–30 second micro-cache exclusively for anonymous GET/HEAD requests to public SSR pages protects Next.js during traffic spikes without ever caching logged-in sessions, API responses, or WebSockets.
Every cache rule must bypass when:
- Any path starts with
/apior/admin(including WebSocket upgrades). - Any auth/session cookie (
accessToken,refreshToken,sessionId,csrfToken) is present. - Request method is not
GETorHEAD.
Nginx configuration
Place the cache zone inside http { ... } (/etc/nginx/nginx.conf), then apply
the bypass rules inside your HTTPS server { ... } block:
proxy_cache_path /var/cache/nginx/bicrypto_ssr
levels=1:2
keys_zone=bicrypto_ssr:32m
max_size=512m
inactive=10m
use_temp_path=off;# Bypass cache whenever user cookies exist or request is non-GET
map $http_cookie $bicrypto_bypass_cache {
default 0;
~*(accessToken|refreshToken|sessionId|csrfToken)= 1;
}
# 1. Immutable Next.js build chunks & uploaded public assets
location ^~ /_next/static/ {
proxy_pass http://127.0.0.1:3000;
expires 365d;
add_header Cache-Control "public, max-age=31536000, immutable";
}
# 2. Never cache API or WebSocket traffic
location /api/ {
proxy_pass http://127.0.0.1:4000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass 1;
proxy_no_cache 1;
}
# 3. Never cache admin routes
location ~ ^/[a-z-]+/admin {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass 1;
proxy_no_cache 1;
}
# 4. Short micro-cache (20s) for anonymous public SSR pages (/en, /en/market, /en/blog, /en/faq, /en/p2p, /en/ico, /en/nft, /en/ecommerce)
location ~ ^/([a-z-]+/?$|[a-z-]+/(market|blog|faq|p2p|staking|ico|nft|ecommerce)(/|$)) {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache bicrypto_ssr;
proxy_cache_methods GET HEAD;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_valid 200 20s;
proxy_cache_lock on;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_bypass $bicrypto_bypass_cache $http_authorization;
proxy_no_cache $bicrypto_bypass_cache $http_authorization;
add_header X-Cache-Status $upstream_cache_status;
}Test and reload Nginx:
nginx -t && systemctl reload nginxApache 2.4 (mod_cache_disk + mod_headers)
On Apache hosts (such as Virtualmin/cPanel-managed Ubuntu VPSes running Apache as
the TLS terminator), keep WebSocket/API rules first and enable short disk caching
only on immutable static bundles (or anonymous public routes using
RequestHeader/cookie exclusions):
# Immutable cache headers for versioned Next.js chunks
<LocationMatch "^/_next/static/">
Header set Cache-Control "public, max-age=31536000, immutable"
</LocationMatch>
# Always keep /api and /admin uncached
<LocationMatch "^/(api|[^/]+/admin)">
Header set Cache-Control "no-store, no-cache, must-revalidate"
</LocationMatch>Keep ProxyPreserveHost On and increase MaxRequestWorkers (mpm_event) in
/etc/apache2/mods-available/mpm_event.conf if serving high concurrent API/WSS
traffic.
2. Cloudflare CDN & edge caching rules
If your domain sits behind Cloudflare:
- Always Bypass Cache for:
(http.request.uri.path starts_with "/api/")(http.request.uri.path contains "/admin")(http.cookie contains "accessToken=")or(http.cookie contains "sessionId=")
- Cache Longer (1 month–1 year) for
/_next/static/*and static uploads (/uploads/*,/img/*). - Optional Edge Cache (15–30s) for anonymous GET requests matching
/en,/en/market,/en/blog*,/en/faq*only when the cookie bypass rule above is active with higher priority. - Keep WebSockets enabled in Cloudflare → Network (
wss://streams).
3. ScyllaDB memory & CPU capping (Ecosystem & Futures only)
ScyllaDB is required only when running the Ecosystem or Futures
addons. Out of the box, ScyllaDB locks memory across all available cores (measured
at ~5,018 MiB RSS and 24 threads on a 6-core server).
On 8–16 GB VPSes running ScyllaDB alongside MariaDB and Next.js:
# Single core in overprovisioned mode for 8 GB VPS (use --smp 2 on 16 GB+)
CPUSET="--cpuset 0 --smp 1 --overprovisioned 1"# Cap ScyllaDB memory allocation (use --memory 2G on 16 GB VPS)
MEM_CONF="--memory 1G --lock-memory=0"Apply and restart ScyllaDB:
systemctl restart scylla-server
systemctl status scylla-server --no-pagerIf you do not run Ecosystem or Futures, stop/disable ScyllaDB completely and
keep SCYLLA_ENABLED="false" in .env to reclaim all ~5 GB of RAM.
4. MariaDB / MySQL tuning by VPS size
Create /etc/mysql/mariadb.conf.d/99-bicrypto.cnf sized to your server RAM.
Never allocate more than ~30–40% of total RAM to innodb_buffer_pool_size on an
all-in-one VPS that also runs Next.js and ScyllaDB:
| VPS RAM | innodb_buffer_pool_size |
innodb_log_file_size |
max_connections |
Recommended DB_POOL_MAX in .env |
|---|---|---|---|---|
| 4 GB (Core+Spot) | 384M–512M |
128M |
120 |
10 |
| 8 GB | 1G |
256M |
150 |
15 |
| 16 GB | 2G–4G |
512M |
200 |
25 |
| 32 GB+ (Dedicated DB / High-Traffic) | 8G–12G |
1G |
300 |
40 |
[mysqld]
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 1
innodb_flush_method = O_DIRECT
max_connections = 150
table_open_cache = 2000
tmp_table_size = 64M
max_heap_table_size = 64M
slow_query_log = 1
long_query_time = 1.0Reload cleanly:
systemctl restart mariadb(Keep innodb_flush_log_at_trx_commit = 1 on exchanges handling ledger balances
for ACID durability.)
5. Redis tuning
Bicrypto uses Redis for sessions, distributed engine leases, pub/sub frame relay,
settings caches, and rate limiting. Unlike v6 (where BullMQ retained large job
histories), a clean v7 install uses under 50 MiB in Redis (db7 measured < 5 MiB).
Still configure a ceiling and disable slow AOF always fsyncs if storing high-frequency
market fanout frames:
maxmemory 256mb
# Keep noeviction so distributed locks, sessions, and lic:snapshot are never evicted mid-flight
maxmemory-policy noeviction
appendonly yes
appendfsync everysecIf sharing Redis with an older v6 install (db 0), run v7 on a dedicated Redis
database index (REDIS_DB=7) or separate Redis instance so legacy BullMQ data
cannot interfere with v7 leases.
6. NVMe swap, live-update build workers & glibc allocator tuning
Add NVMe swap (essential on 4–8 GB VPSes)
Live zero-downtime updates (V7_LIVE_UPDATE=1) build frontend/.next-staging
while the live site keeps serving traffic. Add 8 GB of swap on NVMe/SSD so
temporary Turbopack/PostCSS spikes never trigger the Linux OOM killer:
sudo fallocate -l 8G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstabSet low swappiness so idle services stay resident in RAM while swap absorbs only rare build peaks:
vm.swappiness = 10
net.core.somaxconn = 4096
net.ipv4.tcp_max_syn_backlog = 4096
fs.file-max = 262144Apply sysctl settings:
sudo sysctl --systemBound Next.js parallel build workers (NEXT_BUILD_CPUS)
Bicrypto v7 automatically caps Next.js worker count inside
scripts/finalize-update.mjs (2 workers when host RAM <= 10 GB, 4
workers above 10 GB), and you can override it explicitly in .env before a
manual rebuild (pnpm build:frontend):
# Limit parallel build workers on memory-constrained VPSes (lower peak build RSS)
NEXT_BUILD_CPUS="2"PM2 file descriptor limits & glibc arena bounds (v7)
Bicrypto v7's backend/pm2.rust.config.js sets MALLOC_ARENA_MAX=2,
MALLOC_MMAP_THRESHOLD_=131072, and MALLOC_TRIM_THRESHOLD_=131072 automatically
so multi-threaded Rust services return transient buffers (such as venue ticker/market
JSON snapshots) cleanly back to Linux instead of bloating per-thread malloc arenas.
For high WebSocket concurrency, raise open-file limits for the bicrypto / app user
and PM2 daemon:
* soft nofile 65536
* hard nofile 65536Log out and restart the PM2 daemon (pm2 kill && bicrypto-ops start --profile release && pm2 start pm2.frontend.config.js --env production) for new ulimit -n limits to take effect.
7. When to scale horizontally (Multi-Node Layout)
Once CPU load concentrates on database I/O or SSR traffic (> 2,000+ concurrent
users or high Ecosystem/Futures order volume), split roles in this order:
- Separate database node first: Move MariaDB + ScyllaDB onto a dedicated
storage/DB server with fast NVMe RAID, pointing
DB_HOSTandSCYLLA_CONNECT_POINTSover private VPC networking. - Micro-cache / CDN static & SSR routes: Enable Nginx/Cloudflare caching
(Section 1 & Section 2) so
next-serverhandles only dynamic SSR routes. - Additional frontend or edge replicas: Because v7 state lives in Redis,
MariaDB, and ScyllaDB, stateless
frontendandrust-edgenodes scale behind a load balancer using sticky or shared Redis sessions while lease-arbitrated services (rust-scheduler,rust-markets,rust-trading) automatically keep one active leader per lease key.