Bird
Raised Fist0
Flaskframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main role of Redis when used as a message broker in a Flask application?
easy
A. To serve static files like images and CSS
B. To store user passwords securely
C. To send and receive messages between different parts of the app
D. To handle HTTP requests directly

Solution

  1. Step 1: Understand Redis as a message broker

    Redis acts as a middleman to send messages between parts of an app, enabling communication.
  2. Step 2: Identify Redis's role in Flask

    It is not used for storing passwords, serving files, or handling HTTP requests directly.
  3. Final Answer:

    To send and receive messages between different parts of the app -> Option C
  4. Quick Check:

    Redis message broker = send/receive messages [OK]
Hint: Redis in Flask is for messaging, not storage or HTTP [OK]
Common Mistakes:
  • Confusing Redis with a database for user data
  • Thinking Redis serves static files
  • Assuming Redis handles HTTP requests
2. Which of the following is the correct way to publish a message to a Redis channel in Flask?
easy
A. redis_client.post('channel', 'message')
B. redis_client.send('channel', 'message')
C. redis_client.push('channel', 'message')
D. redis_client.publish('channel', 'message')

Solution

  1. Step 1: Recall Redis publish method

    Redis uses the method publish to send messages to a channel.
  2. Step 2: Check method names

    Only publish is the correct Redis command; others like send, push, or post are incorrect.
  3. Final Answer:

    redis_client.publish('channel', 'message') -> Option D
  4. Quick Check:

    Publish method = publish() [OK]
Hint: Use publish() to send messages in Redis [OK]
Common Mistakes:
  • Using send() instead of publish()
  • Confusing push() with publish()
  • Trying to use post() which is not a Redis method
3. Given the following Flask code snippet using Redis, what will be printed when a message is published to 'test_channel'?
import redis
r = redis.Redis()

pubsub = r.pubsub()
pubsub.subscribe('test_channel')

for message in pubsub.listen():
    if message['type'] == 'message':
        print(message['data'].decode())
        break
medium
A. The published message content decoded as a string
B. The string 'test_channel'
C. The message type 'subscribe'
D. An error because decode() is not valid here

Solution

  1. Step 1: Understand pubsub.listen() behavior

    The loop listens for messages on 'test_channel'. When a message of type 'message' arrives, it prints the decoded data.
  2. Step 2: Decode message data

    The message data is bytes, so decode() converts it to a string, which is printed.
  3. Final Answer:

    The published message content decoded as a string -> Option A
  4. Quick Check:

    Message data decoded = printed string [OK]
Hint: listen() yields messages; decode bytes to string before print [OK]
Common Mistakes:
  • Printing channel name instead of message data
  • Confusing message type 'subscribe' with 'message'
  • Forgetting to decode bytes to string
4. What is the main issue with this Flask Redis subscriber code snippet?
import redis
r = redis.Redis()
pubsub = r.pubsub()
pubsub.subscribe('channel')

for message in pubsub.listen():
    if message['type'] == 'message':
        print(message['data'].decode())
It causes the Flask app to freeze and not respond.
medium
A. The listen() loop blocks the main thread, freezing the app
B. The decode() method is called on a string, causing an error
C. The subscribe() method is missing required parameters
D. Redis client is not connected to the server

Solution

  1. Step 1: Analyze listen() behavior

    The listen() method blocks execution, waiting for messages indefinitely.
  2. Step 2: Understand Flask app impact

    Running listen() in the main thread stops Flask from handling other requests, freezing the app.
  3. Final Answer:

    The listen() loop blocks the main thread, freezing the app -> Option A
  4. Quick Check:

    Blocking listen() = app freeze [OK]
Hint: listen() blocks; run it in background thread [OK]
Common Mistakes:
  • Thinking decode() causes error here
  • Believing subscribe() needs extra parameters
  • Assuming Redis connection is missing without error
5. You want to listen for Redis messages in a Flask app without freezing it. Which approach correctly solves this problem?
hard
A. Call pubsub.listen() directly in the main Flask route handler
B. Run the Redis pubsub.listen() loop inside a background thread
C. Use Redis publish() inside the listen() loop
D. Subscribe to Redis channels without calling listen()

Solution

  1. Step 1: Identify blocking issue with listen()

    Calling listen() in the main thread blocks Flask, freezing the app.
  2. Step 2: Use background thread for listening

    Running listen() in a separate thread allows Flask to stay responsive while listening for messages.
  3. Final Answer:

    Run the Redis pubsub.listen() loop inside a background thread -> Option B
  4. Quick Check:

    Background thread for listen() = no freeze [OK]
Hint: Use background thread to avoid blocking Flask [OK]
Common Mistakes:
  • Running listen() in main route causing freeze
  • Misusing publish() inside listen() loop
  • Subscribing without listen() so no messages received
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard