I understand that switches learn from incoming traffic and thus builds its switching table. Does that also apply for routers?
No, routers only learn routes when they are advertised to them by other routers, by different routing protocols.
If so, how routing table can avoid increasing the routing table size since this is an automatic process?
Now though this is a semi-automatic process i.e. Once you have started running a protocol you are accepting all the routes being advertised by the peer router. So you can have certain methods by which you can limit number of routes and they mostly rotate around route-map:
- apply route-map(is a filter which you can apply between you and your peer) to accept only limited number of prefixes.
- apply route-map to accept only shorter prefixes and reject longer prefixes i.e. If you give /22 to your customer and you apply a route-map to accept no longer than /23 routes. You will have a maximum of 3 prefixes coming from that customer. A /22 and two /23.
Now as Ron said if you are planning to get internet routing table you need good CPU and RAM(greater than 4GB) because the routes are ever increasing on the internet and no you cannot apply caching mechanism because you are not asking for routes or routes are not constantly getting advertised by your peers( that you can monitor them and cache them) they are sent in updates(which maybe far and few and in large time intervals) by peers.
Hope this helps!