Optimising Caching on Pwned Passwords (with Workers)

Junade Ali
8 min readintermediate
--
View Original

Overview

The article discusses the optimization of caching for the Pwned Passwords API, focusing on the implementation of a k-Anonymity model to enhance security and caching efficiency. It highlights the importance of cache hit ratios and the use of Cloudflare Workers to manage cache behavior effectively.

What You'll Learn

1

How to implement a k-Anonymity model for secure password checking

2

Why optimizing cache keys is crucial for API performance

3

How to use Cloudflare Workers to manipulate cache behavior

4

When to apply case insensitivity in API requests to improve caching

Prerequisites & Requirements

  • Understanding of caching mechanisms and API design
  • Familiarity with Cloudflare services and Workers(optional)

Key Questions Answered

How does the k-Anonymity model enhance password security?
The k-Anonymity model allows multiple leaked password hashes to be mapped to a single hash prefix, enhancing security by obscuring individual queries. This method enables efficient caching while maintaining user privacy, as it reduces the likelihood of exposing specific passwords.
What is the significance of cache hit ratios for the Pwned Passwords API?
The Pwned Passwords API has achieved a cache hit ratio of around 94%, which indicates that a high percentage of requests are served from the cache. This efficiency reduces latency and server load, making the API more responsive and scalable for users.
How can Cloudflare Workers improve caching for API requests?
Cloudflare Workers allow developers to customize cache behavior by manipulating request and response headers. This flexibility enables the removal of unnecessary headers, such as the Origin header, from cache keys, which can significantly improve cache hit ratios and reduce duplicate caching of assets.
What caching challenges arise from case sensitivity in API requests?
Case sensitivity in API requests can lead to duplicate caching of the same asset, as different casing results in different cache keys. This issue can be mitigated by standardizing request casing, which helps optimize cache usage and improve overall performance.

Key Statistics & Figures

Cache hit ratio
94%
This ratio was achieved for the Pwned Passwords API over the last week, indicating effective caching strategies.
Daily requests
8M requests
The Pwned Passwords API traffic has doubled, reflecting its growing usage and the importance of efficient caching.

Technologies & Tools

Backend
Cloudflare Workers
Used to manipulate cache behavior and enhance API request handling.
Security
K-anonymity Model
Provides a layer of security for password checking by obscuring individual queries.

Key Actionable Insights

1
Implement a k-Anonymity model to enhance the security of password checks in your applications.
This model allows you to obscure individual password queries while still providing accurate results, thereby improving user privacy and security.
2
Utilize Cloudflare Workers to customize your caching strategy effectively.
By adjusting cache keys and headers, you can significantly increase your cache hit ratio, leading to faster response times and reduced server load.
3
Standardize the casing of API requests to avoid duplicate caching.
By ensuring consistent casing in requests, you can prevent unnecessary cache duplication, which optimizes resource usage and improves performance.
4
Monitor cache hit ratios regularly to identify optimization opportunities.
Understanding your cache hit ratio can help you make informed decisions about caching strategies and identify areas for improvement.

Common Pitfalls

1
Failing to standardize request casing can lead to duplicate caching of assets.
This occurs because different casing generates different cache keys, resulting in inefficient use of cache space. To avoid this, ensure that all requests are normalized to a single casing format.

Related Concepts

Caching Strategies
API Security
Cloudflare Services