In-Process Cache for Lua (within nginx)

291 views
Skip to first unread message

Sparsh Gupta

unread,
May 31, 2012, 4:41:09 AM5/31/12
to openresty
Are there any recommendation for in-process caches that can be used in
Lua code. I am using content_by_lua_file for my main logic and
currently relying on memcache (via Lua-resty-memcache). Since the
memcache is not distributed currently (and wont be moving on) it will
be much faster to have something similar to APC (as in PHP) in Lua.

Any recommendations here?

Thanks
Sparsh Gupta

agentzh

unread,
May 31, 2012, 4:46:19 AM5/31/12
to open...@googlegroups.com
Hello!
For per-process caching, you can check out this ngx_lua documentation section:

http://wiki.nginx.org/HttpLuaModule#Data_Sharing_within_an_Nginx_Worker

For inter-process (but still within the same nginx server) caching,
you can check out this ngx_lua documentation section:

http://wiki.nginx.org/HttpLuaModule#ngx.shared.DICT

Regards,
-agentzh

Sparsh Gupta

unread,
May 31, 2012, 4:53:19 AM5/31/12
to open...@googlegroups.com
Thanks a lot for the pointers :)

I was wondering if there are any benchmarks anyone did comparing
ngx.shared.DICT with memcache (single server, accessed over unix
sockets). I will probably get my hands dirty but if there are any
studies anyone did already, would be interesting to see comparison

Is ngx.shared.DICT recommended over memcache for speed (response times)

Thanks again
Sparsh Gupta
> --
> 邮件自: 列表"openresty",专用于技术讨论!
> 发言: 请发邮件到 open...@googlegroups.com
> 退订: 请发邮件至 openresty+...@googlegroups.com
> 详情: http://groups.google.com/group/openresty
> 官网: http://openresty.org/
> 仓库: https://github.com/agentzh/ngx_openresty
> 建议: 提问的智慧 http://wiki.woodpecker.org.cn/moin/AskForHelp
> 教程: http://agentzh.org/misc/nginx/agentzh-nginx-tutorials-zhcn.html

agentzh

unread,
May 31, 2012, 5:04:20 AM5/31/12
to open...@googlegroups.com
Hello!

On Thu, May 31, 2012 at 4:53 PM, Sparsh Gupta <spars...@gmail.com> wrote:
> I was wondering if there are any benchmarks anyone did comparing
> ngx.shared.DICT with memcache (single server, accessed over unix
> sockets). I will probably get my hands dirty but if there are any
> studies anyone did already, would be interesting to see comparison
>

Disclaimer: "only trust what you can test!"

FWIW, the engineers at the Tencent company (QQ.com) reported 80k q/s
for ngx_lua + lua-resty-redis versus 120k q/s for ngx_lua +
ngx.shared_dict for read-only operations.

> Is ngx.shared.DICT recommended over memcache for speed (response times)
>

For the use cases involving lots of reads and much fewer writes, yes.
For other cases, you'll never know unless you do the benchmark
yourself.

Regards,
-agentzh

Sparsh Gupta

unread,
May 31, 2012, 5:15:28 AM5/31/12
to open...@googlegroups.com
Awesome, I will post some benchmarking results once we have it. Looking forward

I have 2 follow up questions:
1. Is there any best practices document while using ngx.shared.DICT in
a lua code? (e.g. recommended size of cache, how efficiency of cache
changes with size of cache, etc)

2. We primarily rely on 2 types of cache keys for serving our content.
One of the keys almost always fetch data below 500bytes (But have many
hundred thousands of such keys if not millions) and the other key
fetch data around 5-10Kb (Usually around 10,000 keys). Will there be
any difference in the following two scenerios (we will test them but
logically what do you think)

2.1 if we use a common DICT for both the keys
2.1. If we use a separate DICT (DICT1, DICT2) for the two types of
keys. If we adapt this, can we tune the cache in a way that it
utilizes the key-value size information for faster performance?

Thanks again for help
Sparsh Gupta

