651.288.7000 info@intertech.com

Getting Started with Redis: Sessions, Caching, Counting, Pub/Sub, & More

by | Jul 23, 2019

Redis is a popular option for fast storage and retrieval of data. It’s a type of database that relies on key-value relationships to look up values in constant time. Additionally, Redis databases exist in-memory, instead of on disk. As a result, lookups are blazing fast from RAM. However, storage can be volatile and you need to save changes right after making them in order to guarantee persistence.

If you’re new to Redis or wondering how to get started, then this post will walk you through the basics. All things considered, we’ll see why Redis is so popular for caching and performance applications.

 

Redis Features & Limitations

Emphatically, Redis is not a replacement for a traditional database, nor does it try to be. Firstly, Redis data is stored in-memory, so you can only operate on as much data as you have available RAM. Secondly, values in Redis can’t be related and queried in the same way as relational (e.g. MySQL) or document model (e.g. MongoDB) databases.

Nevertheless, these tradeoffs enable massive performance benefits to Redis. Most importantly, it’s incredibly fast, reaching up to 100,000 reads/writes per second. Additionally, it’s tiny. The entire Redis implementation is 16,000 lines of C code. Furthermore, Redis implements useful data structures:

 

Key-value pairs with O(1) lookup

Redis keys are strings that represent a given value. That value can be accessed, incremented, appended, or deleted as an atomic operation.

Code sample:

 

Optimized lists with left and right push/pop to work as both a stack and queue

Even with millions of data points in a Redis list, you can insert near the head or tail of the list in constant time. Lookups in the middle of the list are linear – O(n).

Code sample:

 

Sets that allow add, remove, and test membership of groups in O(1) time

Sets are unordered collections. You can make changes and queries on sets very fast. It’s also quick to compare or combine multiple sets.

Code sample:

 

Sorted sets that rank members of a set by your defined score metric

One of the more complex Redis data types, sorted sets allow you to rank your data while ensuring that each data point is unique.

Code sample:

 

Hashes that group data together

We can use hashes to relate certain data together. They turn individual keys into objects with multiple fields.

Code sample:

 

When to Use Redis

As you can imagine, you can get a lot done with just these simple, efficient data structures. The beauty of Redis is its simplicity.

Because Redis is so lightweight, you can start using it immediately in addition to whatever data store you’re already using. It doesn’t need to replace parts of your architecture. Instead it can just supplement where you need better performance.

Here are the top use cases for Redis.

 

Session & Page Data Caching

Most web frameworks have libraries that allow for easy integration of Redis in order to provide caching. It makes a lot of sense to use Redis for your cache because lookups and writes are super fast. Additionally, Redis has some advantages over popular caching options like Memcached in terms of functionality and persistence.

 

Message Queues

Passing and pausing data around with Redis is easy because it allows constant pushing and popping from both the head and tail of a list. You can implement stacks and queues in Redis the same as you would in any programming language. However, now your queue can exist as an independent, in-memory data store.

 

Counting, Recent Updates, & Leaderboards

If you want to increment a counter every time a unique event happens, then Redis keys solve that efficiently. Also, Redis can keep a list of the most recent updates/comments/posts/etc that you can display as a simple request for the last elements of a list instead of needing to write a database query like, “SELECT * FROM table WHERE … ORDER BY time DESC LIMIT 10”. Similarly leaderboards/top scores/price trackers are great candidates for Redis lists or sorted sets.

 

Publish-Subscribe

Redis’s Pub/Sub support is phenomenal. Event triggers, chats, notifications, dashboard updates, and more are all possible using Redis Pub/Sub or other tools built atop Redis. Natively, Redis implements PUBLISH, SUBSCRIBE, and UNSUBSCRIBE commands and then creates push messages to all subscribers when a new event is published.

 

The Power of Redis

Obviously, this post only scratches the surface of what you can do with Redis. However, the important thing to realize is that Redis can work alongside other technologies you’re already using. Try it out for small applications where it makes sense, and you can expand from there!

 

About Intertech

Founded in 1991, Intertech delivers software development consulting and IT training to Fortune 500, Government and Leading Technology institutions. Learn more about us. Whether you are a developer interested in working for a company that invests in its employees or a company looking to partner with a team of technology leaders who provide solutions, mentor staff and add true business value, we’d like to meet you. 

 

Follow Us

Categories

Intertech Consulting
Course Finder
Design and Code Review Checklist

Like What You've Read?

Subscribe to the Blog.

Every Friday we send that week's content from our Developers via email. Try it out!

Some ad blockers can block the form below.

You have Successfully Subscribed!

Pin It on Pinterest

Share This