0
0
Flaskframework~20 mins

Redis as message broker in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Redis Message Broker Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a Flask app publishes a message to Redis?

Consider a Flask app using Redis as a message broker. When the app publishes a message to a Redis channel, what is the immediate effect?

Flask
import redis

r = redis.Redis()
r.publish('channel1', 'Hello')
AThe message is sent to all clients subscribed to 'channel1' immediately.
BThe message is stored in Redis and delivered when clients connect later.
CThe message is saved in a Redis list for later retrieval.
DThe message triggers a Redis error because publish requires a subscriber.
Attempts:
2 left
💡 Hint

Think about how Redis Pub/Sub works with channels and subscribers.

state_output
intermediate
2:00remaining
What is the output of this Redis subscription code in Flask?

Given this Flask code snippet subscribing to a Redis channel, what will be printed when a message is published?

Flask
import redis

r = redis.Redis()
ps = r.pubsub()
ps.subscribe('channel1')

for message in ps.listen():
    if message['type'] == 'message':
        print(message['data'].decode())
        break
ANo output because the loop breaks before printing.
BA bytes object representing the message.
CThe string 'subscribe' indicating subscription confirmation.
DThe exact message string published to 'channel1'.
Attempts:
2 left
💡 Hint

Remember the message dictionary keys and their meanings in Redis Pub/Sub.

📝 Syntax
advanced
2:00remaining
Which option correctly initializes Redis Pub/Sub in Flask?

Choose the correct way to set up Redis Pub/Sub in a Flask app to listen to a channel.

A
r = redis.Redis()
ps = r.pubsub()
ps.subscribe('channel1')
B
ps = redis.pubsub()
ps.subscribe('channel1')
C
r = redis.Redis()
ps = r.subscribe('channel1')
Dps = redis.Redis().pubsub('channel1')
Attempts:
2 left
💡 Hint

Check the Redis Python client API for Pub/Sub setup.

🔧 Debug
advanced
2:00remaining
Why does this Flask Redis subscriber never print messages?

Given this code, why does the subscriber never print any messages?

Flask
import redis

r = redis.Redis()
ps = r.pubsub()
ps.subscribe('channel1')

for message in ps.listen():
    if message['type'] == 'message':
        print(message['data'].decode())
AThe subscription is missing a call to ps.run() to start listening.
BThe Redis server is not running, so no messages arrive.
CThe message data is bytes and needs decoding before printing.
DThe loop breaks immediately after the first message, so nothing prints.
Attempts:
2 left
💡 Hint

Consider the data type of message['data'] and how print handles it.

🧠 Conceptual
expert
2:00remaining
What is a key limitation of using Redis Pub/Sub as a message broker in Flask apps?

Identify the main limitation of Redis Pub/Sub when used as a message broker for Flask applications.

AMessages are encrypted by default, causing performance overhead.
BMessages are not stored; if a subscriber is offline, it misses messages.
CRedis Pub/Sub requires complex setup with multiple Redis instances.
DRedis Pub/Sub only supports JSON messages, limiting data types.
Attempts:
2 left
💡 Hint

Think about message delivery guarantees and offline subscribers.