Feature Flags with Postgres and an In-Memory Cache in Go
How one small team replaced a $400/month feature flag service with a four-column Postgres table and a Go in-memory cache, and when a vendor still fits.
· 2 min read · Erik Kubica
A SaaS client recently asked me to review their infrastructure spending to see where they could trim monthly costs.
Between their AWS compute charges and their Stripe fees was a $400/month LaunchDarkly subscription, about $4,800 a year.
They had twelve active flags, four developers and one production environment. For that setup the tool was more than they needed, and the vendor SDK also ran background polling goroutines, used around 45 MB of memory, and occasionally added latency of about 80 ms while syncing flag states on cold boots of their application server.
A Four-Column Table
For a setup this small, you may not need a hosted platform. If you already run a database, a table can do the job.
Here is the PostgreSQL table I used to replace their subscription:
-- Production feature flag schema in plain Postgres
CREATE TABLE feature_flags (
key TEXT PRIMARY KEY,
enabled BOOLEAN NOT NULL DEFAULT false,
rollout_pct INT NOT NULL DEFAULT 0 CHECK (rollout_pct BETWEEN 0 AND 100),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
To roll out a new checkout flow to 20% of users, you run a single SQL query:
UPDATE feature_flags
SET enabled = true, rollout_pct = 20, updated_at = NOW()
WHERE key = 'new_checkout_flow';
An In-Memory Cache
You usually don't want request handlers making a network hop or a database query just to check a flag.
In Go, I wrap this table in a thread-safe in-memory cache using sync.RWMutex:
type FlagStore struct {
mu sync.RWMutex
flags map[string]Flag
}
// Check evaluates flag in 0.002ms without touching network or disk
func (s *FlagStore) IsEnabled(key string, userID int64) bool {
s.mu.RLock()
flag, exists := s.flags[key]
s.mu.RUnlock()
if !exists || !flag.Enabled {
return false
}
if flag.RolloutPct >= 100 {
return true
}
// Deterministic hashing for percentage rollouts
if userID < 0 {
userID = -userID
}
return (userID % 100) < int64(flag.RolloutPct)
}
A background goroutine polls the feature_flags table every 30 seconds and swaps the pointer. If you want faster updates without polling, PostgreSQL LISTEN / NOTIFY can push changes to your Go process over an existing database connection; in my case that took a few milliseconds.
Rough Comparison
| Metric | LaunchDarkly SaaS | Postgres + In-Memory RWMutex |
|---|---|---|
| Monthly Cost | $400 / mo | No extra cost (uses existing DB) |
| Evaluation Latency | roughly 15 – 80 ms (SDK sync, on that setup) | roughly 0.002 ms (in-memory) |
| Runtime Memory Overhead | about 45 MB RSS | under 100 KB RSS |
| External Dependencies | 1 SaaS vendor + SDK | none extra |
For twelve flags and four developers, a small table was enough. A hosted tool can make sense once you have many flags, targeting rules, audit needs or non-technical people managing them.
I removed the vendor SDK, added the table to their existing database, and hooked a simple admin page into their dashboard. On that project it saved about $4,800 a year, took roughly 30 ms off tail latency, and removed one external dependency. What you give up is the polished console, targeting rules and audit trail, so check whether you actually use those before switching.