# Limit\_req\_zone key cache lifetime

**URL:** <https://www.ruby-forum.com/t/limit-req-zone-key-cache-lifetime/244940>\
**Category:** NGINX\
**Created:** [July 14, 2016, 9:22pm UTC](https://www.ruby-forum.com/t/limit-req-zone-key-cache-lifetime/244940 "2016-07-14T21:22:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [July 14, 2016, 9:22pm UTC](https://www.ruby-forum.com/t/limit-req-zone-key-cache-lifetime/244940/1 "2016-07-14T21:22:44Z")

</div>

I’m looking for documentation or explanation for how keys expire in the  
limit\_req\_zone. I have the basic documenations here:

_A client IP address serves as a key. Note that instead of $remote\_addr,  
the $binary\_remote\_addr variable is used here.  
The $binary\_remote\_addr variable’s size is always 4 bytes for  
IPv4 addresses or 16 bytes for IPv6 addresses. The stored state always  
occupies 64 bytes on 32-bit platforms and 128 bytes on 64-bit platforms.  
One megabyte zone can keep about 16 thousand 64-byte states or about 8  
thousand 128-byte states. If the zone storage is exhausted, the server  
will  
return the 503 (Service Temporarily Unavailable) error to all further  
requests._

But there’s no explanation for how the key cache eventually clears  
itself.  
Is any available?

---

<div class="post-metadata">

**Author:** ![Chad\_Hansen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9d8465/32.png) [@Chad\_Hansen](https://www.ruby-forum.com/u/Chad_Hansen)\
**Post date:** [July 14, 2016, 10:27pm UTC](https://www.ruby-forum.com/t/limit-req-zone-key-cache-lifetime/244940/2 "2016-07-14T22:27:00Z")

</div>

Hello!

On Thu, Jul 14, 2016 at 07:22:06PM +0000, Chad Hansen wrote:

> return the 503 (Service Temporarily Unavailable) error to all further  
> requests.\*
> 
> But there’s no explanation for how the key cache eventually clears itself.  
> Is any available?

The same page also specifies the algorithm used,  
[http://nginx.org/en/docs/http/ngx\_http\_limit\_req\_module.html:](http://nginx.org/en/docs/http/ngx_http_limit_req_module.html:)

: The limitation is done using the “leaky bucket” method.

See [Leaky bucket - Wikipedia](https://en.wikipedia.org/wiki/Leaky_bucket) for further  
details about the algorithm itself.

The algorithm implies that there is no need to store anything for  
keys where there are no excessive requests. Such zero states are  
automatically removed by the code when nginx is about to allocate  
a new state.

–  
Maxim D.

> **[nginx](http://nginx.org/)**

---

<div class="post-metadata">

**Author:** ![Chad\_Hansen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9d8465/32.png) [@Chad\_Hansen](https://www.ruby-forum.com/u/Chad_Hansen)\
**Post date:** [July 14, 2016, 10:31pm UTC](https://www.ruby-forum.com/t/limit-req-zone-key-cache-lifetime/244940/3 "2016-07-14T22:31:05Z")

</div>

Great, thank you!
