Meeting Legal Requirements, Limiting Breach Damage, and Minimizing Storage Cost
Every log you keep is a double-edged asset. It is evidence when you need to investigate an incident or satisfy an auditor, and it is liability if it leaks. It also costs money every month it sits on disk. Keep too little and you violate regulations or lose the evidence you need. Keep too much and you inflate your storage bill and the blast radius of a breach.
In this post we turn that tension into a small optimization problem and solve it in Python. For six log categories we choose two numbers each:
- the total retention period $R$ (days), and
- the hot-storage period $h$ (days), the number of days the data stays on fast, expensive storage before moving to cheap archive.
The Model
For a category with monthly volume $v$ (GB/month), the monthly cost is the sum of five terms:

The symbols are:
- $c_{\text{hot}}, c_{\text{cold}}$: storage price per GB-month for the hot and cold tiers.
- $p$: annual probability of a breach, and $c_{\text{leak}}$: damage per sensitivity-weighted GB-month of exposed data.
- $s$: sensitivity weight of the category.
- $\lambda$: number of investigations or audit requests per month.
- $C_{\text{miss}}$: cost incurred when the needed evidence has already been deleted.
- $\theta, k$: scale and shape of a Weibull distribution describing how late a request may arrive.
- $C_{\text{ret}}$: cost of restoring data from cold storage, and $\ell$: the typical lookback of an investigation (days).
The optimization problem is:
$$
\min_{R,,h}; J(R,h)\quad\text{s.t.}\quad R\ge L_{\text{legal}},\qquad 0\le h\le R
$$
The key insight is that the breach term grows linearly in $R$, while the evidence-missing term decays exponentially. The optimum sits where the marginal breach exposure equals the marginal reduction in expected evidence loss:
$$
\frac{\partial J}{\partial R}=v,c_{\text{cold}}+\frac{p}{12},c_{\text{leak}},s,v-\lambda,C_{\text{miss}},\frac{k}{\theta}\left(\frac{R}{\theta}\right)^{k-1}e^{-(R/\theta)^{k}}=0
$$
When this root lies below the legal minimum $L_{\text{legal}}$, the legal constraint is binding and the best choice is to delete exactly at the legal limit.
The Example
We compare three policies for six log categories:
- Naive: keep everything for 7 years (2,555 days), all on hot storage.
- Legal minimum: keep each category only for its legal period, on cold storage with a 30-day hot window.
- Optimized: the solution of the problem above.
| Category | Volume (GB/mo) | Legal minimum (days) | Sensitivity |
|---|---|---|---|
| Auth | 40 | 365 | 3.0 |
| Payment audit | 5 | 2,555 | 5.0 |
| Web access | 300 | 90 | 1.0 |
| DB audit | 60 | 730 | 4.0 |
| App debug | 500 | 30 | 0.5 |
| Network flow | 800 | 180 | 1.5 |
Source Code
1 | import numpy as np |
Code Walkthrough
Parameters. The first block defines unit prices, the breach probability, and the table of six categories. Each row carries the legal minimum, sensitivity, audit frequency, the penalty for missing evidence, and the retrieval penalty. Changing these numbers is all you need to adapt the model to your own environment.
components. This function returns the five cost terms of $J(R,h)$ separately, so we can both sum them for optimization and plot them individually later. It is written with NumPy operations only, so it accepts scalars and whole arrays alike. The line h = np.minimum(h, R) enforces $h \le R$ even when the grid contains infeasible pairs.
optimize. This is where the speed-up happens. Instead of nested Python loops over $(R,h)$, we build the entire grid at once with np.meshgrid (600 values of $R$ times 81 values of $h$, about 48,000 points) and evaluate the cost in a single vectorized call. Infeasible points, those with $R<L_{\text{legal}}$ or $h>R$, are set to $\infty$, and np.argmin plus np.unravel_index locate the global minimum on the grid. Because the cost is evaluated exhaustively, we get the true grid optimum with no risk of a local minimum, in a fraction of a second per category.
Solving and baselines. For every category we compute the optimum, the naive 7-year all-hot cost, and the legal-minimum cost. The final table reports the savings relative to both.
Plot data. We prepare four datasets. The first is a coarse $(R,h)$ surface for the Auth category. The second is a cost curve versus $R$ at the optimal hot period, split into components. The third is a sensitivity map: the optimal $R$ for Auth over a $10\times10$ grid of breach probabilities and leak-damage factors (100 full optimizations, still quick thanks to vectorization). The fourth is the cost breakdown at each category’s optimum.
Figure. A single figure holds six panels in 2 rows by 3 columns. Panels 1 and 5 are 3D surfaces, and the others are 2D charts.
Console Output
=== Optimal retention policy per category (monthly cost in USD) ===
category legal_min_days R_opt_days hot_opt_days cost_naive cost_legal_min cost_opt saving_vs_naive_% saving_vs_legal_%
Auth 365 616.0 75.0 7461.4 2510.3 1944.5 73.9 22.5
Payment audit 2555 2556.0 160.0 1407.2 1388.1 1183.0 15.9 14.8
Web access 90 171.0 20.0 30404.5 2474.2 2267.3 92.5 8.4
DB audit 730 731.0 70.0 13792.6 4832.6 4777.4 65.4 1.1
App debug 30 31.0 0.0 40028.3 752.6 548.4 98.6 27.1
Network flow 180 181.0 5.0 98112.0 5454.2 5258.7 94.6 3.6
=== Total monthly cost (USD) ===
cost_naive 191206.0
cost_legal_min 17412.0
cost_opt 15979.0
Result Graphs

