Quantify Cyber Risk Now

header ads

Making Process Faster by Caching Responses with Redis | Lucideus Research

What is Redis Caching?
Normally, when you make a web server with a database, each request to the server entails one or more requests to the database, and some processing of the results before sending back a response.

For example, consider a database which has a list of people names, along with their age. Our server handles requests which contain a person name, and returns their age after querying the database.

This seems pretty straightforward, but can sometimes be inefficient. If there are a lot of people who want to know Doug’s age, and we get many requests to our server asking for it, we should find a better way than to query the database each time we receive a request (Since so many database queries can be expensive).

Enter : Redis Cache Layer
Due to its high performance, developers have turned to Redis when the volume of read and write operations exceed the capabilities of traditional databases. With Redis’s capability to easily persist the data to disk, it is a superior alternative to the traditional memcached solution for caching.
The Redis cache layer is a temporary datastore, which is much faster than the database. The server, after receiving a request, first checks if the cache has the response available. If so, it sends it to the client. If not, it queries the database as usual, and stores the response in the cache before sending it back to the client. This way, every response is either cached, or retrieved from the cache, and as a result, the load to our server and database is reduced.

Key Features: High-Level Data Structures: Provides five possible data types for values: strings, lists, sets, hashes, and sorted sets. Operations that are unique to those data types are provided and come with well documented time-complexity (Big O notation). High Performance : Due to its in-memory nature, the project maintainer’s commitment to keeping complexity at a minimum, and an event-based programming model, Redis boasts exceptional performance for read and write operations. Lightweight With No Dependencies : Written in ANSI C, and has no external dependencies. Works well in all POSIX environments. Windows is not officially supported, but an experimental build is provided by Microsoft. High Availability : Built-in support for asynchronous, non-blocking, master/slave replication to ensure high availability of data. There is currently a high-availability solution called Redis Sentinel that is currently usable, but is still considered a work in progress.

Building Our Server

This example uses PHP on CI , along with redis for our cache, so make sure you have those installed first.You have to install phpredis (predis) to your server.Using Predis To Connect Custom PHP With Redis.For installation and configuration refer to documentation:

You can check Redis in phpinfo()
Redis Cluster
Redis Cluster provides a way to run a Redis installation where data is automatically sharded across multiple Redis nodes.

Redis Cluster also provides some degree of availability during partitions, that is in practical terms the ability to continue the operations when some nodes fail or are not able to communicate. However the cluster stops to operate in the event of larger failures (for example when the majority of masters are unavailable).

So in practical terms, what you get with Redis Cluster?

The ability to automatically split your dataset among multiple nodes.
The ability to continue operations when a subset of the nodes are experiencing failures or are unable to communicate with the rest of the cluster.

How Fast is Redis?
Redis includes the redis-benchmark utility that simulates running commands done by N clients at the same time sending M total queries (it is similar to the Apache's ab utility). Below you'll find the full output of a benchmark executed against a Linux box.

You don't need to run all the default tests every time you execute redis-benchmark. The simplest thing to select only a subset of tests is to use the -t option like in the following example:

$ redis-benchmark -t set,lpush -n 100000 -q

Where -q :Quiet. Just show query/sec values
-n:Total number of requests (default 100000)

Basic Commands:
Start up your redis server using the command : redis-server

Stop your redis server using the command : /etc/init.d/redis-server stop

Conclusion :
There are an enormous amount of people doing diverse things with Redis. In casual conversations, I asked people how they were using Redis. I got quite a few people saying “I don’t use it enough.” So, I expect the community and usage to continue to grow. There is skepticism about Redis as a primary datastore, but I think that there were enough examples that this too will shift to it becoming more commonplace in many applications.

Post a comment