Feature flag snapshot with periodic refresh and atomic reads

4823
0

I like feature flags that are boring at runtime: reads should be lock-free and refresh should happen in the background. The pattern here stores a JSON flag snapshot in an atomic.Value, which makes reads cheap and race-free. A ticker refreshes the snapshot periodically from a backend (S3, database, config service) using a short context.WithTimeout. If refresh fails, we keep the last good snapshot, which is usually the safest behavior in production. The other key is exposing a small API (Enabled("new_checkout")) so call sites don’t learn about storage formats. This approach works well for “static-ish” flags like gradual rollouts and kill switches. For targeting by user, you can extend the model later, but the baseline stays simple and safe under load.