Interpreting the Results
Total cost. Keeping everything for seven years on hot storage costs about $191,206 per month across the six categories. Applying only the legal minimum with cold storage brings it down to about $17,412. The optimized policy reaches about $15,979, a 91.6% reduction versus the naive policy and a further 8.2% reduction versus the legal-minimum policy. The first jump comes from tiering and deleting what no one needs. The second comes from tuning each category individually.
Panel 1 (3D cost surface, Auth). The surface has a clear valley. Along the hot-period axis, too little hot time is penalized by retrieval costs, and too much by hot-storage prices. Along the retention axis, cost falls as evidence-loss risk shrinks, then climbs again as storage and breach exposure grow. The optimum is a basin, not a corner.
Panel 2 (cost components, Auth). The legal minimum is 365 days, but the optimum is 616 days. The evidence-missing cost (the area that shrinks as $R$ grows) is large for authentication logs, because investigations of account compromise often arrive late. Extending retention beyond the legal minimum pays off until the rising cold-storage and breach-exposure areas outweigh the shrinking evidence-loss area. The hot period at the optimum is 75 days.
Panel 3 (policy comparison). On a log scale, the optimized bar is always the lowest. The gap to the naive policy is enormous for high-volume, low-risk data such as Network flow (94.6% saving) and App debug (98.6%), because these are exactly the categories where seven years of hot storage is wasteful.
Panel 4 (optimal vs legal retention). For four of the six categories, the optimum is essentially the legal minimum (one grid step above it). In those cases the legal constraint is binding: the marginal cost of keeping data longer exceeds the marginal evidence benefit. Only Auth deviates materially, because its evidence-loss penalty is high relative to its small volume.
Panel 5 (3D sensitivity). Higher breach probability and higher leak damage both push the optimal retention down toward the legal floor, while low risk lets the optimum rise. The surface quantifies a governance question: how much does your risk appetite change how long you should keep authentication logs? When risk estimates are uncertain, this panel shows which direction is safe to err in.
Panel 6 (cost breakdown). At the optimum, the dominant cost differs by category. Voluminous logs are dominated by storage and breach exposure, while the small but critical Payment audit and DB audit logs are dominated by the legal-driven retention itself. This tells you where to spend engineering effort: compress and tier the big categories, and focus on access control and encryption for the sensitive ones.
Takeaways
- Legal minimums are floors, not targets. For most categories the best policy is to delete as soon as the law allows. For categories with expensive missing-evidence scenarios, going beyond the minimum can be rational.
- Tiering matters as much as retention. The hot period $h$ is a separate lever. App debug logs need no hot window at all, while Payment audit logs justify 160 days.
- Risk parameters should be explicit. Breach probability and damage estimates move the optimum, and a sensitivity surface makes that dependence visible and debatable instead of hidden in a policy document.
- Vectorized grid search is enough. With two decision variables per category, evaluating the full grid is fast, exact on the grid, and easy to audit, which is a good property for compliance-related decisions.
















