Explained step by step
Base62 encodes with 62 symbols: the 26 uppercase letters, the 26 lowercase letters and the 10 digits. The number of possible links is 62 to the power of N, where N is the number of characters. With 5 there are about 916 million; with 6, about 56.8 billion; with 7, about 3.5 trillion. It's the simplest solution, but on its own it doesn't guarantee no repeats. It's like license plates: with more letters and numbers, there's room for more cars.
Another option is MD5, a widely used hash function producing a 128-bit value, or 32 hexadecimal digits. You compute the MD5 of the original link, encode it in Base62 and take 7 characters. The problem: when the hash is cut, 2 different links can give the same key, a collision. You can recompute until you find a unique one, but that adds work. It's like building a username from the first 3 letters of first and last name: «John Smith» and «Johanna Smithers» clash.
With a counter, 1 server keeps the count: each request gets the next number, which is encoded in Base62. The problem is that this server is a single point of failure, and with several counters there can be repeats. The solution is Zookeeper, which assigns each server a range: server 1 uses 1 to 1,000,000, server 2 uses 1,000,001 to 2,000,000, and so on. When one finishes its range, it gets another unused one. It's like raffle ticket books: each seller has their numbers and they never overlap.
A separate key generation service (KGS) generates unique keys ahead of time and stores them in its own database, ready to use. When a link is created, the KGS hands out 1 key and marks it as used. With 6 characters there are about 56.8 billion keys, taking about 390 GB. That sounds like a lot, but it's for the service's whole lifetime and doesn't grow like the main database. It's like a printer of queue tickets: it prints them beforehand, and at the counter they're just torn off.
If several servers read keys from the KGS at once, 2 could take the same one. The simplest solution is storing keys in 2 tables: as soon as one is used, it's moved to the used table, with a lock that stops anyone else from taking it. To read faster, some keys can be kept in memory. It's like 2 cashiers with 1 single ticket roll: if they tear at the same time, they may end up with the same number.
If expired links are deleted, there are 2 ways. Active cleanup: a separate service, as light as a cron job, periodically deletes expired ones from the database and cache, for example every 1 hour. Passive cleanup: the link is deleted when someone tries to visit it and it's expired; it's a lazy cleanup. It's like the fridge: either you check it every Sunday (active), or you throw something out when you go to use it and it's expired (passive).
For the shortener's cache, the LRU (Least Recently Used) policy fits: when it's full, the key unused for the longest time is discarded first. On a cache miss, the server goes straight to the database and updates the cache with the new link. That way popular links stay in memory and forgotten ones leave. It's like your desk: what you used today stays on top, and what you haven't touched in 1 month goes in the drawer.