Secrets management in Apache Airflow - Time & Space Complexity
When managing secrets in Airflow, it is important to understand how the time to retrieve secrets grows as the number of secrets increases.
We want to know how the system behaves when fetching secrets from a backend as the secret count grows.
Analyze the time complexity of the following Airflow secrets retrieval code.
from airflow.secrets import BaseSecretsBackend
class MySecretsBackend(BaseSecretsBackend):
def __init__(self, secrets: dict):
self._secrets = secrets
def get_secret(self, key: str) -> str | None:
for secret_key, secret_value in self._secrets.items():
if secret_key == key:
return secret_value
return None
This code defines a simple secrets backend that looks up a secret by checking each stored secret key one by one.
- Primary operation: Looping through all stored secrets to find a matching key.
- How many times: Up to once for each secret stored in the backend.
As the number of secrets increases, the time to find a secret grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The number of checks grows linearly with the number of secrets stored.
Time Complexity: O(n)
This means the time to find a secret grows directly with the number of secrets stored.
[X] Wrong: "Looking up a secret always takes the same time no matter how many secrets there are."
[OK] Correct: Because this code checks each secret one by one, more secrets mean more checks and longer lookup time.
Understanding how secret lookup time grows helps you design better systems and shows you can think about performance in real tools like Airflow.
"What if we changed the secrets storage to a dictionary lookup instead of looping? How would the time complexity change?"