Showing posts with label Redis. Show all posts
Showing posts with label Redis. Show all posts

Wednesday, 28 January 2026

How to test Redis connectivity




We first need to check if Redis DNS name resolves:

% nslookup redis.example.com

Server: 192.168.1.1
Address: 192.168.1.1#53

Non-authoritative answer:
redis.example.com canonical name = example-prod-redis-serverless-kg5o59.serverless.use2.cache.amazonaws.com.
example-prod-redis-serverless-kg5o59.serverless.use2.cache.amazonaws.com canonical name = default.example-prod-redis-serverless-kg5o59.serverless.use2.cache.amazonaws.com.
Name: default.example-prod-redis-serverless-kg5o59.serverless.use2.cache.amazonaws.com
Address: 10.0.3.74
...


Let's try to make a TCP connection:

% nc -vz redis.example.com 6379

nc: connectx to redis.example.com port 6379 (tcp) failed: Operation timed out

After adding remote client's IP address to inbound rules of Redis security group (firewall):

% nc -vz redis.example.com 6379

Connection to redis.example.com port 6379 [tcp/*] succeeded!

 
Let's now install Redis client so we can try to connect to the server:

% brew install redis
==> Fetching downloads for: redis
✔︎ Bottle Manifest redis (8.4.0)                                                                                                                                                            Downloaded   10.9KB/ 10.9KB
✔︎ Bottle Manifest ca-certificates (2025-12-02)                                                                                                                                             Downloaded    2.0KB/  2.0KB
✔︎ Bottle ca-certificates (2025-12-02)                                                                                                                                                      Downloaded  131.8KB/131.8KB
✔︎ Bottle redis (8.4.0)                                                                                                                                                                     Downloaded    1.2MB/  1.2MB
==> Installing redis dependency: ca-certificates
==> Pouring ca-certificates--2025-12-02.all.bottle.tar.gz
==> Regenerating CA certificate bundle from keychain, this may take a while...
🍺  /opt/homebrew/Cellar/ca-certificates/2025-12-02: 4 files, 236.4KB
==> Pouring redis--8.4.0.arm64_sequoia.bottle.tar.gz
==> Caveats
To start redis now and restart at login:
  brew services start redis
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/redis/bin/redis-server /opt/homebrew/etc/redis.conf
==> Summary
🍺  /opt/homebrew/Cellar/redis/8.4.0: 15 files, 3MB
==> Running `brew cleanup redis`...
...
==> Caveats
==> redis
To start redis now and restart at login:
  brew services start redis
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/redis/bin/redis-server /opt/homebrew/etc/redis.conf


Let's connect to Redis server and execute ping command (expected response is PONG) and also get some information about the server:


% redis-cli \       
  --tls \
  -h redis.example.com \
  -p 6379

redis.example.com:6379> ping
PONG
redis.example.com:6379> info server 
# Server
redis_version:7.1
redis_mode:cluster
os:Amazon ElastiCache
arch_bits:64
run_id:0
redis.example.com:6379> 


---

Thursday, 1 August 2024

Introduction to Redis

What is Redis?


Redis (Remote Dictionary Server):
  • open-source, in-memory data structure store
  • commonly used as:
    • database
    • cache
    • message broker
  • supports various data structures such as:
    • strings
    • hashes
    • lists
    • sets
    • sorted sets with range queries
    • bitmaps
    • hyperloglogs
    • geospatial indexes
  • chosen for its high performance, flexibility, simplicity, and support for a variety of use cases
  • popular choice for developers and system architects

Key use cases for Redis


Caching:

  • Performance Improvement: To improve application performance, Redis is often used to cache:
    • results of database queries
    • API calls
    • complex computations
  • Session Storage: Storing user session data to ensure quick access and reduce the load on traditional databases.

Real-time Analytics:

  • Counters and Stats: Redis' in-memory nature allows for fast operations, making it suitable for maintaining real-time counters and statistical information.

Message Queue:

  • Pub/Sub Messaging: Redis can be used for building real-time messaging applications using its publish/subscribe capabilities.
  • Task Queues: Used in conjunction with other tools like Celery to manage background job processing.

Data Storage:

  • Key-Value Store: Serving as a NoSQL database where data is stored as key-value pairs.
  • Persistent Storage: Although primarily an in-memory store, Redis supports persistence, allowing data to be saved to disk.

Leaderboards and Counting:

  • Sorted Sets: Implementing leaderboards or other applications where items need to be sorted by scores or other criteria.

Geospatial Data:

  • Geospatial Indexes: Redis can handle geospatial data with radius queries, making it suitable for location-based services.

Streams:

  • Log Management: Redis Streams can be used for collecting and processing log data in a highly scalable manner.

Machine Learning and AI:

  • Feature Store: Storing features for machine learning models to quickly access and update them during training or inference.


image source: Architecture Notes — System Design & Software Development | Mahdi Yusuf | Substack
*) HA = Highly Available


How can WordPress website benefit from using Redis?


A WordPress website can benefit significantly from using Redis in several ways, primarily through improved performance and scalability. Here are some key benefits:

Caching:

  • Object Caching: Redis can cache database query results, reducing the number of database queries and speeding up page load times. This is particularly useful for dynamic websites with frequent database interactions.
  • Full-Page Caching: Redis can store full HTML pages, which can be served directly to users, bypassing the need for PHP and database processing for each request. This dramatically reduces the time to first byte (TTFB).

Session Storage:

  • User Session Management: Storing user sessions in Redis instead of the default file system or database can lead to faster session retrieval and better performance, especially for high-traffic sites.

Transient API:

  • Improved Transient Storage: WordPress uses the Transients API to store temporary data. Redis can serve as a fast storage backend for transients, leading to quicker data retrieval and better site responsiveness.

Scalability:

  • Handling High Traffic: By offloading database queries and storing frequently accessed data in memory, Redis helps WordPress sites handle higher traffic loads without significant performance degradation.

Reduced Database Load:

  • Database Performance: By caching query results and other frequently accessed data, Redis reduces the load on the MySQL database, allowing it to handle more concurrent requests and improving overall site performance.

Search Optimization:

  • Speeding Up Search Queries: Redis can cache search query results, providing faster search experiences for users.

Improved Reliability:

  • Failover and Replication: Redis supports replication and can be configured for high availability, ensuring that cached data remains accessible even if some servers go down.

---

To integrate Redis with a WordPress website, you can use plugins such as:

  • Redis Object Cache: This plugin enables WordPress to use Redis as an object cache backend.
  • W3 Total Cache: A comprehensive caching plugin that supports Redis for object caching, page caching, and more.
  • WP Rocket: Another popular caching plugin that integrates with Redis for enhanced caching capabilities.

Implementation Steps:

  • Install Redis: Set up a Redis server on your hosting environment. This may be done via your hosting provider’s dashboard, package manager, or Docker.
  • Configure WordPress: Install and configure a Redis caching plugin on your WordPress site.
  • Adjust wp-config.php: Add necessary configurations to your wp-config.php file to enable Redis as the caching backend.

wp-config.php:

define('WP_CACHE', true);
define('WP_REDIS_HOST', '127.0.0.1'); // or your Redis server IP

By leveraging Redis, WordPress websites can achieve faster load times, improved user experience, and better handling of high traffic volumes, making it a valuable addition to the site's optimization toolkit.