惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
V
Visual Studio Blog
罗磊的独立博客
爱范儿
爱范儿
J
Java Code Geeks
博客园 - 司徒正美
N
Netflix TechBlog - Medium
Microsoft Security Blog
Microsoft Security Blog
美团技术团队
小众软件
小众软件
Google DeepMind News
Google DeepMind News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
V2EX
博客园 - 聂微东
云风的 BLOG
云风的 BLOG
WordPress大学
WordPress大学
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Jina AI
Jina AI
Y
Y Combinator Blog
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Martin Fowler
Martin Fowler
Vercel News
Vercel News

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
When the Treasure Hunt Engine ate my weekend
pretty ncube · 2026-05-27 · via DEV Community
Cover image for When the Treasure Hunt Engine ate my weekend

pretty ncube

The Problem We Were Actually Solving

Last April the Veltrix game servers began to stutter during the weekly global hunt. Players in Mumbai, São Paulo and Seattle all reported the same second-long freeze at minute 47, exactly when the treasure table exploded from 500 k rows to 2 million. Prometheus showed P99 latency climbing from 12 ms to 2.1 s and allocator stalls in jemalloc at 4 GB. The interesting detail was that the freeze happened only when the treasure table was larger than main-memory; once it spilled to SSD the GC pauses were gone. That meant the problem was not I/O but the runtimes idea of what memory safety looked like. We had tuned PostgreSQL, our CDN, even the kernels dirty-ratio, but the garbage collector was the invisible bottleneck.

What We Tried First (And Why It Failed)

We first blamed the SQL. Running pg_stat_statements showed the treasure lookup was a single CTE with an ORDER BY and a LIMIT. A 20-line Ruby script cached the winner in Redis and served the event, but the freeze persisted. We added read-replicas; P99 stayed the same. We put the whole table in TimescaleDBs in-memory cache; the freeze moved to minute 56 when the cache finally evicted something. The graph was still a hockey-stick.

Then we tried JRuby with the new incremental GC. The GC logs showed 200 ms safepoints every 800 ms. Latency still hit 2.4 s. We switched to TruffleRuby, hoping Graals native image would help. The startup time alone was 4.3 s and the allocation rate tripled because of the polyglot sandbox. The ops team said you cannot hot-patch a GraalVM node at 3 a.m. So the runtime was the constraint, but nothing we tried had removed the GC wall.

The Architecture Decision

I spent a sleepless Saturday running flamegraphs inside a flamegraph. The top entry was always malloc_hook in jemalloc and, deeper, objc_msgSend on the Ruby side. That told me the root cause was the language runtime interpreting every object dispatch. We could keep the Ruby logic—it was 1200 lines of battle-tested treasure geometry—but we needed a runtime that did not interpret.

We rewrote the treasure picker in Rust. Not idiomatic Rust with Vec>, but a flat arena of u32 indices and precomputed bounding boxes so the whole table lived as two slices: one for coordinates, one for rewards. We used BTreeMap only for the pruning phase; the hot path was linear in SIMD registers via the packed_simd crate. The allocator was mimalloc with large-page support so jemalloc never touched the treasure arena. The change was a 12-hour rewrite of the reward-selection loop; we left the REST API, Redis cache and PostgreSQL untouched.

The latency test looked like this:

Baseline (MRI 2.7): P99 2.1 s, alloc 1.8 GB, GC 24 %
Rust (mimalloc): P99 48 ms, alloc 120 MB, GC 0 %

After shipping to 10 % of players the error budget stayed flat. We rolled it out globally.

What The Numbers Said After

We left Prometheus scraping jemalloc and mimalloc for three weeks. The jemalloc stall events dropped from 47 per minute to zero. The treasure events RSS stayed at 140 MB even when the table grew to 5 million rows. The p95 tail was now dominated by PostgreSQLs seq scan, not by our code. The interesting detail was that the Rust binary used 30 % less CPU overall because the CPU spent zero cycles in a write barrier.

What I Would Do Differently

I would not have waited for the GC flamegraph. If I had instrumented malloc immediately I would have seen jemalloc was the bottleneck two days earlier. We also over-optimised the arena too early; the first Rust version still boxed every treasure struct and the GC pauses moved to the arena allocator. Only when we switched to raw slices did the GC truly vanish. Lastly, I would insist on a single cross-language profiler next time. perf every 10 seconds was not enough; we needed eBPF-based heap flamegraphs to see the malloc path without recompiling.