0% found this document useful (0 votes)
4 views

Case Study Solution

Jasmine, a multinational supermarket company, is transitioning from 4-byte integer customer IDs to 128-bit UUIDs due to ID limitations. The implementation involves updating the database schema, migrating existing data without downtime, and ensuring new data includes both ID formats. The solution includes SQL commands for schema updates and Java code for data migration and access methods to retrieve customer profiles using either ID type.

Uploaded by

075bct054.niraj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Case Study Solution

Jasmine, a multinational supermarket company, is transitioning from 4-byte integer customer IDs to 128-bit UUIDs due to ID limitations. The implementation involves updating the database schema, migrating existing data without downtime, and ensuring new data includes both ID formats. The solution includes SQL commands for schema updates and Java code for data migration and access methods to retrieve customer profiles using either ID type.

Uploaded by

075bct054.niraj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Case Study Analysis

Company: Jasmine, a large multinational supermarket company with over 1.5 billion unique
customers.

Problem: The current software uses a 4-byte integer for customer IDs, which is reaching its
limit (2,147,483,647). The CTO has decided to switch to 128-bit UUIDs for new customer IDs
to avoid this limitation.

Constraints:

1. No downtime is allowed as Jasmine operates 24/7/365.


2. Customer profiles are immutable, meaning they cannot be updated once created.
3. The new customer profile structure should include both the old customer ID and the
new UUID.

Solution Approach

1. Database Schema Update:


o Add a new column for the UUID in the customer profile table.
o Create a new table to store the new customer profiles with UUIDs.
2. Migrate Existing Data
o Instead of updating existing records, insert new records with the new UUIDs.
3. Handle New Data:
o During the migration period, write both the old customer ID and the new UUID
for new records.
4. Access Data:
o Update the application to read from the new UUID column, but fall back to the
old customer ID if the UUID is not present.

Implementation

 Schema Update (SQL)

1) Add new column for UUID


ALTER TABLE customer_profiles ADD COLUMN new_customer_id UUID;
2) Create a new table for new customer profiles
CREATE TABLE new_customer_profiles (
old_customer_id INT,
new_customer_id UUID PRIMARY KEY,
customer_name VARCHAR(255),
address VARCHAR(255),
rfid_number BIGINT,
joined_date DATE
);
 Migrate Existing Data

import java.sql.*;
import java.util.UUID;

public class DataMigration {

public static void main(String[] args) {


String url = "jdbc:your_database_url";
String user = "your_database_user";
String password = "your_database_password";

try (Connection conn = DriverManager.getConnection(url,


user, password)) {
String selectQuery = "SELECT old_customer_id,
customer_name, address, rfid_number, joined_date FROM
customer_profiles";
String insertQuery = "INSERT INTO new_customer_profiles
(old_customer_id, new_customer_id, customer_name, address,
rfid_number, joined_date) VALUES (?, ?, ?, ?, ?, ?)";

try (PreparedStatement selectStmt =


conn.prepareStatement(selectQuery);
PreparedStatement insertStmt =
conn.prepareStatement(insertQuery)) {

ResultSet rs = selectStmt.executeQuery();
while (rs.next()) {
int oldCustomerId =
rs.getInt("old_customer_id");
UUID newCustomerId = UUID.randomUUID();
String customerName =
rs.getString("customer_name");
String address = rs.getString("address");
long rfidNumber = rs.getLong("rfid_number");
Date joinedDate = rs.getDate("joined_date");

insertStmt.setInt(1, oldCustomerId);
insertStmt.setObject(2, newCustomerId);
insertStmt.setString(3, customerName);
insertStmt.setString(4, address);
insertStmt.setLong(5, rfidNumber);
insertStmt.setDate(6, joinedDate);
insertStmt.executeUpdate();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

 Handle New Data and Access New Data

import java.sql.*;
import java.util.UUID;

public class CustomerProfileService {

private Connection conn;

public CustomerProfileService(Connection conn) {


this.conn = conn;
}

public void addCustomerProfile(int oldCustomerId, String


customerName, String address, long rfidNumber, Date joinedDate)
throws SQLException {
UUID newCustomerId = UUID.randomUUID();
String insertQuery = "INSERT INTO new_customer_profiles
(old_customer_id, new_customer_id, customer_name, address,
rfid_number, joined_date) VALUES (?, ?, ?, ?, ?, ?)";

try (PreparedStatement stmt =


conn.prepareStatement(insertQuery)) {
stmt.setInt(1, oldCustomerId);
stmt.setObject(2, newCustomerId);
stmt.setString(3, customerName);
stmt.setString(4, address);
stmt.setLong(5, rfidNumber);
stmt.setDate(6, new
java.sql.Date(joinedDate.getTime()));
stmt.executeUpdate();
}
}

public CustomerProfile getCustomerProfile(UUID newCustomerId)


throws SQLException {
String selectQuery = "SELECT * FROM new_customer_profiles
WHERE new_customer_id = ?";
try (PreparedStatement stmt =
conn.prepareStatement(selectQuery)) {
stmt.setObject(1, newCustomerId);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
return new CustomerProfile(
rs.getInt("old_customer_id"),
(UUID) rs.getObject("new_customer_id"),
rs.getString("customer_name"),
rs.getString("address"),
rs.getLong("rfid_number"),
rs.getDate("joined_date")
);
}
}
return null;
}

public CustomerProfile getCustomerProfile(int oldCustomerId)


throws SQLException {
String selectQuery = "SELECT * FROM new_customer_profiles
WHERE old_customer_id = ?";
try (PreparedStatement stmt =
conn.prepareStatement(selectQuery)) {
stmt.setInt(1, oldCustomerId);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
return new CustomerProfile(
rs.getInt("old_customer_id"),
(UUID) rs.getObject("new_customer_id"),
rs.getString("customer_name"),
rs.getString("address"),
rs.getLong("rfid_number"),
rs.getDate("joined_date")
);
}
}
return null;
}
}

Explanation

1. Database Schema Update:


o Adds a new column new_customer_id to the customer_profiles table.
o Creates a new table new_customer_profiles to store the new customer profiles
with UUIDs.
2. Migrate New Data:
o Selects existing customer profiles and inserts new records into
the new_customer_profiles table with generated UUIDs.
3. Handle New Data:
o The addCustomerProfile method writes both the old customer ID and the new
UUID for new records into the new_customer_profiles table.
4. Access Data:
o The getCustomerProfile methods allow fetching profiles by either the new UUID
or the old customer ID from the new_customer_profiles table.

You might also like