KatInterview

Thread-Safe Config Registry with Stale-While-Revalidate

System DesignModel answer
HardModel answerSystem Designconcurrencyreliabilitypython

Last updated

Design a ConfigRegistry class that serves configuration to thousands of concurrent threads with lazy loading, stale fallback on failure, and no thundering herd.

Background

You are building a ConfigRegistry component for a high-throughput streaming service. Thousands of worker threads run simultaneously, and each one must retrieve the current configuration before it can process an event. The configuration lives in a remote database that is slow to query and occasionally unreachable.

Your Task

Design and implement a thread-safe ConfigRegistry class in Python (or Java) that satisfies the following requirements:

1. Read performance under high concurrency Reads should not block each other. Lock contention on the hot path must be negligible.

2. Lazy initialization The registry should not contact the remote database until a caller actually asks for the configuration. Fetching it eagerly on startup is not acceptable.

3. Graceful degradation during outages If the remote database cannot be reached, the registry must return the most recently fetched configuration rather than raising an error. It should also schedule a background refresh attempt rather than letting the stale value sit forever.

Assume a function fetch_remote_config() already exists. It is slow and may time out.

class ConfigRegistry:
    def get_config(self) -> Config:
        # your implementation here

Discussion Questions

After presenting your implementation, be ready to discuss:

  1. What concurrency primitive makes lazy initialization correct here, and why does a naive single-checked approach fail?
  2. How does your design balance returning a stale value (availability) against returning a consistent value (accuracy)? When would you choose stronger consistency instead?
  3. If fetch_remote_config() calls start saturating the network or database connection pool, what mechanism limits the damage?

Edge Cases to Address

  • Two threads both call get_config() for the very first time, simultaneously, before any configuration has been loaded. What happens?
  • The configuration TTL expires at the same moment for many threads across many service instances. How do you prevent every thread — or every node — from issuing a refresh at once?

Hints

Hint 1

The hot read path should touch zero locks. Think about what you can store as a single immutable reference so that reading it is atomic by definition.

Hint 2

For the cold-start race, double-checked locking is the classical solution — but in Java it only works correctly if the shared field is declared volatile. In Python, assignment of a single attribute is atomic in CPython, but you still need an explicit lock to prevent two threads from both deciding to fetch.

Hint 3

To prevent thundering herd on TTL expiry, only one thread should schedule the background refresh. A non-blocking lock acquire (acquire(blocking=False)) is a clean single-flight guard: the thread that wins schedules the work, everyone else skips and returns the stale value immediately. Add jitter to backoff delays to desynchronize retries across a fleet.