Lua LRU Cache & ngx.shared dict VS Redis & Memcached

511 views
Skip to first unread message

alexande...@gmail.com

unread,
Apr 3, 2018, 12:26:25 AM4/3/18
to openresty-en
Hi,

I have OpenResty server with 2 GB RAM and I need to cache the data from PostgreSQL (many arrays of strings ~ 2kb).
In the cache I have 50 reads per 1 write. So what in-memory data structure will has more performance?

Thanks,
Alexander.

tokers

unread,
Apr 3, 2018, 1:40:51 AM4/3/18
to openresty-en
Hello!

The implementation of lua-resty-lrucache can cache any data structure like Lua table or some userdata, 
but it is per-worker, multiple workers cannot share the cache.

The ngx.shared.dict can be shared among multiple workers, but it store the serialized data like number, string,
so you have to serialize the complex structure by yourself before you store it to ngx.shared.dict.

Puts the cache to an external service like redis or memcached can share the data across a single machine, 
but it costs some RTT to complete the communication.

I think you can combine these tools rather than use only one of them.

Some good libraries about this part:

alexande...@gmail.com

unread,
Apr 3, 2018, 1:59:27 AM4/3/18
to openresty-en
Do you think that using built-in features of resty/nginx is more efficient way to get data?

Also I am worried that if lua-resty-shcache / nginx.shared.dict has some memory leaks / undefined behaviour when the server has a small amount of memory, it could make the web server down.

Jim Robinson

unread,
Apr 3, 2018, 8:34:08 PM4/3/18
to openresty-en
Keep in mind that constraining your cache to one machine's memory increases the complexity if you might need to scale out in the future (because you'd be dealing with duplicate records across machines).   Using a memcache or redis pool lets you separate managing a pool of HTTP servers from the concern of managing a pool of cached resources.  For us at least the extra overhead of talking to memcache seemed worth the increased scalability.

Reply all
Reply to author
Forward
0 new messages