A real production bug from a payment service where class-level Ruby constants are evaluated once at class load time. If a database lookup returns nil at boot (due to autoload timing, deploy race conditions, or replica lag), the constant is permanently set to nil for the entire process lifetime — silently writing NULL to the database on every subsequent call. The fix replaces load-time constant assignment with lazy per-instance caching using the Singleton pattern and find_by! (which raises on missing rows instead of returning nil), ensuring the cache is never poisoned with a missing-row nil.

7m read timeFrom zhisme.com
Post cover image
Table of contents
The QuestionThe Symptom in ProductionThe Root Cause: The Class Body Runs OnceWhy Only Some Pods, Why Only SometimesThe FixWhy It’s a Good Interview Question
1.4K Impressions