Dump the code
Best ressources to improve your Python Skills

Nginx reverse proxy : lock the cache

Created 11 months ago
Posted By admin
4min read
The proxy_cache_lock directive is used to control the behavior of cache locking when multiple requests are trying to refresh the same cache key simultaneously. Cache locking is a mechanism that prevents multiple parallel requests from updating the cache simultaneously, avoiding cache stampedes and potential issues that may arise from concurrent cache updates.

Imagine a situation where a particular resource, let's say an image or a piece of dynamic content, is not present in the cache. Multiple clients simultaneously request this resource, resulting in cache misses. In a typical scenario without cache locking, each of these requests might independently reach out to the backend server to fetch the missing content and cache it.

Without cache locking, here's what might happen:

1. Multiple requests simultaneously:
   - Request 1 and Request 2 both realize that the content is not in the cache.
   - Both requests independently contact the backend server to fetch the content.

2. Backend Load:
   - The backend server receives simultaneous requests for the same content.
   - This could potentially overload the backend server, especially if fetching or generating the content is resource-intensive.

3. Cache stampede:
   - Once the content is fetched by one of the requests, the subsequent requests might still be trying to fetch the content independently.
   - This simultaneous fetching of the same content by multiple requests is often referred to as a "cache stampede."

4. Unnecessary load:
   - The backend server might end up doing the same work multiple times for the same content, putting unnecessary load on the server.

By enabling proxy_cache_lock, you introduce a mechanism to prevent this scenario:

- With Cache Locking:
  - Request 1 realizes the content is not in the cache and acquires a lock.
  - Request 2, arriving simultaneously, sees that there's a lock and waits until the lock is released.
  - Request 1 fetches the content and updates the cache while holding the lock.
  - Once done, Request 1 releases the lock.
  - Now, Request 2, which has been waiting, proceeds to fetch the content without causing a stampede.

Enabling cache locking in Nginx helps ensure that only one request at a time is responsible for updating the cache for a specific key, preventing unnecessary load on the backend server and avoiding the inefficiencies associated with simultaneous cache updates for the same content. It's a mechanism to introduce order and avoid contention when multiple requests are trying to cache the same resource concurrently.

http {
    proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;

    server {
        location / {

            # previous config

            # Set a cache locking
            proxy_cache_lock on;
            proxy_cache_lock_timeout 5s;  # Optional: specify lock timeout
        }
    }
}
  • timeout: Specifies the maximum time a request can spend waiting for the lock. If the lock is not acquired within the specified time, the request proceeds without caching.

Enabling cache locking in Nginx helps ensure that only one request at a time is responsible for updating the cache for a specific key, preventing unnecessary load on the backend server and avoiding the inefficiencies associated with simultaneous cache updates for the same content. It's a mechanism to introduce order and avoid contention when multiple requests are trying to cache the same resource concurrently.
Topics

Mastering Nginx

27 articles

Bash script

2 articles

Crontab

2 articles