What happens is namenode on the basis of rack-awareness algorithm selects a datanode where the data is written, the next replication happens in another data node and so on. The last data node on which data has been written sends an acknowledgment to the previously written datanode and so on. Finally, the datanode which had been written first sends a signal to the name node that the data has been successfully written in the data nodes.