Memcached Setup for Web Application Caching

Imagine your Laravel e-store serves a catalog page in 2 seconds at 100 RPS. You add Memcached — load time drops to 50 ms, the server breathes. Without caching, the database chokes on N+1 queries. We solve this turnkey with a guaranteed hit rate above 90%. Memcached is a distributed in-memory cache w

Development and maintenance of all types of websites:

Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Our competencies:

Frequently Asked Questions

Latest works

  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1287
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1244
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    983
  • image_crm_chasseurs_493_0.webp
    CRM development for Chasseurs
    1034
  • image_website-sbh_0.webp
    Website development for SBH Partners
    1108
  • image_website-_0.webp
    Website development for Red Pear
    555

Imagine your Laravel e-store serves a catalog page in 2 seconds at 100 RPS. You add Memcached — load time drops to 50 ms, the server breathes. Without caching, the database chokes on N+1 queries. We solve this turnkey with a guaranteed hit rate above 90%. Memcached is a distributed in-memory cache with a hit latency of 0.1–0.5 ms. It is ideal for read-heavy loads: we cache SQL results, serialized objects, HTML fragments. Setup includes server configuration, cache-aside pattern implementation, key invalidation, and monitoring. Our engineers are certified in Memcached with 10+ years of experience. Order a turnkey setup — get a free consultation.

Problems Memcached Solves

  • Page load speed: at 10,000 RPS every millisecond matters. Memcached reduces TTFB by 5–10 times.
  • Database load: repeated queries kill PostgreSQL/MariaDB. Cache-aside offloads it by up to 80%.
  • Traffic spikes: Memcached handles surges without adding servers.

Our Setup Process

We start with an audit: load profile, growth points, existing code. Then:

  1. Install and configure: memory, threads, network, max item size.
  2. Integrate with the framework: for Laravel — Illuminate\Cache\MemcachedStore, for Symfony — MemcachedCache.
  3. Implement cache-aside: replicate on key endpoints.
  4. Set up invalidation: tags via versioned namespace.
  5. Add monitoring: Prometheus + Grafana.

Installation and Configuration

# Example for Ubuntu apt install memcached libmemcached-dev # /etc/memcached.conf -d -m 2048 -p 11211 u memcache -l 127.0.0.1 -c 2048 -t 8 -I 10m -o modern 

PHP Integration

$mc = new Memcached(); $mc->addServer('127.0.0.1', 11211); $mc->setOptions([ Memcached::OPT_CONNECT_TIMEOUT => 50, Memcached::OPT_COMPRESSION => true, Memcached::OPT_SERIALIZER => Memcached::SERIALIZER_IGBINARY, Memcached::OPT_NO_BLOCK => true, ]); 

Cache-Aside Pattern (SQL Queries)

class ProductRepository { private Memcached $cache; private PDO $db; public function findById(int $id): ?array { $key = "product:v2:{$id}"; $product = $this->cache->get($key); if ($this->cache->getResultCode() === Memcached::RES_SUCCESS) { return $product; } $stmt = $this->db->prepare('SELECT * FROM products WHERE id = ? AND active = 1'); $stmt->execute([$id]); $product = $stmt->fetch(PDO::FETCH_ASSOC) ?: null; if ($product !== null) { $this->cache->set($key, $product, 300); // 5 minutes } return $product; } public function invalidateProduct(int $id): void { $this->cache->delete("product:v2:{$id}"); } } 

How to Choose Optimal Memory for Memcached?

Memory size depends on the volume of cached data and desired hit rate. We recommend starting with 1–2 GB per server and monitoring evictions. If evictions >0, increase memory. Large projects may require 16+ GB. We select configuration after profiling the load.

Why Monitor Evictions?

Evictions are eviction of old data when memory is full. If evictions >0, hit rate drops and caching becomes inefficient. Monitoring evictions via memcached_exporter and Grafana allows timely memory increase or TTL optimization. We set up alerts for eviction thresholds. Key metrics: get_hits/(get_hits+get_misses) — hit rate >90%, evictions = 0, curr_connections not exceeding limit.

How to Organize Cache Invalidation in Memcached?

Memcached does not support tags. Solution: versioned namespace.

class CacheTagManager { private Memcached $mc; public function getTagVersion(string $tag): int { $version = $this->mc->get("tag_version:{$tag}"); if ($this->mc->getResultCode() !== Memcached::RES_SUCCESS) { $version = time(); $this->mc->set("tag_version:{$tag}", $version, 0); } return (int)$version; } public function buildKey(string $base, array $tags): string { $versions = array_map(fn($tag) => $this->getTagVersion($tag), $tags); return $base . ':' . implode(':', $versions); } public function invalidateTag(string $tag): bool { return $this->mc->increment("tag_version:{$tag}", 1, time()) !== false; } } 

Diagnosing Low Hit Rate

Low hit rate is usually caused by too short TTL, mass invalidation, insufficient memory (evictions), or race conditions (cache stampede). We set up mutex locks to prevent stampede and auto-tune TTL.

Memcached vs Redis

Memcached is 5–10 times faster than Redis for simple caching, as confirmed by independent benchmarks. Wikipedia

Parameter Memcached Redis
Read latency 0.1–0.5 ms 1–3 ms
Persistence No Yes (AOF/RDB)
Data types Key-value Strings, lists, sets, etc.
Scaling Consistent hashing Redis Cluster
Complexity Minimal Medium

Recommended TTL for Different Data Types

Data Type Recommended TTL Reason
Reference categories 5–10 minutes Rarely change
Search results 1–2 minutes Depends on updates
User sessions 30 minutes Security
HTML fragments 5 minutes Balance between freshness and speed

What's Included

  • Audit of current architecture and load profiling.
  • Server configuration selection (RAM, threads, network).
  • Deployment and cluster setup (consistent hashing).
  • Integration with the application (PHP, Python, Node.js, Go).
  • Implementation of cache-aside pattern and invalidation.
  • Monitoring (Prometheus + Grafana) and alerting.
  • Documentation and team training.
Common Mistakes in Memcached Setup
  • Too little memory → evictions, drop in hit rate.
  • No TTL → memory overflow, leaks.
  • Invalidating entire cache on any change → loss of efficiency.
  • Ignoring cache stampede → avalanche of DB queries.
  • Using Memcached for data requiring persistence.

Timeline and Cost

Basic single-server setup: from 1 day. Cluster with integration: 2–3 days. Cost is calculated individually after the audit. Memcached can reduce server infrastructure costs by up to 50% by decreasing the number of servers. Contact us for a free project evaluation.

Our engineers are certified in Memcached with 10+ years of experience. We have implemented dozens of projects. We guarantee a hit rate >90% after setup.