Lazy initialization and singleton patterns

Optimizing Code in Java

Pavlos Kosmetatos

Lead Engineer @Wealthyhood

Building a Redis cache client

  • Imagine we are building a cache client that hits Redis
  • Our Redis client is hosted at a third-party provider
  • Setting up the connection to our client involves a network call - imagine this takes 500ms

$$

Call over the network to establish Redis connection

Optimizing Code in Java

A simple (eager) implementation

public class RedisCache {
    // The client library we use to connect to Redis
    private final RedisClient client;

    public RedisCache() {
        // The connection is setup inside the constructor
        connection = new RedisClient("cache.mycompany.com");
    }
}
  • Eager initialization of a Redis connection
Optimizing Code in Java

A potential issue with this approach

This seems simple - and would work - but it has a potential issue

What if we don't need the Redis client at all?

  • We wasted precious time at startup
  • We established unnecessary connections
Optimizing Code in Java

Lazy initialization

public class RedisCache {
    private RedisClient client;

    // Instead of setting up the connection in the constructor,
    // we only set it up when someone needs to get the client.
    public RedisClient getClient() {
        if (connection == null) {
            connection = new RedisClient("cache.mycompany.com");
        }
        return connection;
    }
}
Optimizing Code in Java

Another issue with our approach

// UserService needs cache access
public class UserService {
    private RedisCache userCache = new RedisCache();  // First connection
}

// PaymentService also needs cache
public class PaymentService {
    private RedisCache paymentCache = new RedisCache();  // Second connection
}

// ...same for OrderService ...
Optimizing Code in Java

The singleton pattern

public class RedisCache {
    private static RedisCache instance;
    private RedisClient client;

    // The constructor is private so that we ensure we only
    // create RedisCache inside this class
    private RedisCache() {}

    public static RedisCache getInstance() 
        // We only create a RedisCache if one does not already exist
        if (instance == null) { instance = new RedisCache(); }
        return instance;
    }

    // ... The rest is the same as before ...
}
Optimizing Code in Java

Let's practice!

Optimizing Code in Java

Preparing Video For Download...