I feel like I failed

I have an 8 and 4 year old...both boys.  When my oldest was around 15 mo I can remember when I put him in time out he would stay there until someone walked over to get him out.  He wouldn't leave just by you telling him it was ok.  My youngest has always pushed the boundaries.  Time out never had the same impact on him.  My kids are so different but awesome is their own unique way.  I am concerned though for my youngest.  At a pet store he walked off to look at the fish and then shortly after I found him and told him it was not ok to wonder off, he did it again.  This time a lady found him outside the store sliding doors.  Thankfully she was a nice person who brought him in, but he could have been hit by a car or taken away.  Today he ran off at school and stepped off the sidewalk to avoid a crowd...almost getting backed into by a car.  I have tried all sorts of things...like time out, taking away toys or things he may want to do, talking to him about his actions...I am trying to help him understand there are consequences.  He is only 4 and still young, but I just don't want anything bad to happen when he decides he doesn't want to listen.  I feel like I failed and am a horrible mom.  Where did I go wrong...