Using Deta Space KV Store for Flask Session Management
Learn how to use Deta Space’s built‑in key‑value store to persist Flask user sessions on a serverless platform, with a working code example and notes on limits and TTL behavior.
02 Feb 2026, 03:55 UTC

Problem: Stateless functions lose session data
When you run a Flask application on a serverless platform, each request may be handled by a different container instance. Without a shared store, user‑session data such as authentication tokens disappears between requests, forcing you to re‑authenticate or rely on client‑side storage that is harder to secure.
Why Deta Space KV fits the need
Deta Space provides a built‑in key‑value (KV) store that is automatically provisioned when you deploy a micro. The store offers strong consistency for small values, atomic set, get and delete operations, and a TTL feature that can expire session entries after a chosen interval. Because the KV lives inside the same Space as your code, you avoid the operational overhead of provisioning an external database.
Worked example: Flask app that stores sessions in Deta KV
Add the Deta SDK to requirements.txt:
Flask>=2.0
deta>=1.0
Create a helper module session_store.py:
import os
from deta import Deta
deta = Deta()
kv = deta.Base('sessions')
def save(session_id, data, ttl_seconds=1800):
kv.put({'key': session_id, 'data': data}, expire_in=ttl_seconds)
def get(session_id):
item = kv.get(session_id)
return item.get('data') if item else None
def delete(session_id):
kv.delete(session_id)
Use the helper in your Flask app:
from flask import Flask, redirect, session, url_for
import uuid
from session_store import save, get
app = Flask(__name__)
app.secret_key = os.urandom(24)
@app.route('/login', methods=['POST'])
def login():
# assume credentials verified
sid = str(uuid.uuid4())
save(sid, {'user_id': 123})
session['sid'] = sid
return redirect(url_for('index'))
@app.route('/')
def index():
sid = session.get('sid')
data = get(sid) if sid else None
if not data:
return 'Please log in'
return 'Hello, user {}'.format(data.get('user_id'))
Deploy with the Deta CLI:
deta login
deta new --python
deta deploy
After deployment, open the Deta Space dashboard → Storage → KV → sessions base. You should see a new item after a login request. The Expires In column shows the TTL rounded to the nearest minute.
Trade‑offs and limitations
- Free tier: 1 GB storage, 10 000 requests per day. Monitor the Requests graph and upgrade if needed.
- TTL rounded to the nearest minute; sub‑minute expiry is not precise.
- No secondary indexes; look‑ups are by key only.
Actionable next steps
- Sign up for Deta Space and install the CLI (
deta login). - Add
detatorequirements.txtand copy the helper and app code above. - Run
deta deployto provision the KV base and release the update. - Check the dashboard for KV items and request counts.
- Test TTL by setting a short
expire_invalue and verifying the item disappears after the displayed minute.
Using Deta Space KV lets you keep Flask micros stateless while retaining a simple, managed session store.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.