Introduction - What Is Redis?
Redis is an in-memory data store, implemented as a NoSQL database. Basically, normal databases store all of the data on-disk (meaning it gets written to a Hard Drive; with Redis, data is stored in-memory. meaning it stays in RAM so everything can be accessed quickly. Redis also takes care of a few tricks that might cause errors in other database types.
Why would you use Redis? Because it's fast. If your website or API has data that needs to be super accurate in real time, and you have a lot of users accessing that data at the same time, then Redis is good. (This is especially true if you don't have too many items in the database.) Let's look at using Redis. How To Use Redis
So how do we actually use Redis? Redis is just a database, so we can access it either through a terminal (command line), or from a programming language, just like we would with any other database. (Of course, actual entry into the database will vary a bit depending on which cloud service you're using as your database server.)
Redis Commands (Language Syntax)
To communicate with a Redis database, a programming language is required. (Sort of.) There are certain commands that are able to be performed on Redis.
First, go through this tutorial to get a general feeling for how the commands work. Then, keep this command list as a reference whenever you need to use Redis. Using Redis Locally (On Your Computer)
To set up a Redis server locally on your computer, for testing purposes, simply install redis-server on the command line.
In your terminal (on Ubuntu), use the following command to install redis-server.
sudo apt-get install redis-server
Whenever you want to simulate a redis server on your local computer,
redis-server
This will turn the terminal into a redis server. (By default, the port is 6379. Other details can be read in the terminal.)
Using Redis From The Command Line
Speaking to our redis database from the command line can be done by using the redis-cli. In your normal terminal (when using the local server setup), prefix your redis commands with "redis-cli" to speak with the redis database.
For example, the following two commands will create a variable named "srcCounter" and increment it by 1 (the default is 0), and then it'll display that number with a GET. redis-cli incr srcCounter redis-cli get srcCounter
For the official redis CLI tutorial, click here. (It'll show you the way to connect to a redis DB on a cloud server, if you're working with a real DB and not just from our local computer.)
And don't forget to review the commands to know how to interact with the redis database. Using Redis From A Programming Language
Often, we only use the command line to interact with our databases for initial setup or for testing. The most important way we use databases is by interacting with them using a programming language.
Redis has library support for many programming languages. Here's an official list of supported redis libraries. Depending on the language your program is in, choose the officially supported language and use it. Most of the time, it's a simple "connect to the redis database server, and interact with is using the usual redis commands", so it's not that hard to implement redis if you've been following along in the previous sections. Conclusion
Redis is a popular tool, and to be honest, it's a really cool and useful utility. (Their company is pretty good about supporting it, too.)
Try to use Redis if your data is needs to be accessed by a lot of users, and speed is REALLY important.
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake
References:
1. Redis Homepage. 2. Redis commands. 3. Redis command tutorial online. 4. Install redis-server. 5. Redis-CLI document. Comments are closed.
|
AuthorHi, I'm srcmake. I play video games and develop software. Pro-tip: Click the "DIRECTORY" button in the menu to find a list of blog posts.
License: All code and instructions are provided under the MIT License.
|