It doesn’t seem fair. Even David was distressed by the consequences his sin had on innocent people (see 1Ch 21:17). We may make our own choices, but we cannot control the extent of the consequences of sin.
Because of our Western individualism we struggle to understand the Eastern tradition in which the head of a family, tribe or nation represented the people under them. The members were treated as a whole, sharing in the blessings or punishments resulting from the actions of their leaders. Adam’s sin had consequences for all humanity (see Ro 5:12). When Achan sinned, God said Israel has sinned (see Jos 7:1–11). Joshua had to identify the tribe, clan and family to which the sinner belonged.
In this case, it may have been Israel’s sin as a nation that led to David’s sin. The Lord was angry with Israel before David was incited to take a census (see 2Sa 24:1). For this reason, some see this as a plague upon a nation of people who had themselves sinned.
David’s sin deserved personal punishment, but David’s death might have been worse for the nation than the plague. Political turmoil in Israel could have brought invading armies that would have killed even more people. David suffered remorse and grief. Along with the leaders who may have supported his call for a census, David mourned and repented. God graciously forgave him and intervened to spare Jerusalem. Later, the temple was built on the site where David offered his sacrifice and saw the plague halted.