Backend Engineering · 8 min read

Serialization, Deserialization, Authentication and Authorization

Backend fundamentals: serialization, auth, and authz.

On this page
  1. The Art of Packing and Unpacking Data
  2. What Is Serialization?
  3. What Is Deserialization?
  4. Now, why Does Backend Engineering care so much about this?
  5. The Popular Formats
  6. 1. JSON: The most widely used
  7. 2. XML: The Veteran
  8. 3. Protocol Buffers (Protobuf): The Quicksilver here (Sorry)
  9. 4. MessagePack: The Binary JSON
  10. 5. Avro: The Schema Enforcer
  11. Authentication and Authorization
  12. Authentication: Proving Your Identity
  13. The Classic: Username + Password
  14. The Token Problem: Staying Logged In
  15. Multi-Factor Authentication (MFA): Trust But Verify Again
  16. OAuth 2.0: "Login With Google"
  17. Authorization: What You're Allowed To Do
  18. Role-Based Access Control (RBAC)
  19. Attribute-Based Access Control (ABAC)
  20. Permission-Based / ACL (Access Control Lists)
  21. How Authorization Happens in Code
  22. 1. Middleware / Route Level
  23. 2. Service / Business Logic Level
  24. 3. Database / Query Level
  25. Best Practices at a Glance
  26. The Full Picture in One Flow

Welcome to the 2nd article of this backend series, folks!

In the 1st article, we talked about HTTP & routing-related stuff. Now, we're gonna learn about some more interesting & important features of backend engineering.

The Art of Packing and Unpacking Data

Imagine you want to send an item to your friend in another city. You can't just teleport it, right? You need to wrap it up, put it in a box, ship it, and then your friend unwraps it on the other side. That's exactly what serialization and deserialization do, but for data.

What Is Serialization?

It is the process of converting an in-memory object (a Python dict, a Java class instance, a JavaScript object) into a format that can be stored or transmitted (For eg: JSON, XML, or binary bytes).

Your backend has a User object living in memory (Python, for eg):

python
user = User(id=42, name="Harry", role="admin", active=True)

To send this over HTTP or save it to a file, you serialize it:

json
{
  "id": 42,
  "name": "Harry",
  "role": "admin",
  "active": true
}

The rich, complex object becomes a flat, portable string. It has been "packed for the journey".

What Is Deserialization?

Deserialization is the reverse of what we just learned, bois & grills. Taking that flat, serialized format and reconstructing the original object back in memory.

Your friend's server receives the JSON string and goes:

python
data = json.loads(raw_json)
user = User(**data)  # Harry is back here

The string comes back to life as a fully usable object. It's been unpacked and reassembled.

Now, why Does Backend Engineering care so much about this?

Because backends are obsessed with moving data around. Every single day, your systems are:

  • Sending API responses to mobile apps and browsers
  • Publishing messages to queues like Kafka or RabbitMQ
  • Caching objects in Redis
  • Storing records in databases
  • Communicating between microservices via gRPC or REST

None of that works without serialization.

1. JSON: The most widely used

The most widely used format on the web. Human-readable, language-agnostic, and supported everywhere.

json
{ "product": "Laptop", "price": 999.99, "inStock": true }
  • Pros: Easy to debug, universal support
  • Cons: No schema enforcement

2. XML: The Veteran

Older, more verbose, but still king in enterprise systems and SOAP APIs.

xml
<product>
  <name>Laptop</name>
  <price>999.99</price>
</product>

3. Protocol Buffers (Protobuf): The Quicksilver here (Sorry)

Google's binary format. You define a schema (.proto file), and it generates compact, fast binary output. Beloved in microservice architectures.

proto
message Product {
  string name = 1;
  float price = 2;
  bool in_stock = 3;
}
  • Pros: Tiny payload size, blazing fast
  • Cons: Not human-readable, requires schema setup

4. MessagePack: The Binary JSON

Like JSON, but encoded in binary. Drop-in speed upgrade for systems that already use JSON semantics.

  • Pros: Faster than JSON, compact
  • Cons: Not human-readable

