Redis Cache Configuration for Web Application Performance

Your Laravel web application starts slowing down: pages load in 3+ seconds, the database collapses under 1000 RPS. A typical scenario — SQL queries with JOIN and aggregation take 200–500 ms, and the query queue grows with user count. The solution is to introduce a Redis cache layer. We have been con

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

Your Laravel web application starts slowing down: pages load in 3+ seconds, the database collapses under 1000 RPS. A typical scenario — SQL queries with JOIN and aggregation take 200–500 ms, and the query queue grows with user count. The solution is to introduce a Redis cache layer. We have been configuring Redis for web application caching for over 5 years, delivering more than 50 projects. We guarantee a hit rate above 90% and reduce response time to 50 ms. Order turnkey Redis configuration and get a free engineer consultation.

Why Redis is Faster Than SQL

Redis stores data in RAM, so reads take <1 ms vs 10-500 ms for SQL. This makes it up to 100x faster for GET operations. Additionally, Redis supports atomic operations and data structures (lists, sets), enabling complex queries without SQL.

What Problems Does Redis Solve?

  • Slow SQL queries: SELECT with COUNT, SUM, GROUP BY taking 200-500 ms. Caching reduces time to 1 ms.
  • N+1 queries: one HTTP request spawns dozens of SQL queries in a loop. Caching with keys per ID eliminates this issue.
  • High computation cost: expensive operations (rating calculations, recommendations) are cached for 5–10 minutes.

Installation and Basic Configuration

Install Redis via package manager: apt install redis-server. Configuration in /etc/redis/redis.conf:

bind 127.0.0.1 requirepass YourStrongRedisPassword maxmemory 2gb maxmemory-policy allkeys-lru save "" appendonly no databases 16 timeout 300 tcp-keepalive 300 
More about settings `maxmemory` sets the memory limit for Redis. When exceeded, the eviction policy triggers (e.g., `allkeys-lru`). Disable `save` and `appendonly` for a pure cache.

Eviction policies are described in detail in the Redis documentation. We recommend allkeys-lru for pure cache, allkeys-lfu for skewed access patterns.

Why Use a Separate Redis Database for Cache?

In Laravel, we specify database: 1 in the cache configuration. This isolates the cache from sessions and queues, preventing eviction of important data:

// config/database.php 'redis' => [ 'client' => env('REDIS_CLIENT', 'phpredis'), 'options' => [ 'cluster' => env('REDIS_CLUSTER', 'redis'), 'prefix' => env('REDIS_PREFIX', 'myapp_'), ], 'default' => [ 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD'), 'port' => env('REDIS_PORT', '6379'), 'database' => env('REDIS_DB', '0'), ], 'cache' => [ 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD'), 'port' => env('REDIS_PORT', '6379'), 'database' => env('REDIS_CACHE_DB', '1'), ], ], 

Basic Caching Patterns

Cache-Aside (Lazy Loading) — the application manages the cache: first reads from Redis, on miss reads from database, then stores in Redis:

class ProductRepository { private const CACHE_TTL = 3600; public function findById(int $id): ?Product { $cacheKey = "product:{$id}"; $cached = $this->redis->get($cacheKey); if ($cached !== false) { return unserialize($cached); } $product = $this->db->find(Product::class, $id); if ($product) { $this->redis->setex($cacheKey, self::CACHE_TTL, serialize($product)); } return $product; } public function save(Product $product): void { $this->db->persist($product); $this->db->flush(); $this->redis->del("product:{$product->getId()}"); } } 

Write-Through — on write to database, simultaneously update cache. Pro: cache always fresh. Con: writes slower, caches data that may not be read.

How to Choose an Eviction Policy?

Choice depends on scenario:

Policy Description When to Use Expected Hit Rate
allkeys-lru Removes least recently used keys Pure cache, uniform access >95%
allkeys-lfu Removes least frequently used keys Zipf distribution (20% keys get 80% requests) >90%
volatile-lru Removes keys with TTL When there are persistent keys without TTL >85%
noeviction Returns error on memory full Queues, sessions

Recommended TTL for Different Data Types

Data Type TTL Example
Product list 1 hour products:featured
Product card 2 hours product:{id}
Dashboard stats 5 minutes stats:dashboard
User sessions 24 hours session:{token}

Caching in Laravel

Laravel supports Redis as a cache driver out of the box. In config/cache.php, just set 'default' => env('CACHE_DRIVER', 'redis'). Then you can use:

// Cache with automatic computation on miss $products = Cache::remember('products:featured', 3600, function () { return Product::where('is_featured', true)->with('category')->get(); }); // Tags for group invalidation $product = Cache::tags(['products', 'category:5'])->remember( "product:{$id}", 3600, fn() => Product::find($id) ); Cache::tags(['products'])->flush(); 

Tags only work with Redis and Memcached. For heavy aggregations — dashboard stats with TTL of 5 minutes.

Cache Monitoring

For production, we use Redis Exporter + Grafana (dashboard ID 11835). Key metrics:

redis-cli -a password INFO stats | grep -E "keyspace_hits|keyspace_misses|used_memory_human" # Hit rate = hits / (hits + misses) > 90% # Size of each key group redis-cli -a password --bigkeys 

Running the exporter:

docker run -d --name redis_exporter -p 9121:9121 oliver006/redis_exporter --redis.addr=redis://localhost:6379 --redis.password=YourPassword 

Monitoring is described in the Laravel documentation. We recommend setting up alerts when hit rate drops below 85%.

Our Work Process

  1. Audit current code: identify bottlenecks and N+1 queries.
  2. Design cache layer: decide what to cache, TTL, keys.
  3. Implementation: configure Redis, integrate caching into code, set up invalidation.
  4. Testing: load testing, measure hit rate.
  5. Deploy and monitor: set up exporter, dashboard, alerts.

What's Included

  • Redis configuration tailored to your load.
  • Integration with Laravel cache driver.
  • Caching implementation for 3–5 key queries (as agreed).
  • Monitoring setup (Grafana + Redis Exporter).
  • Documentation of used keys and TTL.
  • Team training (1 hour).
  • Support for 1 month after deployment.

Timeline and Pricing

Basic Redis setup with Laravel integration — from 2 to 5 business days. Pricing is calculated individually, depending on project complexity and number of cached items. Contact us for an estimate.

Typical Caching Mistakes

  • Caching everything: cache is not needed for seldom-read or frequently changing data.
  • Too long TTL: data becomes stale, users see outdated information.
  • Forgot invalidation: cache not cleared on data update, causing errors.
  • Wrong eviction policy: frequently used keys may be evicted when Redis fills up.
  • No monitoring: cannot evaluate cache effectiveness without metrics.

By avoiding these mistakes and using the described patterns, you will get a stable and fast web application. Order Redis configuration — our engineers will help implement a cache layer with guaranteed results.