> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# URL Shortener

> Guide to running a local URL shortener lab that creates a short link, generates traffic, and verifies each redirect is recorded in Postgres.

This lab walks you through running the provided URL-shortener service locally, creating a real short link, and generating traffic against it to observe how each redirect is recorded in Postgres. Follow the steps below — hints and full solutions are available in the lab if you need them.

## What you'll do

* Start the application and its dependencies with Docker Compose.
* Mint a short link via the `/shorten` endpoint.
* Use the provided hammer script to send 200 requests to that short link.
* Inspect the application logs (and Postgres) to confirm every request was recorded.

## Prerequisites

* Docker and Docker Compose installed
* curl (or any HTTP client)
* Access to the lab environment where the repository and `hammer.sh` script exist

## Quick lab steps

1. Start services:

```bash theme={null}
docker compose up app redis pg
```

2. Mint a short link:

```bash theme={null}
POST /shorten - returns a short code in JSON
```

3. Hammer the short link:

```bash theme={null}
./hammer.sh <SHORT_CODE>
```

4. Inspect logs to confirm every request hit Postgres.

## Detailed example session

Start the services:

```bash theme={null}
docker compose up app redis pg
```

Mint a short link (example using curl):

```bash theme={null}
curl -X POST localhost:8000/shorten -d 'https://kodekloud.com'
```

Example response:

```json theme={null}
{"code": "2Bi"}
```

The service responds with a JSON payload containing the short code (for example, `2Bi`). Use that returned code when running the hammer script. For example, if the service returned `2Bi`, run:

```bash theme={null}
./hammer.sh 2Bi
```

That script will send 200 requests against the short link. Then inspect the application logs (or Postgres) to verify each request was recorded.

<Callout icon="lightbulb" color="#1CB2FE">
  If your environment uses a different short code (for example some exercises may reference `cat42`), substitute that code when invoking `./hammer.sh`. The important part is to target the actual short code returned by the `/shorten` endpoint.
</Callout>

## Inspecting logs and Postgres

* Watch the application logs printed to the terminal from `docker compose up` — you should see log lines for each redirect request.
* Alternatively, connect to the Postgres container and query the table that stores hits (table name may vary by lab). Example:

```bash theme={null}
# enter the postgres container (example container name: pg)
docker exec -it <pg-container> psql -U postgres

# inside psql, run a simple count (replace table_name with actual table)
SELECT COUNT(*) FROM table_name WHERE short_code = '2Bi';
```

## Endpoints reference

| Endpoint | Method | Description |
| - | - | - |
| `/shorten` | `POST` | Mint a new short code for the provided long URL (request body is the long URL). |
| `/<code>` | `GET` | Redirects to the original URL associated with `<code>` and records the hit in the database. |

## Troubleshooting tips

* If you get an unexpected short code, always use the returned code when running `./hammer.sh`.
* Ensure the `app`, `redis`, and `pg` services are healthy in `docker compose ps` before sending traffic.
* If logs show errors connecting to Postgres, confirm network/service names in `docker-compose.yml` and that the DB has initialized.

## Links and references

* Docker Compose: [https://docs.docker.com/compose/](https://docs.docker.com/compose/)
* curl manual: [https://curl.se/docs/manpage.html](https://curl.se/docs/manpage.html)
* PostgreSQL documentation: [https://www.postgresql.org/docs/](https://www.postgresql.org/docs/)

Happy testing — this lab helps you observe real-world behavior where every redirect generates a database write, which is important when considering scalability and caching strategies.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/system-design-for-interview-prep/module/bf4e82c2-1292-42c6-b85f-8b6a0dad559f/lesson/42ecd4cb-50ba-490e-9cee-1560fd4f16c0" />

  <Card title="Practice Lab" icon="flask-conical" cta="Learn more" href="https://learn.kodekloud.com/user/courses/system-design-for-interview-prep/module/bf4e82c2-1292-42c6-b85f-8b6a0dad559f/lesson/77a1ee36-2d0e-4ddb-9f96-70006b63602c" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.