Hello World is a simple way to start exploring a new programming language, and it's almost always the first program people create. If you're reading this, you're probably new to Redis or Python and want to learn. To help you do that, let's build a "Hello Redis" program.
Redis, which stands for REmote DIctionary Server, is a BSD-licensed, in-memory data structure store released in 2009 by Salvatore Sanfilippo. One of the big differences between Redis and other NoSQL databases is the data structures that Redis provides. Instead of working with a table abstraction, Redis developers can leverage data structures like strings, hashes, lists, sets, and sorted sets using commands similar to the collection operations in most programming languages. Redis has replication capabilities, a server-side scripting language (Lua), transactions, and different modes of disk persistence.
Unless you've installed Redis for another project, more than likely it's not bundled with your operating system distribution. Using your OS package manager or third-party port system, you can download and install Redis on Linux and MacOS systems. Most packages will install a basic Redis configuration that starts up listening on port 6379, the default port for Redis.
Python is an interpreted, high-level programming language created by Guido van Rossum. It was released in 1991 under the Python Software Foundation License. The foundation oversees the development of Python, with Guido serving as the project's Benevolent Dictator for Life (BDFL). Python was designed to be approachable, with a simple syntax and an emphasis on readability. Its notable feature is that the indenting level of the code is used to indicate block structure.
Most Linux distributions and MacOS install Python by default, so it's likely you already have an appropriate version. The current version is Python 3, so the code in this article is written to run under it, but many operating systems install Python 2, and our code will work with Python 2.7.
You will need to install the
redis-py package to connect to Redis, and you can do so using
pip3 (the Python package manager) with the following command:
pip3 install redis
If you see a message similar to
Requirement already satisfied: Redis, the
Redis-py package is already installed on your system.
Our Hello Redis program uploads a "hello" message to the Redis server, downloads the message that was just uploaded, and displays it to the user. Writing this program in Python (as in most languages) takes five basic steps:
- Import the Redis library
- Define connection parameters
- Instantiate the Redis connection object
- Upload a message to Redis using the connection object
- Download the message from Redis using the connection object
Each step is implemented in the script below:
# step 1: import the redis-py client package
# step 2: define our connection information for Redis
# Replaces with your configuration information
redis_host = "localhost"
redis_port = 6379
redis_password = ""
"""Example Hello Redis Program"""
# step 3: create the Redis Connection object
# The decode_repsonses flag here directs the client to convert the responses from Redis into Python strings
# using the default encoding utf-8. This is client specific.
r = redis.StrictRedis(host=redis_host, port=redis_port, password=redis_password, decode_responses=True)
# step 4: Set the hello message in Redis
r.set("msg:hello", "Hello Redis!!!")
# step 5: Retrieve the hello message from Redis
msg = r.get("msg:hello")
except Exception as e:
if __name__ == '__main__':
Once you copy the code into a file and change the connection parameters in step 2 to connect to your own Redis instance, you can run this script directly from a Unix shell. If your connection parameters are specified correctly, you will see the message
Once you're comfortable with this code, modify it to use the
get methods to upload different data. From there, you can experiment with some of the other Redis data types linked above.