# How to set html files into memcache

**URL:** <https://www.ruby-forum.com/t/how-to-set-html-files-into-memcache/220688>\
**Category:** NGINX\
**Created:** [June 17, 2012, 12:11am UTC](https://www.ruby-forum.com/t/how-to-set-html-files-into-memcache/220688 "2012-06-17T00:11:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [June 17, 2012, 12:11am UTC](https://www.ruby-forum.com/t/how-to-set-html-files-into-memcache/220688/1 "2012-06-17T00:11:37Z")

</div>

Using HttpMemcachedModule I could easily integrate memcache with nginx.

I wanted to know how do I set the missing htmls into memcache. Like  
this

If $uri in memchache  
serve from memcache  
else  
set into memcache  
server from proxy

Please suggest

Posted at Nginx Forum:

> **[How to set html files into memcache](https://forum.nginx.org/read.php?2,227610,227610#msg-227610)**
>
> Using HttpMemcachedModule I could easily integrate memcache with nginx. I wanted to know how do I set the missing htmls into memcache. Like this If $uri in memchache serve from memcache else set into memcache server from proxy

---

<div class="post-metadata">

**Author:** ![amodpandey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@amodpandey](https://www.ruby-forum.com/u/amodpandey)\
**Post date:** [June 17, 2012, 9:03am UTC](https://www.ruby-forum.com/t/how-to-set-html-files-into-memcache/220688/2 "2012-06-17T09:03:52Z")

</div>

Hello!

On Sun, Jun 17, 2012 at 6:11 AM, amodpandey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Using HttpMemcachedModule I could easily integrate memcache with nginx.
> 
> I wanted to know how do I set the missing htmls into memcache. Like  
> this

Take a look at my ngx\_srcache and ngx\_memc modules:

```
http://wiki.nginx.org/HttpSRCacheModule
http://wiki.nginx.org/HttpMemcModule

```

The former provides a generic and transparent response cache layer  
based on the Nginx subrequests while the latter provides a memcached  
client that implements most of the memcached TCP ASCII protocol and  
these two can work together.

Best regards,  
-agentzh

---

<div class="post-metadata">

**Author:** ![amodpandey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@amodpandey](https://www.ruby-forum.com/u/amodpandey)\
**Post date:** [June 17, 2012, 2:07pm UTC](https://www.ruby-forum.com/t/how-to-set-html-files-into-memcache/220688/3 "2012-06-17T14:07:08Z")

</div>

Thank you Agentzh.

I will look into that.

I have an associated question. I have a memcache cluster (multiple  
nodes). So I defined an

upstream memcached\_pass\_stream {  
server node1:11211;  
server node2:11211;  
server node3:11211;  
server node4:11211;  
}

And I pass it  
memcached\_pass $scheme://memcached\_pass\_stream/;

I am using memcached cluster (AWS ElastiCache)

Q1 I hope this is the right way to integrate with multiple nodes.

Q2 If yes, what logic nginx follows to pick the nodes? I should use  
similar logic to set data else there might be many misses.

Posted at Nginx Forum:

> **[Re: How to set html files into memcache](https://forum.nginx.org/read.php?2,227610,227622#msg-227622)**
>
> Using HttpMemcachedModule I could easily integrate memcache with nginx. I wanted to know how do I set the missing htmls into memcache. Like this If $uri in memchache serve from memcache else set into memcache server from proxy

---

<div class="post-metadata">

**Author:** ![amodpandey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@amodpandey](https://www.ruby-forum.com/u/amodpandey)\
**Post date:** [June 17, 2012, 4:12pm UTC](https://www.ruby-forum.com/t/how-to-set-html-files-into-memcache/220688/4 "2012-06-17T16:12:08Z")

</div>

Hello!

On Sun, Jun 17, 2012 at 8:06 PM, amodpandey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> And I pass it  
> memcached\_pass $scheme://memcached\_pass\_stream/;
> 
> I am using memcached cluster (AWS ElastiCache)
> 
> Q1 I hope this is the right way to integrate with multiple nodes.

No, unless all your memcached nodes are read-only and contain exactly  
the same data copy.

> Q2 If yes, what logic nginx follows to pick the nodes? I should use  
> similar logic to set data else there might be many misses.

By default, round-robin is used to pick up nodes. You can do key  
modulo hashing by means of the set\_hashed\_upstream directive provided  
by the ngx\_set\_misc module:

```
http://wiki.nginx.org/HttpSetMiscModule#set_hashed_upstream

```

Alternatively, you can just use some Lua code to calculate the backend  
upstream name in arbitrary way that you like for each individual  
request. See ngx\_lua for details. And here’s an example that  
determines a backend for proxy\_pass on-the-fly by querying a redis  
backend via a little Lua code:

```
http://openresty.org/#DynamicRoutingBasedOnRedis

```

The basic idea is essentially the same.

Regards,  
-agentzh

---

<div class="post-metadata">

**Author:** ![amodpandey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@amodpandey](https://www.ruby-forum.com/u/amodpandey)\
**Post date:** [June 18, 2012, 12:20am UTC](https://www.ruby-forum.com/t/how-to-set-html-files-into-memcache/220688/5 "2012-06-18T00:20:30Z")

</div>

Thank you Agentzh.

I did a test with a simple set up. Amazon large machine with nginx  
sering a file from disk and in another set-up serving teh same file from  
a local memcached. To my surprise I do not see any difference in the  
performance. They were equal. So given the complexity of memcache in  
between I see not having memcache is better.

How could be this possible? I did test to 100, 300 and 500 concurrent  
users with 89kb of html file for 1 minute using sieze.

Posted at Nginx Forum:

> **[Re: How to set html files into memcache](https://forum.nginx.org/read.php?2,227610,227630#msg-227630)**
>
> Using HttpMemcachedModule I could easily integrate memcache with nginx. I wanted to know how do I set the missing htmls into memcache. Like this If $uri in memchache serve from memcache else set into memcache server from proxy

---

<div class="post-metadata">

**Author:** ![amodpandey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@amodpandey](https://www.ruby-forum.com/u/amodpandey)\
**Post date:** [June 18, 2012, 5:48am UTC](https://www.ruby-forum.com/t/how-to-set-html-files-into-memcache/220688/6 "2012-06-18T05:48:16Z")

</div>

Hello!

On Mon, Jun 18, 2012 at 6:20 AM, amodpandey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Thank you Agentzh.

Please do not capitalize my nick. Thank you.

> I did a test with a simple set up. Amazon large machine with nginx  
> sering a file from disk and in another set-up serving teh same file from  
> a local memcached. To my surprise I do not see any difference in the  
> performance. They were equal. So given the complexity of memcache in  
> between I see not having memcache is better.

It’s very likely that your cache does not work at all 🙂 Please ensure  
that the cache hit rate on your side is not zero 🙂

The following slide shows how ngx\_srcache + ngx\_memc performs on  
Amazon EC2’s standard _Small_ instance for caching small resultsets (a  
single-line resultset) from a MySQL custom cluster:

```
http://agentzh.org/misc/slides/libdrizzle-lua-nginx/#84

```

And below is the result for caching big resultsets (100KB) in exactly  
the same setup:

```
http://agentzh.org/misc/slides/libdrizzle-lua-nginx/#85

```

You can walk through all the remaining slides in the same place to  
learn more about ngx\_srcache and ngx\_memc in your web browser by using  
the pagedown/pageup keys on your keyboard.

Regards,  
-agentzh

---

<div class="post-metadata">

**Author:** ![amodpandey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@amodpandey](https://www.ruby-forum.com/u/amodpandey)\
**Post date:** [June 20, 2012, 4:01am UTC](https://www.ruby-forum.com/t/how-to-set-html-files-into-memcache/220688/7 "2012-06-20T04:01:09Z")

</div>

Thank you agentzh.

I is hitting memcache and I have rechecked it.

Posted at Nginx Forum:

> **[Re: How to set html files into memcache](https://forum.nginx.org/read.php?2,227610,227704#msg-227704)**
>
> Using HttpMemcachedModule I could easily integrate memcache with nginx. I wanted to know how do I set the missing htmls into memcache. Like this If $uri in memchache serve from memcache else set into memcache server from proxy

---

<div class="post-metadata">

**Author:** ![amodpandey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@amodpandey](https://www.ruby-forum.com/u/amodpandey)\
**Post date:** [June 20, 2012, 8:26am UTC](https://www.ruby-forum.com/t/how-to-set-html-files-into-memcache/220688/8 "2012-06-20T08:26:30Z")

</div>

Hello!

On Wed, Jun 20, 2012 at 10:00 AM, amodpandey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Thank you agentzh.
> 
> I is hitting memcache and I have rechecked it.

Also, ensure that you’ve configured the memcached connection pool and  
if you’re using local memcached, you’d better use the unix domain  
socket to talk to memcached. Setting worker\_cpu\_affinity usually helps  
as well.

If you’re benchmarking ngx\_srcache + ngx\_memc with nginx’s ngx\_static  
module with a single or just a few URLs, then there’ll little  
difference because the static file requests will hit the operating  
system’s page cache any way.

Best regards,  
-agentzh