Weiqiang

unread,
May 31, 2012, 5:17:18 AM5/31/12
to open...@googlegroups.com
Looking into the codes, I find ngx.shared_dict is quite similar to memcache, but simpler.

Ngx+memcache uses unix sockets to do IPC while ngx.shared_dict uses shared memory method.

By my analysis, ngx.shared_dict will work faster than ngx+memcache

:-)

Weiqiang


Regards,
-agentzh

agentzh

unread,
May 31, 2012, 5:24:43 AM5/31/12
to open...@googlegroups.com
Hello!

On Thu, May 31, 2012 at 5:15 PM, Sparsh Gupta <spars...@gmail.com> wrote:
> Awesome, I will post some benchmarking results once we have it. Looking forward
>

Nice! Looking forward to your results :)

> I have 2 follow up questions:
> 1. Is there any best practices document while using ngx.shared.DICT in
> a lua code? (e.g. recommended size of cache, how efficiency of cache
> changes with size of cache, etc)
>

It mainly depends on concrete scenarios and how much resources you
have on your machine :) Basically, the larger the size of the cache,
the higher the cache hit rate will be.

> 2. We primarily rely on 2 types of cache keys for serving our content.
> One of the keys almost always fetch data below 500bytes (But have many
> hundred thousands of such keys if not millions) and the other key
> fetch data around 5-10Kb (Usually around 10,000 keys). Will there be
> any difference in the following two scenerios (we will test them but
> logically what do you think)
>
> 2.1 if we use a common DICT for both the keys
> 2.2. If we use a separate DICT (DICT1, DICT2) for the two types of
> keys. If we adapt this, can we tune the cache in a way that it
> utilizes the key-value size information for faster performance?
>

Option 2.2, i.e., using separate DICTs, should be better.

Using separate dictionaries can always reduce the over-all locking
effects (there is a global lock in each shared dict).

Memory fragmentation is also much less likely when the size of the
values in the single DICT do not vary much.

Hope this helps,
-agentzh

Sparsh Gupta

unread,
Jun 1, 2012, 5:31:01 AM6/1/12
to open...@googlegroups.com
Hello

On 31 May 2012 14:54, agentzh <age...@gmail.com> wrote:
> Hello!
>
> On Thu, May 31, 2012 at 5:15 PM, Sparsh Gupta <spars...@gmail.com> wrote:
>> Awesome, I will post some benchmarking results once we have it. Looking forward
>>
>
> Nice! Looking forward to your results :)
>

I did some benchmarks in worst case scenarios using a bare minimum
configuration and a very outdated slow machine.

Test 1: Using ngx.shared.DICT
ab -c 1000 -n 20000 http://10.20.30.40/test1
Requests per second: 1358.33 [#/sec] (mean)
Time per request: 0.736 [ms] (mean, across all concurrent requests)


Test 2: Using ngx-resty-memcache (1.4.5 over unix socket connection)
ab -c 1000 -n 20000 http://10.20.30.40/test2
Requests per second: 723.42 [#/sec] (mean)
Time per request: 1.382 [ms] (mean, across all concurrent requests)

For me shared DICT worked almost 88% better. In my production
environment (with better machines and configurations), I am sure the
difference wont be this huge but still it proved ngx.shared.DICT is
way better than memcached :)

Thanks
Sparsh Gupta

agentzh

unread,
Jun 1, 2012, 10:41:18 PM6/1/12
to open...@googlegroups.com
Hello!

On Fri, Jun 1, 2012 at 5:31 PM, Sparsh Gupta <spars...@gmail.com> wrote:
>
> I did some benchmarks in worst case scenarios using a bare minimum
> configuration and a very outdated slow machine.
>

Thanks for sharing the results!

But your benchmark numbers will make more sense to others if you also
share the exact nginx config file (snippets) and Lua code that you use
as well as the exact machine spec ;) Just a small suggestion :)

Thanks!
-agentzh
Reply all
Reply to author
Forward
0 new messages