Magento Redis Magento Redis

Redis (Remote Dictionary Server) is an open-source, in-memory, key-value data store. The project started with Salvatore Sanfilippo, the original developer of Redis. It is now used as a database, cache, message broker, and queue.

Redis delivers sub-millisecond response times, enabling millions of requests per second. It is used for real-time applications in gaming, financial services, healthcare, and IoT.

Redis is popular for caching, session management, real-time analytics, geospatial, media streaming, and pub/sub-applications. In Magento 2, Redis reduces memory usage with tag-based cache cleanup without any FOREACH loops.

The Magento Redis backend works with indexing tags in files. The tag operations do not require a full scan of every cache file. It is useful in multi-server hosting and allows a fast backend cache that does not require a low-level file system cache.

In-memory Data Structures In-memory Data Structures

In-memory Data Structures

Redis is a data structure server providing native data types that assist in caching, queuing, or event processing. It supports strings, hashes, lists, sets, sorted sets, and streams.

Scalable Scalable

Scalable

Horizontal scalability with hash-based sharding, scaling to millions of nodes. Automatic re-partitioning when growing the cluster.

Supports Master/slave Replication Supports Master/slave Replication

Supports Master/slave Replication

Redis supports on-disk save and master/slave replication, enabling high availability and removing a single failure point.

How it works

Redis offers database caching to ensure that the Magento store runs faster. The architecture shows a distributed cache that spans across multi-server environments. It is mainly used for Magento data residing in the database and web session data.

How it works How it works

Benefits

High Availability High Availability

High Availability

Redis offers a primary-replica architecture in a single node primary or a clustered topology. It lets us build highly available solutions with consistent performance and reliability.

Programmability Programmability

Programmability

Redis allows us to execute custom scripts on the server. It offers server-side scripting with Lua and server-side stored procedures with Redis Functions.

Extensibility Extensibility

Extensibility

Redis modules are dynamic libraries and APIs for building custom extensions to Redis in C, C++, and Rust.

Persistence Persistence

Persistence

Keep the dataset in memory for fast access. Redis provides a range of persistence options like RDB Redis Database and AOF -Append Only File.

Clustering Clustering

Clustering

Redis scales horizontally with a deployment topology called Redis Cluster. The data is automatically sharded across multiple Redis nodes.

Asynchronous Replication Asynchronous Replication

Asynchronous Replication

Replication with automatic failover for standalone and clustered deployments. It uses default asynchronous replication that offers low latency.

Flexible Data Structures Flexible Data Structures

Flexible Data Structures

Other key-value data stores offer limited data structures. Redis has many data structures to meet the application’s needs.

Ease-of-use Ease-of-use

Ease-of-use

Redis replaces the traditional complex code with fewer, simpler lines. Write fewer lines of code to store, access, and use data in your applications.

Open Source Open Source

Open Source

Redis is an open-source project with a large community, including AWS. There are no vendor lock-ins, as Redis supports open data formats and features a rich set of clients.

FAQs

The following are the advantages of using Redis in Magento 2 -

  • Redis decreases memory usage with the backend support of tag-based cache cleanup.
  • The metadata and the cache records are stored in the same file rather than separately. It results in fewer file stats and read, write, and unlink operations.
  • The backend works by indexing tags in files. The tag operations don’t have to scan each cache file fully.
  • Redis supports on-disk save and master/slave replication.

Run the setup:config:set command. Specify the parameters of Redis default caching.

bin/magento setup:config: set —cache-backend=redis —cache-backend-redis-server=127.0.0.1 —cache-backend- redis-db=0

The --cache-backend=redis command enables the Redis default caching. Skip this parameter if it is already enabled.

--cache-backend-redis-[parameter_name]=[parameter_value] is the key/value pairs. These key/value pairs configure the default caching.

The list includes -

  • cache-backend-redis-server - The default value of server 127.0.0.1 specifies that Redis is installed on the Magento server.
  • cache-backend-redis-db -It is required if you use Redis for both full-page and default caching. Default value - 0 (database 0. port 6379)
  • cache-backend-redis-password - Setting up a Redis password enables its security feature: the auth command. Configure the password in the Redis caching configuration file /etc/redis/redis.conf.

Configure Redis Page Caching

Run the setup:config:set command with other parameters.

bin/magento setup:config:set —page-cache= redis —page -cache-redis-[parameter_name]=[parameter_value]…

The --page-cache=redis command will enable Redis full page caching.

--page -cache-redis-[parameter_name]=[parameter_value] is the parameter/value pairs that set up page caching. The list includes:

  • Cache-backend-redis-server
  • Cache-backend-redis-port
  • Cache-backend -redis-db
  • Cache-backend-redis-password

Example Command

The example command is used to enable Redis page caching. It sets the host to 127.0.0.1 and assigns the database number to 1.

bin/magento setup:config:set --page-cache=redis
--page-cache-redis-server=127.0.0.1 --page-cache-redis-db =1

Fetching content from the cache by default helps to load the web page faster. Redis is a key-value store available for Magento for back-end and session storage.

Redis caches database values, resulting in less database resource usage. It provides a tunable persistent cache.

If a user requests a web page for the first time, there is a database query on the server. Redis caches the query for future requests.

When a user requests the same page, the server offers results from Redis without querying the actual database. It ensures the Magento web page loads faster.