TeamSQL now supports Redis, a data structure store used as a database, cache, and message broker. Here’s how to connect to your Redis instance and what it’s like working with Redis in TeamSQL.
Connecting TeamSQL to Your Redis Environment
Launch TeamSQL and sign in to your TeamSQL account.
Click the plus sign next to Connections to bring up the Add Connection wizard.
Select Redis as your Database Type to get the appropriate connection configuration screen.
Set the following parameters:
Connection Name A name for your connection
Host The host you want TeamSQL to use to connect to your database
Port The port number you want TeamSQL to use
Connection Timeout The amount of time allowed to elapse before the connection times out if no response is received
Password The password needed to access your database
Default Database The default database of your server
Production Server Indicate whether the server you’re connecting to is a production instance or not
SSH Tunnel Enable to force TeamSQL to use your SSH tunnel. You’ll need to provide the following for your SSH tunnel:
- Host - Port - Username - Password - Private Key File
When done, click Test Connection to see if the parameters you provided are valid or not. If they are, you’ll see the following screen:
Click Save Connection to proceed.
Returning to the main TeamSQL window, you can now connect to your Redis database by clicking the plug icon next to the name of your database.
If TeamSQL successfully connects, the plug icon is replaced with a green indicator.
Click the caret to the left of your Redis connection name to see a list of databases available.
Finally, you can see the key-value pairs in a database by clicking on its row in the tree structure.
Working with Redis in TeamSQL
Working with Redis in TeamSQL is no different from working with Redis in any other environment. To demonstrate just a few of the features available, we have worked through a selected portion of Try Redis in TeamSQL.
Notice that the tree structure used to navigate between servers and tables also lists the keys available. By right-clicking on an individual key, you’ll see the following menu:
You can use Open to retrieve the value stored at that key. It is equivalent to running Get keyName. This becomes clear when you click open and see this:
You can update the value of the key using Set.
We can check that the value was successfully updated by using the Get command again.
We can use Set to create a new key called connections with a value of 10.
INCR lets us increase the value of connections.
We increase the value again.
If we want a key to exist only for a limited amount of time, we can set an expiration value on the key as follows. Using the TTL command, we can see how much time remains before the key expires (if you get a negative value with this command, this means that the key doesn’t exist anymore).
Redis supports several types of complex data structures, so let’s see how these work in TeamSQL.
First, let’s look at lists. Lists are series of ordered values. You can use any key as a list, as long as it doesn’t already exist as a different data type.
In the following example, we push three names to a list (stored in a key named friends) and return elements ranging from 0 to -1 (where the value of -1 means that Redis should retrieve elements to the end of the list).
Using LPOP, we remove and return the first element of the list (conversely, RPOP does the same thing but with the last element of the list).
The next data structure we’ll look at is the set. Let’s say that we’ve added the value flight to the set superpowers. Using SISMEMBER, we can check to see if flight is indeed a part of the set. If it is, the result of this command should be 1 (otherwise we’ll get 0).
If I wanted to get all members of a set, I can use SMEMBERS.
Redis includes several built-in set functions. For example, let’s create a second set called birdpowers. We can combine the superpowers and birdpowerssets and return a list with the values from both using SUNION.
We also have access to sorted sets, which are similar to regular sets, except each value has an associated score. This allows us to sort the elements in the set. In the following example, we create a sorted set called hackers, which includes the names of famous hackers and each person’s year of birth.
In the final line, we return values within the hackers set using ZRANGE, which brings back a subset of the values present.
Finally, let’s look at hashes, which map string fields to string values. We use HSET to set each individual field (note that numerical values are handled the same ways as simple strings, sans quotation marks to distinguish the two data types). When we want to return the saved data, we use HGETALL.
That’s it for our quick dive into working with Redis in TeamSQL. Again, the examples above were an abbreviated version of the Try Redis tutorial. For additional information, please refer to the Redis documentation and command reference.