{"id":25949817,"url":"https://github.com/baala3/kafka_play","last_synced_at":"2025-03-04T12:29:23.645Z","repository":{"id":276091451,"uuid":"928180127","full_name":"baala3/kafka_play","owner":"baala3","description":"play with kafka using ruby on local","archived":false,"fork":false,"pushed_at":"2025-03-04T09:13:53.000Z","size":7,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-04T10:25:08.525Z","etag":null,"topics":["kafka","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/baala3.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-06T07:43:09.000Z","updated_at":"2025-03-04T09:15:14.000Z","dependencies_parsed_at":"2025-02-06T08:46:21.870Z","dependency_job_id":null,"html_url":"https://github.com/baala3/kafka_play","commit_stats":null,"previous_names":["baala3/kafka_play"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baala3%2Fkafka_play","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baala3%2Fkafka_play/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baala3%2Fkafka_play/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baala3%2Fkafka_play/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/baala3","download_url":"https://codeload.github.com/baala3/kafka_play/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241847481,"owners_count":20030263,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["kafka","ruby"],"created_at":"2025-03-04T12:29:23.100Z","updated_at":"2025-03-04T12:29:23.626Z","avatar_url":"https://github.com/baala3.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Kafka Play (ruby + kafka)\n\n** A Simple Session Management with [Karafka gem](https://github.com/karafka/karafka) **\nThis project is for learning purposes only, not producton ready code.\n\n### Problem:\n\nMany applications, especially those requiring authentication (like web apps), need to manage user sessions. This includes handling session expiration due to inactivity or explicit logout. Automatically logging users out after a period of inactivity is crucial for both security and user experience.\n\nSuch session events are continuously emitted, requiring a distributed service that can consume these events and send to DB or other services for further analysis. Kafka can fit here to solve this problem.\n\n### Simple solution with kafka:\n\n**Producer:**\n\nProducer emits user session activity events to a Kafka our topic (named `session_logs`).\nExample event details:\n\n```\n{\n \"user_id\": 123,\n \"session_id\": \"abc123\",\n \"activity_timestamp\": \"2025-02-07T12:00:00Z\"\n}\n```\n\nIn this case, i use a Rack task to emits such logs.\n\n**Consumer 1 (SessionActivityTracker):**\n\n- Subscribed to the `session_logs` topic.\n- Identify inactive sessions by checking if `activity_timestamp` \u003e `INACTIVITY_THRESHOLD`.\n- accumulat such expired session events and flush events to `expired_sessions` topic for every `YIELD_INTERVAL`.\n\n**Consumer 2: (SessionExpiryHandler):**\n\n- Subscribed to `expired_sessions` topic and\n- Once an expired session events are received, it can performs actions (Logging the user out, sending notification or revoke user tokens etc.)(not implemented).\n\n## Setup\n\n1. Run Kafka using docker\n\n```shell\ndocker run -d -p 9092:9092 \\\n  --name broker \\\n  -e KAFKA_NODE_ID=1 \\\n  -e KAFKA_PROCESS_ROLES=broker,controller \\\n  -e KAFKA_LISTENERS=PLAINTEXT://:9092,CONTROLLER://:9093 \\\n  -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://127.0.0.1:9092 \\\n  -e KAFKA_CONTROLLER_LISTENER_NAMES=CONTROLLER \\\n  -e KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT \\\n  -e KAFKA_CONTROLLER_QUORUM_VOTERS=1@localhost:9093 \\\n  -e KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1 \\\n  -e KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR=1 \\\n  -e KAFKA_TRANSACTION_STATE_LOG_MIN_ISR=1 \\\n  -e KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS=0 \\\n  -e KAFKA_NUM_PARTITIONS=3 \\\n  apache/kafka:latest\n```\n\ncheck the connection\n`nc -zv 127.0.0.1 9092` -\u003e Connection to 127.0.0.1 port 9092 succeeded!\n\n2. Clone and install the project\n\n```shell\ngit clone git@github.com:Catsuko/karafka_playground.git\ncd karafka_play\nbundle\n```\n\n3. Create Kafka topics\n\n```shell\nbundle exec karafka topics reset\n```\n\n## Run the App\n\nFirst produce session logs with the following rake task:\n\n```shell\nbundle exec rake producer:produce_session_logs\n```\n\n- Our app filter active logs by consuming the `session_logs` topic with `SessionActivityTracker`, which fetches expired sessions and sends them to `expired_sessions` topic at set intervals.\n- `SessionExpiryHandler` which is subscribed to `expired_sessions` topic then processes these sessions logs.\n\nRun the consumers with the karafka gem and then watch the output to see this in action:\n\n```shell\nbundle exec karafka server\n```\n\n## Kafka Web-UI\n\n[Karafka Web UI](https://karafka.io/docs/Web-UI-About/) is a user interface for the Karafka framework.\n\nYou can run the Web UI locally with the following command:\n\n`rackup karafka_web.ru`\n\nOnce it's running, access the real-time metrics dashboard at:\nhttp://localhost:9292/dashboard\n\n## others\n\nYou can also start and stop multiple servers to see the fault tolerance and horizontal scaling work.\n\nref:\n\n- https://www.youtube.com/watch?v=-NMDqqW1uCE\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaala3%2Fkafka_play","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbaala3%2Fkafka_play","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaala3%2Fkafka_play/lists"}