# Fwd: Lua shared storage inconsistent?

**URL:** <https://www.ruby-forum.com/t/fwd-lua-shared-storage-inconsistent/227392>\
**Category:** NGINX\
**Created:** [March 14, 2013, 1:40am UTC](https://www.ruby-forum.com/t/fwd-lua-shared-storage-inconsistent/227392 "2013-03-14T01:40:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 14, 2013, 1:40am UTC](https://www.ruby-forum.com/t/fwd-lua-shared-storage-inconsistent/227392/1 "2013-03-14T01:40:50Z")

</div>

Hello,

I’m trying to count with a Lua-script the concurrent requests to a given  
virtual host on the backend to which Nginx relays als reverse proxy.

My current code is somewhat more complex, but I try to reduce it to the  
problematic one. Assuming we have a shared storage with sufficient  
memory  
storage (let’s say 100MB) I try the following:

\*\* phase\_access:  
ngx.shared.counter:add(ngx.var.host,0) --makes sure the value exists  
ngx.shared.counter:incr(ngx.var.host,1)

\*\* phase\_log  
ngx.shared.counter:incr(ngx.var.host,-1)

I assumed -since the storage is supposedly atomic and shared- that this  
will work. However when running a benchmark with eg 500 concurrent  
connections, I will always be somewhat around 70 units in the minus at  
the  
end of the benchmark. How come?

I tried setting the incrementer in other phases such as rewrite and the  
decrementer in the body-phase, but that didn’t change anything.  
Also I noticed that taking a copy of the shared storage will not have  
that  
copy update during sleep-loops of the given request and I have to fetch  
a  
new one (with the penalty of keeping allocating new RAM)

Is there anything fundamentally wrong with my understanding of Nginx, is  
there a bug in the lua implementation or what happened?

Thank you for helping me out - I certainly couldn’t so far…
