Welcome Valkey to the Percona family! We are excited to have you join our team of MySQL, PostgreSQL, and MongoDB database experts.

Hang on. What is Valkey? In short, Valkey is a fork of Redis which maintains the original open source BSD license[1]. If you’re new to Redis/Valkey, then this ‘getting started’ post is just for you. We will cover how to install Valkey for the first time using docker, basic CLI usage, and the most common data types available in Valkey. In later posts, we dive into some of the more complex data structures, backups, recovery, replication, and high availability.

What is Valkey?

Paraphrased from https://valkey.io, Valkey is a high-performance key-value datastore. It typically sits alongside or in between the Application-DB stack for caching and message queues or can even act as a NoSQL-style database. Valkey can run as a single instance or in a cluster, with options for replication and high availability.

Applications typically use Valkey as a query cache for their database. Now that MySQL 8 has removed its native query results cache, Valkey is a great option to bring back that efficiency.

Valkey can cache and store just about anything, up to 512MB per key-value pair. Does your app generate temporary PDFs or images? Store them in Valkey with a native TTL expiration. Users can download this cached version until it is auto-deleted, thereby saving heavy processing time. How about finding common friends between users in a social app? Use Valkey Sets to calculate unions and intersections. Most Valkey interactions are O(1), constant time access, so it’s blazing fast! The options for Valkey are seemingly endless.

Install Valkey

Valkey 7.2.5 was released as the first GA version. It is currently available as an official docker container, on Fedora/EPEL yum, and as binary tarballs for Ubuntu Bionic, and Ubuntu Focal.

There are other options for authorization, persistent storage, providing your own config file, etc. We will keep it basic for this post. To connect to valkey, we can exec into the server container and run the CLI:

Valkey data types

Now that we can access Valkey/Redis, what can we do? At the heart of Valkey/Redis is a simple Key-Value storage engine. You provide a key, which can be an arbitrary byte string up to 512MB in size (yes, that’s right, a 512MB string. Please don’t do that if you care about performance and latency). That key maps to a value, which is also a byte string up to 512MB.

You will see references to “string” a lot when dealing with Valkey/Redis. Internally, Valkey encodes most data types into a byte string. Consider the following storage, fetch, and increment of a key ‘mynumber’:

Notice how the “type” of the key is string, but the internal encoding is integer.

There are six basic data types in Valkey:

  • String
  • List
  • Set
  • Hash
  • Sorted Set
  • Bitfield

As you can see from that list, there is no integer, big integer, date, datetime, or boolean. As shown above, when you store an integer-type, it will be encoded as such.

Let’s cover two of these six to keep this post short: String and List

String

This is pretty straightforward. You can store any string up to 512MB in size.

The command is “SET <key> <value>”. You can fetch values using “GET <key>”. Here are a few examples:

The first and fourth examples are basic strings. In the second example, I encoded a JPG image of my cat to a base64 string. (e.g., cat mycat.jpg | base64)

The third example uses a common key naming convention using “:” (colon) to separate spaces.

NOTE: Valkey/Redis have no concept of namespaces, domains, nested key structure, etc. Valkey has one single flat namespace; that’s it. The use of “:” as a separator is purely cosmetic, though you may adopt something in your own code to implement a structure of some kind.

List

In Valkey/Redis, a list is a container for holding an insert-ordered list of string values. Think of this like an array or a linked list. There is a head and a tail. You can push elements to the front, and back of the list; you can pop elements off the front, and back of the list. A common use case of lists in Valkey/Redis is to create a queue.

Here’s a visualization of the list as it grows and shrinks using the various commands:

As you can see above, in the RPUSH example, we can push several items at once, and the LRANGE <list> <start> <stop> command can be used to view the entire contents of the list. (Negative values count backward from the end).

Wrap up

Valkey is the current open source GA replacement for Redis, which maintains the original spirit of truly open source software. It is easy to install and get up and running. While maintaining a simplistic structure, it is capable of handling complex data storage types.

In our next posts, we will cover Sets, Sorted Sets, Hashes, and Bitfields.

Footnote

 

[1] In case you missed the announcement earlier this year, Redis has decided to forgo traditional open source licenses, like GPL, MIT, and BSD, in favor of the more business-centric Server-Side Public License. This is Redis’s attempt to curtail third party cloud providers from making products and profits “for free” by using Redis’s software. (For reference, this is also what MongoDB did back in 2018 and Hashicorp did in 2023.)

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments