Bird
Raised Fist0
Redisquery~20 mins

Redis with Java (Jedis, Lettuce) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis Java Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the output of this Jedis command sequence?
Given a Jedis connection, what will be the output of the following commands?
jedis.set("key1", "value1");
String val = jedis.get("key1");
System.out.println(val);
Redis
jedis.set("key1", "value1");
String val = jedis.get("key1");
System.out.println(val);
AThrows NullPointerException
Bnull
CThrows JedisConnectionException
Dvalue1
Attempts:
2 left
💡 Hint
Remember that set stores a string value and get retrieves it.
📝 Syntax
intermediate
2:00remaining
Which option correctly initializes a Lettuce Redis client?
Choose the correct Java code snippet to create a Lettuce Redis client connected to localhost on default port 6379.
ARedisClient client = RedisClient.connect("localhost");
BRedisClient client = new RedisClient("localhost", 6379);
CRedisClient client = RedisClient.create("redis://localhost:6379");
DRedisClient client = RedisClient.create("http://localhost:6379");
Attempts:
2 left
💡 Hint
Lettuce uses URI format starting with redis://
❓ optimization
advanced
2:00remaining
How to optimize multiple Redis commands in Jedis?
You want to execute multiple Redis commands efficiently in Jedis. Which approach reduces network overhead the most?
AOpen and close a new Jedis connection for each command
BUse Jedis pipelining to batch commands before sending
CUse multiple threads each with its own Jedis instance
DSend commands one by one synchronously
Attempts:
2 left
💡 Hint
Batching commands reduces round-trip time.
🔧 Debug
advanced
2:00remaining
Why does this Lettuce code throw a NullPointerException?
Consider this Lettuce code snippet:
RedisClient client = RedisClient.create("redis://localhost:6379");
StatefulRedisConnection connection = null;
RedisCommands syncCommands = connection.sync();
syncCommands.set("key", "value");

What causes the NullPointerException?
Redis
RedisClient client = RedisClient.create("redis://localhost:6379");
StatefulRedisConnection<String, String> connection = null;
RedisCommands<String, String> syncCommands = connection.sync();
syncCommands.set("key", "value");
Aconnection is null, so calling sync() causes NullPointerException
BRedisClient.create returns null causing failure
Cset command is invalid syntax in Lettuce
DsyncCommands is null because set returns null
Attempts:
2 left
💡 Hint
Check which variable is null before method call.
🧠 Conceptual
expert
2:00remaining
What is the main difference between Jedis and Lettuce clients?
Which statement best describes the key difference between Jedis and Lettuce Redis clients in Java?
AJedis is synchronous and blocking; Lettuce supports asynchronous and reactive APIs
BJedis supports clustering; Lettuce does not support clustering
CLettuce is only for Redis 6+; Jedis supports all Redis versions
DJedis uses Netty; Lettuce uses raw sockets
Attempts:
2 left
💡 Hint
Think about how each client handles command execution style.

Practice

(1/5)
1. What is the primary purpose of Jedis and Lettuce in Java applications?
easy
A. To connect and interact with Redis databases
B. To create graphical user interfaces
C. To manage Java threads
D. To compile Java code

Solution

  1. Step 1: Understand Jedis and Lettuce roles

    Jedis and Lettuce are Java libraries designed to connect to Redis servers.
  2. Step 2: Identify their main function

    They allow Java programs to store and retrieve data from Redis easily.
  3. Final Answer:

    To connect and interact with Redis databases -> Option A
  4. Quick Check:

    Jedis and Lettuce connect Java apps to Redis [OK]
Hint: Remember: Jedis and Lettuce are Redis clients for Java [OK]
Common Mistakes:
  • Confusing Jedis/Lettuce with UI or threading libraries
  • Thinking they compile Java code
  • Assuming they manage Java memory
2. Which of the following is the correct way to create a Jedis connection in Java?
easy
A. Jedis jedis = new JedisClient("localhost");
B. Jedis jedis = Jedis.connect("localhost");
C. Jedis jedis = Jedis.create("localhost", 6379);
D. Jedis jedis = new Jedis("localhost", 6379);

Solution

  1. Step 1: Recall Jedis connection syntax

    Jedis connections are created using the constructor with host and port: new Jedis("host", port).
  2. Step 2: Verify options

    Only Jedis jedis = new Jedis("localhost", 6379); uses the correct constructor syntax: new Jedis("localhost", 6379); others use invalid methods or classes.
  3. Final Answer:

    Jedis jedis = new Jedis("localhost", 6379); -> Option D
  4. Quick Check:

    Use new Jedis(host, port) to connect [OK]
Hint: Use new Jedis("host", port) to create connection [OK]
Common Mistakes:
  • Using non-existent methods like connect() or create()
  • Confusing class names like JedisClient
  • Omitting port number
3. Given the following Jedis code snippet, what will be the output?
Jedis jedis = new Jedis("localhost", 6379);
jedis.set("key1", "value1");
String val = jedis.get("key1");
System.out.println(val);
jedis.close();
medium
A. value1
B. null
C. key1
D. Throws an exception

Solution

  1. Step 1: Analyze the set operation

    The code sets the key "key1" with value "value1" in Redis using jedis.set().
  2. Step 2: Analyze the get operation and output

    Then it retrieves the value for "key1" with jedis.get(), which returns "value1". This is printed to the console.
  3. Final Answer:

    value1 -> Option A
  4. Quick Check:

    Set then get returns stored value [OK]
Hint: Set key then get key returns stored value [OK]
Common Mistakes:
  • Expecting null if not connected properly
  • Confusing key and value in output
  • Forgetting to close connection causes resource leaks
4. Identify the error in this Lettuce code snippet:
RedisClient client = RedisClient.create("redis://localhost:6379");
StatefulRedisConnection connection = client.connect();
RedisCommands commands = connection.sync();
commands.set("name", "Alice");
System.out.println(commands.get("name"));
// Missing connection.close() and client.shutdown()
medium
A. Incorrect Redis URI format
B. Missing closing connection and shutting down client
C. Using sync() instead of async() causes error
D. Commands object should be created before connection

Solution

  1. Step 1: Review resource management in Lettuce

    Connections and clients must be closed to avoid resource leaks.
  2. Step 2: Identify missing cleanup calls

    The code lacks connection.close() and client.shutdown(), which are needed to release resources properly.
  3. Final Answer:

    Missing closing connection and shutting down client -> Option B
  4. Quick Check:

    Always close connection and shutdown client [OK]
Hint: Always close connection and shutdown client after use [OK]
Common Mistakes:
  • Ignoring resource cleanup causing leaks
  • Thinking sync() causes errors
  • Misunderstanding URI format
5. You want to store multiple user scores in Redis using Jedis efficiently. Which approach is best to minimize network calls?
hard
A. Open and close connection for each set command
B. Call jedis.set() for each user score separately
C. Use pipelining to batch multiple set commands
D. Use multiple Jedis instances concurrently

Solution

  1. Step 1: Understand network call overhead

    Calling jedis.set() repeatedly sends many network requests, slowing performance.
  2. Step 2: Use pipelining to batch commands

    Pipelining batches commands to send them together, reducing network round-trips and improving speed.
  3. Final Answer:

    Use pipelining to batch multiple set commands -> Option C
  4. Quick Check:

    Pipelining batches commands to reduce network calls [OK]
Hint: Batch commands with pipelining to speed Redis writes [OK]
Common Mistakes:
  • Opening/closing connection repeatedly wastes resources
  • Using multiple Jedis instances unnecessarily
  • Ignoring pipelining benefits
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