DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Microservices
  4. Redis Running Slowly? Here’s What You Can Do About it

Redis Running Slowly? Here’s What You Can Do About it

Itamar Haber user avatar by
Itamar Haber
·
Oct. 17, 14 · Interview
Like (0)
Save
Tweet
Share
19.17K Views

Join the DZone community and get the full member experience.

Join For Free

Redis is blazing fast and can easily handle hundreds of thousands to millions of operations per second (of course, YMMV depending on your setup), but there are cases in which you may feel that it is underperforming. This slowness of operations - or latency - can be caused by a variety of things, but once you've ruled out the usual suspects (i.e. the server's hardware/virtualware, storage and network) you should also examine your Redis settings to see what can be optimized.

A good place to start is by verifying that the CPU load of your Redis server is indeed working at full throttle and not blocked somehow. If your Redis process is at a stable 100%, then your issue may be attributed to one or both of two things: your volume of queries and/or slow queries. The optimization of slow running queries (and in most cases the underlying data structures) is an art in itself, but there are still quite a few things that you can try before tryin other measures like upgrading your hardware or clustering Redis.

Review your Redis' SLOWLOG to ensure that there aren't any particularly slow queries in it - these should be easily identifiable by their high execution times (the third integer in each log entry). Higher execution times mean higher latency, but remember that some of Redis' commands (such as Sorted Set operations) can take longer to complete, depending on their arguments and the size of data that they process. By using Redis Cloud's enhanced version of the SLOWLOG or doing the math in your head, you can further drill down to identify the troublemakers.

Next, take a look at the output of the INFO ALL command. Use your experience, keen judgment and cold logic to answer this question - does anything look weird? :) When not tracking persistent storage, networking or replication bottlenecks, you should focus on the stats and commandstats sections.

When the value of your total_connections_received in the stats section is absurdly high, it usually means that your application is opening and closing a connection for every request it makes. Opening a connection is an expensive operation that adds to both client and server latency. To rectify this, consult your Redis client's documentation and configure it to use persistent connections.

Use the information in the commandstats section to hone in on the commands that occupy most of your server's resources. Look for easy kills by replacing commands with their variadic counterparts. Some of Redis' commands (e.g. GET, SET, HGET & HSET) sport a variadic version (i.e. MGET, MSET, HMGET & HMSET, respectively), whereas others have infinite arity built right into them (DEL, HDEL, LPUSH, SADD, ZADD, etc). By replacing multiple calls to the same command with a single call, you'll be shaving off precious microseconds, while saving on both server and client resources.

If you're managing big data structures in your Redis database and you're fetching all their content (using HGETALL, SMEMBERS or ZRANGE, for example), consider using the respective SCAN command instead. SCAN iterates through the Redis keys' namespace and should always be used instead of the "evil" KEYS command. As an added bonus, SCAN's variants for Hashes, Sets and Sorted Sets (HSCAN, SSCAN and ZSCAN) can also help you free up Redis just enough to reduce overall latencies.

Another way to reduce the latency of Redis queries is by using pipelining. When you pipeline a group of operations, they are sent in a single batch that, while bigger and slower to process, requires only a single request-response round trip. This consolidation of trips makes for substantial overall latency reductions. Note, however, that a pipeline operation could block your application if it is waiting for replies without sending the pipeline - Redis will provide all of the replies to the pipelined stream of commands only after the pipeline has been sent. Also bear in mind that when using pipelining, Redis caches all the responses in memory before returning them to the client in bulk, so pipelining thousands of queries (especially those that return a large amount of data) can be taxing to both the server and client. If that is the case, use smaller pipeline sizes.

When pipelining isn't an option, you can still save big time on round-trip time (RTT) latency by taking the "moonlit" scripting road. With Lua scripting in Redis, you can use logic that interacts with the data locally, thus saving many potential client-server round trips. To further reduce bandwidth consumption, latency, and to avoid script recompilations, make sure to use SCRIPT LOAD and EVALSHA.

Lua Tips: Refrain from generating dynamic scripts, which can cause your Lua cache to grow and get out of control. If you have to use dynamic scripting, then just use plain EVAL, as there's no point in loading them first. Also, remember to track your Lua memory consumption and flush the cache periodically with a SCRIPT FLUSH. Also, do not hardcode and/or programmatically generate key names in your Lua scripts, because doing so will render them useless in a clustered Redis setup.

These are just some of the ways that you can easily get more out of your Redis database without bringing in the heavy artillery. Adopting even one of these suggestions can make a world of difference and lower your latency substantially. Know more tricks of the trade? Have any questions or feedback? Feel free to shout at me, I'm highly available :)


Redis (company)

Published at DZone with permission of Itamar Haber, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Distributed Stateful Edge Platforms
  • Architecture and Code Design, Pt. 2: Polyglot Persistence Insights To Use Today and in the Upcoming Years
  • How To Create a Stub in 5 Minutes
  • New MacBook Air Beats M1 Max for Java Development

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: