This has been covered elsewhere, but if you are not familiar w/ MSO vs. ADSO, it can be confusing. When you sign up for HPSP, you automatically incur an 8 year military service obligation (MSO- active duty + reserve time). If you decide to sign up for a 4 year scholarship, you will incur at least a 4 year active duty service obligation (ADSO). Residency will count towards fulfilling your MSO, but not your HPSP ADSO. To confuse things even further, residency will actually incur additional yrs of ADSO, but can be served concurrently w/ HPSP ADSO - most don't realize this. You will start your ADSO after residency completion. If you decide to pursue fellowship, you will add additional years of ADSO, but can be served concurrently with HPSP/residency ADSO. Also, any remaining years on MSO after ADSO is fulfilled will be served in IRR (Inactive Ready Reserves)
For example, if you signed a 4 year HPSP contract, after completing 1 yr internship/2 yrs of residency, you will need to remain on active duty for another 4 yrs to complete your ADSO (2 yr residency = 2 yr ADSO, but served concurrently w/ 4 yr HPSP) after which you can separate (ETS), but still owe 1 more year of MSO in IRR.
However, this is where it may get even more confusing. Let's say 2 yrs after residency, you get picked up for a 3 yr fellowship. Prior to starting fellowship, you have already completed 2 yrs ADSO and only owe 2 more years, but a 3 yr fellowship will entail an additional 3 yr ADSO, but can be served concurrently with HPSP ADSO. In essence, you will have to serve 3 more years after completing fellowship.
Of note, there have been changes to how fellowships are awarded. Priority is given to those who have deployed, served overseas or worked as Battalion/Brigade/Flight Surgeons and less based on actual academic/clinical merit. I am told programs are less inclined or discouraged from picking up fellows straight after residency. Therefore, a 1 or 2 yr gap after residency is a realistic scenario if you choose to pursue fellowship. I opted to pursue a civilian fellowship. Good luck!