Python Redis programming
Redis through self.redis - keys, lists, hashmaps and transactions with redis-py.
Configure the Redis connection in Dashboard and then any programming feature that Redis offers will be available to your services via the underlying redis-py library, as in the examples below.
Configuring the connection
In Dashboard, go to Connections -> NoSQL -> Redis.

Fill in the form:
- Host and port - where the server listens, e.g.
localhostand6379 - DB number - the Redis logical database to use,
0by default - Username and password - credentials to authenticate with, leave them empty if the server does not require authentication
The SSL/TLS section lets you connect to servers that require encrypted connections - turn the SSL/TLS checkbox on and point the CA file to the PEM certificate of the authority that signed the server's certificate. For mutual TLS, add the client certificate and key files too.
Click Test to confirm that Zato can reach the server and Save to store the configuration - the connection is reconfigured on the fly and services use the new details right away.
Simple keys
In your services, self.redis is a Redis client, so keys and data structures are accessed the way redis-py does it.
# -*- coding: utf-8 -*-
# Zato
from zato.server.service import Service
class MyService(Service):
def handle(self):
self.redis.set('my-key', 'my-value')
self.logger.info(self.redis.get('my-key'))
Lists
# -*- coding: utf-8 -*-
# Zato
from zato.server.service import Service
class MyService(Service):
def handle(self):
# Clear/delete the list
self.redis.delete('my-list')
# Push 10 elements
for value in range(10):
self.redis.lpush('my-list', value)
# Read them back
self.logger.info(self.redis.lrange('my-list', 0, 9999))
Dictionaries (hashmaps)
# -*- coding: utf-8 -*-
# Zato
from zato.server.service import Service
class MyService(Service):
def handle(self):
# Clear/delete the dictionary
self.redis.delete('my-dict')
# Set 10 keys and values
for value in range(10):
self.redis.hset('my-dict', value, value**value)
# Read one of them back
self.logger.info(self.redis.hget('my-dict', 7))
Transactions
Pipelines can be used to improve performance of the execution of multiple operations and to invoke the operations atomically.
# -*- coding: utf-8 -*-
# Zato
from zato.server.service import Service
class MyService(Service):
def handle(self):
# Either deletes all the keys specified and adds a new one or none
# of it is performed.
with self.redis.pipeline() as pipeline:
# Deletes
pipeline.delete('my-key-1')
pipeline.delete('my-key-2')
pipeline.delete('my-key-3')
# Add new key
pipeline.set('my-key-4', 'my-value')
# Execute all the commands queued up in a single transaction
pipeline.execute()