Master Server Sending Incorrect Commands to Its Replicas

I consider myself a beginner with TCP connections, but I wanted to learn more about it so I started a project called the “Build Your Own Redis Challenge” from codecrafters.io. I am trying to implement the WAIT Redis command. I have set up a master server that connects to its replicas as well as a client. Basically, how the tester works is that it creates replicas that connect to my server, then switches to a client that sends REDIS commands (SET, PING, etc.) to my server and expects responses from the server (“+OKrn”, etc.). For example, when a client sends a “SET” command, it expects a”+OKrn” simple string response back from the server and for that server to propagate that SET command to its replicas. I have noticed that after the tester switches to the client and sends a SET command, the server sends the “+OKrn” response to both the client and to one of its replicas, even though it is only supposed to send the response to just the client. Here is the code for my server:

const server = net.createServer((connection) => {
// Handle connection
connection.type = 'client';
connection.on('data', async (data) => {
const command = await readData(data);
let commands = command.slice(3).split('rn'); // parses Redis response into an array
commands.pop(); // removes empty element at the end
console.log("Commands", commands);
if (commands.includes("SET")){
// only the client enters here
// server handles the SET command then writes an OK simple string response
connection.write("+OKrn");
propagateToReplicas(command); // sends client command to master's replicas
} else if (commands.includes("PSYNC")){
// replicas enter here
// replicas complete handshake with server here
// replica connection is then added to replicas array
connection.type = 'replica'; // Set type as replica
replicas.push(connection);
numOfReplicas += 1;
}
})
}
const propagateToReplicas = (command) => {
if (replicas.length == 0){
return
}
replicas.forEach((replica) => {
console.log("Command to be Propagated", command);
replica.write(command);
})
}

Here are the logs that I am receiving. Notice that one of the replicas receives the “+OKrn” response instead of the propagated command.
Logs of responses

I suspected the issue might have been that the socket connection between the client and the replicas might be shared, so I tried to tag each socket connection with an attribute (connection.type = “client” and connection.type = “replica” for example) to make a distinction, but that hasn’t worked either.

New contributor

jonathan nwokeji is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật