And vertical scaling isn’t an option

When in the course of human events you encounter a scenario that drives redis CPU into the red zone, what is your move? I recently encountered such a scenario.

Redis is a popular open source in memory database. It is available as a managed service on AWS (Elasticache) and Google Cloud (Memorystore).

Here’s a chart of Google Memorystore CPU utilization for the past 2 weeks.

On March 3, CPU crossed the 80% threshold. You may be wondering what kind of workload can drive redis CPU to this level.

Here’s a timechart by redis operation over the same period.

The workload is almost entirely scans peaking at over 25,000 scans/second!

Now you may feel a sense of moral indignation at this scan heavy workload. Scan is used to incrementally iterate over all the keys in the database. Each iteration is an O(n) operation.

Here’s the block of code driving this load.

    if Similarity.is_similar(sim, self.__lp_sim_threshold) and (best_match is None or best_match[1] < sim):
best_match = LicensePlate(lp_candidate), sim

The block iterates over a set of keys, extracts a license plate, and compares this candidate to a search key using a fuzzy similarity function. If the candidate is deemed more similar than any candidate seen so far, it becomes the new best_match.

Here are some example keys to make this more clear.

The application here is a car wash analytics platform. As the car arrives into view, we’re interested in knowing if we have seen it previously (i.e. at another video camera at the same location).

It may be possible to solve this problem in a more efficient manner. Perhaps we can maintain sets of keys by location id and limit iteration to the relevant set. Perhaps a different technology that supports indexing would be more appropriate.

But what can be done quickly? How can we buy time? Once we’ve removed this bottleneck to scale, we can come back and refactor the solution.

First, it’s not an option to turn a dial in Google Memorystore and give redis more CPU. Redis runs on a single core. While it is possible to give a Memorystore instance more memory, vertical scaling of Memorystore CPU is not possible.

Second, as described in the FAQ, Memorystore does not support redis cluster. There is no out of the box scale out option.

Proxy Service: twemproxy

One option is twemproxy. Twemproxy is an open source proxy component that supports the redis protocol. It also supports sharding across redis instances using a hash key.

I put together an example of how we can shard a redis workload across multiple nodes using twemproxy. In the example, we start 4 redis containers, a twemproxy container, and a client container. The client will set values and these will be sharded across redis instances.

A nice thing about this approach is that no change to the client code is required in order to scale out: the client just points at the proxy.

A problem with this approach is that it does not support the scan operation. That’s a dealbreaker for our use case.

Client-side sharding: redis-shard

Another option is client side sharding. You could roll your own, or you could try and use a library available in your application language. I tried the latter and explored the Python redis-shard package.

I put together an example of how we can apply redis-shard. The example starts 4 redis containers, and a client container. The client sets keys and values and these are sharded across instances.

Like twemproxy, redis-shard supports a hash_tag feature, which enables us to send keys with a common substring to the same node.

The substring in curly braces is used as the hash key.

However, redis-shard does not support the scan operation.

Again, dealbreaker.

Redis Enterprise

The next option is heavy artillery. Some of the folks behind the redis project have created a Redis Enterprise product. This is redis cluster available as a managed service. It is available for purchase on the Google Cloud.

This may be our Obi Wan Kenobi. I’ll let you know after I experiment.

One man‘s pursuit.