I want to create service where just by providing address i can fetch all the token (erc 20 & erc721) balance just like etherscan for a private network? is there any way to do it by writing for loop for somthing? do etherscan store its data in some database? how do etherscan work in the core?
All the data regarding token and ether balances, transaction etc. are present in the blockchain. Etherscan just fetches them from the blockchain. For an ERC20 token, when it is created, the contract needs to implement the
balanceOf function. Calling the function of that ERC20 contract passing an address can get that token's balance for that address.
Similarly you can fetch for all if you know all the deployed tokens. Ideally you should ask the user to chose which all ERC20 token balances is she interested in like Metamask does so that you don't simply waste time fetching all the various token balances for that address which would be zero and the user is not interested in.