5. Avro: The Schema Enforcer

Apache's format, heavily used in the Kafka/data pipeline world. Schema is embedded or stored in a registry.

  • Pros: Schema evolution support, great for streaming
  • Cons: More complex setup

P.S. Serialization and deserialization are the invisible plumbing of every backend system. They're so fundamental that most developers take them for granted, until something breaks. A renamed field crashes a mobile app. A binary format confuses a debugging session. An untrusted payload exploits a deserializer.

Authentication and Authorization

Basically - "Who Are You?" vs "What Can You Do?"

Imagine you arrive at a fancy corporate office (or at your college's gate). The security guard at the front desk checks your ID card. That's him figuring out who you are. Then he checks a list to see which floors you're allowed to visit. That's him deciding what you can do.

That two-step process happens in every backend system, billions of times a day. It has a name:

  • Authentication (AuthN) means Who are you?
  • Authorization (AuthZ) means What are you allowed to do?

They sound similar. They are deeply different.

Authentication: Proving Your Identity

Authentication is the process of verifying that you are who you claim to be. The backend doesn't trust you just because you say "I'm Harry." It needs proof.

The Classic: Username + Password

The oldest trick in the book. You send credentials, the backend checks them against a stored (hashed) password, and grants you a session or token.

http
POST /login
{
  "email": "harry@gmail.com",
  "password": "secret123"
}

The server responds with proof of identity i.e. a token you carry from here on.

There is a golden rule: Never store passwords in plain text. Always hash with a slow algorithm like bcrypt, Argon2, or scrypt. MD5 and SHA1 are not acceptable. They're fast, and fast is bad for password hashing.

python
# Hashing a password
hashed = bcrypt.hashpw(password.encode(), bcrypt.gensalt())

# Verifying on login
bcrypt.checkpw(entered_password.encode(), hashed)  # True or False

The Token Problem: Staying Logged In

HTTP is stateless. The server remembers nothing between requests. So after login, how does the server know it's still you on the next request?

Two classic answers:

Sessions (The Old School Way)

After login, the server creates a session record in its database or memory store (like Redis), and sends you a session_id cookie.

text
Login -> Server creates Session { id: "abc123", userId: 10 }
       -> Sends cookie: session_id=abc123

Next Request -> Browser sends cookie automatically
             -> Server looks up "abc123" -> finds userId 10 -> Goodie!

It is easy to revoke (just delete the session) but the server must store state, so it doesn't scale beautifully across many servers.

JWT - JSON Web Tokens (The Modern Way)

Instead of storing anything server-side, the server signs a token and gives it to the client. The client sends it back with every request. The server just verifies the signature. No database lookup needed.

A JWT looks like this:

text
eyJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOjQyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

Three parts, separated by dots:

text
HEADER.PAYLOAD.SIGNATURE

Header    → { "alg": "HS256", "typ": "JWT" }
Payload   → { "userId": 100, "role": "admin", "exp": 1735689600 }
Signature → HMAC(header + payload, secret_key)

The server signs the token with a secret. If anyone tampers with the payload, the signature breaks. Forgery detected.

text
Request -> Bearer eyJhbGci...
Server  -> Verifies signature -> Decodes payload -> userId: 100
  • Pros: Stateless and scalable, works great across microservices
  • Cons: Hard to revoke before expiry (need a blocklist for that). If the secret leaks, everything is compromised

Multi-Factor Authentication (MFA): Trust But Verify Again

Passwords alone are weak. MFA adds extra proof layers:

  • Something you know: Password, PINs
  • Something you have: OTP app (Microsoft Authenticator), SMS code, hardware key
  • Something you are: Fingerprint, Face ID

Two-factor authentication (2FA) combines any two. Even if your password leaks, an attacker still can't log in without your phone.

OAuth 2.0: "Login With Google"

Sometimes you don't want to handle authentication yourself at all. OAuth 2.0 lets your users authenticate via a trusted third party — Google, GitHub, Apple — and your backend receives a token confirming who they are.

  1. User clicks "Login with Google"
  2. Google authenticates the user
  3. Google sends your backend an authorization code
  4. Your backend exchanges it for an access token
  5. You know who the user is without ever seeing their password

You're not the one doing authentication, you're delegating it to Google. This is the core idea of OAuth 2.0 and OpenID Connect (OIDC), which sits on top of it specifically for identity.

Authorization: What You're Allowed To Do

Once the system knows who you are, it must decide what you can access. This is authorization, and it's a design problem as much as a security one.

Role-Based Access Control (RBAC)

The most common model. Users are assigned roles, and roles have permissions.

text
Roles:
  admin  : can read, write, delete anything
  editor : can read and write posts
  viewer : can only read posts

User Harry -> role: editor
User Dimps -> role: viewer

Harry tries to delete a post -> Forbidden
Harry tries to edit a post   -> Allowed

Clean, easy to reason about, easy to manage. Works great for most applications.

Attribute-Based Access Control (ABAC)

More powerful and flexible. Access decisions are based on attributes of the user, the resource, and the environment, evaluated against policies.

python
# Can this user access this document?
def can_access(user, document, action):
    return (
        user.department == document.department and
        user.clearance_level >= document.sensitivity and
        action in user.allowed_actions and
        is_business_hours(current_time())
    )
  • Pros: Extremely fine-grained, handles complex, conditional rules
  • Cons: Harder to reason about and audit

Permission-Based / ACL (Access Control Lists)

Instead of roles, each resource carries a list of who can do what to it, like Unix file permissions or Google Docs sharing settings.

text
Document "Q4_Report.pdf":
  Harry  - read, write
  Dimps  - read
  Public - no access
  • Pros: Very precise per-resource control
  • Cons: Gets unwieldy at scale (millions of resources × thousands of users)

How Authorization Happens in Code

In practice, authorization checks are enforced at multiple layers:

1. Middleware / Route Level

python
@app.route("/admin/users")
@require_role("admin")  # <- Gate at the door
def list_users():
    return User.query.all()

2. Service / Business Logic Level

python
def delete_post(requesting_user, post_id):
    post = Post.get(post_id)
    if post.author_id != requesting_user.id and not requesting_user.is_admin:
        raise PermissionError("You can't delete someone else's post")
    post.delete()

3. Database / Query Level

python
# Never return data the user shouldn't see, even accidentally
posts = Post.query.filter_by(author_id=current_user.id).all()

Checking only at the route level and trusting the rest is a classic mistake that leads to Insecure Direct Object Reference (IDOR) bugs, where a user guesses another user's resource ID and accesses it freely.

Best Practices at a Glance

PracticeWhy
Hash passwords with bcrypt/Argon2Slow hashing defeats brute-force attacks
Use short-lived JWTs + refresh tokensLimits damage if a token is stolen
Always authorize at the resource levelDon't just check if logged in. Check if allowed
Rate-limit login endpointsPrevents brute-force and credential stuffing
Use HTTPS everywhereTokens in plaintext HTTP are trivially stolen
Principle of Least PrivilegeGive users only the permissions they actually need
Log auth eventsFailed logins, token refresh, permission denials - all audit-worthy

The Full Picture in One Flow

mermaid
flowchart TD
  Req["User: Let me delete Post #35;10"] --> AuthN{"Authentication<br/>(who are you?)<br/>Is the token still valid?"}
  AuthN -->|"no"| E401["401 Unauthorized"]
  AuthN -->|"yes, authenticated"| AuthZ{"Authorization<br/>(can you do this?)<br/>Author of the post, or a role that allows it?"}
  AuthZ -->|"not the author, no role"| E403["403 Forbidden"]
  AuthZ -->|"author or admin"| OK["Post #35;10 deleted"]

Authentication and authorization are the gatekeepers of your entire system. Get them wrong, and it doesn't matter how elegant your database schema is or how fast your API is.. your users' data is exposed, your business is liable, and trust is gone.