Home » Php » php – Memcached on EC2

php – Memcached on EC2

Posted by: admin July 12, 2020 Leave a comment


Am I right in thinking that until I am able to afford dedicated servers or have any spare servers, I could successfully run a small number of memcached servers through EC2?

With the annoucement of the new auto-scaling and load balancing by Amazon today, do you guys think this would be a viable option?

And what would be the basic technical steps you’d recommend me taking?


Currently, I have one dedicated server and no memcached servers. I want to use the power of EC2 to setup a few instances and run such memcached servers. That’s my current setup.

How to&Answers:
  • Load balancing has nothing to do with Memcached — it uses a hash algorithm for connecting to servers
  • I highly recommend not using autoscaling with Memcached — adding servers breaks the hashing algorithm and invalidates your cache. Data will go missing and you’ll have to recache.
  • You’ll want to check the latency from your servers to EC2 — if it’s more than 50ms, you’ll be hurting your performance significantly. Well, I’d assume anyway.

You can pull multiple keys (see here for how) with one request to reduce the latency effect, but you’ll still take the initial hit. And it also means you need to know all the keys your going to get before you make the call. Otherwise each request adds 50ms (or more) to the execution time of your script.

Consider the data your trying to cache. Is a 64mb slab large enough to help you? You can probably run it on your main servers.


To really take advantage of memcached you need to have your memcache communicating with your code as quickly as possible. You may want to investigate how much latency you’d have between the EC2 servers and your own.

Ultimately, you might be better served upping the ram on your current box to something like 4 gigs (should run you about 50 bucks) and putting memcached on the main server. The documentation actually recommends that you install memcached on the same server that is serving out requests. Depending on the size of your application and what it does, a memcached instance with a gig or two may be way more than what you need.

Also, if you’re not using a php object caching engine like APC or Eaccelerator, that will also help.


Recently AWS has released a new web service – Amazon ElasticCache. This service is protocol-complaint with Memcached.

For more details refer to : http://aws.amazon.com/elasticache/


How much free memory do you normally have on your current box? Could you not just set up a memcached instance there? I’m thinking that it’s possible the latency/overhead/etc. from having remote caches is such that you’d negate any benefits, but perhaps that’s not the case.


More in general:
If you want to use any type of caching mechanism, it makes sense to have your servers VERY CLOSE to your cache servers. Example: Database servers and Memcached servers, they should be in the same colocation, or same AWS “Region”.
If you try to use a caching system, is because you want to improve performance. If you put the caching system away from your servers, you’re basically wasting all the benefits.